001: package snow.utils.gui;
002:
003: import javax.swing.*;
004: import java.awt.*;
005: import java.awt.event.*;
006: import javax.swing.event.*;
007:
008: public class FontSelector extends JDialog {
009:
010: private static Font defaultFont = new Font("Dialog", Font.PLAIN, 12);
011: private static FontSelector dialog;
012: private static String NameValue;
013: private static int StyleValue;
014: private static int SizeValue;
015: private static String SaveNameValue;
016: private static int SaveStyleValue;
017: private static int SaveSizeValue;
018: private static boolean FontChosen = false;
019: private JList theFontNamesList;
020: private JTextField SizeField;
021: private JList styleList;
022: private JCheckBox fontBold, fontItalic;
023: private JLabel sampleTextLabel;
024: private Container contentPane;
025: final JButton cancelButton = new JButton("Cancel");
026: final JButton setButton = new JButton("Set");
027:
028: private FontSelector(Frame frame, String[] fontNames, String title) {
029: super (frame, title, true);
030:
031: // Button Listeners :
032:
033: cancelButton.addActionListener(new ActionListener() {
034: public void actionPerformed(ActionEvent e) {
035: FontChosen = false;
036: FontSelector.dialog.setVisible(false);
037: }
038: });
039:
040: setButton.addActionListener(new ActionListener() {
041: public void actionPerformed(ActionEvent e) {
042: // put the 3 font parms to the outer static parms :
043: Object[] selected = theFontNamesList
044: .getSelectedValues();
045: if ((selected != null) && (selected.length > 0)) {
046: boolean valid = true;
047: NameValue = selected[0].toString();
048: try {
049: SizeValue = Integer.parseInt(SizeField
050: .getText());
051: if ((SizeValue > 0) && (SizeValue < 65)) {
052: StyleValue = (fontBold.isSelected() ? Font.BOLD
053: : 0)
054: + (fontItalic.isSelected() ? Font.ITALIC
055: : 0);
056: // and exit
057: FontChosen = true;
058: FontSelector.dialog.setVisible(false);
059: } else {
060: valid = false;
061: }
062: } catch (Exception ex) {
063: valid = false;
064: }
065: if (!valid) {
066: JOptionPane.showMessageDialog(
067: FontSelector.this ,
068: "Please enter valid values.",
069: "Invalid Value",
070: JOptionPane.INFORMATION_MESSAGE);
071: SizeField.requestFocus();
072: SizeField.selectAll();
073: }
074: }
075: }
076: });
077: getRootPane().setDefaultButton(setButton);
078:
079: // make the font face JList :
080: theFontNamesList = new JList(fontNames);
081: theFontNamesList
082: .setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
083:
084: // look for the initial fontname and select it, if found :
085: this .theFontNamesList.setSelectedValue(NameValue, true);
086:
087: theFontNamesList
088: .addListSelectionListener(new MyNameListSelectionListener());
089: JScrollPane listScroller = new JScrollPane(theFontNamesList);
090:
091: // set the size :
092: Dimension listDim = new Dimension(SizeValue * 30,
093: SizeValue * 10);
094: listScroller.setPreferredSize(listDim);
095: listScroller.setMinimumSize(listDim);
096: listScroller.setMaximumSize(listDim);
097:
098: //Create a container so that we can add a title around
099: //the scroll pane. Can't add a title directly to the
100: //scroll pane because its background would be white.
101: //Lay out the label and scroll pane from top to button.
102: JPanel listPane = new JPanel();
103: listPane.setLayout(new BoxLayout(listPane, BoxLayout.Y_AXIS));
104: listPane.add(Box.createRigidArea(new Dimension(0, 5)));
105: listPane.add(listScroller);
106: listPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10,
107: 10));
108:
109: // FontSize/Style entry :
110:
111: Integer theValue = new Integer(SizeValue);
112: SizeField = new JTextField(theValue.toString(), 4);
113: SizeField.setHorizontalAlignment(SwingConstants.RIGHT);
114:
115: fontBold = new JCheckBox("Bold");
116: if ((StyleValue & Font.BOLD) > 0) {
117: fontBold.setSelected(true);
118: } else {
119: fontBold.setSelected(false);
120: }
121:
122: fontItalic = new JCheckBox("Italic");
123: if ((StyleValue & Font.ITALIC) > 0) {
124: fontItalic.setSelected(true);
125: } else {
126: fontItalic.setSelected(false);
127: }
128:
129: // define an actionlistener for all font change
130: // events triggered by the size field :
131: ActionListener fontActionListener = new ActionListener() {
132: public void actionPerformed(ActionEvent evt) {
133: Object[] selected = theFontNamesList
134: .getSelectedValues();
135: if ((selected != null) && (selected.length > 0)) {
136: boolean valid = true;
137: NameValue = selected[0].toString();
138: try {
139: SizeValue = Integer.parseInt(SizeField
140: .getText());
141:
142: if ((SizeValue > 0) && (SizeValue < 65)) {
143: StyleValue = (fontBold.isSelected() ? Font.BOLD
144: : 0)
145: + (fontItalic.isSelected() ? Font.ITALIC
146: : 0);
147: Font selectedFont = new Font(NameValue,
148: StyleValue, SizeValue);
149: sampleTextLabel.setFont(selectedFont);
150: dialog.pack();
151: dialog.repaint();
152: sampleTextLabel.repaint();
153: } else {
154: valid = false;
155: }
156: } catch (Exception ex) {
157: valid = false;
158: }
159: if (!valid) {
160: JOptionPane.showMessageDialog(
161: FontSelector.this ,
162: "Please enter valid values.",
163: "Invalid Value",
164: JOptionPane.INFORMATION_MESSAGE);
165: SizeField.requestFocus();
166: SizeField.selectAll();
167: }
168: }
169: }
170: };
171: SizeField.addActionListener(fontActionListener);
172: fontBold.addActionListener(fontActionListener);
173: fontItalic.addActionListener(fontActionListener);
174:
175: JPanel stylePane = new JPanel();
176: stylePane.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));
177: JLabel txtlabel = new JLabel(" Font Size : ", JLabel.CENTER);
178: txtlabel.setFont(new Font(NameValue, StyleValue, SizeValue));
179: txtlabel.setForeground(Color.black);
180: stylePane.add(txtlabel);
181: stylePane.add(SizeField);
182: stylePane.add(fontBold);
183: stylePane.add(fontItalic);
184:
185: // Cancel and Set Buttons :
186:
187: JPanel buttonPane = new JPanel();
188: buttonPane.add(cancelButton);
189: buttonPane.add(setButton);
190:
191: // sample text
192: JPanel samplePane = new JPanel();
193: samplePane.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
194: sampleTextLabel = new JLabel("This is a sample text.",
195: JLabel.CENTER);
196: sampleTextLabel.setFont(new Font(NameValue, StyleValue,
197: SizeValue));
198: sampleTextLabel.setForeground(Color.black);
199: samplePane.add(sampleTextLabel);
200:
201: JPanel bottomPane = new JPanel();
202: bottomPane.setLayout(new BorderLayout(5, 5));
203: bottomPane.add(samplePane, BorderLayout.NORTH);
204: bottomPane.add(buttonPane, BorderLayout.SOUTH);
205:
206: this .setLayout(new BorderLayout(5, 5));
207:
208: //Put everything together, using the content pane's BorderLayout.
209: contentPane = getContentPane();
210: contentPane.add(listPane, BorderLayout.NORTH);
211: contentPane.add(stylePane, BorderLayout.CENTER);
212: contentPane.add(bottomPane, BorderLayout.SOUTH);
213:
214: pack();
215: }
216:
217: class MyNameListSelectionListener implements ListSelectionListener {
218: public void valueChanged(ListSelectionEvent evt) {
219: if (!evt.getValueIsAdjusting()) {
220: JList this List = (JList) evt.getSource();
221: Object[] selected = this List.getSelectedValues();
222:
223: if ((selected != null) && (selected.length > 0)) {
224: try {
225: FontSelector.NameValue = selected[0].toString();
226: FontSelector.SizeValue = Integer
227: .parseInt(SizeField.getText());
228: FontSelector.StyleValue = (fontBold
229: .isSelected() ? Font.BOLD : 0)
230: + (fontItalic.isSelected() ? Font.ITALIC
231: : 0);
232:
233: Font selectedFont = new Font(NameValue,
234: StyleValue, SizeValue);
235: sampleTextLabel.setFont(selectedFont);
236: dialog.pack();
237: dialog.repaint();
238: sampleTextLabel.repaint();
239: } catch (NumberFormatException ignored) {
240: System.out
241: .println("Cannot parse an integer number from "
242: + SizeField.getText());
243: }
244: }
245: }
246: }
247: }
248:
249: public static Font showDialog(Component comp) {
250: if (dialog != null) {
251: dialog.setLocationRelativeTo(comp);
252: int selIndex = dialog.theFontNamesList.getSelectedIndex();
253: if (selIndex >= 0) {
254: Rectangle selRect = dialog.theFontNamesList
255: .getCellBounds(selIndex, selIndex);
256: dialog.theFontNamesList.scrollRectToVisible(selRect);
257: }
258: dialog.setVisible(true);
259: } else {
260: System.out
261: .println("ListDialog requires you to call initialize "
262: + "before calling showDialog.");
263: }
264: if (FontChosen) {
265: return new Font(NameValue, StyleValue, SizeValue);
266: } else {
267: return new Font(SaveNameValue, SaveStyleValue,
268: SaveSizeValue);
269: }
270: }
271:
272: public static Font chooseFont(Component parentFrame) {
273: return FontSelector.chooseFont(parentFrame, defaultFont);
274: }
275:
276: public static Font chooseFont(Component parentFrame,
277: Font defaultFont_)
278: // version with passed defaultfont
279: {
280: defaultFont = defaultFont_; // used, if the cancelbutton is pressed
281: NameValue = defaultFont.getName(); // static
282: SizeValue = defaultFont.getSize(); // static
283: if (SizeValue < 6) {
284: SizeValue = 6;
285: }
286: if (SizeValue > 100) {
287: SizeValue = 100;
288: }
289: StyleValue = defaultFont.getStyle(); // static
290: // backup, case cancel is pressed :
291: SaveNameValue = NameValue; // static
292: SaveSizeValue = SizeValue; // static
293: SaveStyleValue = StyleValue; // static
294: String[] fontNames = GraphicsEnvironment
295: .getLocalGraphicsEnvironment()
296: .getAvailableFontFamilyNames();
297: Frame frame = JOptionPane.getFrameForComponent(parentFrame);
298: dialog = new FontSelector(frame, fontNames, "Choose a Font");
299: return FontSelector.showDialog(parentFrame);
300: }
301:
302: }
|