KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > swing > FontChooser


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

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 JavaDoc;
39 import javax.swing.event.ListSelectionListener JavaDoc;
40 import java.awt.*;
41 import java.awt.event.*;
42
43 /**
44  * FontChooser, adapted from NwFontChooserS by Noah Wairauch.
45  * (see http:///forum.java.sun.com/thread.jsp?forum=57&thread=195067)
46  *
47  * @version $Id: FontChooser.java 3869 2006-05-27 04:24:47Z mgricken $
48  */

49
50 public class FontChooser extends JDialog {
51   /**
52    * Available font styles.
53    */

54   private static final String JavaDoc[] STYLES =
55       new String JavaDoc[] { "Plain", "Bold", "Italic", "Bold Italic" };
56
57   /**
58    * Available font sizes.
59    */

60   private static final String JavaDoc[] SIZES =
61       new String JavaDoc[] { "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   // Lists to display
66
private NwList _styleList;
67   private NwList _fontList;
68   private NwList _sizeList;
69
70   // Swing elements
71
private JButton _okButton;
72   private JButton _cancelButton;
73   private JLabel _sampleText = new JLabel();
74
75   private boolean _clickedOK = false;
76
77   /**
78    * Constructs a new modal FontChooser for the given frame,
79    * using the specified font.
80    */

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     //this.setResizable(false);
89
resize();
90   }
91
92   /**
93    * Method used to show the font chooser, and select a new font.
94    *
95    * @param parent The parent frame.
96    * @param title The title for this window.
97    * @param font The previously chosen font.
98    * @return the newly chosen font.
99    */

100   public static Font showDialog(Frame parent, String JavaDoc 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   /**
116    * Shows the font chooser with a standard title ("Font Chooser").
117    */

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 JavaDoc 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 JavaDoc nfe) {
198     }
199     String JavaDoc 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   /**
210    * Returns whether the user clicked OK when the dialog was closed.
211    * (If false, the user clicked cancel.)
212    */

213   public boolean clickedOK() {
214     return _clickedOK;
215   }
216
217   /**
218    * Returns the currently selected Font.
219    */

220   public Font getFont() {
221     return _sampleText.getFont();
222   }
223
224   /**
225    * Private inner class for a list which displays a list of options in addition to a label
226    * indicating the currently selected item.
227    */

228   public class NwList extends JPanel {
229     JList jl;
230     JScrollPane sp;
231     JLabel jt;
232     String JavaDoc si = " ";
233
234     public NwList(String JavaDoc[] 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 JavaDoc() {
245         public void valueChanged(ListSelectionEvent JavaDoc e) {
246           jt.setText((String JavaDoc) jl.getSelectedValue());
247           si = (String JavaDoc) 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 JavaDoc getSelectedValue() {
263       return (si);
264     }
265
266     public void setSelectedItem(String JavaDoc s) {
267       jl.setSelectedValue(s, true);
268     }
269
270   }
271 }
Popular Tags