001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.util.swing;
038:
039: import edu.rice.cs.drjava.ui.MainFrame;
040: import javax.swing.*;
041: import javax.swing.event.ListSelectionEvent;
042: import javax.swing.event.ListSelectionListener;
043: import java.awt.*;
044: import java.awt.event.*;
045:
046: /**
047: * FontChooser, adapted from NwFontChooserS by Noah Wairauch.
048: * (see http:///forum.java.sun.com/thread.jsp?forum=57&thread=195067)
049: *
050: * @version $Id: FontChooser.java 4255 2007-08-28 19:17:37Z mgricken $
051: */
052:
053: public class FontChooser extends JDialog {
054: /**
055: * Available font styles.
056: */
057: private static final String[] STYLES = new String[] { "Plain",
058: "Bold", "Italic", "Bold Italic" };
059:
060: /**
061: * Available font sizes.
062: */
063: private static final String[] SIZES = new String[] { "3", "4", "5",
064: "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
065: "16", "17", "18", "19", "20", "22", "24", "27", "30", "34",
066: "39", "45", "51", "60" };
067:
068: // Lists to display
069: private NwList _styleList;
070: private NwList _fontList;
071: private NwList _sizeList;
072:
073: // Swing elements
074: private JButton _okButton;
075: private JButton _cancelButton;
076: private JLabel _sampleText = new JLabel();
077:
078: private boolean _clickedOK = false;
079:
080: /**
081: * Constructs a new modal FontChooser for the given frame,
082: * using the specified font.
083: */
084: private FontChooser(Frame parent, Font font) {
085: super (parent, true);
086: initAll();
087: if (font == null)
088: font = _sampleText.getFont();
089: _fontList.setSelectedItem(font.getName());
090: _sizeList.setSelectedItem(font.getSize() + "");
091: _styleList.setSelectedItem(STYLES[font.getStyle()]);
092: //this.setResizable(false);
093: resize();
094: }
095:
096: /**
097: * Method used to show the font chooser, and select a new font.
098: *
099: * @param parent The parent frame.
100: * @param title The title for this window.
101: * @param font The previously chosen font.
102: * @return the newly chosen font.
103: */
104: public static Font showDialog(Frame parent, String title, Font font) {
105: FontChooser fd = new FontChooser(parent, font);
106: fd.setTitle(title);
107:
108: MainFrame.setPopupLoc(fd, parent);
109: fd.setVisible(true);
110:
111: Font chosenFont = null;
112: if (fd.clickedOK()) {
113: chosenFont = fd.getFont();
114: }
115: fd.dispose();
116: return (chosenFont);
117: }
118:
119: /**
120: * Shows the font chooser with a standard title ("Font Chooser").
121: */
122: public static Font showDialog(Frame parent, Font font) {
123: return showDialog(parent, "Font Chooser", font);
124: }
125:
126: private void initAll() {
127: getContentPane().setLayout(null);
128: setBounds(50, 50, 425, 400);
129: _sampleText = new JLabel();
130: addLists();
131: addButtons();
132: _sampleText.setForeground(Color.black);
133: getContentPane().add(_sampleText);
134: addWindowListener(new WindowAdapter() {
135: public void windowClosing(java.awt.event.WindowEvent e) {
136: setVisible(false);
137: }
138: });
139: addComponentListener(new ComponentAdapter() {
140: public void componentResized(ComponentEvent evt) {
141: resize();
142: }
143: });
144: }
145:
146: private void resize() {
147: int w = getWidth();
148: int h = getHeight();
149: double wf = (double) w / 425;
150: int w2 = (int) (80 * wf);
151: int w3 = (int) (50 * wf);
152: if (w3 < 30)
153: w3 = 30;
154: int w1 = w - w2 - w3 - 25;
155: _fontList.setBounds(5, 5, w1, h - 91);
156: _styleList.setBounds(w1 + 10, 5, w2, h - 91);
157: _sizeList.setBounds(w1 + w2 + 15, 5, w3, h - 91);
158: _sampleText.setBounds(10, h - 78, w - 20, 45);
159: _okButton.setBounds(w - 165, h - 55, 70, 20);
160: _cancelButton.setBounds(w - 81, h - 55, 70, 20);
161: validate();
162: }
163:
164: private void addLists() {
165: _fontList = new NwList(GraphicsEnvironment
166: .getLocalGraphicsEnvironment()
167: .getAvailableFontFamilyNames());
168: _styleList = new NwList(STYLES);
169: _sizeList = new NwList(SIZES);
170: getContentPane().add(_fontList);
171: getContentPane().add(_styleList);
172: getContentPane().add(_sizeList);
173: }
174:
175: private void addButtons() {
176: _okButton = new JButton("OK");
177: _okButton.setMargin(new Insets(0, 0, 0, 0));
178: _cancelButton = new JButton("Cancel");
179: _cancelButton.setMargin(new Insets(0, 0, 0, 0));
180: _okButton.setFont(new Font(" ", 1, 11));
181: _cancelButton.setFont(new Font(" ", 1, 12));
182: getContentPane().add(_okButton);
183: getContentPane().add(_cancelButton);
184: _okButton.addActionListener(new ActionListener() {
185: public void actionPerformed(ActionEvent e) {
186: setVisible(false);
187: _clickedOK = true;
188: }
189: });
190: _cancelButton.addActionListener(new ActionListener() {
191: public void actionPerformed(ActionEvent e) {
192: setVisible(false);
193: _clickedOK = false;
194: }
195: });
196: }
197:
198: private void showSample() {
199: int g = 0;
200: try {
201: g = Integer.parseInt(_sizeList.getSelectedValue());
202: } catch (NumberFormatException nfe) {
203: }
204: String st = _styleList.getSelectedValue();
205: int s = Font.PLAIN;
206: if (st.equalsIgnoreCase("Bold"))
207: s = Font.BOLD;
208: if (st.equalsIgnoreCase("Italic"))
209: s = Font.ITALIC;
210: if (st.equalsIgnoreCase("Bold Italic"))
211: s = Font.BOLD | Font.ITALIC;
212: _sampleText
213: .setFont(new Font(_fontList.getSelectedValue(), s, g));
214: _sampleText
215: .setText("The quick brown fox jumped over the lazy dog.");
216: _sampleText.setVerticalAlignment(SwingConstants.TOP);
217: }
218:
219: /**
220: * Returns whether the user clicked OK when the dialog was closed.
221: * (If false, the user clicked cancel.)
222: */
223: public boolean clickedOK() {
224: return _clickedOK;
225: }
226:
227: /**
228: * Returns the currently selected Font.
229: */
230: public Font getFont() {
231: return _sampleText.getFont();
232: }
233:
234: /**
235: * Private inner class for a list which displays a list of options in addition to a label
236: * indicating the currently selected item.
237: */
238: public class NwList extends JPanel {
239: JList jl;
240: JScrollPane sp;
241: JLabel jt;
242: String si = " ";
243:
244: public NwList(String[] values) {
245: setLayout(null);
246: jl = new JList(values);
247: sp = new JScrollPane(jl);
248: jt = new JLabel();
249: jt.setBackground(Color.white);
250: jt.setForeground(Color.black);
251: jt.setOpaque(true);
252: jt.setBorder(new JTextField().getBorder());
253: jt.setFont(getFont());
254: jl.addListSelectionListener(new ListSelectionListener() {
255: public void valueChanged(ListSelectionEvent e) {
256: jt.setText((String) jl.getSelectedValue());
257: si = (String) jl.getSelectedValue();
258: showSample();
259: }
260: });
261: add(sp);
262: add(jt);
263: }
264:
265: public void setBounds(int x, int y, int w, int h) {
266: super .setBounds(x, y, w, h);
267: sp.setBounds(0, y + 16, w, h - 23);
268: sp.revalidate();
269: jt.setBounds(0, 0, w, 20);
270: }
271:
272: public String getSelectedValue() {
273: return (si);
274: }
275:
276: public void setSelectedItem(String s) {
277: jl.setSelectedValue(s, true);
278: }
279:
280: }
281: }
|