package toy.dialog; import java.awt.*; import java.awt.datatransfer.*; /** * * @author Brian Tsang * @version 7.0 */ public abstract class ToyToolkit { private static final Frame STATIC_FRAME = new Frame(); //as it turns out, IE 5.5 of Windows is known not to have a Dialog(Dialog) //method, but in an application, dialogs who take precedence over other //dialogs *must* be children over the less-immediate dialog in order //to recieve mouse/keyboard input. Our solution? Test to see if we can //make a dialog be a child of another dialog private static boolean dialogParentCanBeDialog; private static boolean hasFilePermissions; private static boolean hasClipboardPermissions; static { try { Dialog test = new Dialog(new Dialog(STATIC_FRAME)); dialogParentCanBeDialog = true; } catch (Throwable err) { dialogParentCanBeDialog = false; } try { SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkWrite(".\temp.toy"); } FileDialog test = new FileDialog(STATIC_FRAME, "test"); hasFilePermissions = true; } catch (Throwable err) { hasFilePermissions = false; } try { Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); hasClipboardPermissions = true; } catch (Throwable err) { hasClipboardPermissions = false; } } public static FontMetrics getFontMetrics(Font font) { return STATIC_FRAME.getFontMetrics(font); } public static boolean dialogParentCanBeDialog() { return dialogParentCanBeDialog; } public static boolean hasFilePermissions() { return hasFilePermissions; } public static boolean hasClipboardPermissions() { return hasClipboardPermissions; } public static GridBagConstraints createGridBagConstraints( int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int anchor, int fill, Insets insets, int ipadx, int ipady) { GridBagConstraints answer = new GridBagConstraints(); answer.gridx = gridx; answer.gridy = gridy; answer.gridwidth = gridwidth; answer.gridheight = gridheight; answer.weightx = weightx; answer.weighty = weighty; answer.anchor = anchor; answer.fill = fill; answer.insets = insets; answer.ipadx = ipadx; answer.ipady = ipady; return answer; } //////////////////////////////////////////////////////////////////////////// // wordWrap() takes a string and returns a word-wrapped array of strings // given a specific font and a maximum width public static String[] wordWrap(String parseString, int characters) { String wrappedString[] = new String[0]; //replace tabs with 4 spaces int tabIndex; while ((tabIndex = parseString.indexOf('\t')) != -1) { parseString = parseString.substring(0, tabIndex) + " " + parseString.substring(tabIndex + 1); } //divide the warning up into 76-character long strings while(parseString != null && parseString.length() > 0) { int endCtr = 0, peekCtr = 0; do { //peekCtr passed the while test, advance endCtr endCtr = peekCtr; //advance to the end of the first word first while (peekCtr < parseString.length() && parseString.charAt(peekCtr) == ' ') peekCtr++; while (peekCtr < parseString.length() && parseString.charAt(peekCtr) != ' ' && parseString.charAt(peekCtr) != '\n') peekCtr++; } while (peekCtr < characters && endCtr < parseString.length() && parseString.charAt(endCtr) != '\n'); //if we have a really big word, cut it off if (endCtr == 0 && parseString.charAt(0) != '\n') endCtr = Math.min(parseString.length(), characters); //append the substring to our string array String oldWrappedString[] = wrappedString; wrappedString = new String[wrappedString.length + 1]; for (int ctr = 0; ctr < oldWrappedString.length; ctr++) wrappedString[ctr] = oldWrappedString[ctr]; wrappedString[wrappedString.length - 1] = " " + parseString.substring(0, endCtr); //chop off the substring parseString = parseString.substring(endCtr); //if the first character is a newline, chop it off. Otherwise, chop //off all the spaces in the beginning if (parseString.length() > 0 && parseString.charAt(0) == '\n') parseString = parseString.substring(1); else while (parseString.length() > 0 && parseString.charAt(0) == ' ') parseString = parseString.substring(1); } return wrappedString; } public static String[] wordWrap(String parseString, Font font, int maxWidth) { //replace tabs with 4 spaces int tabIndex; while ((tabIndex = parseString.indexOf('\t')) != -1) { parseString = parseString.substring(0, tabIndex) + " " + parseString.substring(tabIndex + 1); } //parse the text into strings that represent the word-wrapped string FontMetrics fontMetrics = getFontMetrics(font); String wrappedString[] = new String[0]; while(parseString != null && parseString.length() > 0) { int endCtr = 0, peekCtr = 0; do { //peekCtr passed the while test, advance endCtr endCtr = peekCtr; //advance to the end of the first word first while (peekCtr < parseString.length() && parseString.charAt(peekCtr) == ' ') peekCtr++; while (peekCtr < parseString.length() && parseString.charAt(peekCtr) != ' ' && parseString.charAt(peekCtr) != '\n') peekCtr++; } while (fontMetrics.stringWidth(parseString.substring(0, peekCtr)) < maxWidth && endCtr < parseString.length() && parseString.charAt(endCtr) != '\n'); //if we have a really big word, cut it off if (endCtr == 0 && parseString.charAt(0) != '\n') { peekCtr = 0; while (peekCtr < parseString.length() && fontMetrics.stringWidth(parseString.substring(0, peekCtr)) < maxWidth) peekCtr++; endCtr = peekCtr - 1; } //append the substring to our string array String oldWrappedString[] = wrappedString; wrappedString = new String[wrappedString.length + 1]; for (int ctr = 0; ctr < oldWrappedString.length; ctr++) wrappedString[ctr] = oldWrappedString[ctr]; wrappedString[wrappedString.length - 1] = parseString.substring(0, endCtr); //chop off the substring parseString = parseString.substring(endCtr); //if the first character is a newline, chop it off. Otherwise, chop //off all the spaces in the beginning if (parseString.length() > 0 && parseString.charAt(0) == '\n') parseString = parseString.substring(1); else while (parseString.length() > 0 && parseString.charAt(0) == ' ') parseString = parseString.substring(1); } return wrappedString; } }