1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui;
21
22 import java.awt.Component;
23 import java.awt.Dimension;
24 import java.awt.Graphics;
25 import java.awt.GridBagConstraints;
26 import java.awt.GridBagLayout;
27 import java.awt.Insets;
28 import java.awt.event.KeyAdapter;
29 import java.awt.event.KeyEvent;
30
31 import javax.swing.Icon;
32 import javax.swing.ImageIcon;
33 import javax.swing.JLabel;
34 import javax.swing.JPanel;
35 import javax.swing.JTextField;
36 import javax.swing.SwingConstants;
37
38 import com.cosylab.gui.components.util.IconHelper;
39 import com.cosylab.gui.components.util.PaintHelper;
40
41
42
43
44
45
46
47
48
49 public class IconEditor extends JPanel {
50
51 private static final long serialVersionUID = 7625485410292542663L;
52
53 public static final String ICON = "icon";
54
55 private JLabel iconLabel;
56 private JLabel titleLabel;
57 private JTextField pathField;
58 private String title;
59 private Icon selectedIcon;
60 private static final int HEIGHT = 32;
61 private static final int WIDTH = 32;
62 private static final Icon NULL_ICON = new Icon() {
63 public int getIconHeight() {
64 return HEIGHT;
65 }
66 public int getIconWidth() {
67 return WIDTH;
68 }
69 public void paintIcon(Component c, Graphics g, int x, int y) {
70 PaintHelper.paintDisabled(g, x, y, WIDTH, HEIGHT);
71 }
72 };
73
74
75
76
77
78 public IconEditor() {
79 this("Icon");
80 }
81
82
83
84
85
86
87 public IconEditor(String title) {
88 super();
89 this.title = title;
90 initialize();
91 }
92
93 private void initialize() {
94 setLayout(new GridBagLayout());
95 titleLabel = new JLabel(title);
96 this.add(titleLabel, new GridBagConstraints(0,0,2,1,1,0,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(11,11,1,11),0,0));
97 this.add(getPathField(), new GridBagConstraints(0,1,1,1,1,0,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(1,11,11,0),0,0));
98 this.add(getIconLabel(), new GridBagConstraints(1,1,1,1,0,0,GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(1,4,11,11),0,0));
99 }
100
101 private JTextField getPathField() {
102 if (pathField == null) {
103 pathField = new JTextField();
104 pathField.setMaximumSize(new Dimension(100,21));
105 pathField.setPreferredSize(new Dimension(100,21));
106 pathField.setMinimumSize(new Dimension(100,21));
107 pathField.addKeyListener(new KeyAdapter() {
108 @Override
109 public void keyReleased(KeyEvent e) {
110 internalSetIcon(IconHelper.createIcon(pathField.getText()), false);
111 }
112 });
113 pathField.setToolTipText("Type image file path from classpath.");
114 }
115
116 return pathField;
117 }
118
119 private JLabel getIconLabel() {
120 if (iconLabel == null) {
121 iconLabel = new JLabel();
122 iconLabel.setHorizontalAlignment(SwingConstants.RIGHT);
123 iconLabel.setMinimumSize(new Dimension(WIDTH + 5, HEIGHT + 5));
124 iconLabel.setMaximumSize(new Dimension(WIDTH + 5, HEIGHT + 5));
125 iconLabel.setPreferredSize(new Dimension(WIDTH + 5, HEIGHT + 5));
126 iconLabel.setIcon(NULL_ICON);
127 }
128 return iconLabel;
129 }
130
131
132
133
134
135
136 public void setIcon(Icon icon) {
137 internalSetIcon(icon, true);
138 }
139
140
141
142
143
144
145 public void setIcon(String path) {
146 String old = getIconPath();
147 if ((old != null && old.equals(path)) || (old == null && path == null)) return;
148 internalSetIcon(IconHelper.createIcon(path), true);
149 getPathField().setText(path);
150 }
151
152 private void internalSetIcon(Icon icon, boolean setPathField) {
153 Icon oldValue = getIconLabel().getIcon();
154 if ((oldValue != null && oldValue.equals(icon)) ||
155 (oldValue == null && icon == null)) return;
156 selectedIcon = icon;
157 if (icon instanceof ImageIcon && icon.getIconHeight() > HEIGHT && icon.getIconWidth() > WIDTH) {
158 getIconLabel().setIcon(IconCustomizer.getScaledIcon((ImageIcon)icon, HEIGHT,WIDTH));
159 } else if (icon != null){
160 getIconLabel().setIcon(icon);
161 } else {
162 getIconLabel().setIcon(NULL_ICON);
163 }
164
165 if (icon != null && setPathField)
166 getPathField().setText(icon.toString());
167
168 firePropertyChange(ICON, oldValue, icon);
169 }
170
171
172
173
174
175 public Icon getIcon() {
176 return selectedIcon;
177 }
178
179
180
181
182
183
184 public String getTitle() {
185 return titleLabel.getText();
186 }
187
188
189
190
191
192
193 public String getIconPath() {
194 return getPathField().getText();
195 }
196
197
198
199
200
201
202 public void setTitle(String title) {
203 this.title = title;
204 titleLabel.setText(title);
205 }
206 }