KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > designer > UIFactory


1 package com.opensymphony.workflow.designer;
2
3 import java.awt.*;
4 import java.awt.event.ActionListener JavaDoc;
5 import java.text.NumberFormat JavaDoc;
6 import javax.swing.*;
7 import javax.swing.text.html.HTMLDocument JavaDoc;
8 import javax.swing.text.View JavaDoc;
9 import javax.swing.text.DefaultFormatterFactory JavaDoc;
10 import javax.swing.text.NumberFormatter JavaDoc;
11
12 import com.jgoodies.forms.factories.ButtonBarFactory;
13 import com.jgoodies.forms.builder.DefaultFormBuilder;
14 import com.jgoodies.forms.layout.FormLayout;
15
16 /**
17  * @author Hani Suleiman (hani@formicary.net)
18  * Date: May 21 2003
19  * Time: 9:46:11 PM
20  */

21 public class UIFactory
22 {
23   private static final DefaultFormatterFactory JavaDoc INT_FORMATTER_FACTORY;
24
25   static
26   {
27     NumberFormat JavaDoc format = NumberFormat.getIntegerInstance();
28     format.setGroupingUsed(false);
29     INT_FORMATTER_FACTORY = new DefaultFormatterFactory JavaDoc(new NumberFormatter JavaDoc(format));
30   }
31
32   public static JFormattedTextField createIntegerField()
33   {
34     JFormattedTextField field = new JFormattedTextField();
35     field.setFormatterFactory(INT_FORMATTER_FACTORY);
36     return field;
37   }
38
39   public static JPanel getAddRemovePropertiesBar(ActionListener JavaDoc listener, String JavaDoc prefix, String JavaDoc[] names)
40   {
41     if(prefix == null) prefix = "";
42     JButton[] buttons = new JButton[names.length];
43     for(int i = 0; i < buttons.length; i++)
44     {
45       buttons[i] = new JButton(ResourceManager.getString(names[i]));
46       buttons[i].setActionCommand(prefix + names[i]);
47       buttons[i].addActionListener(listener);
48     }
49       return ButtonBarFactory.buildAddRemovePropertiesBar(buttons[0], buttons[1], buttons[2]);
50   }
51
52   public static DefaultFormBuilder getDialogBuilder(String JavaDoc separator, Container contentPane)
53   {
54     FormLayout layout = new FormLayout("2dlu, left:max(40dlu;pref), 3dlu, 110dlu:grow, 7dlu");
55     DefaultFormBuilder builder = createBuilder((JPanel)contentPane, layout, separator);
56     builder.setDefaultDialogBorder();
57     return builder;
58   }
59
60   public static DefaultFormBuilder getTwoColumnBuilder(String JavaDoc separator, JPanel panel)
61   {
62     FormLayout layout = new FormLayout("2dlu, left:pref, 3dlu, pref:grow, 2dlu", "");
63     DefaultFormBuilder builder = createBuilder(panel, layout, separator);
64     return builder;
65   }
66
67   private static DefaultFormBuilder createBuilder(JPanel panel, FormLayout layout, String JavaDoc separator)
68   {
69     DefaultFormBuilder builder = new DefaultFormBuilder(panel, layout, ResourceManager.getBundle());
70     builder.setLeadingColumnOffset(1);
71     if(separator==null)
72     {
73       builder.appendRow(builder.getLineGapSpec());
74       builder.nextLine();
75     }
76     else
77     {
78       builder.appendSeparator(separator);
79     }
80     return builder;
81   }
82
83   public static JPanel getOKCancelBar(ActionListener JavaDoc listener, String JavaDoc prefix)
84   {
85     if(prefix == null) prefix = "";
86     JButton ok = new JButton(ResourceManager.getString("ok"));
87     ok.setActionCommand(prefix + "ok");
88     JButton cancel = new JButton(ResourceManager.getString("cancel"));
89     cancel.setActionCommand(prefix + "cancel");
90     ok.addActionListener(listener);
91     cancel.addActionListener(listener);
92     return ButtonBarFactory.buildOKCancelBar(ok, cancel);
93   }
94
95   public static JPanel getButtonBar(ActionListener JavaDoc[] listener, String JavaDoc prefix, String JavaDoc[] names)
96   {
97     if(prefix == null) prefix = "";
98     if(listener.length!=names.length) throw new IllegalArgumentException JavaDoc("listener.length=" + listener.length + " but names.length=" + names.length);
99     JButton[] buttons = new JButton[names.length];
100     for(int i = 0; i < buttons.length; i++)
101     {
102       buttons[i] = new JButton(ResourceManager.getString(names[i]));
103       buttons[i].setActionCommand(prefix + names[i]);
104       buttons[i].addActionListener(listener[i]);
105     }
106     return ButtonBarFactory.buildCenteredBar(buttons);
107   }
108
109   public static JComponent createTablePanel(JTable table)
110   {
111     Color background = UIManager.getColor("window");
112     JScrollPane scrollPane = new JScrollPane(table);
113     scrollPane.getViewport().setOpaque(false);
114     scrollPane.getViewport().setBackground(background);
115     scrollPane.setOpaque(false);
116     scrollPane.setCorner(JScrollPane.UPPER_RIGHT_CORNER, new JPanel());
117     scrollPane.setCorner(JScrollPane.LOWER_RIGHT_CORNER, new JPanel());
118     JPanel panel = new JPanel(new GridLayout(0, 1))
119     {
120       public void updateUI()
121       {
122         super.updateUI();
123         setBackground(UIManager.getColor("window"));
124       }
125     };
126     panel.setPreferredSize(new Dimension(220, 100));
127     panel.add(scrollPane);
128     return panel;
129   }
130
131   public static JTextField createReadOnlyTextField(int width)
132   {
133     JTextField field = new JTextField()
134     {
135       public boolean isFocusable()
136       {
137         return false;
138       }
139
140       protected void processEvent(AWTEvent e)
141       {
142       }
143     };
144     field.setEditable(false);
145     field.setOpaque(false);
146     field.setForeground(UIManager.getColor("TextField.foreground"));
147     if(width!=-1)
148     {
149       field.setColumns(width);
150     }
151     return field;
152   }
153
154   public static JTextField createReadOnlyTextField()
155   {
156     return createReadOnlyTextField(-1);
157   }
158   
159   public static void htmlize(JComponent component)
160   {
161     Font defaultFont = UIManager.getFont("Button.font");
162
163     String JavaDoc stylesheet = "body { margin-top: 0; margin-bottom: 0; margin-left: 0; margin-right: 0; font-family: " + defaultFont.getName() + "; font-size: " + defaultFont.getSize() + "pt; }" + "a, p, li { margin-top: 0; margin-bottom: 0; margin-left: 0; margin-right: 0; font-family: " + defaultFont.getName() + "; font-size: " + defaultFont.getSize() + "pt; }";
164
165     try
166     {
167       HTMLDocument JavaDoc doc = null;
168       if(component instanceof JEditorPane)
169       {
170         doc = (HTMLDocument JavaDoc)((JEditorPane)component).getDocument();
171       }
172       else
173       {
174         View JavaDoc v = (View JavaDoc)component.getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey);
175         if(v != null)
176         {
177           doc = (HTMLDocument JavaDoc)v.getDocument();
178         }
179       }
180       if(doc != null)
181       {
182         doc.getStyleSheet().loadRules(new java.io.StringReader JavaDoc(stylesheet), null);
183       } // end of if (doc != null)
184
}
185     catch(Exception JavaDoc e)
186     {
187       e.printStackTrace();
188     }
189   }
190 }
191
Popular Tags