]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/style/FontChooser.java
85524e2ed9d0299b2e57a652b4c2d467d2d650e6
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / actions / style / FontChooser.java
1 package org.simantics.modeling.ui.actions.style;
2
3 import java.awt.BorderLayout;
4 import java.awt.Color;
5 import java.awt.Component;
6 import java.awt.Dimension;
7 import java.awt.Font;
8 import java.awt.GraphicsEnvironment;
9 import java.awt.event.ActionEvent;
10 import java.awt.event.ActionListener;
11
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;
24
25 @SuppressWarnings({"rawtypes", "unchecked"})
26 public class FontChooser extends JPanel {
27
28     private static final long serialVersionUID = -53650261362110193L;
29
30     private static Font DEFAULT_FONT = new Font("Arial", Font.PLAIN, 16);
31
32     private String sampleText;
33     private JLabel text;
34
35     private JList fontList;
36     private JComboBox sizeComboBox;
37     private JCheckBox boldCheckBox;
38     private JCheckBox italicCheckBox;
39
40     private String fonts[];
41
42     private Font originalFont;
43     private Font font;
44
45     public FontChooser(String sampleText) {
46         super();
47
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));
54         textPanel.add(text);
55         textPanel.setMinimumSize(new Dimension(100, 100));
56         textPanel.setPreferredSize(new Dimension(200, 100));
57         this.add(textPanel,BorderLayout.NORTH);
58
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);
64
65         fontList = new JList(fonts);
66         fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
67         fontList.setLayoutOrientation(JList.VERTICAL);
68         fontList.setVisibleRowCount(-1);
69
70         fontList.addListSelectionListener(new ListSelectionListener() {
71
72             @Override
73             public void valueChanged(ListSelectionEvent e) {
74                 if (!e.getValueIsAdjusting()) {
75                     int index = fontList.getSelectedIndex();
76                     if (index != -1) {
77                         selectFont(index);
78                     }
79                 }
80             }
81         });
82
83         fontList.setCellRenderer(new FontListRenderer());
84
85
86         JScrollPane listScrollPane = new JScrollPane(fontList);
87         listScrollPane.setPreferredSize(new Dimension(400, 200));
88         this.add(listScrollPane, BorderLayout.CENTER);
89
90         JPanel controlPanel = new JPanel();
91         this.add(controlPanel,BorderLayout.SOUTH);
92
93         ActionListener listener = new ActionListener() {
94             @Override
95             public void actionPerformed(ActionEvent e) {
96                 selectFont(fontList.getSelectedIndex());
97             }
98         };
99
100         // FIXME: hack
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);
107
108         boldCheckBox = new JCheckBox("Bold");
109         boldCheckBox.addActionListener(listener);
110         controlPanel.add(boldCheckBox);
111
112         italicCheckBox = new JCheckBox("Italic");
113         italicCheckBox.addActionListener(listener);
114         controlPanel.add(italicCheckBox);
115
116         fontList.setSelectedIndex(0);
117     }
118
119     private void selectFont(int index) {
120         if (index < 0)
121             return;
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);
134         text.setFont(f);
135     }
136
137     public Font getFont() {
138         return font;
139     }
140
141     public void setCurrentFont(Font font) {
142         int index = -1;
143         for (int i = 0; i < fonts.length; i++) {
144             if (fonts[i].equals(font.getFamily())) {
145                 index = i;
146                 break;
147             }
148         }
149         if (index == -1)
150             return;
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;
157         selectFont(index);
158     }
159
160     public class FontListRenderer extends JLabel implements ListCellRenderer {
161
162         private static final long serialVersionUID = 1237633327794383545L;
163
164         public FontListRenderer() {
165             setOpaque(true);
166             setHorizontalAlignment(LEFT);
167             setVerticalAlignment(CENTER);
168         };
169
170         @Override
171         public Component getListCellRendererComponent(JList list, Object value,
172                 int index, boolean isSelected, boolean cellHasFocus) {
173             if (isSelected) {
174                 setBackground(list.getSelectionBackground());
175                 setForeground(list.getSelectionForeground());
176             } else {
177                 setBackground(list.getBackground());
178                 setForeground(list.getForeground());
179             }
180
181             String text = (String) value;
182             setText(text);
183
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.
186             // Thus removed.
187             //setFont(index > 0 ? new Font(text,Font.PLAIN,16) : DEFAULT_FONT);
188
189             return this;
190         }
191     }
192 }