1 package com.opensymphony.workflow.designer; 2 3 import java.awt.*; 4 import java.awt.event.ActionListener ; 5 import java.text.NumberFormat ; 6 import javax.swing.*; 7 import javax.swing.text.html.HTMLDocument ; 8 import javax.swing.text.View ; 9 import javax.swing.text.DefaultFormatterFactory ; 10 import javax.swing.text.NumberFormatter ; 11 12 import com.jgoodies.forms.factories.ButtonBarFactory; 13 import com.jgoodies.forms.builder.DefaultFormBuilder; 14 import com.jgoodies.forms.layout.FormLayout; 15 16 21 public class UIFactory 22 { 23 private static final DefaultFormatterFactory INT_FORMATTER_FACTORY; 24 25 static 26 { 27 NumberFormat format = NumberFormat.getIntegerInstance(); 28 format.setGroupingUsed(false); 29 INT_FORMATTER_FACTORY = new DefaultFormatterFactory (new NumberFormatter (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 listener, String prefix, String [] 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 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 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 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 listener, String 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 [] listener, String prefix, String [] names) 96 { 97 if(prefix == null) prefix = ""; 98 if(listener.length!=names.length) throw new IllegalArgumentException ("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 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 doc = null; 168 if(component instanceof JEditorPane) 169 { 170 doc = (HTMLDocument )((JEditorPane)component).getDocument(); 171 } 172 else 173 { 174 View v = (View )component.getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey); 175 if(v != null) 176 { 177 doc = (HTMLDocument )v.getDocument(); 178 } 179 } 180 if(doc != null) 181 { 182 doc.getStyleSheet().loadRules(new java.io.StringReader (stylesheet), null); 183 } } 185 catch(Exception e) 186 { 187 e.printStackTrace(); 188 } 189 } 190 } 191 | Popular Tags |