001: /*
002: * Created on 17.03.2005 for PIROL
003: *
004: * SVN header information:
005: * $Author: javamap $
006: * $Rev: 856 $
007: * $Date: 2007-06-18 21:15:27 -0700 (Mon, 18 Jun 2007) $
008: * $Id: DialogTools.java 856 2007-06-19 04:15:27Z javamap $
009: */
010: package de.fho.jump.pirol.ui.tools;
011:
012: import java.awt.Component;
013: import java.awt.Dimension;
014: import java.awt.GridLayout;
015: import java.awt.Window;
016: import java.text.DecimalFormatSymbols;
017: import java.util.ArrayList;
018: import java.util.List;
019:
020: import javax.swing.JComponent;
021: import javax.swing.JLabel;
022: import javax.swing.JPanel;
023:
024: import com.vividsolutions.jump.workbench.ui.GUIUtil;
025:
026: import de.fho.jump.pirol.utilities.apiTools.ToolToMakeYourLifeEasier;
027:
028: /**
029: * @author <strong>Ole Rahn, Stefan Ostermann, Carsten Schulze</strong>
030: * <br>
031: * <br>FH Osnabrück - University of Applied Sciences Osnabrück
032: * <br>Project PIROL 2005
033: * <br>Daten- und Wissensmanagement
034: *
035: * @since 1.2: changed by Stefan Ostermann at 2005-04-26: added method to return localized
036: * string of a double value.
037: * @since <br>1.3 changed by Carsten Schulze at 2005-05-22: added a method to
038: * center a given AWT-Window (or subclasses) on the screen.
039: * @version $Rev: 856 $
040: */
041: public class DialogTools extends ToolToMakeYourLifeEasier {
042:
043: /**
044: * This method centers the window (or subclasses of it) on the screen.
045: * @param window the java.awt.Window (or a subclass of it) that should be
046: * displayed in the middle of the screen.
047: */
048: public static void centerOnScreen(Window window) {
049: // Dimension screenDim = window.getToolkit().getScreenSize();
050: // Dimension windowDim = window.getSize();
051: // int x = (screenDim.width / 2) - (windowDim.width / 2);
052: // int y = (screenDim.height / 2) - (windowDim.height / 2);
053: //
054: // window.setLocation(x,y);
055:
056: GUIUtil.centreOnScreen(window);
057: }
058:
059: public static void centerOnWindow(Component component) {
060: GUIUtil.centreOnWindow(component);
061: }
062:
063: public static void centerOnWindow(Component component2move,
064: Component component2CenterOn) {
065: GUIUtil.centre(component2move, component2CenterOn);
066: }
067:
068: /**
069: * This method creates a JPanel with several JLabels on it. For another
070: * method to display multiline bold text, have a look at the
071: * {javax.swing.JTextArea} and the {@link java.awt.Font} object.
072: * @param text the text to split up into some JLabels.
073: * @param charsPerLine the maximum number of characters per line text.
074: * @return the panel with the labels on it.
075: */
076: public static JPanel getPanelWithLabels(String text,
077: int charsPerLine) {
078: List<String> labelTextParts = new ArrayList<String>();
079: if (text.length() > charsPerLine) {
080: int estimatedStrings = (int) Math.ceil((float) text
081: .length()
082: / charsPerLine);
083:
084: String copyLabelText = text.toString();
085: for (int i = 0; i < estimatedStrings; i++) {
086: if (copyLabelText.indexOf(" ", charsPerLine) > -1)
087: labelTextParts.add(copyLabelText
088: .substring(0, copyLabelText.indexOf(" ",
089: charsPerLine) + 1));
090: else
091: labelTextParts.add(copyLabelText);
092: copyLabelText = copyLabelText
093: .substring(((String) labelTextParts.get(i))
094: .length());
095: }
096:
097: } else {
098: labelTextParts.add(text);
099: }
100:
101: JPanel texts = new JPanel();
102: GridLayout gl = new GridLayout(labelTextParts.size(), 1);
103: gl.setHgap(0);
104: texts.setLayout(gl);
105: //texts.setPreferredSize( new Dimension(400, 50));
106: for (int i = 0; i < labelTextParts.size(); i++) {
107: texts.add(new JLabel(" " + labelTextParts.get(i) + " "));
108: }
109: texts.doLayout();
110: return texts;
111: }
112:
113: /**
114: * This method replaces the localized decimal seperator with a dot.
115: * @param s the String containing the double value.
116: * @return the now dotted double value.
117: * @see #numberStringToLocalNumberString(String)
118: * @see #numberToLocalNumberString(double)
119: */
120: public static double localNumberStringToDouble(String s) {
121: DecimalFormatSymbols ds = new DecimalFormatSymbols();
122: s = s.replace(ds.getDecimalSeparator(), '.');
123: return Double.parseDouble(s);
124: }
125:
126: /**
127: * This method replaces the dot with the localized decimal seperator.
128: * @param s the String containing the double value.
129: * @return the localized String containing the double value.
130: * @see #localNumberStringToDouble(String)
131: * @see #numberToLocalNumberString(double)
132: */
133: public static String numberStringToLocalNumberString(String s) {
134: DecimalFormatSymbols ds = new DecimalFormatSymbols();
135: s = s.replace('.', ds.getDecimalSeparator());
136: return s;
137: }
138:
139: /**
140: * This method replaces the dot with the localized decimal seperator.
141: * @param number the double value.
142: * @return the localized String containing the double value.
143: * @see #numberStringToLocalNumberString(String)
144: * @see #localNumberStringToDouble(String)
145: */
146: public static String numberToLocalNumberString(double number) {
147: String s = new Double(number).toString();
148: return DialogTools.numberStringToLocalNumberString(s);
149: }
150:
151: /**
152: * Sets the prefered width of an JComponent and keeps it prefered height.
153: * @param component the component to alter
154: * @param width the new prefered width
155: */
156: public static void setPreferedWidth(JComponent component, int width) {
157: int preferedHeight = component.getPreferredSize().height;
158: component
159: .setPreferredSize(new Dimension(width, preferedHeight));
160: }
161:
162: /**
163: * Sets the prefered height of an JComponent and keeps it prefered height.
164: * @param component the component to alter
165: * @param height the new prefered width
166: */
167: public static void setPreferedHeight(JComponent component,
168: int height) {
169: int preferedWidth = component.getPreferredSize().width;
170: component
171: .setPreferredSize(new Dimension(preferedWidth, height));
172: }
173:
174: public static void setMaximumWidth(JComponent component, int width) {
175: int preferedHeight = component.getPreferredSize().height;
176: component.setMaximumSize(new Dimension(width, preferedHeight));
177: }
178:
179: public static void setMaximumHeight(JComponent component, int height) {
180: int preferedWidth = component.getPreferredSize().width;
181: component.setMaximumSize(new Dimension(preferedWidth, height));
182: }
183: }
|