Please take time to read the code disclaimer.
package broadscope;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.logging.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.xml.sax.*;
/**
*
* @author Kent
*/
public class StaticClass {
public static final String newline = System.getProperty("line.separator");
public static final String homeDir = System.getProperty("user.home");
public static final int ALL = -1;
public static final int AVAILCONF = 0;
public static final int SELCONF = 1;
public static final int AVAILSESS = 2;
public static final int SELSESS = 3;
public static final int AVAILLANG = 4;
public static final int SELLANG = 5;
public StaticClass() {
}
//
/**
* This method packs, sets the size, and sets the position of the window given
* @param window
*/
public static void centerAndPack(Window window) {
window.pack();
int y = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2) - window.getHeight() / 2;
int x = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2) - window.getWidth() / 2;
window.setLocation(x, y);
window.setMinimumSize(new Dimension(window.getWidth(), window.getHeight()));
}
/**
* this method maximizes the given item
* @param frame
*/
public static void maximizeWindow(JFrame frame) {
frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
}
/**
* this method adds a given image to the bottom right of the layout
* @param container where you want the object
* @param c of the object you want the image on
* @param file the image you wish to add
* @param setWidth if true will set the width as the given size, if false, will set the height as the given size
* @param size 0 - 4 is smallest to biggest
* @param anchor use java.awt.GridBagConstraints.CENTER (for example)
* @param bottom if true, image will appear on the bottom, if false, image will appear on the right.
*/
public static void addImage(Container container, Class c, String file, boolean setWidth, int size, int anchor, boolean bottom) {
GridBagLayout gbl = (java.awt.GridBagLayout) container.getLayout();
JPanel logoPanel = new JPanel();
logoPanel.setLayout(new java.awt.GridBagLayout());
int[][] dim = gbl.getLayoutDimensions();
int cols = dim[0].length;
int rows = dim[1].length;
java.awt.GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
if (bottom) {
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = rows;
gridBagConstraints.gridwidth = cols;
} else {
gridBagConstraints.gridy = 0;
gridBagConstraints.gridx = cols;
gridBagConstraints.gridheight = rows;
}
gridBagConstraints.anchor = anchor;
gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
container.add(logoPanel, gridBagConstraints);
JLabel logo = new JLabel();
logo.setIcon(resizeImage(c, file, true, size));
logoPanel.add(logo);
}
/**
* This adds the full logo (it's just so I don't have to copy and paste the location of the image).
*/
public static void addFullLogo(Container container, Class c, boolean setWidth, int size, int anchor, boolean bottom) {
addImage(container, c, "/resources/more_good_logo_blue.jpg", setWidth, size, anchor, bottom);
}
/**
* This adds the torch only (it's just so I don't have to copy and paste the location of the image).
*/
public static void addPartLogo(Container container, Class c, boolean setWidth, int size, int anchor, boolean bottom) {
addImage(container, c, "/resources/more_good_logo_torch_blue.jpg", setWidth, size, anchor, bottom);
}
/**
* This method resizes the given image
* @param c
* @param location
* @param setWidth set as true if the given size is for the width, false if it's for the height
* @param size the size of the width or height
* @return
*/
public static ImageIcon resizeImage(Class c, String location, boolean setWidth, int size) {
int width = -1;
int height = -1;
if (setWidth) {
width = size;
} else {
height = size;
}
ImageIcon imageIcon = null;
try {
imageIcon = new ImageIcon(c.getResource(location));
} catch (NullPointerException e) {
imageIcon = new ImageIcon(location);
}
Image image = imageIcon.getImage();
Image newimg = image.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH);
return new ImageIcon(newimg);
}
/**
* Sets the background color of all the components in the frame
* Color the frame what you want the background for all components to be
* @param container (start with the frame, it will recursively go through containers
* @param color
*/
public static void setUnifiedColor(Container container, Color color) {
for (Component component : container.getComponents()) {
component.setBackground(color);
if (component instanceof Container) {
setUnifiedColor((Container) component, color);
}
}
}
//
//
/**
* This method creates a doc
* @return Document object called doc
*/
public static Document createDoc() {
Document doc = null;
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
doc = dBuilder.newDocument();
} catch (ParserConfigurationException ex) {
Logger.getLogger(StaticClass.class.getName()).log(Level.SEVERE, null, ex);
}
return doc;
}
/**
* This method will transform an XML doc to a String
* @param doc
* @return xmlString
*/
public static String transformDoc(Document doc) {
String xmlString = "XMLSTRING EMPTY!";
try {
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
xmlString = writer.toString();
} catch (TransformerException ex) {
ex.printStackTrace();
}
return xmlString;
}
/**
* This method will Read the XML and act accordingly
* @param xmlString - the XML String
* @return the list of elements within the XML
*/
public static Document readXML(String xmlString) {
Document doc = null;
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
InputSource xmlStream = new InputSource();
xmlStream.setCharacterStream(new StringReader(xmlString));
doc = dBuilder.parse(xmlStream);
} catch (Exception e) {
e.printStackTrace();
}
return doc;
}
//
/**
* This method reads a file into a string
* @param resourceLocation location of the resource in the resources folder
* @return String of the file
*/
public static String fileToString(String resourceLocation) {
StringBuilder fileString = new StringBuilder();
InputStream resourceAsStream = StaticClass.class.getResourceAsStream(resourceLocation);
Scanner fileScanner = new Scanner(resourceAsStream);
while (fileScanner.hasNext()) {
fileString.append(fileScanner.nextLine()).append(newline);
}
return fileString.toString();
}
/**
* Using the given url, this method creates and returns the buffered reader for that url
* @param urlString
* @return
* @throws MalformedURLException
* @throws IOException
*/
public static BufferedReader getBufferedReader(String urlString) throws MalformedURLException, IOException {
BufferedReader br = null;
URL url = new URL(urlString);
InputStream is = url.openStream();
br = new BufferedReader(new InputStreamReader(is));
return br;
}
/**
* This method will make a string filename safe
* @param string
* @return filenameSafeString
*/
public static String makeFilenameSafe(String string) {
StringBuilder filenameSafeString = new StringBuilder(string.length());
filenameSafeString.setLength(string.length());
char[] badCharacters = new char[]{'\\', '/', ':', '*', '?', '\"', '<', '>', '|'};
int current = 0;
for (int i = 0; i < string.length(); i++) {
char cur = string.charAt(i);
boolean found = false;
for (int j = 0; j < badCharacters.length; j++) {
if (cur == badCharacters[j]) {
found = true;
break;
}
}
if (!found) {
filenameSafeString.setCharAt(current++, cur);
}
}
return filenameSafeString.toString();
}
/**
* This method will make the display text for the current folders
* @param originalString the text to be shortened
* @param maxLength maximum length of the string
* @param percentStart the percentage of the string length to go before inserting the dots (for 50% give .5)
* @return the text to be displayed
*/
public static String stringShortener(String originalString, int maxLength, double percentStart) {
String newString = originalString;
double endLength = (maxLength * percentStart) - 3;
double beginLength = maxLength * (1 - percentStart);
if (originalString.length() > maxLength + 3) {
newString = originalString.substring(0, (int) beginLength) + "..." + originalString.substring(originalString.length() - (int) endLength);
} else {
newString = originalString;
}
return newString;
}
}