1 package org.simantics.spreadsheet.ui;
3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.awt.Dimension;
8 import java.awt.GraphicsEnvironment;
9 import java.awt.GridLayout;
10 import java.awt.event.ActionEvent;
11 import java.awt.event.FocusAdapter;
12 import java.awt.event.FocusEvent;
13 import java.awt.event.KeyAdapter;
14 import java.awt.event.KeyEvent;
15 import java.awt.event.WindowAdapter;
16 import java.awt.event.WindowEvent;
18 import javax.swing.AbstractAction;
19 import javax.swing.Action;
20 import javax.swing.ActionMap;
21 import javax.swing.BorderFactory;
22 import javax.swing.BoxLayout;
23 import javax.swing.InputMap;
24 import javax.swing.JButton;
25 import javax.swing.JComponent;
26 import javax.swing.JDialog;
27 import javax.swing.JLabel;
28 import javax.swing.JList;
29 import javax.swing.JPanel;
30 import javax.swing.JScrollPane;
31 import javax.swing.JTextField;
32 import javax.swing.KeyStroke;
33 import javax.swing.ListSelectionModel;
34 import javax.swing.SwingUtilities;
35 import javax.swing.border.Border;
36 import javax.swing.event.DocumentEvent;
37 import javax.swing.event.DocumentListener;
38 import javax.swing.event.ListSelectionEvent;
39 import javax.swing.event.ListSelectionListener;
40 import javax.swing.text.BadLocationException;
41 import javax.swing.text.Document;
42 import javax.swing.text.JTextComponent;
43 import javax.swing.text.Position;
46 * The <code>JFontChooser</code> class is a swing component
48 * This class has <code>JFileChooser</code> like APIs.
49 * The following code pops up a font chooser dialog.
51 * JFontChooser fontChooser = new JFontChooser();
52 * int result = fontChooser.showDialog(parent);
53 * if (result == JFontChooser.OK_OPTION)
55 * Font font = fontChooser.getSelectedFont();
56 * System.out.println("Selected Font : " + font);
60 public class JFontChooser extends JComponent
62 private static final long serialVersionUID = -8803227254655562584L;
65 * Return value from <code>showDialog()</code>.
68 public static final int OK_OPTION = 0;
70 * Return value from <code>showDialog()</code>.
73 public static final int CANCEL_OPTION = 1;
75 * Return value from <code>showDialog()</code>.
78 public static final int ERROR_OPTION = -1;
79 private static final Font DEFAULT_SELECTED_FONT = new Font("Serif", Font.PLAIN, 12);
80 private static final Font DEFAULT_FONT = new Font("Dialog", Font.PLAIN, 10);
81 private static final int[] FONT_STYLE_CODES =
83 Font.PLAIN, Font.BOLD, Font.ITALIC, Font.BOLD | Font.ITALIC
85 private static final String[] DEFAULT_FONT_SIZE_STRINGS =
87 "8", "9", "10", "11", "12", "14", "16", "18", "20",
88 "22", "24", "26", "28", "36", "48", "72",
92 protected int dialogResultValue = ERROR_OPTION;
96 private String[] fontStyleNames = null;
97 private String[] fontFamilyNames = null;
98 private String[] fontSizeStrings = null;
99 private JTextField fontFamilyTextField = null;
100 private JTextField fontStyleTextField = null;
101 private JTextField fontSizeTextField = null;
102 private JList fontNameList = null;
103 private JList fontStyleList = null;
104 private JList fontSizeList = null;
105 private JPanel fontNamePanel = null;
106 private JPanel fontStylePanel = null;
107 private JPanel fontSizePanel = null;
108 private JPanel samplePanel = null;
109 private JTextField sampleText = null;
112 * Constructs a <code>JFontChooser</code> object.
114 public JFontChooser()
116 this(DEFAULT_FONT_SIZE_STRINGS);
120 * Constructs a <code>JFontChooser</code> object using the given font size array.
121 * @param fontSizeStrings the array of font size string.
123 public JFontChooser(String[] fontSizeStrings)
125 if (fontSizeStrings == null)
127 fontSizeStrings = DEFAULT_FONT_SIZE_STRINGS;
129 this.fontSizeStrings = fontSizeStrings;
131 JPanel selectPanel = new JPanel();
132 selectPanel.setLayout(new BoxLayout(selectPanel, BoxLayout.X_AXIS));
133 selectPanel.add(getFontFamilyPanel());
134 selectPanel.add(getFontStylePanel());
135 selectPanel.add(getFontSizePanel());
137 JPanel contentsPanel = new JPanel();
138 contentsPanel.setLayout(new GridLayout(2, 1));
139 contentsPanel.add(selectPanel, BorderLayout.NORTH);
140 contentsPanel.add(getSamplePanel(), BorderLayout.CENTER);
142 this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
143 this.add(contentsPanel);
144 this.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
145 this.setSelectedFont(DEFAULT_SELECTED_FONT);
148 public JTextField getFontFamilyTextField()
150 if (fontFamilyTextField == null)
152 fontFamilyTextField = new JTextField();
153 fontFamilyTextField.addFocusListener(
154 new TextFieldFocusHandlerForTextSelection(fontFamilyTextField));
155 fontFamilyTextField.addKeyListener(
156 new TextFieldKeyHandlerForListSelectionUpDown(getFontFamilyList()));
157 fontFamilyTextField.getDocument().addDocumentListener(
158 new ListSearchTextFieldDocumentHandler(getFontFamilyList()));
159 fontFamilyTextField.setFont(DEFAULT_FONT);
162 return fontFamilyTextField;
165 public JTextField getFontStyleTextField()
167 if (fontStyleTextField == null)
169 fontStyleTextField = new JTextField();
170 fontStyleTextField.addFocusListener(
171 new TextFieldFocusHandlerForTextSelection(fontStyleTextField));
172 fontStyleTextField.addKeyListener(
173 new TextFieldKeyHandlerForListSelectionUpDown(getFontStyleList()));
174 fontStyleTextField.getDocument().addDocumentListener(
175 new ListSearchTextFieldDocumentHandler(getFontStyleList()));
176 fontStyleTextField.setFont(DEFAULT_FONT);
178 return fontStyleTextField;
181 public JTextField getFontSizeTextField()
183 if (fontSizeTextField == null)
185 fontSizeTextField = new JTextField();
186 fontSizeTextField.addFocusListener(
187 new TextFieldFocusHandlerForTextSelection(fontSizeTextField));
188 fontSizeTextField.addKeyListener(
189 new TextFieldKeyHandlerForListSelectionUpDown(getFontSizeList()));
190 fontSizeTextField.getDocument().addDocumentListener(
191 new ListSearchTextFieldDocumentHandler(getFontSizeList()));
192 fontSizeTextField.setFont(DEFAULT_FONT);
194 return fontSizeTextField;
197 public JList getFontFamilyList()
199 if (fontNameList == null)
201 fontNameList = new JList(getFontFamilies());
202 fontNameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
203 fontNameList.addListSelectionListener(
204 new ListSelectionHandler(getFontFamilyTextField()));
205 fontNameList.setSelectedIndex(0);
206 fontNameList.setFont(DEFAULT_FONT);
207 fontNameList.setFocusable(false);
212 public JList getFontStyleList()
214 if (fontStyleList == null)
216 fontStyleList = new JList(getFontStyleNames());
217 fontStyleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
218 fontStyleList.addListSelectionListener(
219 new ListSelectionHandler(getFontStyleTextField()));
220 fontStyleList.setSelectedIndex(0);
221 fontStyleList.setFont(DEFAULT_FONT);
222 fontStyleList.setFocusable(false);
224 return fontStyleList;
227 public JList getFontSizeList()
229 if (fontSizeList == null)
231 fontSizeList = new JList(this.fontSizeStrings);
232 fontSizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
233 fontSizeList.addListSelectionListener(
234 new ListSelectionHandler(getFontSizeTextField()));
235 fontSizeList.setSelectedIndex(0);
236 fontSizeList.setFont(DEFAULT_FONT);
237 fontSizeList.setFocusable(false);
243 * Get the family name of the selected font.
244 * @return the font family of the selected font.
246 * @see #setSelectedFontFamily
248 public String getSelectedFontFamily()
250 String fontName = (String) getFontFamilyList().getSelectedValue();
255 * Get the style of the selected font.
256 * @return the style of the selected font.
257 * <code>Font.PLAIN</code>, <code>Font.BOLD</code>,
258 * <code>Font.ITALIC</code>, <code>Font.BOLD|Font.ITALIC</code>
260 * @see java.awt.Font#PLAIN
261 * @see java.awt.Font#BOLD
262 * @see java.awt.Font#ITALIC
263 * @see #setSelectedFontStyle
265 public int getSelectedFontStyle()
267 int index = getFontStyleList().getSelectedIndex();
268 return FONT_STYLE_CODES[index];
272 * Get the size of the selected font.
273 * @return the size of the selected font
275 * @see #setSelectedFontSize
277 public int getSelectedFontSize()
280 String fontSizeString = getFontSizeTextField().getText();
285 fontSize = Integer.parseInt(fontSizeString);
288 catch (NumberFormatException e)
290 fontSizeString = (String) getFontSizeList().getSelectedValue();
291 getFontSizeTextField().setText(fontSizeString);
299 * Get the selected font.
300 * @return the selected font
302 * @see #setSelectedFont
305 public Font getSelectedFont()
307 Font font = new Font(getSelectedFontFamily(),
308 getSelectedFontStyle(), getSelectedFontSize());
313 * Set the family name of the selected font.
314 * @param name the family name of the selected font.
316 * @see getSelectedFontFamily
318 public void setSelectedFontFamily(String name)
320 String[] names = getFontFamilies();
321 for (int i = 0; i < names.length; i++)
323 if (names[i].toLowerCase().equals(name.toLowerCase()))
325 getFontFamilyList().setSelectedIndex(i);
333 * Set the style of the selected font.
334 * @param style the size of the selected font.
335 * <code>Font.PLAIN</code>, <code>Font.BOLD</code>,
336 * <code>Font.ITALIC</code>, or
337 * <code>Font.BOLD|Font.ITALIC</code>.
339 * @see java.awt.Font#PLAIN
340 * @see java.awt.Font#BOLD
341 * @see java.awt.Font#ITALIC
342 * @see #getSelectedFontStyle
344 public void setSelectedFontStyle(int style)
346 for (int i = 0; i < FONT_STYLE_CODES.length; i++)
348 if (FONT_STYLE_CODES[i] == style)
350 getFontStyleList().setSelectedIndex(i);
358 * Set the size of the selected font.
359 * @param size the size of the selected font
361 * @see #getSelectedFontSize
363 public void setSelectedFontSize(int size)
365 String sizeString = String.valueOf(size);
366 for (int i = 0; i < this.fontSizeStrings.length; i++)
368 if (this.fontSizeStrings[i].equals(sizeString))
370 getFontSizeList().setSelectedIndex(i);
374 getFontSizeTextField().setText(sizeString);
379 * Set the selected font.
380 * @param font the selected font
382 * @see #getSelectedFont
385 public void setSelectedFont(Font font)
387 setSelectedFontFamily(font.getFamily());
388 setSelectedFontStyle(font.getStyle());
389 setSelectedFontSize(font.getSize());
392 public String getVersionString()
398 * Show font selection dialog.
399 * @param parent Dialog's Parent component.
400 * @return OK_OPTION, CANCEL_OPTION or ERROR_OPTION
403 * @see #CANCEL_OPTION
406 public int showDialog(Component parent)
408 dialogResultValue = ERROR_OPTION;
409 JDialog dialog = createDialog(parent);
410 dialog.addWindowListener(new WindowAdapter()
412 public void windowClosing(WindowEvent e)
414 dialogResultValue = CANCEL_OPTION;
418 dialog.setVisible(true);
422 return dialogResultValue;
425 protected class ListSelectionHandler implements ListSelectionListener
427 private JTextComponent textComponent;
429 ListSelectionHandler(JTextComponent textComponent)
431 this.textComponent = textComponent;
434 public void valueChanged(ListSelectionEvent e)
436 if (e.getValueIsAdjusting() == false)
438 JList list = (JList) e.getSource();
439 String selectedValue = (String) list.getSelectedValue();
441 String oldValue = textComponent.getText();
442 textComponent.setText(selectedValue);
443 if (!oldValue.equalsIgnoreCase(selectedValue))
445 textComponent.selectAll();
446 textComponent.requestFocus();
454 protected class TextFieldFocusHandlerForTextSelection extends FocusAdapter
456 private JTextComponent textComponent;
458 public TextFieldFocusHandlerForTextSelection(JTextComponent textComponent)
460 this.textComponent = textComponent;
463 public void focusGained(FocusEvent e)
465 textComponent.selectAll();
468 public void focusLost(FocusEvent e)
470 textComponent.select(0, 0);
475 protected class TextFieldKeyHandlerForListSelectionUpDown extends KeyAdapter
477 private JList targetList;
479 public TextFieldKeyHandlerForListSelectionUpDown(JList list)
481 this.targetList = list;
484 public void keyPressed(KeyEvent e)
486 int i = targetList.getSelectedIndex();
487 switch (e.getKeyCode())
490 i = targetList.getSelectedIndex() - 1;
495 targetList.setSelectedIndex(i);
497 case KeyEvent.VK_DOWN:
498 int listSize = targetList.getModel().getSize();
499 i = targetList.getSelectedIndex() + 1;
504 targetList.setSelectedIndex(i);
512 protected class ListSearchTextFieldDocumentHandler implements DocumentListener
516 public ListSearchTextFieldDocumentHandler(JList targetList)
518 this.targetList = targetList;
521 public void insertUpdate(DocumentEvent e)
526 public void removeUpdate(DocumentEvent e)
531 public void changedUpdate(DocumentEvent e)
536 private void update(DocumentEvent event)
538 String newValue = "";
541 Document doc = event.getDocument();
542 newValue = doc.getText(0, doc.getLength());
544 catch (BadLocationException e)
549 if (newValue.length() > 0)
551 int index = targetList.getNextMatch(newValue, 0, Position.Bias.Forward);
556 targetList.ensureIndexIsVisible(index);
558 String matchedName = targetList.getModel().getElementAt(index).toString();
559 if (newValue.equalsIgnoreCase(matchedName))
561 if (index != targetList.getSelectedIndex())
563 SwingUtilities.invokeLater(new ListSelector(index));
569 public class ListSelector implements Runnable
573 public ListSelector(int index)
580 targetList.setSelectedIndex(this.index);
585 protected class DialogOKAction extends AbstractAction
587 protected static final String ACTION_NAME = "OK";
588 private JDialog dialog;
590 protected DialogOKAction(JDialog dialog)
592 this.dialog = dialog;
593 putValue(Action.DEFAULT, ACTION_NAME);
594 putValue(Action.ACTION_COMMAND_KEY, ACTION_NAME);
595 putValue(Action.NAME, (ACTION_NAME));
598 public void actionPerformed(ActionEvent e)
600 dialogResultValue = OK_OPTION;
601 dialog.setVisible(false);
605 protected class DialogCancelAction extends AbstractAction
607 protected static final String ACTION_NAME = "Cancel";
608 private JDialog dialog;
610 protected DialogCancelAction(JDialog dialog)
612 this.dialog = dialog;
613 putValue(Action.DEFAULT, ACTION_NAME);
614 putValue(Action.ACTION_COMMAND_KEY, ACTION_NAME);
615 putValue(Action.NAME, (ACTION_NAME));
618 public void actionPerformed(ActionEvent e)
620 dialogResultValue = CANCEL_OPTION;
621 dialog.setVisible(false);
625 protected JDialog createDialog(Component parent)
627 Frame frame = parent instanceof Frame ? (Frame) parent
628 : (Frame) SwingUtilities.getAncestorOfClass(Frame.class, parent);
629 JDialog dialog = new JDialog(frame, ("Select Font"), true);
631 Action okAction = new DialogOKAction(dialog);
632 Action cancelAction = new DialogCancelAction(dialog);
634 JButton okButton = new JButton(okAction);
635 okButton.setFont(DEFAULT_FONT);
636 JButton cancelButton = new JButton(cancelAction);
637 cancelButton.setFont(DEFAULT_FONT);
639 JPanel buttonsPanel = new JPanel();
640 buttonsPanel.setLayout(new GridLayout(2, 1));
641 buttonsPanel.add(okButton);
642 buttonsPanel.add(cancelButton);
643 buttonsPanel.setBorder(BorderFactory.createEmptyBorder(25, 0, 10, 10));
645 ActionMap actionMap = buttonsPanel.getActionMap();
646 actionMap.put(cancelAction.getValue(Action.DEFAULT), cancelAction);
647 actionMap.put(okAction.getValue(Action.DEFAULT), okAction);
648 InputMap inputMap = buttonsPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
649 inputMap.put(KeyStroke.getKeyStroke("ESCAPE"), cancelAction.getValue(Action.DEFAULT));
650 inputMap.put(KeyStroke.getKeyStroke("ENTER"), okAction.getValue(Action.DEFAULT));
652 JPanel dialogEastPanel = new JPanel();
653 dialogEastPanel.setLayout(new BorderLayout());
654 dialogEastPanel.add(buttonsPanel, BorderLayout.NORTH);
656 dialog.getContentPane().add(this, BorderLayout.CENTER);
657 dialog.getContentPane().add(dialogEastPanel, BorderLayout.EAST);
659 dialog.setLocationRelativeTo(frame);
663 protected void updateSampleFont()
665 Font font = getSelectedFont();
666 getSampleTextField().setFont(font);
669 protected JPanel getFontFamilyPanel()
671 if (fontNamePanel == null)
673 fontNamePanel = new JPanel();
674 fontNamePanel.setLayout(new BorderLayout());
675 fontNamePanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
676 fontNamePanel.setPreferredSize(new Dimension(180, 130));
678 JScrollPane scrollPane = new JScrollPane(getFontFamilyList());
679 scrollPane.getVerticalScrollBar().setFocusable(false);
680 scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
682 JPanel p = new JPanel();
683 p.setLayout(new BorderLayout());
684 p.add(getFontFamilyTextField(), BorderLayout.NORTH);
685 p.add(scrollPane, BorderLayout.CENTER);
687 JLabel label = new JLabel(("Font Name"));
688 label.setHorizontalAlignment(JLabel.LEFT);
689 label.setHorizontalTextPosition(JLabel.LEFT);
690 label.setLabelFor(getFontFamilyTextField());
691 label.setDisplayedMnemonic('F');
693 fontNamePanel.add(label, BorderLayout.NORTH);
694 fontNamePanel.add(p, BorderLayout.CENTER);
697 return fontNamePanel;
700 protected JPanel getFontStylePanel()
702 if (fontStylePanel == null)
704 fontStylePanel = new JPanel();
705 fontStylePanel.setLayout(new BorderLayout());
706 fontStylePanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
707 fontStylePanel.setPreferredSize(new Dimension(140, 130));
709 JScrollPane scrollPane = new JScrollPane(getFontStyleList());
710 scrollPane.getVerticalScrollBar().setFocusable(false);
711 scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
713 JPanel p = new JPanel();
714 p.setLayout(new BorderLayout());
715 p.add(getFontStyleTextField(), BorderLayout.NORTH);
716 p.add(scrollPane, BorderLayout.CENTER);
718 JLabel label = new JLabel(("Font Style"));
719 label.setHorizontalAlignment(JLabel.LEFT);
720 label.setHorizontalTextPosition(JLabel.LEFT);
721 label.setLabelFor(getFontStyleTextField());
722 label.setDisplayedMnemonic('Y');
724 fontStylePanel.add(label, BorderLayout.NORTH);
725 fontStylePanel.add(p, BorderLayout.CENTER);
727 return fontStylePanel;
730 protected JPanel getFontSizePanel()
732 if (fontSizePanel == null)
734 fontSizePanel = new JPanel();
735 fontSizePanel.setLayout(new BorderLayout());
736 fontSizePanel.setPreferredSize(new Dimension(70, 130));
737 fontSizePanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
739 JScrollPane scrollPane = new JScrollPane(getFontSizeList());
740 scrollPane.getVerticalScrollBar().setFocusable(false);
741 scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
743 JPanel p = new JPanel();
744 p.setLayout(new BorderLayout());
745 p.add(getFontSizeTextField(), BorderLayout.NORTH);
746 p.add(scrollPane, BorderLayout.CENTER);
748 JLabel label = new JLabel(("Font Size"));
749 label.setHorizontalAlignment(JLabel.LEFT);
750 label.setHorizontalTextPosition(JLabel.LEFT);
751 label.setLabelFor(getFontSizeTextField());
752 label.setDisplayedMnemonic('S');
754 fontSizePanel.add(label, BorderLayout.NORTH);
755 fontSizePanel.add(p, BorderLayout.CENTER);
757 return fontSizePanel;
760 protected JPanel getSamplePanel()
762 if (samplePanel == null)
764 Border titledBorder = BorderFactory.createTitledBorder(
765 BorderFactory.createEtchedBorder(), ("Sample"));
766 Border empty = BorderFactory.createEmptyBorder(5, 10, 10, 10);
767 Border border = BorderFactory.createCompoundBorder(titledBorder, empty);
769 samplePanel = new JPanel();
770 samplePanel.setLayout(new BorderLayout());
771 samplePanel.setBorder(border);
773 samplePanel.add(getSampleTextField(), BorderLayout.CENTER);
778 protected JTextField getSampleTextField()
780 if (sampleText == null)
782 Border lowered = BorderFactory.createLoweredBevelBorder();
784 sampleText = new JTextField(("AaBbYyZz"));
785 sampleText.setBorder(lowered);
786 sampleText.setPreferredSize(new Dimension(300, 100));
791 protected String[] getFontFamilies()
793 if (fontFamilyNames == null)
795 GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
796 fontFamilyNames = env.getAvailableFontFamilyNames();
798 return fontFamilyNames;
801 protected String[] getFontStyleNames()
803 if (fontStyleNames == null)
806 fontStyleNames = new String[4];
807 fontStyleNames[i++] = ("Plain");
808 fontStyleNames[i++] = ("Bold");
809 fontStyleNames[i++] = ("Italic");
810 fontStyleNames[i++] = ("BoldItalic");
812 return fontStyleNames;