]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/style/FontChooser.java
Migrated source code from Simantics SVN
[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;\r
2 \r
3 import java.awt.BorderLayout;\r
4 import java.awt.Color;\r
5 import java.awt.Component;\r
6 import java.awt.Dimension;\r
7 import java.awt.Font;\r
8 import java.awt.GraphicsEnvironment;\r
9 import java.awt.event.ActionEvent;\r
10 import java.awt.event.ActionListener;\r
11 \r
12 import javax.swing.BorderFactory;\r
13 import javax.swing.JCheckBox;\r
14 import javax.swing.JComboBox;\r
15 import javax.swing.JLabel;\r
16 import javax.swing.JList;\r
17 import javax.swing.JPanel;\r
18 import javax.swing.JScrollPane;\r
19 import javax.swing.ListCellRenderer;\r
20 import javax.swing.ListSelectionModel;\r
21 import javax.swing.SwingConstants;\r
22 import javax.swing.event.ListSelectionEvent;\r
23 import javax.swing.event.ListSelectionListener;\r
24 \r
25 @SuppressWarnings({"rawtypes", "unchecked"})\r
26 public class FontChooser extends JPanel {\r
27 \r
28     private static final long serialVersionUID = -53650261362110193L;\r
29 \r
30     private static Font DEFAULT_FONT = new Font("Arial", Font.PLAIN, 16);\r
31 \r
32     private String sampleText;\r
33     private JLabel text;\r
34 \r
35     private JList fontList;\r
36     private JComboBox sizeComboBox;\r
37     private JCheckBox boldCheckBox;\r
38     private JCheckBox italicCheckBox;\r
39 \r
40     private String fonts[];\r
41 \r
42     private Font originalFont;\r
43     private Font font;\r
44 \r
45     public FontChooser(String sampleText) {\r
46         super();\r
47 \r
48         this.sampleText = sampleText;\r
49         JPanel textPanel = new JPanel();\r
50         text = new JLabel(sampleText, SwingConstants.CENTER);\r
51         text.setVerticalAlignment(SwingConstants.CENTER);\r
52         this.setLayout(new BorderLayout());\r
53         textPanel.setBorder(BorderFactory.createLineBorder(Color.darkGray));\r
54         textPanel.add(text);\r
55         textPanel.setMinimumSize(new Dimension(100, 100));\r
56         textPanel.setPreferredSize(new Dimension(200, 100));\r
57         this.add(textPanel,BorderLayout.NORTH);\r
58 \r
59         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();\r
60         String[] ff = ge.getAvailableFontFamilyNames();\r
61         fonts = new String[ff.length + 1];\r
62         fonts[0] = "-- keep current font --";\r
63         System.arraycopy(ff, 0, fonts, 1, ff.length);\r
64 \r
65         fontList = new JList(fonts);\r
66         fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);\r
67         fontList.setLayoutOrientation(JList.VERTICAL);\r
68         fontList.setVisibleRowCount(-1);\r
69 \r
70         fontList.addListSelectionListener(new ListSelectionListener() {\r
71 \r
72             @Override\r
73             public void valueChanged(ListSelectionEvent e) {\r
74                 if (!e.getValueIsAdjusting()) {\r
75                     int index = fontList.getSelectedIndex();\r
76                     if (index != -1) {\r
77                         selectFont(index);\r
78                     }\r
79                 }\r
80             }\r
81         });\r
82 \r
83         fontList.setCellRenderer(new FontListRenderer());\r
84 \r
85 \r
86         JScrollPane listScrollPane = new JScrollPane(fontList);\r
87         listScrollPane.setPreferredSize(new Dimension(400, 200));\r
88         this.add(listScrollPane, BorderLayout.CENTER);\r
89 \r
90         JPanel controlPanel = new JPanel();\r
91         this.add(controlPanel,BorderLayout.SOUTH);\r
92 \r
93         ActionListener listener = new ActionListener() {\r
94             @Override\r
95             public void actionPerformed(ActionEvent e) {\r
96                 selectFont(fontList.getSelectedIndex());\r
97             }\r
98         };\r
99 \r
100         // FIXME: hack\r
101         Integer[] sizes = {4, 6, 7, 8, 9, 10, 11, 12, 14, 18, 20, 22, 24, 36, 40, 48, 56, 64, 72, 144};\r
102         sizeComboBox = new JComboBox(sizes);\r
103         sizeComboBox.addActionListener(listener);\r
104         sizeComboBox.setSelectedIndex(7);\r
105         controlPanel.add(new JLabel("Size: "));\r
106         controlPanel.add(sizeComboBox);\r
107 \r
108         boldCheckBox = new JCheckBox("Bold");\r
109         boldCheckBox.addActionListener(listener);\r
110         controlPanel.add(boldCheckBox);\r
111 \r
112         italicCheckBox = new JCheckBox("Italic");\r
113         italicCheckBox.addActionListener(listener);\r
114         controlPanel.add(italicCheckBox);\r
115 \r
116         fontList.setSelectedIndex(0);\r
117     }\r
118 \r
119     private void selectFont(int index) {\r
120         if (index < 0)\r
121             return;\r
122         String name = fonts[index];\r
123         Integer size = (Integer)sizeComboBox.getSelectedItem();\r
124         int style = Font.PLAIN; // plain == 0\r
125         if (boldCheckBox.isSelected())\r
126             style = style | Font.BOLD;\r
127         if (italicCheckBox.isSelected())\r
128             style = style | Font.ITALIC;\r
129         Font original = originalFont != null ? originalFont : DEFAULT_FONT;\r
130         // Index 0 is reserved for keeping the original font (family)\r
131         Font f = index == 0 ? original.deriveFont(style, size) : new Font(name, style, size);\r
132         font = !f.equals(original) ? f : null;\r
133         text.setText(sampleText);\r
134         text.setFont(f);\r
135     }\r
136 \r
137     public Font getFont() {\r
138         return font;\r
139     }\r
140 \r
141     public void setCurrentFont(Font font) {\r
142         int index = -1;\r
143         for (int i = 0; i < fonts.length; i++) {\r
144             if (fonts[i].equals(font.getFamily())) {\r
145                 index = i;\r
146                 break;\r
147             }\r
148         }\r
149         if (index == -1)\r
150             return;\r
151         fontList.setSelectedIndex(index);\r
152         fontList.scrollRectToVisible(fontList.getCellBounds(index, index));\r
153         boldCheckBox.setSelected((font.getStyle() & Font.BOLD) > 0);\r
154         italicCheckBox.setSelected((font.getStyle() & Font.ITALIC) > 0);\r
155         sizeComboBox.setSelectedItem(font.getSize());\r
156         this.originalFont = font;\r
157         selectFont(index);\r
158     }\r
159 \r
160     public class FontListRenderer extends JLabel implements ListCellRenderer {\r
161 \r
162         private static final long serialVersionUID = 1237633327794383545L;\r
163 \r
164         public FontListRenderer() {\r
165             setOpaque(true);\r
166             setHorizontalAlignment(LEFT);\r
167             setVerticalAlignment(CENTER);\r
168         };\r
169 \r
170         @Override\r
171         public Component getListCellRendererComponent(JList list, Object value,\r
172                 int index, boolean isSelected, boolean cellHasFocus) {\r
173             if (isSelected) {\r
174                 setBackground(list.getSelectionBackground());\r
175                 setForeground(list.getSelectionForeground());\r
176             } else {\r
177                 setBackground(list.getBackground());\r
178                 setForeground(list.getForeground());\r
179             }\r
180 \r
181             String text = (String) value;\r
182             setText(text);\r
183 \r
184             // Not really that helpful when one can't even read the name of\r
185             // some fonts with the sample text. Also keeps the list visually cleaner.\r
186             // Thus removed.\r
187             //setFont(index > 0 ? new Font(text,Font.PLAIN,16) : DEFAULT_FONT);\r
188 \r
189             return this;\r
190         }\r
191     }\r
192 }\r