001: package org.ofbiz.rules.utensil;
002:
003: import java.awt.*;
004: import java.awt.event.*; // import com.sun.java.swing.*;
005: // import com.sun.java.swing.border.*;
006: import javax.swing.*;
007: import javax.swing.border.*;
008:
009: /**
010: * <p><b>Title:</b> Swing Utensil
011: * <p><b>Description:</b> None
012: * <p>Copyright (c) 1999 Steven J. Metsker.
013: * <p>Copyright (c) 2001 The Open For Business Project - www.ofbiz.org
014: *
015: * <p>Permission is hereby granted, free of charge, to any person obtaining a
016: * copy of this software and associated documentation files (the "Software"),
017: * to deal in the Software without restriction, including without limitation
018: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
019: * and/or sell copies of the Software, and to permit persons to whom the
020: * Software is furnished to do so, subject to the following conditions:
021: *
022: * <p>The above copyright notice and this permission notice shall be included
023: * in all copies or substantial portions of the Software.
024: *
025: * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
026: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
027: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
028: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
029: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
030: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
031: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
032: *
033: * <br>
034: * <p>This class harbors a few handy swing utilities.
035: *
036: * @author Steven J. Metsker
037: * @version 1.0
038: */
039: public class SwingUtensil {
040:
041: /**
042: * Set the location of a frame to be the center of the
043: * screen.
044: */
045: public static void center(Frame f) {
046: Dimension sDim = Toolkit.getDefaultToolkit().getScreenSize();
047:
048: Dimension fDim = f.getSize();
049:
050: if (fDim.height > sDim.height) {
051: fDim.height = sDim.height;
052: }
053: if (fDim.width > sDim.width) {
054: fDim.width = sDim.width;
055: }
056:
057: f.setLocation((sDim.width - fDim.width) / 2,
058: (sDim.height - fDim.height) / 2);
059: }
060:
061: /**
062: * A standard font for interactive development environment
063: * text areas.
064: */
065: public static Font ideFont() {
066: int style = Font.PLAIN;
067: int size = 18;
068: String name = "lucida sans typewriter";
069: Font f = new Font(name, style, size);
070:
071: if (!f.getFamily().equals(name)) {
072: name = "Monospaced";
073: f = new Font(name, style, size);
074: }
075: return f;
076: }
077:
078: /**
079: * Return a standard text area, with the standard font
080: * and border, and with word wrapping enabled.
081: */
082: public static JTextArea ideTextArea() {
083: JTextArea ta = new JTextArea();
084:
085: ta.setFont(ideFont());
086: ta.setBorder(new EmptyBorder(5, 5, 5, 5));
087: ta.setLineWrap(true);
088: return ta;
089: }
090:
091: /**
092: * Return a standard titled border.
093: */
094: public static TitledBorder ideTitledBorder(String title) {
095: TitledBorder tb = BorderFactory.createTitledBorder(
096: BorderFactory.createBevelBorder(BevelBorder.RAISED),
097: title, TitledBorder.LEFT, TitledBorder.TOP);
098:
099: tb.setTitleColor(Color.black);
100: tb.setTitleFont(ideFont());
101: return tb;
102: }
103:
104: /**
105: * Create a frame with the given title around the given
106: * component; center and display the frame; listen for
107: * exit, closing the frame's window on exit.
108: */
109: public static JFrame launch(Component c, String title) {
110: JFrame frame = new JFrame(title);
111:
112: frame.getContentPane().add(c);
113: SwingUtensil.listen(frame);
114: frame.pack();
115: SwingUtensil.center(frame);
116: frame.setVisible(true);
117: return frame;
118: }
119:
120: /**
121: * Set up an exit listener for a frame.
122: */
123: public static void listen(Frame f) {
124: f.addWindowListener(new WindowAdapter() {
125: public void windowClosing(WindowEvent e) {
126: System.exit(0);
127: };
128: });
129: }
130:
131: /**
132: * Returns a standard text panel, with a scroll pane around
133: * a text area, and with a title border.
134: *
135: * @param String the panel title
136: *
137: * @param JTextArea the text area to wrap
138: *
139: * @param Dimension the preferred size for this panel
140: *
141: * @param Dimension the minimum size for this panel
142: *
143: * @return a standard text panel, with a scroll pane
144: * around a text area, and with a title border.
145: */
146: public static JPanel textPanel(String title, JTextArea ta,
147: Dimension pref, Dimension min) {
148:
149: // scroll pane around text area
150: JScrollPane s1 = new JScrollPane(ta);
151:
152: s1.setPreferredSize(pref);
153: s1.setMinimumSize(min);
154:
155: // titled panel that contains scrolling text area
156: JPanel p = new JPanel();
157:
158: p.setLayout(new BorderLayout());
159: p.setBorder(SwingUtensil.ideTitledBorder(title));
160: p.add(s1, "Center");
161: return p;
162: }
163: }
|