-package org.simantics.spreadsheet.ui;\r
-\r
-import java.awt.BorderLayout;\r
-import java.awt.Component;\r
-import java.awt.Dimension;\r
-import java.awt.Font;\r
-import java.awt.Frame;\r
-import java.awt.GraphicsEnvironment;\r
-import java.awt.GridLayout;\r
-import java.awt.event.ActionEvent;\r
-import java.awt.event.FocusAdapter;\r
-import java.awt.event.FocusEvent;\r
-import java.awt.event.KeyAdapter;\r
-import java.awt.event.KeyEvent;\r
-import java.awt.event.WindowAdapter;\r
-import java.awt.event.WindowEvent;\r
-\r
-import javax.swing.AbstractAction;\r
-import javax.swing.Action;\r
-import javax.swing.ActionMap;\r
-import javax.swing.BorderFactory;\r
-import javax.swing.BoxLayout;\r
-import javax.swing.InputMap;\r
-import javax.swing.JButton;\r
-import javax.swing.JComponent;\r
-import javax.swing.JDialog;\r
-import javax.swing.JLabel;\r
-import javax.swing.JList;\r
-import javax.swing.JPanel;\r
-import javax.swing.JScrollPane;\r
-import javax.swing.JTextField;\r
-import javax.swing.KeyStroke;\r
-import javax.swing.ListSelectionModel;\r
-import javax.swing.SwingUtilities;\r
-import javax.swing.border.Border;\r
-import javax.swing.event.DocumentEvent;\r
-import javax.swing.event.DocumentListener;\r
-import javax.swing.event.ListSelectionEvent;\r
-import javax.swing.event.ListSelectionListener;\r
-import javax.swing.text.BadLocationException;\r
-import javax.swing.text.Document;\r
-import javax.swing.text.JTextComponent;\r
-import javax.swing.text.Position;\r
-\r
-/**\r
- * The <code>JFontChooser</code> class is a swing component \r
- * for font selection.\r
- * This class has <code>JFileChooser</code> like APIs.\r
- * The following code pops up a font chooser dialog.\r
- * <pre>\r
- * JFontChooser fontChooser = new JFontChooser();\r
- * int result = fontChooser.showDialog(parent);\r
- * if (result == JFontChooser.OK_OPTION)\r
- * {\r
- * Font font = fontChooser.getSelectedFont(); \r
- * System.out.println("Selected Font : " + font); \r
- * }\r
- * <pre>\r
- **/\r
-public class JFontChooser extends JComponent\r
-{\r
- private static final long serialVersionUID = -8803227254655562584L;\r
- // class variables\r
- /**\r
- * Return value from <code>showDialog()</code>.\r
- * @see #showDialog\r
- **/\r
- public static final int OK_OPTION = 0;\r
- /**\r
- * Return value from <code>showDialog()</code>.\r
- * @see #showDialog\r
- **/\r
- public static final int CANCEL_OPTION = 1;\r
- /**\r
- * Return value from <code>showDialog()</code>.\r
- * @see #showDialog\r
- **/\r
- public static final int ERROR_OPTION = -1;\r
- private static final Font DEFAULT_SELECTED_FONT = new Font("Serif", Font.PLAIN, 12);\r
- private static final Font DEFAULT_FONT = new Font("Dialog", Font.PLAIN, 10);\r
- private static final int[] FONT_STYLE_CODES =\r
- {\r
- Font.PLAIN, Font.BOLD, Font.ITALIC, Font.BOLD | Font.ITALIC\r
- };\r
- private static final String[] DEFAULT_FONT_SIZE_STRINGS =\r
- {\r
- "8", "9", "10", "11", "12", "14", "16", "18", "20",\r
- "22", "24", "26", "28", "36", "48", "72",\r
- };\r
-\r
- // instance variables\r
- protected int dialogResultValue = ERROR_OPTION;\r
-\r
-\r
-\r
- private String[] fontStyleNames = null;\r
- private String[] fontFamilyNames = null;\r
- private String[] fontSizeStrings = null;\r
- private JTextField fontFamilyTextField = null;\r
- private JTextField fontStyleTextField = null;\r
- private JTextField fontSizeTextField = null;\r
- private JList fontNameList = null;\r
- private JList fontStyleList = null;\r
- private JList fontSizeList = null;\r
- private JPanel fontNamePanel = null;\r
- private JPanel fontStylePanel = null;\r
- private JPanel fontSizePanel = null;\r
- private JPanel samplePanel = null;\r
- private JTextField sampleText = null;\r
-\r
- /**\r
- * Constructs a <code>JFontChooser</code> object.\r
- **/\r
- public JFontChooser()\r
- {\r
- this(DEFAULT_FONT_SIZE_STRINGS);\r
- }\r
-\r
- /**\r
- * Constructs a <code>JFontChooser</code> object using the given font size array.\r
- * @param fontSizeStrings the array of font size string.\r
- **/\r
- public JFontChooser(String[] fontSizeStrings)\r
- {\r
- if (fontSizeStrings == null)\r
- {\r
- fontSizeStrings = DEFAULT_FONT_SIZE_STRINGS;\r
- }\r
- this.fontSizeStrings = fontSizeStrings;\r
-\r
- JPanel selectPanel = new JPanel();\r
- selectPanel.setLayout(new BoxLayout(selectPanel, BoxLayout.X_AXIS));\r
- selectPanel.add(getFontFamilyPanel());\r
- selectPanel.add(getFontStylePanel());\r
- selectPanel.add(getFontSizePanel());\r
-\r
- JPanel contentsPanel = new JPanel();\r
- contentsPanel.setLayout(new GridLayout(2, 1));\r
- contentsPanel.add(selectPanel, BorderLayout.NORTH);\r
- contentsPanel.add(getSamplePanel(), BorderLayout.CENTER);\r
-\r
- this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));\r
- this.add(contentsPanel);\r
- this.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));\r
- this.setSelectedFont(DEFAULT_SELECTED_FONT);\r
- }\r
-\r
- public JTextField getFontFamilyTextField()\r
- {\r
- if (fontFamilyTextField == null)\r
- {\r
- fontFamilyTextField = new JTextField();\r
- fontFamilyTextField.addFocusListener(\r
- new TextFieldFocusHandlerForTextSelection(fontFamilyTextField));\r
- fontFamilyTextField.addKeyListener(\r
- new TextFieldKeyHandlerForListSelectionUpDown(getFontFamilyList()));\r
- fontFamilyTextField.getDocument().addDocumentListener(\r
- new ListSearchTextFieldDocumentHandler(getFontFamilyList()));\r
- fontFamilyTextField.setFont(DEFAULT_FONT);\r
-\r
- }\r
- return fontFamilyTextField;\r
- }\r
-\r
- public JTextField getFontStyleTextField()\r
- {\r
- if (fontStyleTextField == null)\r
- {\r
- fontStyleTextField = new JTextField();\r
- fontStyleTextField.addFocusListener(\r
- new TextFieldFocusHandlerForTextSelection(fontStyleTextField));\r
- fontStyleTextField.addKeyListener(\r
- new TextFieldKeyHandlerForListSelectionUpDown(getFontStyleList()));\r
- fontStyleTextField.getDocument().addDocumentListener(\r
- new ListSearchTextFieldDocumentHandler(getFontStyleList()));\r
- fontStyleTextField.setFont(DEFAULT_FONT);\r
- }\r
- return fontStyleTextField;\r
- }\r
-\r
- public JTextField getFontSizeTextField()\r
- {\r
- if (fontSizeTextField == null)\r
- {\r
- fontSizeTextField = new JTextField();\r
- fontSizeTextField.addFocusListener(\r
- new TextFieldFocusHandlerForTextSelection(fontSizeTextField));\r
- fontSizeTextField.addKeyListener(\r
- new TextFieldKeyHandlerForListSelectionUpDown(getFontSizeList()));\r
- fontSizeTextField.getDocument().addDocumentListener(\r
- new ListSearchTextFieldDocumentHandler(getFontSizeList()));\r
- fontSizeTextField.setFont(DEFAULT_FONT);\r
- }\r
- return fontSizeTextField;\r
- }\r
-\r
- public JList getFontFamilyList()\r
- {\r
- if (fontNameList == null)\r
- {\r
- fontNameList = new JList(getFontFamilies());\r
- fontNameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);\r
- fontNameList.addListSelectionListener(\r
- new ListSelectionHandler(getFontFamilyTextField()));\r
- fontNameList.setSelectedIndex(0);\r
- fontNameList.setFont(DEFAULT_FONT);\r
- fontNameList.setFocusable(false);\r
- }\r
- return fontNameList;\r
- }\r
-\r
- public JList getFontStyleList()\r
- {\r
- if (fontStyleList == null)\r
- {\r
- fontStyleList = new JList(getFontStyleNames());\r
- fontStyleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);\r
- fontStyleList.addListSelectionListener(\r
- new ListSelectionHandler(getFontStyleTextField()));\r
- fontStyleList.setSelectedIndex(0);\r
- fontStyleList.setFont(DEFAULT_FONT);\r
- fontStyleList.setFocusable(false);\r
- }\r
- return fontStyleList;\r
- }\r
-\r
- public JList getFontSizeList()\r
- {\r
- if (fontSizeList == null)\r
- {\r
- fontSizeList = new JList(this.fontSizeStrings);\r
- fontSizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);\r
- fontSizeList.addListSelectionListener(\r
- new ListSelectionHandler(getFontSizeTextField()));\r
- fontSizeList.setSelectedIndex(0);\r
- fontSizeList.setFont(DEFAULT_FONT);\r
- fontSizeList.setFocusable(false);\r
- }\r
- return fontSizeList;\r
- }\r
-\r
- /**\r
- * Get the family name of the selected font.\r
- * @return the font family of the selected font.\r
- *\r
- * @see #setSelectedFontFamily\r
- **/\r
- public String getSelectedFontFamily()\r
- {\r
- String fontName = (String) getFontFamilyList().getSelectedValue();\r
- return fontName;\r
- }\r
-\r
- /**\r
- * Get the style of the selected font.\r
- * @return the style of the selected font.\r
- * <code>Font.PLAIN</code>, <code>Font.BOLD</code>,\r
- * <code>Font.ITALIC</code>, <code>Font.BOLD|Font.ITALIC</code>\r
- *\r
- * @see java.awt.Font#PLAIN\r
- * @see java.awt.Font#BOLD\r
- * @see java.awt.Font#ITALIC\r
- * @see #setSelectedFontStyle\r
- **/\r
- public int getSelectedFontStyle()\r
- {\r
- int index = getFontStyleList().getSelectedIndex();\r
- return FONT_STYLE_CODES[index];\r
- }\r
-\r
- /**\r
- * Get the size of the selected font.\r
- * @return the size of the selected font\r
- *\r
- * @see #setSelectedFontSize\r
- **/\r
- public int getSelectedFontSize()\r
- {\r
- int fontSize = 1;\r
- String fontSizeString = getFontSizeTextField().getText();\r
- while (true)\r
- {\r
- try\r
- {\r
- fontSize = Integer.parseInt(fontSizeString);\r
- break;\r
- }\r
- catch (NumberFormatException e)\r
- {\r
- fontSizeString = (String) getFontSizeList().getSelectedValue();\r
- getFontSizeTextField().setText(fontSizeString);\r
- }\r
- }\r
-\r
- return fontSize;\r
- }\r
-\r
- /**\r
- * Get the selected font.\r
- * @return the selected font\r
- *\r
- * @see #setSelectedFont\r
- * @see java.awt.Font\r
- **/\r
- public Font getSelectedFont()\r
- {\r
- Font font = new Font(getSelectedFontFamily(),\r
- getSelectedFontStyle(), getSelectedFontSize());\r
- return font;\r
- }\r
-\r
- /**\r
- * Set the family name of the selected font.\r
- * @param name the family name of the selected font. \r
- *\r
- * @see getSelectedFontFamily\r
- **/\r
- public void setSelectedFontFamily(String name)\r
- {\r
- String[] names = getFontFamilies();\r
- for (int i = 0; i < names.length; i++)\r
- {\r
- if (names[i].toLowerCase().equals(name.toLowerCase()))\r
- {\r
- getFontFamilyList().setSelectedIndex(i);\r
- break;\r
- }\r
- }\r
- updateSampleFont();\r
- }\r
-\r
- /**\r
- * Set the style of the selected font.\r
- * @param style the size of the selected font.\r
- * <code>Font.PLAIN</code>, <code>Font.BOLD</code>,\r
- * <code>Font.ITALIC</code>, or\r
- * <code>Font.BOLD|Font.ITALIC</code>.\r
- *\r
- * @see java.awt.Font#PLAIN\r
- * @see java.awt.Font#BOLD\r
- * @see java.awt.Font#ITALIC\r
- * @see #getSelectedFontStyle\r
- **/\r
- public void setSelectedFontStyle(int style)\r
- {\r
- for (int i = 0; i < FONT_STYLE_CODES.length; i++)\r
- {\r
- if (FONT_STYLE_CODES[i] == style)\r
- {\r
- getFontStyleList().setSelectedIndex(i);\r
- break;\r
- }\r
- }\r
- updateSampleFont();\r
- }\r
-\r
- /**\r
- * Set the size of the selected font.\r
- * @param size the size of the selected font\r
- *\r
- * @see #getSelectedFontSize\r
- **/\r
- public void setSelectedFontSize(int size)\r
- {\r
- String sizeString = String.valueOf(size);\r
- for (int i = 0; i < this.fontSizeStrings.length; i++)\r
- {\r
- if (this.fontSizeStrings[i].equals(sizeString))\r
- {\r
- getFontSizeList().setSelectedIndex(i);\r
- break;\r
- }\r
- }\r
- getFontSizeTextField().setText(sizeString);\r
- updateSampleFont();\r
- }\r
-\r
- /**\r
- * Set the selected font.\r
- * @param font the selected font\r
- *\r
- * @see #getSelectedFont\r
- * @see java.awt.Font\r
- **/\r
- public void setSelectedFont(Font font)\r
- {\r
- setSelectedFontFamily(font.getFamily());\r
- setSelectedFontStyle(font.getStyle());\r
- setSelectedFontSize(font.getSize());\r
- }\r
-\r
- public String getVersionString()\r
- {\r
- return ("Version");\r
- }\r
-\r
- /**\r
- * Show font selection dialog.\r
- * @param parent Dialog's Parent component.\r
- * @return OK_OPTION, CANCEL_OPTION or ERROR_OPTION\r
- *\r
- * @see #OK_OPTION \r
- * @see #CANCEL_OPTION\r
- * @see #ERROR_OPTION\r
- **/\r
- public int showDialog(Component parent)\r
- {\r
- dialogResultValue = ERROR_OPTION;\r
- JDialog dialog = createDialog(parent);\r
- dialog.addWindowListener(new WindowAdapter()\r
- {\r
- public void windowClosing(WindowEvent e)\r
- {\r
- dialogResultValue = CANCEL_OPTION;\r
- }\r
- });\r
-\r
- dialog.setVisible(true);\r
- dialog.dispose();\r
- dialog = null;\r
-\r
- return dialogResultValue;\r
- }\r
-\r
- protected class ListSelectionHandler implements ListSelectionListener\r
- {\r
- private JTextComponent textComponent;\r
-\r
- ListSelectionHandler(JTextComponent textComponent)\r
- {\r
- this.textComponent = textComponent;\r
- }\r
-\r
- public void valueChanged(ListSelectionEvent e)\r
- {\r
- if (e.getValueIsAdjusting() == false)\r
- {\r
- JList list = (JList) e.getSource();\r
- String selectedValue = (String) list.getSelectedValue();\r
-\r
- String oldValue = textComponent.getText();\r
- textComponent.setText(selectedValue);\r
- if (!oldValue.equalsIgnoreCase(selectedValue))\r
- {\r
- textComponent.selectAll();\r
- textComponent.requestFocus();\r
- }\r
-\r
- updateSampleFont();\r
- }\r
- }\r
- }\r
-\r
- protected class TextFieldFocusHandlerForTextSelection extends FocusAdapter\r
- {\r
- private JTextComponent textComponent;\r
-\r
- public TextFieldFocusHandlerForTextSelection(JTextComponent textComponent)\r
- {\r
- this.textComponent = textComponent;\r
- }\r
-\r
- public void focusGained(FocusEvent e)\r
- {\r
- textComponent.selectAll();\r
- }\r
-\r
- public void focusLost(FocusEvent e)\r
- {\r
- textComponent.select(0, 0);\r
- updateSampleFont();\r
- }\r
- }\r
-\r
- protected class TextFieldKeyHandlerForListSelectionUpDown extends KeyAdapter\r
- {\r
- private JList targetList;\r
-\r
- public TextFieldKeyHandlerForListSelectionUpDown(JList list)\r
- {\r
- this.targetList = list;\r
- }\r
-\r
- public void keyPressed(KeyEvent e)\r
- {\r
- int i = targetList.getSelectedIndex();\r
- switch (e.getKeyCode())\r
- {\r
- case KeyEvent.VK_UP:\r
- i = targetList.getSelectedIndex() - 1;\r
- if (i < 0)\r
- {\r
- i = 0;\r
- }\r
- targetList.setSelectedIndex(i);\r
- break;\r
- case KeyEvent.VK_DOWN:\r
- int listSize = targetList.getModel().getSize();\r
- i = targetList.getSelectedIndex() + 1;\r
- if (i >= listSize)\r
- {\r
- i = listSize - 1;\r
- }\r
- targetList.setSelectedIndex(i);\r
- break;\r
- default:\r
- break;\r
- }\r
- }\r
- }\r
-\r
- protected class ListSearchTextFieldDocumentHandler implements DocumentListener\r
- {\r
- JList targetList;\r
-\r
- public ListSearchTextFieldDocumentHandler(JList targetList)\r
- {\r
- this.targetList = targetList;\r
- }\r
-\r
- public void insertUpdate(DocumentEvent e)\r
- {\r
- update(e);\r
- }\r
-\r
- public void removeUpdate(DocumentEvent e)\r
- {\r
- update(e);\r
- }\r
-\r
- public void changedUpdate(DocumentEvent e)\r
- {\r
- update(e);\r
- }\r
-\r
- private void update(DocumentEvent event)\r
- {\r
- String newValue = "";\r
- try\r
- {\r
- Document doc = event.getDocument();\r
- newValue = doc.getText(0, doc.getLength());\r
- }\r
- catch (BadLocationException e)\r
- {\r
- e.printStackTrace();\r
- }\r
-\r
- if (newValue.length() > 0)\r
- {\r
- int index = targetList.getNextMatch(newValue, 0, Position.Bias.Forward);\r
- if (index < 0)\r
- {\r
- index = 0;\r
- }\r
- targetList.ensureIndexIsVisible(index);\r
-\r
- String matchedName = targetList.getModel().getElementAt(index).toString();\r
- if (newValue.equalsIgnoreCase(matchedName))\r
- {\r
- if (index != targetList.getSelectedIndex())\r
- {\r
- SwingUtilities.invokeLater(new ListSelector(index));\r
- }\r
- }\r
- }\r
- }\r
-\r
- public class ListSelector implements Runnable\r
- {\r
- private int index;\r
-\r
- public ListSelector(int index)\r
- {\r
- this.index = index;\r
- }\r
-\r
- public void run()\r
- {\r
- targetList.setSelectedIndex(this.index);\r
- }\r
- }\r
- }\r
-\r
- protected class DialogOKAction extends AbstractAction\r
- {\r
- protected static final String ACTION_NAME = "OK";\r
- private JDialog dialog;\r
-\r
- protected DialogOKAction(JDialog dialog)\r
- {\r
- this.dialog = dialog;\r
- putValue(Action.DEFAULT, ACTION_NAME);\r
- putValue(Action.ACTION_COMMAND_KEY, ACTION_NAME);\r
- putValue(Action.NAME, (ACTION_NAME));\r
- }\r
-\r
- public void actionPerformed(ActionEvent e)\r
- {\r
- dialogResultValue = OK_OPTION;\r
- dialog.setVisible(false);\r
- }\r
- }\r
-\r
- protected class DialogCancelAction extends AbstractAction\r
- {\r
- protected static final String ACTION_NAME = "Cancel";\r
- private JDialog dialog;\r
-\r
- protected DialogCancelAction(JDialog dialog)\r
- {\r
- this.dialog = dialog;\r
- putValue(Action.DEFAULT, ACTION_NAME);\r
- putValue(Action.ACTION_COMMAND_KEY, ACTION_NAME);\r
- putValue(Action.NAME, (ACTION_NAME));\r
- }\r
-\r
- public void actionPerformed(ActionEvent e)\r
- {\r
- dialogResultValue = CANCEL_OPTION;\r
- dialog.setVisible(false);\r
- }\r
- }\r
-\r
- protected JDialog createDialog(Component parent)\r
- {\r
- Frame frame = parent instanceof Frame ? (Frame) parent\r
- : (Frame) SwingUtilities.getAncestorOfClass(Frame.class, parent);\r
- JDialog dialog = new JDialog(frame, ("Select Font"), true);\r
-\r
- Action okAction = new DialogOKAction(dialog);\r
- Action cancelAction = new DialogCancelAction(dialog);\r
-\r
- JButton okButton = new JButton(okAction);\r
- okButton.setFont(DEFAULT_FONT);\r
- JButton cancelButton = new JButton(cancelAction);\r
- cancelButton.setFont(DEFAULT_FONT);\r
-\r
- JPanel buttonsPanel = new JPanel();\r
- buttonsPanel.setLayout(new GridLayout(2, 1));\r
- buttonsPanel.add(okButton);\r
- buttonsPanel.add(cancelButton);\r
- buttonsPanel.setBorder(BorderFactory.createEmptyBorder(25, 0, 10, 10));\r
-\r
- ActionMap actionMap = buttonsPanel.getActionMap();\r
- actionMap.put(cancelAction.getValue(Action.DEFAULT), cancelAction);\r
- actionMap.put(okAction.getValue(Action.DEFAULT), okAction);\r
- InputMap inputMap = buttonsPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);\r
- inputMap.put(KeyStroke.getKeyStroke("ESCAPE"), cancelAction.getValue(Action.DEFAULT));\r
- inputMap.put(KeyStroke.getKeyStroke("ENTER"), okAction.getValue(Action.DEFAULT));\r
-\r
- JPanel dialogEastPanel = new JPanel();\r
- dialogEastPanel.setLayout(new BorderLayout());\r
- dialogEastPanel.add(buttonsPanel, BorderLayout.NORTH);\r
-\r
- dialog.getContentPane().add(this, BorderLayout.CENTER);\r
- dialog.getContentPane().add(dialogEastPanel, BorderLayout.EAST);\r
- dialog.pack();\r
- dialog.setLocationRelativeTo(frame);\r
- return dialog;\r
- }\r
-\r
- protected void updateSampleFont()\r
- {\r
- Font font = getSelectedFont();\r
- getSampleTextField().setFont(font);\r
- }\r
-\r
- protected JPanel getFontFamilyPanel()\r
- {\r
- if (fontNamePanel == null)\r
- {\r
- fontNamePanel = new JPanel();\r
- fontNamePanel.setLayout(new BorderLayout());\r
- fontNamePanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));\r
- fontNamePanel.setPreferredSize(new Dimension(180, 130));\r
-\r
- JScrollPane scrollPane = new JScrollPane(getFontFamilyList());\r
- scrollPane.getVerticalScrollBar().setFocusable(false);\r
- scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);\r
-\r
- JPanel p = new JPanel();\r
- p.setLayout(new BorderLayout());\r
- p.add(getFontFamilyTextField(), BorderLayout.NORTH);\r
- p.add(scrollPane, BorderLayout.CENTER);\r
-\r
- JLabel label = new JLabel(("Font Name"));\r
- label.setHorizontalAlignment(JLabel.LEFT);\r
- label.setHorizontalTextPosition(JLabel.LEFT);\r
- label.setLabelFor(getFontFamilyTextField());\r
- label.setDisplayedMnemonic('F');\r
-\r
- fontNamePanel.add(label, BorderLayout.NORTH);\r
- fontNamePanel.add(p, BorderLayout.CENTER);\r
-\r
- }\r
- return fontNamePanel;\r
- }\r
-\r
- protected JPanel getFontStylePanel()\r
- {\r
- if (fontStylePanel == null)\r
- {\r
- fontStylePanel = new JPanel();\r
- fontStylePanel.setLayout(new BorderLayout());\r
- fontStylePanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));\r
- fontStylePanel.setPreferredSize(new Dimension(140, 130));\r
-\r
- JScrollPane scrollPane = new JScrollPane(getFontStyleList());\r
- scrollPane.getVerticalScrollBar().setFocusable(false);\r
- scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);\r
-\r
- JPanel p = new JPanel();\r
- p.setLayout(new BorderLayout());\r
- p.add(getFontStyleTextField(), BorderLayout.NORTH);\r
- p.add(scrollPane, BorderLayout.CENTER);\r
-\r
- JLabel label = new JLabel(("Font Style"));\r
- label.setHorizontalAlignment(JLabel.LEFT);\r
- label.setHorizontalTextPosition(JLabel.LEFT);\r
- label.setLabelFor(getFontStyleTextField());\r
- label.setDisplayedMnemonic('Y');\r
-\r
- fontStylePanel.add(label, BorderLayout.NORTH);\r
- fontStylePanel.add(p, BorderLayout.CENTER);\r
- }\r
- return fontStylePanel;\r
- }\r
-\r
- protected JPanel getFontSizePanel()\r
- {\r
- if (fontSizePanel == null)\r
- {\r
- fontSizePanel = new JPanel();\r
- fontSizePanel.setLayout(new BorderLayout());\r
- fontSizePanel.setPreferredSize(new Dimension(70, 130));\r
- fontSizePanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));\r
-\r
- JScrollPane scrollPane = new JScrollPane(getFontSizeList());\r
- scrollPane.getVerticalScrollBar().setFocusable(false);\r
- scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);\r
-\r
- JPanel p = new JPanel();\r
- p.setLayout(new BorderLayout());\r
- p.add(getFontSizeTextField(), BorderLayout.NORTH);\r
- p.add(scrollPane, BorderLayout.CENTER);\r
-\r
- JLabel label = new JLabel(("Font Size"));\r
- label.setHorizontalAlignment(JLabel.LEFT);\r
- label.setHorizontalTextPosition(JLabel.LEFT);\r
- label.setLabelFor(getFontSizeTextField());\r
- label.setDisplayedMnemonic('S');\r
-\r
- fontSizePanel.add(label, BorderLayout.NORTH);\r
- fontSizePanel.add(p, BorderLayout.CENTER);\r
- }\r
- return fontSizePanel;\r
- }\r
-\r
- protected JPanel getSamplePanel()\r
- {\r
- if (samplePanel == null)\r
- {\r
- Border titledBorder = BorderFactory.createTitledBorder(\r
- BorderFactory.createEtchedBorder(), ("Sample"));\r
- Border empty = BorderFactory.createEmptyBorder(5, 10, 10, 10);\r
- Border border = BorderFactory.createCompoundBorder(titledBorder, empty);\r
-\r
- samplePanel = new JPanel();\r
- samplePanel.setLayout(new BorderLayout());\r
- samplePanel.setBorder(border);\r
-\r
- samplePanel.add(getSampleTextField(), BorderLayout.CENTER);\r
- }\r
- return samplePanel;\r
- }\r
-\r
- protected JTextField getSampleTextField()\r
- {\r
- if (sampleText == null)\r
- {\r
- Border lowered = BorderFactory.createLoweredBevelBorder();\r
-\r
- sampleText = new JTextField(("AaBbYyZz"));\r
- sampleText.setBorder(lowered);\r
- sampleText.setPreferredSize(new Dimension(300, 100));\r
- }\r
- return sampleText;\r
- }\r
-\r
- protected String[] getFontFamilies()\r
- {\r
- if (fontFamilyNames == null)\r
- {\r
- GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();\r
- fontFamilyNames = env.getAvailableFontFamilyNames();\r
- }\r
- return fontFamilyNames;\r
- }\r
-\r
- protected String[] getFontStyleNames()\r
- {\r
- if (fontStyleNames == null)\r
- {\r
- int i = 0;\r
- fontStyleNames = new String[4];\r
- fontStyleNames[i++] = ("Plain");\r
- fontStyleNames[i++] = ("Bold");\r
- fontStyleNames[i++] = ("Italic");\r
- fontStyleNames[i++] = ("BoldItalic");\r
- }\r
- return fontStyleNames;\r
- }\r
+package org.simantics.spreadsheet.ui;
+
+import java.awt.BorderLayout;
+import java.awt.Component;
+import java.awt.Dimension;
+import java.awt.Font;
+import java.awt.Frame;
+import java.awt.GraphicsEnvironment;
+import java.awt.GridLayout;
+import java.awt.event.ActionEvent;
+import java.awt.event.FocusAdapter;
+import java.awt.event.FocusEvent;
+import java.awt.event.KeyAdapter;
+import java.awt.event.KeyEvent;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+
+import javax.swing.AbstractAction;
+import javax.swing.Action;
+import javax.swing.ActionMap;
+import javax.swing.BorderFactory;
+import javax.swing.BoxLayout;
+import javax.swing.InputMap;
+import javax.swing.JButton;
+import javax.swing.JComponent;
+import javax.swing.JDialog;
+import javax.swing.JLabel;
+import javax.swing.JList;
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+import javax.swing.JTextField;
+import javax.swing.KeyStroke;
+import javax.swing.ListSelectionModel;
+import javax.swing.SwingUtilities;
+import javax.swing.border.Border;
+import javax.swing.event.DocumentEvent;
+import javax.swing.event.DocumentListener;
+import javax.swing.event.ListSelectionEvent;
+import javax.swing.event.ListSelectionListener;
+import javax.swing.text.BadLocationException;
+import javax.swing.text.Document;
+import javax.swing.text.JTextComponent;
+import javax.swing.text.Position;
+
+/**
+ * The <code>JFontChooser</code> class is a swing component
+ * for font selection.
+ * This class has <code>JFileChooser</code> like APIs.
+ * The following code pops up a font chooser dialog.
+ * <pre>
+ * JFontChooser fontChooser = new JFontChooser();
+ * int result = fontChooser.showDialog(parent);
+ * if (result == JFontChooser.OK_OPTION)
+ * {
+ * Font font = fontChooser.getSelectedFont();
+ * System.out.println("Selected Font : " + font);
+ * }
+ * <pre>
+ **/
+public class JFontChooser extends JComponent
+{
+ private static final long serialVersionUID = -8803227254655562584L;
+ // class variables
+ /**
+ * Return value from <code>showDialog()</code>.
+ * @see #showDialog
+ **/
+ public static final int OK_OPTION = 0;
+ /**
+ * Return value from <code>showDialog()</code>.
+ * @see #showDialog
+ **/
+ public static final int CANCEL_OPTION = 1;
+ /**
+ * Return value from <code>showDialog()</code>.
+ * @see #showDialog
+ **/
+ public static final int ERROR_OPTION = -1;
+ private static final Font DEFAULT_SELECTED_FONT = new Font("Serif", Font.PLAIN, 12);
+ private static final Font DEFAULT_FONT = new Font("Dialog", Font.PLAIN, 10);
+ private static final int[] FONT_STYLE_CODES =
+ {
+ Font.PLAIN, Font.BOLD, Font.ITALIC, Font.BOLD | Font.ITALIC
+ };
+ private static final String[] DEFAULT_FONT_SIZE_STRINGS =
+ {
+ "8", "9", "10", "11", "12", "14", "16", "18", "20",
+ "22", "24", "26", "28", "36", "48", "72",
+ };
+
+ // instance variables
+ protected int dialogResultValue = ERROR_OPTION;
+
+
+
+ private String[] fontStyleNames = null;
+ private String[] fontFamilyNames = null;
+ private String[] fontSizeStrings = null;
+ private JTextField fontFamilyTextField = null;
+ private JTextField fontStyleTextField = null;
+ private JTextField fontSizeTextField = null;
+ private JList fontNameList = null;
+ private JList fontStyleList = null;
+ private JList fontSizeList = null;
+ private JPanel fontNamePanel = null;
+ private JPanel fontStylePanel = null;
+ private JPanel fontSizePanel = null;
+ private JPanel samplePanel = null;
+ private JTextField sampleText = null;
+
+ /**
+ * Constructs a <code>JFontChooser</code> object.
+ **/
+ public JFontChooser()
+ {
+ this(DEFAULT_FONT_SIZE_STRINGS);
+ }
+
+ /**
+ * Constructs a <code>JFontChooser</code> object using the given font size array.
+ * @param fontSizeStrings the array of font size string.
+ **/
+ public JFontChooser(String[] fontSizeStrings)
+ {
+ if (fontSizeStrings == null)
+ {
+ fontSizeStrings = DEFAULT_FONT_SIZE_STRINGS;
+ }
+ this.fontSizeStrings = fontSizeStrings;
+
+ JPanel selectPanel = new JPanel();
+ selectPanel.setLayout(new BoxLayout(selectPanel, BoxLayout.X_AXIS));
+ selectPanel.add(getFontFamilyPanel());
+ selectPanel.add(getFontStylePanel());
+ selectPanel.add(getFontSizePanel());
+
+ JPanel contentsPanel = new JPanel();
+ contentsPanel.setLayout(new GridLayout(2, 1));
+ contentsPanel.add(selectPanel, BorderLayout.NORTH);
+ contentsPanel.add(getSamplePanel(), BorderLayout.CENTER);
+
+ this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
+ this.add(contentsPanel);
+ this.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
+ this.setSelectedFont(DEFAULT_SELECTED_FONT);
+ }
+
+ public JTextField getFontFamilyTextField()
+ {
+ if (fontFamilyTextField == null)
+ {
+ fontFamilyTextField = new JTextField();
+ fontFamilyTextField.addFocusListener(
+ new TextFieldFocusHandlerForTextSelection(fontFamilyTextField));
+ fontFamilyTextField.addKeyListener(
+ new TextFieldKeyHandlerForListSelectionUpDown(getFontFamilyList()));
+ fontFamilyTextField.getDocument().addDocumentListener(
+ new ListSearchTextFieldDocumentHandler(getFontFamilyList()));
+ fontFamilyTextField.setFont(DEFAULT_FONT);
+
+ }
+ return fontFamilyTextField;
+ }
+
+ public JTextField getFontStyleTextField()
+ {
+ if (fontStyleTextField == null)
+ {
+ fontStyleTextField = new JTextField();
+ fontStyleTextField.addFocusListener(
+ new TextFieldFocusHandlerForTextSelection(fontStyleTextField));
+ fontStyleTextField.addKeyListener(
+ new TextFieldKeyHandlerForListSelectionUpDown(getFontStyleList()));
+ fontStyleTextField.getDocument().addDocumentListener(
+ new ListSearchTextFieldDocumentHandler(getFontStyleList()));
+ fontStyleTextField.setFont(DEFAULT_FONT);
+ }
+ return fontStyleTextField;
+ }
+
+ public JTextField getFontSizeTextField()
+ {
+ if (fontSizeTextField == null)
+ {
+ fontSizeTextField = new JTextField();
+ fontSizeTextField.addFocusListener(
+ new TextFieldFocusHandlerForTextSelection(fontSizeTextField));
+ fontSizeTextField.addKeyListener(
+ new TextFieldKeyHandlerForListSelectionUpDown(getFontSizeList()));
+ fontSizeTextField.getDocument().addDocumentListener(
+ new ListSearchTextFieldDocumentHandler(getFontSizeList()));
+ fontSizeTextField.setFont(DEFAULT_FONT);
+ }
+ return fontSizeTextField;
+ }
+
+ public JList getFontFamilyList()
+ {
+ if (fontNameList == null)
+ {
+ fontNameList = new JList(getFontFamilies());
+ fontNameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
+ fontNameList.addListSelectionListener(
+ new ListSelectionHandler(getFontFamilyTextField()));
+ fontNameList.setSelectedIndex(0);
+ fontNameList.setFont(DEFAULT_FONT);
+ fontNameList.setFocusable(false);
+ }
+ return fontNameList;
+ }
+
+ public JList getFontStyleList()
+ {
+ if (fontStyleList == null)
+ {
+ fontStyleList = new JList(getFontStyleNames());
+ fontStyleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
+ fontStyleList.addListSelectionListener(
+ new ListSelectionHandler(getFontStyleTextField()));
+ fontStyleList.setSelectedIndex(0);
+ fontStyleList.setFont(DEFAULT_FONT);
+ fontStyleList.setFocusable(false);
+ }
+ return fontStyleList;
+ }
+
+ public JList getFontSizeList()
+ {
+ if (fontSizeList == null)
+ {
+ fontSizeList = new JList(this.fontSizeStrings);
+ fontSizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
+ fontSizeList.addListSelectionListener(
+ new ListSelectionHandler(getFontSizeTextField()));
+ fontSizeList.setSelectedIndex(0);
+ fontSizeList.setFont(DEFAULT_FONT);
+ fontSizeList.setFocusable(false);
+ }
+ return fontSizeList;
+ }
+
+ /**
+ * Get the family name of the selected font.
+ * @return the font family of the selected font.
+ *
+ * @see #setSelectedFontFamily
+ **/
+ public String getSelectedFontFamily()
+ {
+ String fontName = (String) getFontFamilyList().getSelectedValue();
+ return fontName;
+ }
+
+ /**
+ * Get the style of the selected font.
+ * @return the style of the selected font.
+ * <code>Font.PLAIN</code>, <code>Font.BOLD</code>,
+ * <code>Font.ITALIC</code>, <code>Font.BOLD|Font.ITALIC</code>
+ *
+ * @see java.awt.Font#PLAIN
+ * @see java.awt.Font#BOLD
+ * @see java.awt.Font#ITALIC
+ * @see #setSelectedFontStyle
+ **/
+ public int getSelectedFontStyle()
+ {
+ int index = getFontStyleList().getSelectedIndex();
+ return FONT_STYLE_CODES[index];
+ }
+
+ /**
+ * Get the size of the selected font.
+ * @return the size of the selected font
+ *
+ * @see #setSelectedFontSize
+ **/
+ public int getSelectedFontSize()
+ {
+ int fontSize = 1;
+ String fontSizeString = getFontSizeTextField().getText();
+ while (true)
+ {
+ try
+ {
+ fontSize = Integer.parseInt(fontSizeString);
+ break;
+ }
+ catch (NumberFormatException e)
+ {
+ fontSizeString = (String) getFontSizeList().getSelectedValue();
+ getFontSizeTextField().setText(fontSizeString);
+ }
+ }
+
+ return fontSize;
+ }
+
+ /**
+ * Get the selected font.
+ * @return the selected font
+ *
+ * @see #setSelectedFont
+ * @see java.awt.Font
+ **/
+ public Font getSelectedFont()
+ {
+ Font font = new Font(getSelectedFontFamily(),
+ getSelectedFontStyle(), getSelectedFontSize());
+ return font;
+ }
+
+ /**
+ * Set the family name of the selected font.
+ * @param name the family name of the selected font.
+ *
+ * @see getSelectedFontFamily
+ **/
+ public void setSelectedFontFamily(String name)
+ {
+ String[] names = getFontFamilies();
+ for (int i = 0; i < names.length; i++)
+ {
+ if (names[i].toLowerCase().equals(name.toLowerCase()))
+ {
+ getFontFamilyList().setSelectedIndex(i);
+ break;
+ }
+ }
+ updateSampleFont();
+ }
+
+ /**
+ * Set the style of the selected font.
+ * @param style the size of the selected font.
+ * <code>Font.PLAIN</code>, <code>Font.BOLD</code>,
+ * <code>Font.ITALIC</code>, or
+ * <code>Font.BOLD|Font.ITALIC</code>.
+ *
+ * @see java.awt.Font#PLAIN
+ * @see java.awt.Font#BOLD
+ * @see java.awt.Font#ITALIC
+ * @see #getSelectedFontStyle
+ **/
+ public void setSelectedFontStyle(int style)
+ {
+ for (int i = 0; i < FONT_STYLE_CODES.length; i++)
+ {
+ if (FONT_STYLE_CODES[i] == style)
+ {
+ getFontStyleList().setSelectedIndex(i);
+ break;
+ }
+ }
+ updateSampleFont();
+ }
+
+ /**
+ * Set the size of the selected font.
+ * @param size the size of the selected font
+ *
+ * @see #getSelectedFontSize
+ **/
+ public void setSelectedFontSize(int size)
+ {
+ String sizeString = String.valueOf(size);
+ for (int i = 0; i < this.fontSizeStrings.length; i++)
+ {
+ if (this.fontSizeStrings[i].equals(sizeString))
+ {
+ getFontSizeList().setSelectedIndex(i);
+ break;
+ }
+ }
+ getFontSizeTextField().setText(sizeString);
+ updateSampleFont();
+ }
+
+ /**
+ * Set the selected font.
+ * @param font the selected font
+ *
+ * @see #getSelectedFont
+ * @see java.awt.Font
+ **/
+ public void setSelectedFont(Font font)
+ {
+ setSelectedFontFamily(font.getFamily());
+ setSelectedFontStyle(font.getStyle());
+ setSelectedFontSize(font.getSize());
+ }
+
+ public String getVersionString()
+ {
+ return ("Version");
+ }
+
+ /**
+ * Show font selection dialog.
+ * @param parent Dialog's Parent component.
+ * @return OK_OPTION, CANCEL_OPTION or ERROR_OPTION
+ *
+ * @see #OK_OPTION
+ * @see #CANCEL_OPTION
+ * @see #ERROR_OPTION
+ **/
+ public int showDialog(Component parent)
+ {
+ dialogResultValue = ERROR_OPTION;
+ JDialog dialog = createDialog(parent);
+ dialog.addWindowListener(new WindowAdapter()
+ {
+ public void windowClosing(WindowEvent e)
+ {
+ dialogResultValue = CANCEL_OPTION;
+ }
+ });
+
+ dialog.setVisible(true);
+ dialog.dispose();
+ dialog = null;
+
+ return dialogResultValue;
+ }
+
+ protected class ListSelectionHandler implements ListSelectionListener
+ {
+ private JTextComponent textComponent;
+
+ ListSelectionHandler(JTextComponent textComponent)
+ {
+ this.textComponent = textComponent;
+ }
+
+ public void valueChanged(ListSelectionEvent e)
+ {
+ if (e.getValueIsAdjusting() == false)
+ {
+ JList list = (JList) e.getSource();
+ String selectedValue = (String) list.getSelectedValue();
+
+ String oldValue = textComponent.getText();
+ textComponent.setText(selectedValue);
+ if (!oldValue.equalsIgnoreCase(selectedValue))
+ {
+ textComponent.selectAll();
+ textComponent.requestFocus();
+ }
+
+ updateSampleFont();
+ }
+ }
+ }
+
+ protected class TextFieldFocusHandlerForTextSelection extends FocusAdapter
+ {
+ private JTextComponent textComponent;
+
+ public TextFieldFocusHandlerForTextSelection(JTextComponent textComponent)
+ {
+ this.textComponent = textComponent;
+ }
+
+ public void focusGained(FocusEvent e)
+ {
+ textComponent.selectAll();
+ }
+
+ public void focusLost(FocusEvent e)
+ {
+ textComponent.select(0, 0);
+ updateSampleFont();
+ }
+ }
+
+ protected class TextFieldKeyHandlerForListSelectionUpDown extends KeyAdapter
+ {
+ private JList targetList;
+
+ public TextFieldKeyHandlerForListSelectionUpDown(JList list)
+ {
+ this.targetList = list;
+ }
+
+ public void keyPressed(KeyEvent e)
+ {
+ int i = targetList.getSelectedIndex();
+ switch (e.getKeyCode())
+ {
+ case KeyEvent.VK_UP:
+ i = targetList.getSelectedIndex() - 1;
+ if (i < 0)
+ {
+ i = 0;
+ }
+ targetList.setSelectedIndex(i);
+ break;
+ case KeyEvent.VK_DOWN:
+ int listSize = targetList.getModel().getSize();
+ i = targetList.getSelectedIndex() + 1;
+ if (i >= listSize)
+ {
+ i = listSize - 1;
+ }
+ targetList.setSelectedIndex(i);
+ break;
+ default:
+ break;
+ }
+ }
+ }
+
+ protected class ListSearchTextFieldDocumentHandler implements DocumentListener
+ {
+ JList targetList;
+
+ public ListSearchTextFieldDocumentHandler(JList targetList)
+ {
+ this.targetList = targetList;
+ }
+
+ public void insertUpdate(DocumentEvent e)
+ {
+ update(e);
+ }
+
+ public void removeUpdate(DocumentEvent e)
+ {
+ update(e);
+ }
+
+ public void changedUpdate(DocumentEvent e)
+ {
+ update(e);
+ }
+
+ private void update(DocumentEvent event)
+ {
+ String newValue = "";
+ try
+ {
+ Document doc = event.getDocument();
+ newValue = doc.getText(0, doc.getLength());
+ }
+ catch (BadLocationException e)
+ {
+ e.printStackTrace();
+ }
+
+ if (newValue.length() > 0)
+ {
+ int index = targetList.getNextMatch(newValue, 0, Position.Bias.Forward);
+ if (index < 0)
+ {
+ index = 0;
+ }
+ targetList.ensureIndexIsVisible(index);
+
+ String matchedName = targetList.getModel().getElementAt(index).toString();
+ if (newValue.equalsIgnoreCase(matchedName))
+ {
+ if (index != targetList.getSelectedIndex())
+ {
+ SwingUtilities.invokeLater(new ListSelector(index));
+ }
+ }
+ }
+ }
+
+ public class ListSelector implements Runnable
+ {
+ private int index;
+
+ public ListSelector(int index)
+ {
+ this.index = index;
+ }
+
+ public void run()
+ {
+ targetList.setSelectedIndex(this.index);
+ }
+ }
+ }
+
+ protected class DialogOKAction extends AbstractAction
+ {
+ protected static final String ACTION_NAME = "OK";
+ private JDialog dialog;
+
+ protected DialogOKAction(JDialog dialog)
+ {
+ this.dialog = dialog;
+ putValue(Action.DEFAULT, ACTION_NAME);
+ putValue(Action.ACTION_COMMAND_KEY, ACTION_NAME);
+ putValue(Action.NAME, (ACTION_NAME));
+ }
+
+ public void actionPerformed(ActionEvent e)
+ {
+ dialogResultValue = OK_OPTION;
+ dialog.setVisible(false);
+ }
+ }
+
+ protected class DialogCancelAction extends AbstractAction
+ {
+ protected static final String ACTION_NAME = "Cancel";
+ private JDialog dialog;
+
+ protected DialogCancelAction(JDialog dialog)
+ {
+ this.dialog = dialog;
+ putValue(Action.DEFAULT, ACTION_NAME);
+ putValue(Action.ACTION_COMMAND_KEY, ACTION_NAME);
+ putValue(Action.NAME, (ACTION_NAME));
+ }
+
+ public void actionPerformed(ActionEvent e)
+ {
+ dialogResultValue = CANCEL_OPTION;
+ dialog.setVisible(false);
+ }
+ }
+
+ protected JDialog createDialog(Component parent)
+ {
+ Frame frame = parent instanceof Frame ? (Frame) parent
+ : (Frame) SwingUtilities.getAncestorOfClass(Frame.class, parent);
+ JDialog dialog = new JDialog(frame, ("Select Font"), true);
+
+ Action okAction = new DialogOKAction(dialog);
+ Action cancelAction = new DialogCancelAction(dialog);
+
+ JButton okButton = new JButton(okAction);
+ okButton.setFont(DEFAULT_FONT);
+ JButton cancelButton = new JButton(cancelAction);
+ cancelButton.setFont(DEFAULT_FONT);
+
+ JPanel buttonsPanel = new JPanel();
+ buttonsPanel.setLayout(new GridLayout(2, 1));
+ buttonsPanel.add(okButton);
+ buttonsPanel.add(cancelButton);
+ buttonsPanel.setBorder(BorderFactory.createEmptyBorder(25, 0, 10, 10));
+
+ ActionMap actionMap = buttonsPanel.getActionMap();
+ actionMap.put(cancelAction.getValue(Action.DEFAULT), cancelAction);
+ actionMap.put(okAction.getValue(Action.DEFAULT), okAction);
+ InputMap inputMap = buttonsPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
+ inputMap.put(KeyStroke.getKeyStroke("ESCAPE"), cancelAction.getValue(Action.DEFAULT));
+ inputMap.put(KeyStroke.getKeyStroke("ENTER"), okAction.getValue(Action.DEFAULT));
+
+ JPanel dialogEastPanel = new JPanel();
+ dialogEastPanel.setLayout(new BorderLayout());
+ dialogEastPanel.add(buttonsPanel, BorderLayout.NORTH);
+
+ dialog.getContentPane().add(this, BorderLayout.CENTER);
+ dialog.getContentPane().add(dialogEastPanel, BorderLayout.EAST);
+ dialog.pack();
+ dialog.setLocationRelativeTo(frame);
+ return dialog;
+ }
+
+ protected void updateSampleFont()
+ {
+ Font font = getSelectedFont();
+ getSampleTextField().setFont(font);
+ }
+
+ protected JPanel getFontFamilyPanel()
+ {
+ if (fontNamePanel == null)
+ {
+ fontNamePanel = new JPanel();
+ fontNamePanel.setLayout(new BorderLayout());
+ fontNamePanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
+ fontNamePanel.setPreferredSize(new Dimension(180, 130));
+
+ JScrollPane scrollPane = new JScrollPane(getFontFamilyList());
+ scrollPane.getVerticalScrollBar().setFocusable(false);
+ scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
+
+ JPanel p = new JPanel();
+ p.setLayout(new BorderLayout());
+ p.add(getFontFamilyTextField(), BorderLayout.NORTH);
+ p.add(scrollPane, BorderLayout.CENTER);
+
+ JLabel label = new JLabel(("Font Name"));
+ label.setHorizontalAlignment(JLabel.LEFT);
+ label.setHorizontalTextPosition(JLabel.LEFT);
+ label.setLabelFor(getFontFamilyTextField());
+ label.setDisplayedMnemonic('F');
+
+ fontNamePanel.add(label, BorderLayout.NORTH);
+ fontNamePanel.add(p, BorderLayout.CENTER);
+
+ }
+ return fontNamePanel;
+ }
+
+ protected JPanel getFontStylePanel()
+ {
+ if (fontStylePanel == null)
+ {
+ fontStylePanel = new JPanel();
+ fontStylePanel.setLayout(new BorderLayout());
+ fontStylePanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
+ fontStylePanel.setPreferredSize(new Dimension(140, 130));
+
+ JScrollPane scrollPane = new JScrollPane(getFontStyleList());
+ scrollPane.getVerticalScrollBar().setFocusable(false);
+ scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
+
+ JPanel p = new JPanel();
+ p.setLayout(new BorderLayout());
+ p.add(getFontStyleTextField(), BorderLayout.NORTH);
+ p.add(scrollPane, BorderLayout.CENTER);
+
+ JLabel label = new JLabel(("Font Style"));
+ label.setHorizontalAlignment(JLabel.LEFT);
+ label.setHorizontalTextPosition(JLabel.LEFT);
+ label.setLabelFor(getFontStyleTextField());
+ label.setDisplayedMnemonic('Y');
+
+ fontStylePanel.add(label, BorderLayout.NORTH);
+ fontStylePanel.add(p, BorderLayout.CENTER);
+ }
+ return fontStylePanel;
+ }
+
+ protected JPanel getFontSizePanel()
+ {
+ if (fontSizePanel == null)
+ {
+ fontSizePanel = new JPanel();
+ fontSizePanel.setLayout(new BorderLayout());
+ fontSizePanel.setPreferredSize(new Dimension(70, 130));
+ fontSizePanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
+
+ JScrollPane scrollPane = new JScrollPane(getFontSizeList());
+ scrollPane.getVerticalScrollBar().setFocusable(false);
+ scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
+
+ JPanel p = new JPanel();
+ p.setLayout(new BorderLayout());
+ p.add(getFontSizeTextField(), BorderLayout.NORTH);
+ p.add(scrollPane, BorderLayout.CENTER);
+
+ JLabel label = new JLabel(("Font Size"));
+ label.setHorizontalAlignment(JLabel.LEFT);
+ label.setHorizontalTextPosition(JLabel.LEFT);
+ label.setLabelFor(getFontSizeTextField());
+ label.setDisplayedMnemonic('S');
+
+ fontSizePanel.add(label, BorderLayout.NORTH);
+ fontSizePanel.add(p, BorderLayout.CENTER);
+ }
+ return fontSizePanel;
+ }
+
+ protected JPanel getSamplePanel()
+ {
+ if (samplePanel == null)
+ {
+ Border titledBorder = BorderFactory.createTitledBorder(
+ BorderFactory.createEtchedBorder(), ("Sample"));
+ Border empty = BorderFactory.createEmptyBorder(5, 10, 10, 10);
+ Border border = BorderFactory.createCompoundBorder(titledBorder, empty);
+
+ samplePanel = new JPanel();
+ samplePanel.setLayout(new BorderLayout());
+ samplePanel.setBorder(border);
+
+ samplePanel.add(getSampleTextField(), BorderLayout.CENTER);
+ }
+ return samplePanel;
+ }
+
+ protected JTextField getSampleTextField()
+ {
+ if (sampleText == null)
+ {
+ Border lowered = BorderFactory.createLoweredBevelBorder();
+
+ sampleText = new JTextField(("AaBbYyZz"));
+ sampleText.setBorder(lowered);
+ sampleText.setPreferredSize(new Dimension(300, 100));
+ }
+ return sampleText;
+ }
+
+ protected String[] getFontFamilies()
+ {
+ if (fontFamilyNames == null)
+ {
+ GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
+ fontFamilyNames = env.getAvailableFontFamilyNames();
+ }
+ return fontFamilyNames;
+ }
+
+ protected String[] getFontStyleNames()
+ {
+ if (fontStyleNames == null)
+ {
+ int i = 0;
+ fontStyleNames = new String[4];
+ fontStyleNames[i++] = ("Plain");
+ fontStyleNames[i++] = ("Bold");
+ fontStyleNames[i++] = ("Italic");
+ fontStyleNames[i++] = ("BoldItalic");
+ }
+ return fontStyleNames;
+ }
}
\ No newline at end of file