1 package org.columba.core.gui.dialog; 19 20 import java.awt.Dimension ; 21 import java.awt.Font ; 22 import java.awt.GraphicsEnvironment ; 23 import java.awt.GridBagConstraints ; 24 import java.awt.GridBagLayout ; 25 import java.awt.Insets ; 26 import java.awt.event.ActionEvent ; 27 import java.awt.event.ActionListener ; 28 import java.awt.event.KeyEvent ; 29 import java.util.Locale ; 30 31 import javax.swing.JButton ; 32 import javax.swing.JComponent ; 33 import javax.swing.JDialog ; 34 import javax.swing.JLabel ; 35 import javax.swing.JList ; 36 import javax.swing.JScrollPane ; 37 import javax.swing.JTextField ; 38 import javax.swing.KeyStroke ; 39 import javax.swing.event.ListSelectionEvent ; 40 import javax.swing.event.ListSelectionListener ; 41 42 import org.columba.core.gui.base.ButtonWithMnemonic; 43 import org.columba.core.resourceloader.GlobalResourceLoader; 44 45 46 public class FontSelectionDialog extends JDialog implements ActionListener , 47 ListSelectionListener { 48 public static final int APPROVE_OPTION = 1; 49 50 public static final int CANCEL_OPTION = 0; 51 52 private static final String RESOURCE_BUNDLE_PATH = "org.columba.core.i18n.dialog"; 53 54 protected JList fontList; 55 56 protected JList styleList; 57 58 protected JList sizeList; 59 60 protected JTextField preview; 61 62 protected JTextField fontName; 63 64 protected JTextField styleName; 65 66 protected JTextField sizeName; 67 68 protected JLabel fontLabel; 69 70 protected JLabel sizeLabel; 71 72 protected JLabel styleLabel; 73 74 protected JLabel previewLabel; 75 76 protected JButton okButton; 77 78 protected JButton cancelButton; 79 80 protected Font font; 81 82 protected int status; 83 84 public FontSelectionDialog(JDialog parent, Font f) { 85 super(parent, true); 86 87 setTitle(GlobalResourceLoader.getString(RESOURCE_BUNDLE_PATH, "font", 88 "title")); 89 90 font = f; 91 92 setSize(450, 325); 93 94 initData(); 95 initComponents(); 96 97 setLocationRelativeTo(null); 98 } 99 100 private void initComponents() { 101 JScrollPane scroller; 102 GridBagLayout gridbag = new GridBagLayout (); 103 GridBagConstraints c = new GridBagConstraints (); 104 105 final double w1 = 3.0; 106 final double w2 = 2.0; 107 final double w3 = 0.7; 108 109 getContentPane().setLayout(gridbag); 110 111 c.insets = new Insets (1, 3, 1, 3); 113 114 c.gridheight = 1; 116 c.weighty = 1.0; 117 c.weightx = w1; 118 c.fill = GridBagConstraints.HORIZONTAL; 119 c.gridwidth = 1; 120 gridbag.setConstraints(fontLabel, c); 121 getContentPane().add(fontLabel); 122 123 c.weightx = w2; 124 c.fill = GridBagConstraints.HORIZONTAL; 125 c.gridwidth = GridBagConstraints.RELATIVE; 126 gridbag.setConstraints(styleLabel, c); 127 getContentPane().add(styleLabel); 128 129 c.weightx = w3; 130 c.fill = GridBagConstraints.HORIZONTAL; 131 c.gridwidth = GridBagConstraints.REMAINDER; 132 gridbag.setConstraints(sizeLabel, c); 133 getContentPane().add(sizeLabel); 134 135 c.weightx = w1; 137 c.fill = GridBagConstraints.HORIZONTAL; 138 c.gridwidth = 1; 139 gridbag.setConstraints(fontName, c); 140 getContentPane().add(fontName); 141 142 c.weightx = w2; 143 c.fill = GridBagConstraints.HORIZONTAL; 144 c.gridwidth = GridBagConstraints.RELATIVE; 145 gridbag.setConstraints(styleName, c); 146 getContentPane().add(styleName); 147 148 c.weightx = w3; 149 c.fill = GridBagConstraints.HORIZONTAL; 150 c.gridwidth = GridBagConstraints.REMAINDER; 151 gridbag.setConstraints(sizeName, c); 152 getContentPane().add(sizeName); 153 154 c.weighty = 6.0; 156 157 scroller = new JScrollPane (fontList); 158 c.weightx = w1; 159 c.fill = GridBagConstraints.BOTH; 160 c.gridwidth = 1; 161 gridbag.setConstraints(scroller, c); 162 getContentPane().add(scroller); 163 164 scroller = new JScrollPane (styleList); 165 c.weightx = w2; 166 c.gridwidth = GridBagConstraints.RELATIVE; 167 gridbag.setConstraints(scroller, c); 168 getContentPane().add(scroller); 169 170 scroller = new JScrollPane (sizeList); 171 c.weightx = w3; 172 c.gridwidth = GridBagConstraints.REMAINDER; 173 gridbag.setConstraints(scroller, c); 174 getContentPane().add(scroller); 175 176 c.weighty = 1.0; 178 c.fill = GridBagConstraints.HORIZONTAL; 179 c.gridwidth = GridBagConstraints.REMAINDER; 180 gridbag.setConstraints(previewLabel, c); 181 getContentPane().add(previewLabel); 182 183 c.weightx = 1.0; 185 c.weighty = 5.0; 186 c.gridwidth = GridBagConstraints.REMAINDER; 187 c.gridheight = GridBagConstraints.RELATIVE; 188 c.fill = GridBagConstraints.BOTH; 189 gridbag.setConstraints(preview, c); 190 getContentPane().add(preview); 191 192 okButton = new ButtonWithMnemonic(GlobalResourceLoader.getString( 194 "global", "global", "ok")); 195 okButton.addActionListener(this); 196 197 c.weightx = 1.0; 198 c.weighty = 1.0; 199 c.insets = new Insets (10, 5, 10, 5); 200 c.anchor = GridBagConstraints.EAST; 201 c.gridwidth = 1; 202 c.gridheight = GridBagConstraints.REMAINDER; 203 c.fill = GridBagConstraints.NONE; 204 gridbag.setConstraints(okButton, c); 205 getContentPane().add(okButton); 206 207 cancelButton = new ButtonWithMnemonic(GlobalResourceLoader.getString( 208 "global", "global", "cancel")); 209 cancelButton.addActionListener(this); 210 211 c.weightx = 1.0; 212 c.anchor = GridBagConstraints.WEST; 213 c.gridwidth = GridBagConstraints.REMAINDER; 214 c.gridheight = GridBagConstraints.REMAINDER; 215 c.fill = GridBagConstraints.NONE; 216 gridbag.setConstraints(cancelButton, c); 217 getContentPane().add(cancelButton); 218 219 Dimension okSize; 220 Dimension cancelSize; 221 222 okSize = okButton.getPreferredSize(); 223 cancelSize = cancelButton.getPreferredSize(); 224 225 if (okSize.width < cancelSize.width) { 226 okSize.width = cancelSize.width; 227 okButton.setPreferredSize(okSize); 228 } 229 230 getRootPane().setDefaultButton(okButton); 231 getRootPane().registerKeyboardAction(this, 232 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), 233 JComponent.WHEN_IN_FOCUSED_WINDOW); 234 } 235 236 void initData() { 237 GraphicsEnvironment gEnv = GraphicsEnvironment 238 .getLocalGraphicsEnvironment(); 239 240 String [] envfonts = gEnv.getAvailableFontFamilyNames(Locale 242 .getDefault()); 243 fontList = new JList (envfonts); 244 fontList.setSelectedIndex(0); 245 246 styleList = new JList (new Object [] { 247 GlobalResourceLoader.getString(RESOURCE_BUNDLE_PATH, "font", 248 "plain"), 249 GlobalResourceLoader.getString(RESOURCE_BUNDLE_PATH, "font", 250 "bold"), 251 GlobalResourceLoader.getString(RESOURCE_BUNDLE_PATH, "font", 252 "italic"), 253 GlobalResourceLoader.getString(RESOURCE_BUNDLE_PATH, "font", 254 "bold_italic") }); 255 styleList.setSelectedIndex(0); 256 257 Object [] sizes = new String [12]; 259 260 for (int i = 7; i < 19; i++) { 261 sizes[i - 7] = Integer.toString(i); 262 } 263 264 sizeList = new JList (sizes); 265 sizeList.setSelectedIndex(0); 266 267 preview = new JTextField ("abcdefgh ABCDEFGH"); 268 preview.setHorizontalAlignment(JTextField.CENTER); 269 270 styleName = new JTextField (); 271 sizeName = new JTextField (); 272 fontName = new JTextField (); 273 274 if (font == null) { 275 font = preview.getFont(); 276 } 277 278 fontList.setSelectedValue(font.getName(), true); 279 styleList.setSelectedIndex(font.getStyle()); 280 sizeList.setSelectedValue(new Integer (font.getSize()).toString(), true); 281 282 fontName.setText((String ) fontList.getSelectedValue()); 283 styleName.setText((String ) styleList.getSelectedValue()); 284 sizeName.setText((String ) sizeList.getSelectedValue()); 285 286 styleList.addListSelectionListener(this); 287 sizeList.addListSelectionListener(this); 288 fontList.addListSelectionListener(this); 289 290 fontLabel = new JLabel (GlobalResourceLoader.getString( 291 RESOURCE_BUNDLE_PATH, "font", "font")); 292 sizeLabel = new JLabel (GlobalResourceLoader.getString( 293 RESOURCE_BUNDLE_PATH, "font", "size")); 294 styleLabel = new JLabel (GlobalResourceLoader.getString( 295 RESOURCE_BUNDLE_PATH, "font", "style")); 296 previewLabel = new JLabel (GlobalResourceLoader.getString( 297 RESOURCE_BUNDLE_PATH, "font", "preview")); 298 } 299 300 public void actionPerformed(ActionEvent e) { 301 Object source = e.getSource(); 302 303 if (source == okButton) { 304 status = APPROVE_OPTION; 305 } else if (source == cancelButton) { 306 status = CANCEL_OPTION; 307 } 308 309 dispose(); 310 } 311 312 public int showDialog() { 313 setVisible(true); 314 315 return status; 316 } 317 318 public Font getSelectedFont() { 319 return font; 320 } 321 322 public void valueChanged(ListSelectionEvent e) { 323 Object list = e.getSource(); 324 String fontchoice; 325 int stChoice; 326 int siChoice; 327 328 if (list == fontList) { 329 fontName.setText((String ) fontList.getSelectedValue()); 330 } else if (list == styleList) { 331 styleName.setText((String ) styleList.getSelectedValue()); 332 } else if (list == sizeList) { 333 sizeName.setText((String ) sizeList.getSelectedValue()); 334 } 335 336 fontchoice = fontName.getText(); 337 stChoice = styleList.getSelectedIndex(); 338 339 siChoice = new Integer (sizeName.getText()).intValue(); 340 341 font = new Font (fontchoice, stChoice, siChoice); 342 343 preview.setFont(font); 344 preview.repaint(); 345 } 346 } | Popular Tags |