1 30 31 package com.jgoodies.forms.factories; 32 33 import java.awt.Color ; 34 import java.awt.GridBagConstraints ; 35 import java.awt.GridBagLayout ; 36 import java.lang.reflect.InvocationTargetException ; 37 import java.lang.reflect.Method ; 38 39 import javax.swing.*; 40 41 49 50 public class DefaultComponentFactory implements ComponentFactory { 51 52 55 private static final DefaultComponentFactory INSTANCE = 56 new DefaultComponentFactory(); 57 58 61 private static final boolean IS_BEFORE_14 = isBefore14(); 62 63 66 private static final char MNEMONIC_MARKER = '&'; 67 68 69 71 private DefaultComponentFactory() { 72 } 74 75 80 public static DefaultComponentFactory getInstance() { 81 return INSTANCE; 82 } 83 84 85 87 93 public JLabel createLabel(String textWithMnemonic) { 94 JLabel label = new JLabel(); 95 setTextAndMnemonic(label, textWithMnemonic); 96 return label; 97 } 98 99 106 public JLabel createTitle(String textWithMnemonic) { 107 return createTitle(textWithMnemonic, 0); 108 } 109 110 118 private JLabel createTitle(String textWithMnemonic, int gap) { 119 JLabel label = new TitleLabel(); 120 setTextAndMnemonic(label, textWithMnemonic); 121 label.setVerticalAlignment(SwingConstants.CENTER); 122 label.setBorder(BorderFactory.createEmptyBorder(1, 0, 1, gap)); 123 return label; 124 } 125 126 136 public JComponent createSeparator(String text) { 137 return createSeparator(text, SwingConstants.LEFT); 138 } 139 140 151 public JComponent createSeparator(String text, int alignment) { 152 JPanel header = new JPanel(new GridBagLayout ()); 153 GridBagConstraints gbc = new GridBagConstraints (); 154 gbc.weightx = 0.0; 155 gbc.weighty = 1.0; 156 gbc.anchor = GridBagConstraints.SOUTHWEST; 157 gbc.fill = GridBagConstraints.BOTH; 158 gbc.gridwidth = 1; 159 gbc.gridheight = 3; 160 if (text != null && text.length() > 0) { 161 header.add(createTitle(text, 4), gbc); 162 } 163 164 gbc.weightx = 1.0; 165 gbc.weighty = 1.0; 166 gbc.gridwidth = GridBagConstraints.REMAINDER; 167 gbc.gridheight = 1; 168 JSeparator separator = new JSeparator(); 169 header.add(Box.createGlue(), gbc); 170 gbc.weighty = 0.0; 171 header.add(separator, gbc); 172 gbc.weighty = 1.0; 173 header.add(Box.createGlue(), gbc); 174 175 return header; 176 } 177 178 179 181 191 public static void setTextAndMnemonic( 192 JLabel label, 193 String textWithMnemonic) { 194 int markerIndex = textWithMnemonic.indexOf(MNEMONIC_MARKER); 195 if (markerIndex == -1) { 197 label.setText(textWithMnemonic); 198 return; 199 } 200 int mnemonicIndex = -1; 201 int begin = 0; 202 int end; 203 int length = textWithMnemonic.length(); 204 StringBuffer buffer = new StringBuffer (); 205 do { 206 if (markerIndex + 1 < length 208 && textWithMnemonic.charAt(markerIndex + 1) == MNEMONIC_MARKER) { 209 end = markerIndex + 1; 210 } else { 211 end = markerIndex; 212 if (mnemonicIndex == -1) 213 mnemonicIndex = markerIndex; 214 } 215 buffer.append(textWithMnemonic.substring(begin, end)); 216 begin = end + 1; 217 markerIndex = begin < length 218 ? textWithMnemonic.indexOf(MNEMONIC_MARKER, begin) 219 : -1; 220 } while (markerIndex != -1); 221 buffer.append(textWithMnemonic.substring(begin)); 222 223 label.setText(buffer.toString()); 224 if ((mnemonicIndex != -1) && (mnemonicIndex + 1 < length)) { 225 label.setDisplayedMnemonic( 226 textWithMnemonic.charAt(mnemonicIndex + 1)); 227 setDisplayedMnemonicIndex(label, mnemonicIndex); 228 } 229 } 230 231 244 private static void setDisplayedMnemonicIndex(JLabel label, 245 int displayedMnemonicIndex) { 246 Integer index = new Integer (displayedMnemonicIndex); 247 if (IS_BEFORE_14) { 248 label.putClientProperty("displayedMnemonicIndex", index); 249 return; 250 } 251 try { 252 Method method = AbstractButton.class.getMethod( 253 "setDisplayedMnemonicIndex", new Class []{}); 254 method.invoke(label, new Integer []{index}); 255 return; 256 } catch (NoSuchMethodException e) { 257 } catch (InvocationTargetException e) { 259 } catch (IllegalAccessException e) { 261 } 263 } 264 265 268 private static boolean isBefore14() { 269 String version = System.getProperty("java.version"); 270 return version.startsWith("1.2") || version.startsWith("1.3"); 271 } 272 273 private static class TitleLabel extends JLabel { 275 276 private TitleLabel() { 277 } 279 280 private TitleLabel(String text) { 281 super(text); 282 } 283 284 public void updateUI() { 285 super.updateUI(); 286 Color foreground = 287 UIManager.getColor("TitledBorder.titleColor"); 288 if (foreground != null) 289 setForeground(foreground); 290 setFont(UIManager.getFont("TitledBorder.font")); 291 } 292 293 } 294 295 } | Popular Tags |