-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathFontControl.java
More file actions
118 lines (98 loc) · 3.66 KB
/
FontControl.java
File metadata and controls
118 lines (98 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package nodebox.client.parameter;
import nodebox.client.Theme;
import nodebox.node.Parameter;
import javax.swing.*;
import javax.swing.event.ListDataListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* Provides a control for fonts.
* <p/>
* Note that we use the java.awt.Font object as the model object. The displayed name is fontName.
*/
public class FontControl extends AbstractParameterControl implements ActionListener {
private JComboBox fontChooser;
private FontDataModel fontModel;
private String value;
public FontControl(Parameter parameter) {
super(parameter);
setLayout(new FlowLayout(FlowLayout.LEADING, 0, 0));
fontChooser = new JComboBox();
fontModel = new FontDataModel();
FontCellRenderer fontCellRenderer = new FontCellRenderer();
fontChooser.setModel(fontModel);
fontChooser.setRenderer(fontCellRenderer);
fontChooser.putClientProperty("JComponent.sizeVariant", "small");
fontChooser.setPreferredSize(new Dimension(150, 22));
fontChooser.putClientProperty("JComboBox.isPopDown", Boolean.TRUE);
fontChooser.addActionListener(this);
fontChooser.setFont(Theme.SMALL_BOLD_FONT);
add(fontChooser);
setValueForControl(parameter.getValue());
}
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
fontChooser.setEnabled(enabled);
}
public void setValueForControl(Object v) {
String fontName = (String) v;
if (value != null && value.equals(fontName)) return;
fontChooser.setSelectedItem(fontModel.getFont(fontName));
fontChooser.repaint();
value = fontName;
}
public void actionPerformed(ActionEvent e) {
Font font = (Font) fontChooser.getSelectedItem();
if (font == null) return;
String fontName = font.getFontName();
if (!fontName.equals(parameter.asString())) {
setParameterValue(font.getFontName());
}
}
private class FontDataModel implements ComboBoxModel {
private Font[] fonts;
private Font selectedFont;
public FontDataModel() {
fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
}
public Font getFont(String fontName) {
for (Font f : fonts) {
if (f.getFontName().equals(fontName))
return f;
}
return null;
}
public void setSelectedItem(Object anItem) {
selectedFont = (Font) anItem;
}
public Object getSelectedItem() {
return selectedFont;
}
public int getSize() {
return fonts.length;
}
public Object getElementAt(int index) {
return fonts[index];
}
public void addListDataListener(ListDataListener l) {
// Listeners are not supported.
}
public void removeListDataListener(ListDataListener l) {
// Listeners are not supported.
}
}
private class FontCellRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (label == null) return null;
Font f = (Font) value;
if (f == null) return label;
label.setText(f.getFontName());
label.setFont(Theme.SMALL_BOLD_FONT);
return label;
}
}
}