1 package org.simantics.modeling.ui.actions.style;
3 import java.awt.BorderLayout;
5 import java.awt.Component;
6 import java.awt.Dimension;
8 import java.awt.GraphicsEnvironment;
9 import java.awt.event.ActionEvent;
10 import java.awt.event.ActionListener;
12 import javax.swing.BorderFactory;
13 import javax.swing.JCheckBox;
14 import javax.swing.JComboBox;
15 import javax.swing.JLabel;
16 import javax.swing.JList;
17 import javax.swing.JPanel;
18 import javax.swing.JScrollPane;
19 import javax.swing.ListCellRenderer;
20 import javax.swing.ListSelectionModel;
21 import javax.swing.SwingConstants;
22 import javax.swing.event.ListSelectionEvent;
23 import javax.swing.event.ListSelectionListener;
25 @SuppressWarnings({"rawtypes", "unchecked"})
26 public class FontChooser extends JPanel {
28 private static final long serialVersionUID = -53650261362110193L;
30 private static Font DEFAULT_FONT = new Font("Arial", Font.PLAIN, 16);
32 private String sampleText;
35 private JList fontList;
36 private JComboBox sizeComboBox;
37 private JCheckBox boldCheckBox;
38 private JCheckBox italicCheckBox;
40 private String fonts[];
42 private Font originalFont;
45 public FontChooser(String sampleText) {
48 this.sampleText = sampleText;
49 JPanel textPanel = new JPanel();
50 text = new JLabel(sampleText, SwingConstants.CENTER);
51 text.setVerticalAlignment(SwingConstants.CENTER);
52 this.setLayout(new BorderLayout());
53 textPanel.setBorder(BorderFactory.createLineBorder(Color.darkGray));
55 textPanel.setMinimumSize(new Dimension(100, 100));
56 textPanel.setPreferredSize(new Dimension(200, 100));
57 this.add(textPanel,BorderLayout.NORTH);
59 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
60 String[] ff = ge.getAvailableFontFamilyNames();
61 fonts = new String[ff.length + 1];
62 fonts[0] = "-- keep current font --";
63 System.arraycopy(ff, 0, fonts, 1, ff.length);
65 fontList = new JList(fonts);
66 fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
67 fontList.setLayoutOrientation(JList.VERTICAL);
68 fontList.setVisibleRowCount(-1);
70 fontList.addListSelectionListener(new ListSelectionListener() {
73 public void valueChanged(ListSelectionEvent e) {
74 if (!e.getValueIsAdjusting()) {
75 int index = fontList.getSelectedIndex();
83 fontList.setCellRenderer(new FontListRenderer());
86 JScrollPane listScrollPane = new JScrollPane(fontList);
87 listScrollPane.setPreferredSize(new Dimension(400, 200));
88 this.add(listScrollPane, BorderLayout.CENTER);
90 JPanel controlPanel = new JPanel();
91 this.add(controlPanel,BorderLayout.SOUTH);
93 ActionListener listener = new ActionListener() {
95 public void actionPerformed(ActionEvent e) {
96 selectFont(fontList.getSelectedIndex());
101 Integer[] sizes = {4, 6, 7, 8, 9, 10, 11, 12, 14, 18, 20, 22, 24, 36, 40, 48, 56, 64, 72, 144};
102 sizeComboBox = new JComboBox(sizes);
103 sizeComboBox.addActionListener(listener);
104 sizeComboBox.setSelectedIndex(7);
105 controlPanel.add(new JLabel("Size: "));
106 controlPanel.add(sizeComboBox);
108 boldCheckBox = new JCheckBox("Bold");
109 boldCheckBox.addActionListener(listener);
110 controlPanel.add(boldCheckBox);
112 italicCheckBox = new JCheckBox("Italic");
113 italicCheckBox.addActionListener(listener);
114 controlPanel.add(italicCheckBox);
116 fontList.setSelectedIndex(0);
119 private void selectFont(int index) {
122 String name = fonts[index];
123 Integer size = (Integer)sizeComboBox.getSelectedItem();
124 int style = Font.PLAIN; // plain == 0
125 if (boldCheckBox.isSelected())
126 style = style | Font.BOLD;
127 if (italicCheckBox.isSelected())
128 style = style | Font.ITALIC;
129 Font original = originalFont != null ? originalFont : DEFAULT_FONT;
130 // Index 0 is reserved for keeping the original font (family)
131 Font f = index == 0 ? original.deriveFont(style, size) : new Font(name, style, size);
132 font = !f.equals(original) ? f : null;
133 text.setText(sampleText);
137 public Font getFont() {
141 public void setCurrentFont(Font font) {
143 for (int i = 0; i < fonts.length; i++) {
144 if (fonts[i].equals(font.getFamily())) {
151 fontList.setSelectedIndex(index);
152 fontList.scrollRectToVisible(fontList.getCellBounds(index, index));
153 boldCheckBox.setSelected((font.getStyle() & Font.BOLD) > 0);
154 italicCheckBox.setSelected((font.getStyle() & Font.ITALIC) > 0);
155 sizeComboBox.setSelectedItem(font.getSize());
156 this.originalFont = font;
160 public class FontListRenderer extends JLabel implements ListCellRenderer {
162 private static final long serialVersionUID = 1237633327794383545L;
164 public FontListRenderer() {
166 setHorizontalAlignment(LEFT);
167 setVerticalAlignment(CENTER);
171 public Component getListCellRendererComponent(JList list, Object value,
172 int index, boolean isSelected, boolean cellHasFocus) {
174 setBackground(list.getSelectionBackground());
175 setForeground(list.getSelectionForeground());
177 setBackground(list.getBackground());
178 setForeground(list.getForeground());
181 String text = (String) value;
184 // Not really that helpful when one can't even read the name of
185 // some fonts with the sample text. Also keeps the list visually cleaner.
187 //setFont(index > 0 ? new Font(text,Font.PLAIN,16) : DEFAULT_FONT);