1 package snow.utils.gui; 2 3 import javax.swing.*; 4 import java.awt.*; 5 import java.awt.event.*; 6 import javax.swing.event.*; 7 8 9 public class FontSelector extends JDialog 10 { 11 12 private static Font DefaultFont = new Font("Dialog",Font.PLAIN,12); 13 private static FontSelector dialog; 14 private static String NameValue; 15 private static int StyleValue; 16 private static int SizeValue; 17 private static String SaveNameValue; 18 private static int SaveStyleValue; 19 private static int SaveSizeValue; 20 private static boolean FontChosen = false; 21 22 23 private JList theFontNamesList; 24 private JTextField SizeField; 25 private JList styleList; 26 private JCheckBox fontBold, fontItalic; 27 private JLabel sampleTextLabel; 28 private Container contentPane; 29 final JButton cancelButton = new JButton("Cancel"); 30 final JButton setButton = new JButton("Set"); 31 32 33 34 35 36 private FontSelector( Frame frame, 37 String [] fontNames, 38 String title ) 39 { 40 super(frame, title, true); 41 42 44 cancelButton.addActionListener(new ActionListener() 45 { 46 public void actionPerformed(ActionEvent e) 47 { 48 FontChosen = false; 49 FontSelector.dialog.setVisible(false); 50 } 51 }); 52 53 setButton.addActionListener(new ActionListener() 54 { 55 public void actionPerformed(ActionEvent e) 56 { 57 Object [] selected = theFontNamesList.getSelectedValues(); 59 if ( (selected != null) && (selected.length > 0) ) 60 { 61 boolean valid = true; 62 NameValue = selected[0].toString(); 63 try 64 { 65 SizeValue = Integer.parseInt(SizeField.getText()); 66 if ( (SizeValue > 0) && (SizeValue < 65) ) 67 { 68 StyleValue = (fontBold.isSelected() ? Font.BOLD : 0) + 69 (fontItalic.isSelected() ? Font.ITALIC : 0); 70 FontChosen = true; 72 FontSelector.dialog.setVisible(false); 73 } 74 else 75 { 76 valid = false; 77 } 78 } 79 catch (Exception ex) 80 { 81 valid = false; 82 } 83 if (!valid) 84 { 85 JOptionPane.showMessageDialog(FontSelector.this, "Please enter valid values.", "Invalid Value", JOptionPane.INFORMATION_MESSAGE); 86 SizeField.requestFocus(); 87 SizeField.selectAll(); 88 } 89 } 90 } 91 }); 92 getRootPane().setDefaultButton(setButton); 93 94 95 theFontNamesList = new JList(fontNames); 97 theFontNamesList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); 98 99 this.theFontNamesList.setSelectedValue(NameValue,true); 101 102 theFontNamesList.addListSelectionListener( new MyNameListSelectionListener()); 103 JScrollPane listScroller = new JScrollPane(theFontNamesList); 104 105 106 Dimension listDim = new Dimension( SizeValue * 30, SizeValue * 10 ); 108 listScroller.setPreferredSize(listDim); 109 listScroller.setMinimumSize(listDim); 110 listScroller.setMaximumSize(listDim); 111 112 JPanel listPane = new JPanel(); 117 listPane.setLayout(new BoxLayout(listPane, BoxLayout.Y_AXIS)); 118 listPane.add(Box.createRigidArea(new Dimension(0,5))); 119 listPane.add(listScroller); 120 listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); 121 122 124 Integer theValue = new Integer (SizeValue); 125 SizeField = new JTextField(theValue.toString(), 4); 126 SizeField.setHorizontalAlignment(SwingConstants.RIGHT); 127 128 fontBold = new JCheckBox("Bold"); 129 if( (StyleValue & Font.BOLD) > 0) 130 { 131 fontBold.setSelected(true); 132 } else 133 { 134 fontBold.setSelected(false); 135 } 136 137 fontItalic = new JCheckBox("Italic"); 138 if( (StyleValue & Font.ITALIC) > 0) 139 { 140 fontItalic.setSelected(true); 141 } else 142 { 143 fontItalic.setSelected(false); 144 } 145 146 ActionListener fontActionListener = new ActionListener() 149 { 150 public void actionPerformed(ActionEvent evt) 151 { 152 Object [] selected = theFontNamesList.getSelectedValues(); 153 if ( (selected != null) && (selected.length > 0) ) 154 { 155 boolean valid = true; 156 NameValue = selected[0].toString(); 157 try 158 { 159 SizeValue = Integer.parseInt(SizeField.getText()); 160 161 if ( (SizeValue > 0) && (SizeValue < 65) ) 162 { 163 StyleValue = (fontBold.isSelected() ? Font.BOLD : 0) + 164 (fontItalic.isSelected() ? Font.ITALIC : 0); 165 Font selectedFont = new Font(NameValue,StyleValue,SizeValue); 166 sampleTextLabel.setFont(selectedFont); 167 dialog.pack(); 168 dialog.repaint(); 169 sampleTextLabel.repaint(); 170 } 171 else 172 { 173 valid = false; 174 } 175 } 176 catch (Exception ex) 177 { 178 valid = false; 179 } 180 if (!valid) 181 { 182 JOptionPane.showMessageDialog(FontSelector.this, "Please enter valid values.", "Invalid Value", JOptionPane.INFORMATION_MESSAGE); 183 SizeField.requestFocus(); 184 SizeField.selectAll(); 185 } 186 } 187 } 188 }; 189 SizeField.addActionListener(fontActionListener); 190 fontBold.addActionListener(fontActionListener); 191 fontItalic.addActionListener(fontActionListener); 192 193 JPanel stylePane = new JPanel(); 194 stylePane.setLayout(new FlowLayout(FlowLayout.CENTER,20,20)); 195 JLabel txtlabel = new JLabel(" Font Size : ",JLabel.CENTER); 196 txtlabel.setFont( new Font(NameValue,StyleValue,SizeValue) ); 197 txtlabel.setForeground(Color.black); 198 stylePane.add(txtlabel); 199 stylePane.add(SizeField); 200 stylePane.add(fontBold); 201 stylePane.add(fontItalic); 202 203 205 JPanel buttonPane = new JPanel(); 206 buttonPane.add(cancelButton); 207 buttonPane.add(setButton); 208 209 210 JPanel samplePane = new JPanel(); 212 samplePane.setLayout(new FlowLayout(FlowLayout.CENTER,10,10)); 213 sampleTextLabel = new JLabel("This is a sample text.",JLabel.CENTER); 214 sampleTextLabel.setFont( new Font(NameValue,StyleValue,SizeValue) ); 215 sampleTextLabel.setForeground(Color.black); 216 samplePane.add(sampleTextLabel); 217 218 219 JPanel bottomPane = new JPanel(); 220 bottomPane.setLayout(new BorderLayout(5,5)); 221 bottomPane.add(samplePane,BorderLayout.NORTH ); 222 bottomPane.add(buttonPane,BorderLayout.SOUTH ); 223 224 225 this.getContentPane().setLayout(new BorderLayout(5,5)); 226 227 contentPane = getContentPane(); 229 contentPane.add(listPane ,BorderLayout.NORTH ); 230 contentPane.add(stylePane ,BorderLayout.CENTER); 231 contentPane.add(bottomPane ,BorderLayout.SOUTH ); 232 233 pack(); 234 } 235 236 237 class MyNameListSelectionListener implements ListSelectionListener 238 { 239 public void valueChanged(ListSelectionEvent evt) 240 { 241 if( !evt.getValueIsAdjusting()) 242 { 243 JList thisList = (JList)evt.getSource(); 244 Object [] selected = thisList.getSelectedValues(); 245 246 if ( (selected != null) && (selected.length > 0) ) 247 { 248 try 249 { 250 FontSelector.NameValue = selected[0].toString(); 251 FontSelector.SizeValue = Integer.parseInt(SizeField.getText()); 252 FontSelector.StyleValue = (fontBold.isSelected() ? Font.BOLD : 0) + 253 (fontItalic.isSelected() ? Font.ITALIC : 0); 254 255 Font selectedFont = new Font(NameValue,StyleValue,SizeValue); 256 sampleTextLabel.setFont(selectedFont); 257 dialog.pack(); 258 dialog.repaint(); 259 sampleTextLabel.repaint(); 260 } 261 catch (Exception ex) {} 262 } 263 } 264 } 265 } 266 267 268 269 public static Font showDialog(Component comp) 270 { 271 if (dialog != null) 272 { 273 dialog.setLocationRelativeTo(comp); 274 int selIndex = dialog.theFontNamesList.getSelectedIndex(); 275 if( selIndex >= 0 ) 276 { 277 Rectangle selRect = dialog.theFontNamesList.getCellBounds(selIndex,selIndex); 278 dialog.theFontNamesList.scrollRectToVisible( selRect ); 279 } 280 dialog.setVisible(true); 281 } else 282 { 283 System.out.println("ListDialog requires you to call initialize " 284 + "before calling showDialog."); 285 } 286 if (FontChosen) 287 { 288 return new Font(NameValue,StyleValue,SizeValue); 289 } else 290 { 291 return new Font(SaveNameValue,SaveStyleValue,SaveSizeValue); 292 } 293 } 294 295 296 297 298 299 public static Font chooseFont(Component parentFrame) 300 { 302 return FontSelector.chooseFont(parentFrame,DefaultFont); 303 } 304 305 306 307 public static Font chooseFont( Component parentFrame, 308 Font defaultFont ) 309 { 311 DefaultFont = defaultFont; NameValue = DefaultFont.getName(); SizeValue = DefaultFont.getSize(); if( SizeValue < 6 ) { SizeValue = 6; } 315 if( SizeValue > 100 ) { SizeValue = 100; } 316 StyleValue = DefaultFont.getStyle(); SaveNameValue = NameValue; SaveSizeValue = SizeValue; SaveStyleValue = StyleValue; String [] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); 322 Frame frame = JOptionPane.getFrameForComponent(parentFrame); 323 dialog = new FontSelector( frame, 324 fontNames, 325 "Choose a Font"); 326 return FontSelector.showDialog(parentFrame); 327 } 328 329 330 } | Popular Tags |