1 33 34 package edu.rice.cs.util.swing; 35 36 import edu.rice.cs.drjava.ui.MainFrame; 37 import javax.swing.*; 38 import javax.swing.event.ListSelectionEvent ; 39 import javax.swing.event.ListSelectionListener ; 40 import java.awt.*; 41 import java.awt.event.*; 42 43 49 50 public class FontChooser extends JDialog { 51 54 private static final String [] STYLES = 55 new String [] { "Plain", "Bold", "Italic", "Bold Italic" }; 56 57 60 private static final String [] SIZES = 61 new String [] { "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", 62 "13", "14", "15", "16", "17", "18", "19", "20", "22", 63 "24", "27", "30", "34", "39", "45", "51", "60"}; 64 65 private NwList _styleList; 67 private NwList _fontList; 68 private NwList _sizeList; 69 70 private JButton _okButton; 72 private JButton _cancelButton; 73 private JLabel _sampleText = new JLabel(); 74 75 private boolean _clickedOK = false; 76 77 81 private FontChooser(Frame parent, Font font) { 82 super(parent, true); 83 initAll(); 84 if (font == null) font = _sampleText.getFont(); 85 _fontList.setSelectedItem(font.getName()); 86 _sizeList.setSelectedItem(font.getSize() + ""); 87 _styleList.setSelectedItem(STYLES[font.getStyle()]); 88 resize(); 90 } 91 92 100 public static Font showDialog(Frame parent, String title, Font font) { 101 FontChooser fd = new FontChooser(parent, font); 102 fd.setTitle(title); 103 104 MainFrame.setPopupLoc(fd, parent); 105 fd.setVisible(true); 106 107 Font chosenFont = null; 108 if (fd.clickedOK()) { 109 chosenFont = fd.getFont(); 110 } 111 fd.dispose(); 112 return (chosenFont); 113 } 114 115 118 public static Font showDialog(Frame parent, Font font) { 119 return showDialog(parent, "Font Chooser", font); 120 } 121 122 private void initAll() { 123 getContentPane().setLayout(null); 124 setBounds(50, 50, 425, 400); 125 _sampleText = new JLabel(); 126 addLists(); 127 addButtons(); 128 _sampleText.setForeground(Color.black); 129 getContentPane().add(_sampleText); 130 addWindowListener(new WindowAdapter() { 131 public void windowClosing(java.awt.event.WindowEvent e) { 132 setVisible(false); 133 } 134 }); 135 addComponentListener(new ComponentAdapter() { 136 public void componentResized(ComponentEvent evt) { 137 resize(); 138 } 139 }); 140 } 141 142 private void resize() { 143 int w = getWidth(); 144 int h = getHeight(); 145 double wf = (double) w / 425; 146 int w2 = (int) (80 * wf); 147 int w3 = (int) (50 * wf); 148 if (w3 < 30) w3 = 30; 149 int w1 = w - w2 - w3 - 25; 150 _fontList.setBounds(5, 5, w1, h - 91); 151 _styleList.setBounds(w1 + 10, 5, w2, h - 91); 152 _sizeList.setBounds(w1 + w2 + 15, 5, w3, h - 91); 153 _sampleText.setBounds(10, h - 78, w - 20, 45); 154 _okButton.setBounds(w - 165, h - 55, 70, 20); 155 _cancelButton.setBounds(w - 81, h - 55, 70, 20); 156 validate(); 157 } 158 159 private void addLists() { 160 _fontList = new NwList(GraphicsEnvironment.getLocalGraphicsEnvironment() 161 .getAvailableFontFamilyNames()); 162 _styleList = new NwList(STYLES); 163 _sizeList = new NwList(SIZES); 164 getContentPane().add(_fontList); 165 getContentPane().add(_styleList); 166 getContentPane().add(_sizeList); 167 } 168 169 private void addButtons() { 170 _okButton = new JButton("OK"); 171 _okButton.setMargin(new Insets(0, 0, 0, 0)); 172 _cancelButton = new JButton("Cancel"); 173 _cancelButton.setMargin(new Insets(0, 0, 0, 0)); 174 _okButton.setFont(new Font(" ", 1, 11)); 175 _cancelButton.setFont(new Font(" ", 1, 12)); 176 getContentPane().add(_okButton); 177 getContentPane().add(_cancelButton); 178 _okButton.addActionListener(new ActionListener() { 179 public void actionPerformed(ActionEvent e) { 180 setVisible(false); 181 _clickedOK = true; 182 } 183 }); 184 _cancelButton.addActionListener(new ActionListener() { 185 public void actionPerformed(ActionEvent e) { 186 setVisible(false); 187 _clickedOK = false; 188 } 189 }); 190 } 191 192 private void showSample() { 193 int g = 0; 194 try { 195 g = Integer.parseInt(_sizeList.getSelectedValue()); 196 } 197 catch (NumberFormatException nfe) { 198 } 199 String st = _styleList.getSelectedValue(); 200 int s = Font.PLAIN; 201 if (st.equalsIgnoreCase("Bold")) s = Font.BOLD; 202 if (st.equalsIgnoreCase("Italic")) s = Font.ITALIC; 203 if (st.equalsIgnoreCase("Bold Italic")) s = Font.BOLD | Font.ITALIC; 204 _sampleText.setFont(new Font(_fontList.getSelectedValue(), s, g)); 205 _sampleText.setText("The quick brown fox jumped over the lazy dog."); 206 _sampleText.setVerticalAlignment(SwingConstants.TOP); 207 } 208 209 213 public boolean clickedOK() { 214 return _clickedOK; 215 } 216 217 220 public Font getFont() { 221 return _sampleText.getFont(); 222 } 223 224 228 public class NwList extends JPanel { 229 JList jl; 230 JScrollPane sp; 231 JLabel jt; 232 String si = " "; 233 234 public NwList(String [] values) { 235 setLayout(null); 236 jl = new JList(values); 237 sp = new JScrollPane(jl); 238 jt = new JLabel(); 239 jt.setBackground(Color.white); 240 jt.setForeground(Color.black); 241 jt.setOpaque(true); 242 jt.setBorder(new JTextField().getBorder()); 243 jt.setFont(getFont()); 244 jl.addListSelectionListener(new ListSelectionListener () { 245 public void valueChanged(ListSelectionEvent e) { 246 jt.setText((String ) jl.getSelectedValue()); 247 si = (String ) jl.getSelectedValue(); 248 showSample(); 249 } 250 }); 251 add(sp); 252 add(jt); 253 } 254 255 public void setBounds(int x, int y, int w, int h) { 256 super.setBounds(x, y, w, h); 257 sp.setBounds(0, y + 16, w, h - 23); 258 sp.revalidate(); 259 jt.setBounds(0, 0, w, 20); 260 } 261 262 public String getSelectedValue() { 263 return (si); 264 } 265 266 public void setSelectedItem(String s) { 267 jl.setSelectedValue(s, true); 268 } 269 270 } 271 } | Popular Tags |