1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.property.editors;
21
22 import java.awt.Color;
23 import java.awt.Component;
24 import java.awt.Font;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import javax.swing.DefaultComboBoxModel;
29 import javax.swing.DefaultListCellRenderer;
30 import javax.swing.Icon;
31 import javax.swing.JComboBox;
32 import javax.swing.JComponent;
33 import javax.swing.JList;
34
35 import com.cosylab.gui.components.ledder.CustomLedIcon;
36
37
38
39
40 public class EnumEditor extends JComboBox implements PropertyEditor {
41 private static Object NULL_VALUE=new Object() {
42 public String toString() {
43 return "";
44 };
45 };
46 private static String NULL_NAME=" ";
47 private class EnumRenderer extends DefaultListCellRenderer {
48 private Map icons;
49
50 private Icon getIcon(Color c) {
51 if (icons==null) {
52 icons=new HashMap();
53 }
54 if (!icons.containsKey(c)) {
55 CustomLedIcon ci=new CustomLedIcon(c,18,2);
56 icons.put(c,ci);
57 }
58 return (Icon)icons.get(c);
59 }
60
61
62
63
64 public Component getListCellRendererComponent(JList list, Object value,
65 int index, boolean isSelected, boolean cellHasFocus) {
66 String strVal=(String)names.get(value);
67 if (strVal==null) {
68 if (value!=null) strVal=String.valueOf(value);
69 else strVal=" ";
70 }
71 JComponent comp=(JComponent)super.getListCellRendererComponent(list, strVal, index, isSelected, cellHasFocus);
72 if (colors.containsKey(value) && colors.get(value)!=null) {
73 setIcon(getIcon((Color)colors.get(value)));
74 } else {
75 setIcon(null);
76 }
77 return comp;
78 }
79
80 }
81 private Map names=new HashMap();
82 private Map colors=new HashMap();
83 private boolean nullAllowed=false;
84
85
86
87 public EnumEditor(Object[] items) {
88 this(items, null, null);
89 }
90 public EnumEditor(Object[] items, String[] names, Color[] colors) {
91 super();
92 setItems(items, names, colors);
93 initGUI();
94 }
95
96 public void setItems(Object[] newItems, String[] names, Color[] colors) {
97 Object[] items=new Object[newItems.length];
98 System.arraycopy(newItems, 0, items, 0, newItems.length);
99 this.names.clear();
100 this.colors.clear();
101 for (int i = 0; i < items.length; i++) {
102 if (items[i]==null) items[i]=NULL_VALUE;
103
104 String curName=(names==null)?null:names[i];
105 if ((curName==null || curName.length()<1) && items[i]!=null) curName=items[i].toString();
106 if (curName==null || curName.length()<1) curName=NULL_NAME;
107 this.names.put(items[i],curName);
108
109 Color curCol=colors==null?null:colors[i];
110 this.colors.put(items[i],curCol);
111 }
112 setModel(new DefaultComboBoxModel(items));
113 }
114
115 private void initGUI() {
116 setFont(getFont().deriveFont(Font.PLAIN));
117 setMaximumRowCount(20);
118 setRenderer(new EnumRenderer());
119 }
120
121
122
123
124 public Object getPropertyValue() {
125 Object ret=getSelectedItem();
126 if (ret==NULL_VALUE) {
127 return null;
128 }
129 if ("".equals(ret) && nullAllowed) {
130 return null;
131 }
132 return ret;
133 }
134
135
136
137
138 public boolean setPropertyValue(Object value) {
139 setSelectedItem(value);
140 if (getSelectedItem()!=value && !isEditable()) {
141 setEditable(true);
142 setSelectedItem(value);
143 setEditable(false);
144 return getSelectedItem()==value;
145 }
146 return false;
147 }
148
149
150
151
152 public String getDescription() {
153 return null;
154 }
155
156
157
158
159 public void setDescription(String description) {
160 }
161
162 public boolean isNullAllowed() {
163 return nullAllowed;
164 }
165 public void setNullAllowed(boolean nullAllowed) {
166 this.nullAllowed = nullAllowed;
167 DefaultComboBoxModel mdl=(DefaultComboBoxModel)getModel();
168 if (mdl.getIndexOf(NULL_VALUE)<0 && nullAllowed) {
169 mdl.insertElementAt(NULL_VALUE,0);
170 names.put(NULL_VALUE, NULL_NAME);
171 }
172 if (!nullAllowed){
173 if (mdl.getIndexOf(NULL_VALUE)>=0) {
174 mdl.removeElement(NULL_VALUE);
175 names.remove(NULL_VALUE);
176 }
177 }
178 }
179 }