001: package com.opensymphony.workflow.designer;
002:
003: import java.awt.*;
004: import java.awt.event.ActionListener;
005: import java.text.NumberFormat;
006: import javax.swing.*;
007: import javax.swing.text.html.HTMLDocument;
008: import javax.swing.text.View;
009: import javax.swing.text.DefaultFormatterFactory;
010: import javax.swing.text.NumberFormatter;
011:
012: import com.jgoodies.forms.factories.ButtonBarFactory;
013: import com.jgoodies.forms.builder.DefaultFormBuilder;
014: import com.jgoodies.forms.layout.FormLayout;
015:
016: /**
017: * @author Hani Suleiman (hani@formicary.net)
018: * Date: May 21 2003
019: * Time: 9:46:11 PM
020: */
021: public class UIFactory {
022: private static final DefaultFormatterFactory INT_FORMATTER_FACTORY;
023:
024: static {
025: NumberFormat format = NumberFormat.getIntegerInstance();
026: format.setGroupingUsed(false);
027: INT_FORMATTER_FACTORY = new DefaultFormatterFactory(
028: new NumberFormatter(format));
029: }
030:
031: public static JFormattedTextField createIntegerField() {
032: JFormattedTextField field = new JFormattedTextField();
033: field.setFormatterFactory(INT_FORMATTER_FACTORY);
034: return field;
035: }
036:
037: public static JPanel getAddRemovePropertiesBar(
038: ActionListener listener, String prefix, String[] names) {
039: if (prefix == null)
040: prefix = "";
041: JButton[] buttons = new JButton[names.length];
042: for (int i = 0; i < buttons.length; i++) {
043: buttons[i] = new JButton(ResourceManager
044: .getString(names[i]));
045: buttons[i].setActionCommand(prefix + names[i]);
046: buttons[i].addActionListener(listener);
047: }
048: return ButtonBarFactory.buildAddRemovePropertiesBar(buttons[0],
049: buttons[1], buttons[2]);
050: }
051:
052: public static DefaultFormBuilder getDialogBuilder(String separator,
053: Container contentPane) {
054: FormLayout layout = new FormLayout(
055: "2dlu, left:max(40dlu;pref), 3dlu, 110dlu:grow, 7dlu");
056: DefaultFormBuilder builder = createBuilder(
057: (JPanel) contentPane, layout, separator);
058: builder.setDefaultDialogBorder();
059: return builder;
060: }
061:
062: public static DefaultFormBuilder getTwoColumnBuilder(
063: String separator, JPanel panel) {
064: FormLayout layout = new FormLayout(
065: "2dlu, left:pref, 3dlu, pref:grow, 2dlu", "");
066: DefaultFormBuilder builder = createBuilder(panel, layout,
067: separator);
068: return builder;
069: }
070:
071: private static DefaultFormBuilder createBuilder(JPanel panel,
072: FormLayout layout, String separator) {
073: DefaultFormBuilder builder = new DefaultFormBuilder(panel,
074: layout, ResourceManager.getBundle());
075: builder.setLeadingColumnOffset(1);
076: if (separator == null) {
077: builder.appendRow(builder.getLineGapSpec());
078: builder.nextLine();
079: } else {
080: builder.appendSeparator(separator);
081: }
082: return builder;
083: }
084:
085: public static JPanel getOKCancelBar(ActionListener listener,
086: String prefix) {
087: if (prefix == null)
088: prefix = "";
089: JButton ok = new JButton(ResourceManager.getString("ok"));
090: ok.setActionCommand(prefix + "ok");
091: JButton cancel = new JButton(ResourceManager
092: .getString("cancel"));
093: cancel.setActionCommand(prefix + "cancel");
094: ok.addActionListener(listener);
095: cancel.addActionListener(listener);
096: return ButtonBarFactory.buildOKCancelBar(ok, cancel);
097: }
098:
099: public static JPanel getButtonBar(ActionListener[] listener,
100: String prefix, String[] names) {
101: if (prefix == null)
102: prefix = "";
103: if (listener.length != names.length)
104: throw new IllegalArgumentException("listener.length="
105: + listener.length + " but names.length="
106: + names.length);
107: JButton[] buttons = new JButton[names.length];
108: for (int i = 0; i < buttons.length; i++) {
109: buttons[i] = new JButton(ResourceManager
110: .getString(names[i]));
111: buttons[i].setActionCommand(prefix + names[i]);
112: buttons[i].addActionListener(listener[i]);
113: }
114: return ButtonBarFactory.buildCenteredBar(buttons);
115: }
116:
117: public static JComponent createTablePanel(JTable table) {
118: Color background = UIManager.getColor("window");
119: JScrollPane scrollPane = new JScrollPane(table);
120: scrollPane.getViewport().setOpaque(false);
121: scrollPane.getViewport().setBackground(background);
122: scrollPane.setOpaque(false);
123: scrollPane.setCorner(JScrollPane.UPPER_RIGHT_CORNER,
124: new JPanel());
125: scrollPane.setCorner(JScrollPane.LOWER_RIGHT_CORNER,
126: new JPanel());
127: JPanel panel = new JPanel(new GridLayout(0, 1)) {
128: public void updateUI() {
129: super .updateUI();
130: setBackground(UIManager.getColor("window"));
131: }
132: };
133: panel.setPreferredSize(new Dimension(220, 100));
134: panel.add(scrollPane);
135: return panel;
136: }
137:
138: public static JTextField createReadOnlyTextField(int width) {
139: JTextField field = new JTextField() {
140: public boolean isFocusable() {
141: return false;
142: }
143:
144: protected void processEvent(AWTEvent e) {
145: }
146: };
147: field.setEditable(false);
148: field.setOpaque(false);
149: field.setForeground(UIManager.getColor("TextField.foreground"));
150: if (width != -1) {
151: field.setColumns(width);
152: }
153: return field;
154: }
155:
156: public static JTextField createReadOnlyTextField() {
157: return createReadOnlyTextField(-1);
158: }
159:
160: public static void htmlize(JComponent component) {
161: Font defaultFont = UIManager.getFont("Button.font");
162:
163: String stylesheet = "body { margin-top: 0; margin-bottom: 0; margin-left: 0; margin-right: 0; font-family: "
164: + defaultFont.getName()
165: + "; font-size: "
166: + defaultFont.getSize()
167: + "pt; }"
168: + "a, p, li { margin-top: 0; margin-bottom: 0; margin-left: 0; margin-right: 0; font-family: "
169: + defaultFont.getName()
170: + "; font-size: "
171: + defaultFont.getSize() + "pt; }";
172:
173: try {
174: HTMLDocument doc = null;
175: if (component instanceof JEditorPane) {
176: doc = (HTMLDocument) ((JEditorPane) component)
177: .getDocument();
178: } else {
179: View v = (View) component
180: .getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey);
181: if (v != null) {
182: doc = (HTMLDocument) v.getDocument();
183: }
184: }
185: if (doc != null) {
186: doc.getStyleSheet().loadRules(
187: new java.io.StringReader(stylesheet), null);
188: } // end of if (doc != null)
189: } catch (Exception e) {
190: e.printStackTrace();
191: }
192: }
193: }
|