View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
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   * @author <a href="mailto:miha.kadunc@cosylab.com">Miha Kadunc</a>
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          /* (non-Javadoc)
62           * @see javax.swing.DefaultListCellRenderer#getListCellRendererComponent(javax.swing.JList, java.lang.Object, int, boolean, boolean)
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       * @param items
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     /* (non-Javadoc)
122      * @see com.cosylab.gui.property.PropertyEditor#getPropertyValue()
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     /* (non-Javadoc)
136      * @see com.cosylab.gui.property.PropertyEditor#setPropertyValue(java.lang.Object)
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     /* (non-Javadoc)
150      * @see com.cosylab.gui.property.PropertyEditor#getDescription()
151      */
152     public String getDescription() {
153         return null;
154     }
155 
156     /* (non-Javadoc)
157      * @see com.cosylab.gui.property.PropertyEditor#setDescription(java.lang.String)
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 }