1 24 25 package org.gjt.sp.jedit.gui; 26 27 import java.awt.event.*; 29 import java.awt.*; 30 import java.util.Vector ; 31 import javax.swing.border.*; 32 import javax.swing.event.*; 33 import javax.swing.*; 34 import org.gjt.sp.jedit.*; 35 import org.gjt.sp.util.Log; 36 38 44 public class FontSelector extends JButton 45 { 46 51 public FontSelector(Font font) 52 { 53 this(font,false); 54 } 56 63 public FontSelector(Font font, boolean antiAlias) 64 { 65 setFont(font); 66 this.antiAlias = antiAlias; 67 68 updateText(); 69 70 setRequestFocusEnabled(false); 71 72 addActionListener(new ActionHandler()); 73 } 75 public void paintComponent(Graphics g) 77 { 78 setAntiAliasEnabled(g); 79 super.paintComponent(g); 80 } 82 public boolean isAntiAliasEnabled() 84 { 85 return antiAlias; 86 } 88 public void setAntiAliasEnabled(boolean antiAlias) 90 { 91 this.antiAlias = antiAlias; 92 } 94 private void updateText() 96 { 97 Font font = getFont(); 98 String styleString; 99 switch(font.getStyle()) 100 { 101 case Font.PLAIN: 102 styleString = jEdit.getProperty("font-selector.plain"); 103 break; 104 case Font.BOLD: 105 styleString = jEdit.getProperty("font-selector.bold"); 106 break; 107 case Font.ITALIC: 108 styleString = jEdit.getProperty("font-selector.italic"); 109 break; 110 case Font.BOLD | Font.ITALIC: 111 styleString = jEdit.getProperty("font-selector.bolditalic"); 112 break; 113 default: 114 styleString = "UNKNOWN!!!???"; 115 break; 116 } 117 118 setText(font.getName() + " " + font.getSize() + " " + styleString); 119 } 121 void setAntiAliasEnabled(Graphics g) 123 { 124 if (antiAlias) 125 { 126 Graphics2D g2 = (Graphics2D)g; 127 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 128 RenderingHints.VALUE_ANTIALIAS_ON); 129 } 130 } 132 private boolean antiAlias; 133 134 class ActionHandler implements ActionListener 136 { 137 public void actionPerformed(ActionEvent evt) 138 { 139 Font font; 140 141 JDialog dialog = GUIUtilities.getParentDialog(FontSelector.this); 142 if(dialog == null) 143 { 144 font = new FontSelectorDialog( 145 JOptionPane.getFrameForComponent( 146 FontSelector.this),getFont(), 147 FontSelector.this) 148 .getSelectedFont(); 149 } 150 else 151 { 152 font = new FontSelectorDialog(dialog,getFont(), 153 FontSelector.this) 154 .getSelectedFont(); 155 } 156 157 if(font != null) 158 { 159 setFont(font); 160 updateText(); 161 } 162 } 163 } } 166 class FontSelectorDialog extends EnhancedDialog 168 { 169 public FontSelectorDialog(Frame parent, Font font) 171 { 172 super(parent,jEdit.getProperty("font-selector.title"),true); 173 init(font); 174 } 176 public FontSelectorDialog(Dialog parent, Font font) 178 { 179 super(parent,jEdit.getProperty("font-selector.title"),true); 180 init(font); 181 } 183 public FontSelectorDialog(Frame parent, Font font, 185 FontSelector fontSelector) 186 { 187 super(parent,jEdit.getProperty("font-selector.title"),true); 188 this.fontSelector = fontSelector; 189 init(font); 190 } 192 public FontSelectorDialog(Dialog parent, Font font, 194 FontSelector fontSelector) 195 { 196 super(parent,jEdit.getProperty("font-selector.title"),true); 197 this.fontSelector = fontSelector; 198 init(font); 199 } 201 public void ok() 203 { 204 isOK = true; 205 dispose(); 206 } 208 public void cancel() 210 { 211 dispose(); 212 } 214 public Font getSelectedFont() 216 { 217 if(!isOK) 218 return null; 219 220 int size; 221 try 222 { 223 size = Integer.parseInt(sizeField.getText()); 224 } 225 catch(Exception e) 226 { 227 size = 12; 228 } 229 230 return new Font(familyField.getText(),styleList 231 .getSelectedIndex(),size); 232 } 234 236 private FontSelector fontSelector; 238 private boolean isOK; 239 private JTextField familyField; 240 private JList familyList; 241 private JTextField sizeField; 242 private JList sizeList; 243 private JTextField styleField; 244 private JList styleList; 245 private JLabel preview; 246 private JButton ok; 247 private JButton cancel; 248 250 254 private static final String [] HIDEFONTS = { 255 ".bold", 256 ".italic" 257 }; 258 259 private void init(Font font) 261 { 262 JPanel content = new JPanel(new BorderLayout()); 263 content.setBorder(new EmptyBorder(12,12,12,12)); 264 setContentPane(content); 265 266 JPanel listPanel = new JPanel(new GridLayout(1,3,6,6)); 267 268 String [] fonts; 269 try 270 { 271 fonts = getFontList(); 272 } 273 catch(Exception e) 274 { 275 Log.log(Log.ERROR,this,"Broken Java implementation!"); 276 277 Log.log(Log.ERROR,this,e); 278 279 280 fonts = new String [] { "Broken Java implementation!" }; 281 } 282 283 JPanel familyPanel = createTextFieldAndListPanel( 284 "font-selector.family", 285 familyField = new JTextField(), 286 familyList = new JList(fonts)); 287 listPanel.add(familyPanel); 288 289 String [] sizes = { "9", "10", "12", "14", "16", "18", "24" }; 290 JPanel sizePanel = createTextFieldAndListPanel( 291 "font-selector.size", 292 sizeField = new JTextField(), 293 sizeList = new JList(sizes)); 294 listPanel.add(sizePanel); 295 296 String [] styles = { 297 jEdit.getProperty("font-selector.plain"), 298 jEdit.getProperty("font-selector.bold"), 299 jEdit.getProperty("font-selector.italic"), 300 jEdit.getProperty("font-selector.bolditalic") 301 }; 302 303 JPanel stylePanel = createTextFieldAndListPanel( 304 "font-selector.style", 305 styleField = new JTextField(), 306 styleList = new JList(styles)); 307 styleField.setEditable(false); 308 listPanel.add(stylePanel); 309 310 familyList.setSelectedValue(font.getFamily(),true); 311 familyField.setText(font.getFamily()); 312 sizeList.setSelectedValue(String.valueOf(font.getSize()),true); 313 sizeField.setText(String.valueOf(font.getSize())); 314 styleList.setSelectedIndex(font.getStyle()); 315 styleField.setText((String )styleList.getSelectedValue()); 316 317 ListHandler listHandler = new ListHandler(); 318 familyList.addListSelectionListener(listHandler); 319 sizeList.addListSelectionListener(listHandler); 320 styleList.addListSelectionListener(listHandler); 321 322 content.add(BorderLayout.NORTH,listPanel); 323 324 preview = new JLabel(jEdit.getProperty("font-selector.long-text")) { 325 public void paintComponent(Graphics g) 326 { 327 if(fontSelector != null) 328 fontSelector.setAntiAliasEnabled(g); 329 super.paintComponent(g); 330 } 331 }; 332 preview.setBorder(new TitledBorder(jEdit.getProperty( 333 "font-selector.preview"))); 334 335 updatePreview(); 336 337 Dimension prefSize = preview.getPreferredSize(); 338 prefSize.height = 50; 339 preview.setPreferredSize(prefSize); 340 341 content.add(BorderLayout.CENTER,preview); 342 343 JPanel buttons = new JPanel(); 344 buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS)); 345 buttons.setBorder(new EmptyBorder(12,0,0,0)); 346 buttons.add(Box.createGlue()); 347 348 ok = new JButton(jEdit.getProperty("common.ok")); 349 ok.addActionListener(new ActionHandler()); 350 getRootPane().setDefaultButton(ok); 351 buttons.add(ok); 352 353 buttons.add(Box.createHorizontalStrut(6)); 354 355 cancel = new JButton(jEdit.getProperty("common.cancel")); 356 cancel.addActionListener(new ActionHandler()); 357 buttons.add(cancel); 358 359 buttons.add(Box.createGlue()); 360 361 content.add(BorderLayout.SOUTH,buttons); 362 363 pack(); 364 setLocationRelativeTo(getParent()); 365 setVisible(true); 366 } 368 private String [] getFontList() 370 { 371 String [] nameArray = GraphicsEnvironment 372 .getLocalGraphicsEnvironment() 373 .getAvailableFontFamilyNames(); 374 Vector nameVector = new Vector (nameArray.length); 375 376 for(int i = 0, j; i < nameArray.length; i++) 377 { 378 for(j = 0; j < HIDEFONTS.length; j++) 379 { 380 if(nameArray[i].indexOf(HIDEFONTS[j]) >= 0) 381 break; 382 } 383 384 if(j == HIDEFONTS.length) 385 nameVector.addElement(nameArray[i]); 386 } 387 388 String [] _array = new String [nameVector.size()]; 389 nameVector.copyInto(_array); 390 return _array; 391 } 393 private JPanel createTextFieldAndListPanel(String label, 395 JTextField textField, JList list) 396 { 397 GridBagLayout layout = new GridBagLayout(); 398 JPanel panel = new JPanel(layout); 399 400 GridBagConstraints cons = new GridBagConstraints(); 401 cons.gridx = cons.gridy = 0; 402 cons.gridwidth = cons.gridheight = 1; 403 cons.fill = GridBagConstraints.BOTH; 404 cons.weightx = 1.0f; 405 406 JLabel _label = new JLabel(jEdit.getProperty(label)); 407 layout.setConstraints(_label,cons); 408 panel.add(_label); 409 410 cons.gridy = 1; 411 Component vs = Box.createVerticalStrut(6); 412 layout.setConstraints(vs,cons); 413 panel.add(vs); 414 415 cons.gridy = 2; 416 layout.setConstraints(textField,cons); 417 panel.add(textField); 418 419 cons.gridy = 3; 420 vs = Box.createVerticalStrut(6); 421 layout.setConstraints(vs,cons); 422 panel.add(vs); 423 424 cons.gridy = 4; 425 cons.gridheight = GridBagConstraints.REMAINDER; 426 cons.weighty = 1.0f; 427 JScrollPane scroller = new JScrollPane(list); 428 layout.setConstraints(scroller,cons); 429 panel.add(scroller); 430 431 return panel; 432 } 434 private void updatePreview() 436 { 437 String family = familyField.getText(); 438 int size; 439 try 440 { 441 size = Integer.parseInt(sizeField.getText()); 442 } 443 catch(Exception e) 444 { 445 size = 12; 446 } 447 int style = styleList.getSelectedIndex(); 448 449 preview.setFont(new Font(family,style,size)); 450 } 452 454 class ActionHandler implements ActionListener 456 { 457 public void actionPerformed(ActionEvent evt) 458 { 459 if(evt.getSource() == ok) 460 ok(); 461 else if(evt.getSource() == cancel) 462 cancel(); 463 } 464 } 466 class ListHandler implements ListSelectionListener 468 { 469 public void valueChanged(ListSelectionEvent evt) 470 { 471 Object source = evt.getSource(); 472 if(source == familyList) 473 { 474 String family = (String )familyList.getSelectedValue(); 475 if(family != null) 476 familyField.setText(family); 477 } 478 else if(source == sizeList) 479 { 480 String size = (String )sizeList.getSelectedValue(); 481 if(size != null) 482 sizeField.setText(size); 483 } 484 else if(source == styleList) 485 { 486 String style = (String )styleList.getSelectedValue(); 487 if(style != null) 488 styleField.setText(style); 489 } 490 491 updatePreview(); 492 } 493 } } | Popular Tags |