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.Container;
24 import java.beans.PropertyChangeEvent;
25 import java.beans.PropertyChangeListener;
26 import java.beans.PropertyEditor;
27 import java.beans.PropertyEditorSupport;
28
29 import com.cosylab.gui.components.util.RunnerHelper;
30
31
32
33
34
35
36
37
38
39 public class ValueIconPropertyEditor extends PropertyEditorSupport implements PropertyEditor {
40
41 private IconCustomizer editor;
42
43
44
45
46 public ValueIconPropertyEditor() {
47 super();
48 }
49
50
51
52
53
54 public Component getCustomEditor() {
55 if (editor == null) {
56 editor = new IconCustomizer();
57 editor.setIcons((ValueIconPair[]) getValue());
58 editor.addPropertyChangeListener(IconDisplayer.ICONS_PROPERTY,new PropertyChangeListener() {
59 public void propertyChange(PropertyChangeEvent evt) {
60 setValueSilently(editor.getIcons());
61 }
62 });
63 editor.setValueIconMode(true);
64 editor.setDefaultIconMode(false);
65 editor.setSize(400,400);
66 }
67
68 return editor;
69 }
70
71
72
73
74
75 public String getJavaInitializationString() {
76 StringBuffer str = new StringBuffer("new com.cosylab.gui.ValueIconPair[]{");
77 if (getValue() == null) return str.toString()+"}";
78 ValueIconPair[] pairs = (ValueIconPair[]) getValue();
79 String name;
80 int index;
81 for (ValueIconPair pair : pairs) {
82 name = pair.getIconName();
83 name = name.replace('\\','/');
84 index = name.indexOf("!/");
85 if (index > 0) {
86 name = name.substring(index+2);
87 }
88 str.append("new com.cosylab.gui.ValueIconPair(new Long(" +pair.getValue() + "),\"" + name+ "\"),");
89 }
90 str.append("}");
91
92 return str.substring(0);
93 }
94
95
96
97
98
99 public boolean supportsCustomEditor() {
100 return true;
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130 public String getAsText() {
131 ValueIconPair[] pairs = (ValueIconPair[]) getValue();
132 if (pairs == null || pairs.length == 0) return "";
133 StringBuffer buffer = new StringBuffer();
134 for (ValueIconPair cp : pairs) {
135 buffer.append(cp.toString() + ";");
136 }
137 return buffer.toString();
138 }
139
140 private void setValueSilently(Object value) {
141 super.setValue(value);
142 }
143
144
145
146
147 public void setValue(Object value) {
148 super.setValue(value);
149 if (editor!=null) {
150 editor.setIcons((ValueIconPair[]) value);
151 }
152 }
153
154 public static void main(String[] args) {
155 ValueIconPropertyEditor ed = new ValueIconPropertyEditor();
156 ed.addPropertyChangeListener(new PropertyChangeListener() {
157 public void propertyChange(PropertyChangeEvent evt) {
158 System.out.println(evt);
159
160 }
161 });
162 RunnerHelper.runComponent((Container) ed.getCustomEditor(), 500,500);
163 }
164 }
165
166
167