Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / swing / jComboBoxWithImageIconItems / JComboBoxWithImageIconItems.java @ 40561

History | View | Annotate | Download (14.2 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program 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 this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.gui.beans.swing.jComboBoxWithImageIconItems;
25

    
26
import java.awt.Component;
27
import java.awt.Dimension;
28
import java.awt.Font;
29
import java.io.Serializable;
30
import java.util.Vector;
31

    
32
import javax.swing.ImageIcon;
33
import javax.swing.JComboBox;
34
import javax.swing.JLabel;
35
import javax.swing.JList;
36
import javax.swing.ListCellRenderer;
37

    
38
import org.gvsig.gui.beans.Messages;
39

    
40
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
41
 *
42
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
43
 *
44
 * This program is free software; you can redistribute it and/or
45
 * modify it under the terms of the GNU General Public License
46
 * as published by the Free Software Foundation; either version 2
47
 * of the License, or (at your option) any later version.
48
 *
49
 * This program is distributed in the hope that it will be useful,
50
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
51
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
52
 * GNU General Public License for more details.
53
 *
54
 * You should have received a copy of the GNU General Public License
55
 * along with this program; if not, write to the Free Software
56
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
57
 *
58
 * For more information, contact:
59
 *
60
 *  Generalitat Valenciana
61
 *   Conselleria d'Infraestructures i Transport
62
 *   Av. Blasco Ib??ez, 50
63
 *   46010 VALENCIA
64
 *   SPAIN
65
 *
66
 *      +34 963862235
67
 *   gvsig@gva.es
68
 *      www.gvsig.gva.es
69
 *
70
 *    or
71
 *
72
 *   IVER T.I. S.A
73
 *   Salamanca 50
74
 *   46005 Valencia
75
 *   Spain
76
 *
77
 *   +34 963163400
78
 *   dac@iver.es
79
 */
80

    
81
/**
82
 * A JComboBox which items are ImageIcons.<br>
83
 * 
84
 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
85
 */
86
public class JComboBoxWithImageIconItems extends JComboBox implements Serializable {
87
        private static final long serialVersionUID = -429261182461238958L;
88

    
89
        public static final int DEFAULT_ROW_COUNT = 3;
90

    
91
        private JComboBoxWithImageIconItems referenceToThisJComboBoxWithImageIconItems;
92

    
93
    /**
94
     * Default constructor
95
     */
96
    public JComboBoxWithImageIconItems() {
97
            super();
98
            initialize();
99
    }
100

    
101
    /**
102
     * Default constructor with a 'ComboBoxImageIconModel' as parameter
103
     * 
104
     * @param aModel org.gvsig.gui.beans.swing.jComboBoxWithImageIconItems.JComboBoxWithImageIconItems.ComboBoxImageIconModel
105
     */
106
    public JComboBoxWithImageIconItems(ComboBoxImageIconModel aModel) {
107
            super();
108
            initialize();
109
    }
110

    
111
    /**
112
     * Default constructor with an array of 'ImageIconItemInfo' as parameters
113
     * 
114
     * @param items Each item must be an 'ImageIconItemInfo' object
115
     */
116
    public JComboBoxWithImageIconItems(Object[] items) {
117
            super();
118
            initialize();
119
    }
120

    
121
    /**
122
     * Default constructor with a Vector of 'ImageIconItemInfo' as parameter
123
     * 
124
     * @param items Each item must be an 'ImageIconItemInfo' object
125
     */
126
    public JComboBoxWithImageIconItems(Vector items) {
127
            super();
128
            initialize();
129
    }
130

    
131
    /**
132
     * Initialize this component
133
     */
134
    private void initialize() {
135
            this.referenceToThisJComboBoxWithImageIconItems = this;
136
            this.setModel(new ComboBoxImageIconModel());
137
                this.setRenderer(new ComboBoxImageIconRenderer());
138
                this.setMaximumRowCount(DEFAULT_ROW_COUNT);
139
    }
140

    
141
    /**
142
     * Adds an image icon URL path, image icon loaded (or null if not), tool tip text (tool tip text could be "") and if it's neccessary (it's optional)
143
     *   another associated value to this item (if there is no associated value, the 'item_Value' param should be null) 
144
     * 
145
     * @param imageIconPath A path
146
     * @param imageIcon An image icon loaded or null if haven't been loaded
147
     * @param imageIconToolTipText Text
148
     * @param item_Value Another extra value associated to the item added (null if no extra value)
149
     */
150
    public void addImageIconItem(String imageIconPath, ImageIcon imageIcon, String imageIconToolTipText, Object item_Value) {
151
            // Load the image and stor it into the model of the JComboBox
152
            this.addItem(new ImageIconItemInfo(imageIconPath, imageIcon, imageIconToolTipText, item_Value));
153
    }
154
    
155
    /**
156
     * Adds an image icon URL path, image icon loaded (or null if not), and tool tip text (tool tip text could be "")
157
     * 
158
     * @param imageIconItem Information for load an image icon and set its tool tip text
159
     */
160
    public void addImageIconItem(ImageIconItemInfo imageIconItem) {
161
            // Load the image and stor it into the model of the JComboBox
162
            this.addItem(imageIconItem);
163
    }
164
    
165
    /**
166
     * Adds an image icon URL path, tool tip text (tool tip text could be "") and the item value
167
     * If no item matches, does nothing
168
     * (The image icon parameter is ignored)
169
     * 
170
     * @param imageIconPath A path
171
     * @param imageIconToolTipText Text
172
     * @param item_Value An extra value
173
     */
174
    public void removeImageIconItem(String imageIconPath, String imageIconToolTipText, String item_Value) {
175
            int i = 0;
176
            ImageIconItemInfo iiii1;
177

    
178
            while (i < this.getItemCount()) {
179
                    iiii1 = (ImageIconItemInfo)this.getItemAt(i);
180
                    if ((iiii1.getPath().compareTo(imageIconPath) == 0) &&
181
                                    (iiii1.getToolTipText().compareTo(imageIconToolTipText) == 0) && 
182
                                    (((iiii1.getItemValue() == null) && (item_Value == null)) || (iiii1.getItemValue().equals(item_Value)))) {
183
                            this.removeItemAt(i);
184
                            return; // Only remove the first match
185
                    }
186
                    else {
187
                            i ++;
188
                    }
189
            }
190
    }
191

    
192
    /**
193
     * Removes an image icon URL path, tool tip text (tool tip text could be "") and the item value
194
     * If no item matches, does nothing
195
     * (The image icon parameter is ignored)
196
     * 
197
     * @param imageIconPath Information for remove the item
198
     */
199
    public void removeImageIconItem(ImageIconItemInfo imageIconItem) {
200
            int i = 0;
201
            ImageIconItemInfo iiii1;
202
            
203
            while (i < this.getItemCount()) {
204
                    iiii1 = (ImageIconItemInfo)this.getItemAt(i);
205
                    if ((iiii1.getPath().compareTo(imageIconItem.getPath()) == 0) &&
206
                                    (iiii1.getToolTipText().compareTo(imageIconItem.getToolTipText()) == 0) && 
207
                                    (((iiii1.getItemValue() == null) && (imageIconItem.getItemValue() == null)) || (iiii1.getItemValue().equals(imageIconItem.getItemValue())))) {
208
                            this.removeItemAt(i);
209
                            return; // Only remove the first match
210
                    }
211
                    else {
212
                            i ++;
213
                    }
214
            }
215
    }
216

    
217
    /**
218
     * Removes all image icons
219
     */
220
    public void removeAllImageIconItems() {
221
            this.removeAllItems();
222
    }
223

    
224
    /**
225
     * Sets some image icons that their information to load is stored in an array of ImageIconItemInfo
226
     * 
227
     * @param imagePaths An array of ImageIconItemInfo with the URL path, image icon loaded (or null if not), and tool tip text to each image
228
     */
229
    public void addImageIconItems(ImageIconItemInfo[] imageIconItems) {            
230
            // Load the images and store them into the model of the JComboBox
231
            for (int i = 0; i < imageIconItems.length; i++) {
232
                    this.addItem(imageIconItems[i]);
233
            }
234
    }
235

    
236
    /**
237
     * Sets some image icons that their information to load is stored in a Vector of ImageIconItemInfo
238
     * 
239
     * @param imagePaths A Vector of ImageIconItemInfo with the URL path, image icon loaded (or null if not), and tool tip text to each image
240
     */    
241
    public void addImageIconItems(Vector imageIconItems) {
242
            // Load the images and store them into the model of the JComboBox
243
            for (int i = 0; i < imageIconItems.size(); i++) {
244
                    this.addItem(imageIconItems.get(i));
245
            }
246
    }
247

    
248
    /**
249
     * Removes some image icons that their path, tool tip text and item value are stored in an array
250
     * If no item matches, does nothing
251
     * (The image icon parameter is ignored)
252
     * 
253
     * @param imageIconPaths An array with the ImageIconItemInfo
254
     */
255
    public void removeImageIconItems(ImageIconItemInfo[] imageIconItems) {
256
               int i, j;
257
               ImageIconItemInfo iiii1, iiii2;
258

    
259
               // For each item to remove
260
               for (i = 0; i < imageIconItems.length; i++) {
261
                       j = 0;
262
                       iiii1 = (ImageIconItemInfo)imageIconItems[i];
263
                       // Finds first item that it's patch matches and removes it
264
                    while (j < this.getItemCount()) {
265
                            iiii2 = (ImageIconItemInfo)this.getItemAt(j);
266
                            if ((iiii1.getPath().compareTo(iiii2.getPath()) == 0) &&
267
                                            (iiii1.getToolTipText().compareTo(iiii2.getToolTipText()) == 0) && 
268
                                            (((iiii1.getItemValue() == null) && (iiii2.getItemValue() == null)) || (iiii1.getItemValue().equals(iiii2.getItemValue())))) {
269
                                    this.removeItemAt(j);
270
                                    break; // Only remove the first match
271
                            }
272
                            else {
273
                                    j ++;
274
                            }
275
                    }
276
               }
277
    }
278

    
279
    /**
280
     * Removes some image icons that their paths, tool tip text and item value are stored in a Vector
281
     * (The image icon parameter is ignored)
282
     * 
283
     * @param imageIconPaths A Vector with the ImageIconItemInfo
284
     */
285
    public void removeImageIconItems(Vector imageIconItems) {
286
               int i, j;
287
               ImageIconItemInfo iiii1, iiii2;
288

    
289
               // For each item to remove
290
               for (i = 0; i < imageIconItems.size(); i++) {
291
                       j = 0;
292
                       iiii1 = (ImageIconItemInfo)imageIconItems.get(i);
293
                       // Finds first item that it's patch matches and removes it
294
                    while (j < this.getItemCount()) {
295
                            iiii2 = (ImageIconItemInfo)this.getItemAt(j);
296
                            if ((iiii1.getPath().compareTo(iiii2.getPath()) == 0) &&
297
                                            (iiii1.getToolTipText().compareTo(iiii2.getToolTipText()) == 0) && 
298
                                            (((iiii1.getItemValue() == null) && (iiii2.getItemValue() == null)) || (iiii1.getItemValue().equals(iiii2.getItemValue())))) {
299
                                    this.removeItemAt(j);
300
                                    break; // Only remove the first match
301
                            }
302
                            else {
303
                                    j ++;
304
                            }
305
                    }
306
               }            
307
    }
308
    
309
        /**
310
         * @see ComboBoxImageIconModel#setShowImageIconAndTextProperty(boolean)
311
         */
312
        public void setShowImageIconAndTextProperty(boolean b) {
313
                ((ComboBoxImageIconModel)this.getModel()).setShowImageIconAndTextProperty(b);
314
        }
315
        
316
        /**
317
         * @see ComboBoxImageIconModel#getShowImageIconAndTextProperty()
318
         */
319
        public boolean getShowImageIconAndTextProperty() {
320
                return ((ComboBoxImageIconModel)this.getModel()).getShowImageIconAndTextProperty();
321
        }
322
    
323
        /**
324
         * @see ComboBoxRenderer#setPreferredSize(Dimension)
325
         */
326
        public void setJComboBoxRendererPreferredSize (Dimension d) {
327
                ((ComboBoxImageIconRenderer)this.getRenderer()).setPreferredSize(d);
328
        }
329
        
330
        /**
331
         * @see ComboBoxRenderer#getPreferredSize()
332
         */
333
        public Dimension getJComboBoxRendererPreferredSize () {
334
                return ((ComboBoxImageIconRenderer)this.getRenderer()).getPreferredSize();
335
        }
336

    
337
    /**
338
     * This code has been extracted and modified from the Java Sun Microsystems's example: 'CustomComboBoxDemo.java'
339
     *   in http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html
340
     * 
341
     * @author Sun Microsystems
342
     * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
343
     */
344
    private class ComboBoxImageIconRenderer extends JLabel implements ListCellRenderer, Serializable {
345
                private static final long serialVersionUID = -7247546606502171953L;
346

    
347
                private Font uhOhFont;
348

    
349
            /**
350
             * Default constructor
351
             */
352
            public ComboBoxImageIconRenderer() {
353
                    setOpaque(true);
354
                    setHorizontalAlignment(CENTER);
355
                    setVerticalAlignment(CENTER);
356
            }
357

    
358
            /**
359
             * This method finds the image and text corresponding
360
             * to the selected value and returns the label, set up
361
             * to display the text and image.
362
             * 
363
             * @see ListCellRenderer#getListCellRendererComponent(javax.swing.JList, java.lang.Object, int, boolean, boolean)
364
             */
365
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
366
                    if (value != null) {
367
                            if (isSelected) {
368
                                    setBackground(list.getSelectionBackground());
369
                                    setForeground(list.getSelectionForeground());
370
                            } else {
371
                                    setBackground(list.getBackground());
372
                                    setForeground(list.getForeground());
373
                            }
374
                            
375
                            ImageIcon icon = ((ImageIconItemInfo)value).getImageIcon();
376
                            String item = ((ImageIconItemInfo)value).getPath();
377
                            String toolTipText = ((ImageIconItemInfo)value).getToolTipText();
378
                            
379
                            setIcon(icon);
380
                            
381
                            if (toolTipText != null) {
382
                                    setToolTipText(toolTipText);
383
                                    referenceToThisJComboBoxWithImageIconItems.setToolTipText(toolTipText);
384
                            }
385
        
386
                            if (getShowImageIconAndTextProperty()) {
387
                                    if (icon != null) {
388
                                            setText(item);
389
                                            setFont(list.getFont());
390
                                    } else {
391
                                            setUhOhText(item + " (" + Messages.getText("no_image_available") + ")", list.getFont());
392
                                    }
393
                            }
394
                            else {
395
                                     /* In this way we force to show anything in the Popup list 
396
                                                 in order to user knew that no item has been loaded */
397
                                    setText(" ");
398
                            }
399
                    }
400
                    else {
401
                            // Empty previous information
402
                            setIcon(null);
403
                            setToolTipText("");
404
                            referenceToThisJComboBoxWithImageIconItems.setToolTipText("");
405
                            setText("");
406
                    }
407
                    
408
                    return this;
409
            }
410

    
411
            /**
412
             * Set the font and text when no image was found.
413
             * 
414
             * @param uhOhText Text
415
             * @param normalFont Font type
416
             */
417
            private void setUhOhText(String uhOhText, Font normalFont) {
418
                    if (uhOhFont == null) { //lazily create this font
419
                            uhOhFont = normalFont.deriveFont(Font.ITALIC);
420
                    }
421

    
422
                    setFont(uhOhFont);
423
                    setText(uhOhText);
424
            }
425
    }
426
}