Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libUI / src / org / gvsig / gui / beans / swing / jComboBoxWithImageIconItems / JComboBoxWithImageIconItems.java @ 10261

History | View | Annotate | Download (15.4 KB)

1
package org.gvsig.gui.beans.swing.jComboBoxWithImageIconItems;
2

    
3
import java.awt.Component;
4
import java.awt.Dimension;
5
import java.awt.Font;
6
import java.io.Serializable;
7
import java.util.Vector;
8

    
9
import javax.swing.DefaultComboBoxModel;
10
import javax.swing.ImageIcon;
11
import javax.swing.JComboBox;
12
import javax.swing.JLabel;
13
import javax.swing.JList;
14
import javax.swing.ListCellRenderer;
15

    
16
import org.gvsig.gui.beans.Messages;
17

    
18
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
19
 *
20
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
21
 *
22
 * This program is free software; you can redistribute it and/or
23
 * modify it under the terms of the GNU General Public License
24
 * as published by the Free Software Foundation; either version 2
25
 * of the License, or (at your option) any later version.
26
 *
27
 * This program is distributed in the hope that it will be useful,
28
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30
 * GNU General Public License for more details.
31
 *
32
 * You should have received a copy of the GNU General Public License
33
 * along with this program; if not, write to the Free Software
34
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
35
 *
36
 * For more information, contact:
37
 *
38
 *  Generalitat Valenciana
39
 *   Conselleria d'Infraestructures i Transport
40
 *   Av. Blasco Ib??ez, 50
41
 *   46010 VALENCIA
42
 *   SPAIN
43
 *
44
 *      +34 963862235
45
 *   gvsig@gva.es
46
 *      www.gvsig.gva.es
47
 *
48
 *    or
49
 *
50
 *   IVER T.I. S.A
51
 *   Salamanca 50
52
 *   46005 Valencia
53
 *   Spain
54
 *
55
 *   +34 963163400
56
 *   dac@iver.es
57
 */
58

    
59
/**
60
 * A JComboBox which items are ImageIcons.<br>
61
 * 
62
 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
63
 */
64
public class JComboBoxWithImageIconItems extends JComboBox implements Serializable {
65
        private static final long serialVersionUID = -2423483052971097560L;
66

    
67
        public static final int DEFAULT_ROW_COUNT = 3;
68
        
69
        private JComboBoxWithImageIconItems referenceToThisJComboBoxWithImageIconItems;
70

    
71
    /**
72
     * Default constructor
73
     */
74
    public JComboBoxWithImageIconItems() {
75
            initialize();
76
    }
77

    
78
    /**
79
     * Initialize this component
80
     */
81
    private void initialize() {
82
            this.referenceToThisJComboBoxWithImageIconItems = this;
83
            this.setModel(new ComboBoxImageIconModel());
84
                this.setRenderer(new ComboBoxImageIconRenderer());
85
                this.setMaximumRowCount(DEFAULT_ROW_COUNT);
86
    }
87

    
88
    /**
89
     * Set the maximum number of rows showed when the inner JComboBox is opened
90
     * 
91
     * @param rowCount Number of rows
92
     */
93
    public void setMaximumJComboBoxRowCount(int rowCount) {
94
            this.setMaximumRowCount(rowCount);
95
    }
96
    
97
    /**
98
     * Get the maximum number of rows showed when the inner JComboBox is opened
99
     * 
100
     * @return Number of rows
101
     */
102
    public int getMaximumJComboBoxRowCount() {
103
            return this.getMaximumRowCount();
104
    }
105
    
106
    /**
107
     * 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)
108
     *   another associated value to this item (if there is no associated value, the 'item_Value' param should be null) 
109
     * 
110
     * @param imageIconPath A path
111
     * @param imageIcon An image icon loaded or null if haven't been loaded
112
     * @param imageIconToolTipText Text
113
     * @param item_Value Another extra value associated to the item added (null if no extra value)
114
     */
115
    public void addImageIconItem(String imageIconPath, ImageIcon imageIcon, String imageIconToolTipText, Object item_Value) {
116
            // Load the image and stor it into the model of the JComboBox
117
            this.addItem(new ImageIconItemInfo(imageIconPath, imageIcon, imageIconToolTipText, item_Value));
118
    }
119
    
120
    /**
121
     * Adds an image icon URL path, image icon loaded (or null if not), and tool tip text (tool tip text could be "")
122
     * 
123
     * @param imageIconItem Information for load an image icon and set its tool tip text
124
     */
125
    public void addImageIconItem(ImageIconItemInfo imageIconItem) {
126
            // Load the image and stor it into the model of the JComboBox
127
            this.addItem(imageIconItem);
128
    }
129
    
130
    /**
131
     * Adds an image icon URL path, tool tip text (tool tip text could be "") and the item value
132
     * If no item matches, does nothing
133
     * (The image icon parameter is ignored)
134
     * 
135
     * @param imageIconPath A path
136
     * @param imageIconToolTipText Text
137
     * @param item_Value An extra value
138
     */
139
    public void removeImageIconItem(String imageIconPath, String imageIconToolTipText, String item_Value) {
140
            int i = 0;
141
            ImageIconItemInfo iiii1;
142

    
143
            while (i < this.getItemCount()) {
144
                    iiii1 = (ImageIconItemInfo)this.getItemAt(i);
145
                    if ((iiii1.getPath().compareTo(imageIconPath) == 0) &&
146
                                    (iiii1.getToolTipText().compareTo(imageIconToolTipText) == 0) && 
147
                                    (((iiii1.getItemValue() == null) && (item_Value == null)) || (iiii1.getItemValue().equals(item_Value)))) {
148
                            this.removeItemAt(i);
149
                            return; // Only remove the first match
150
                    }
151
                    else {
152
                            i ++;
153
                    }
154
            }
155
    }
156

    
157
    /**
158
     * Removes an image icon URL path, tool tip text (tool tip text could be "") and the item value
159
     * If no item matches, does nothing
160
     * (The image icon parameter is ignored)
161
     * 
162
     * @param imageIconPath Information for remove the item
163
     */
164
    public void removeImageIconItem(ImageIconItemInfo imageIconItem) {
165
            int i = 0;
166
            ImageIconItemInfo iiii1;
167
            
168
            while (i < this.getItemCount()) {
169
                    iiii1 = (ImageIconItemInfo)this.getItemAt(i);
170
                    if ((iiii1.getPath().compareTo(imageIconItem.getPath()) == 0) &&
171
                                    (iiii1.getToolTipText().compareTo(imageIconItem.getToolTipText()) == 0) && 
172
                                    (((iiii1.getItemValue() == null) && (imageIconItem.getItemValue() == null)) || (iiii1.getItemValue().equals(imageIconItem.getItemValue())))) {
173
                            this.removeItemAt(i);
174
                            return; // Only remove the first match
175
                    }
176
                    else {
177
                            i ++;
178
                    }
179
            }
180
    }
181

    
182
    /**
183
     * Removes all image icons
184
     */
185
    public void removeAllImageIconItems() {
186
            this.removeAllItems();
187
    }
188

    
189
    /**
190
     * Sets some image icons that their information to load is stored in an array of ImageIconItemInfo
191
     * 
192
     * @param imagePaths An array of ImageIconItemInfo with the URL path, image icon loaded (or null if not), and tool tip text to each image
193
     */
194
    public void addImageIconItems(ImageIconItemInfo[] imageIconItems) {            
195
            // Load the images and store them into the model of the JComboBox
196
            for (int i = 0; i < imageIconItems.length; i++) {
197
                    this.addItem(imageIconItems[i]);
198
            }
199
    }
200

    
201
    /**
202
     * Sets some image icons that their information to load is stored in a Vector of ImageIconItemInfo
203
     * 
204
     * @param imagePaths A Vector of ImageIconItemInfo with the URL path, image icon loaded (or null if not), and tool tip text to each image
205
     */    
206
    public void addImageIconItems(Vector imageIconItems) {
207
            // Load the images and store them into the model of the JComboBox
208
            for (int i = 0; i < imageIconItems.size(); i++) {
209
                    this.addItem(imageIconItems.get(i));
210
            }
211
    }
212

    
213
    /**
214
     * Removes some image icons that their path, tool tip text and item value are stored in an array
215
     * If no item matches, does nothing
216
     * (The image icon parameter is ignored)
217
     * 
218
     * @param imageIconPaths An array with the ImageIconItemInfo
219
     */
220
    public void removeImageIconItems(ImageIconItemInfo[] imageIconItems) {
221
               int i, j;
222
               ImageIconItemInfo iiii1, iiii2;
223

    
224
               // For each item to remove
225
               for (i = 0; i < imageIconItems.length; i++) {
226
                       j = 0;
227
                       iiii1 = (ImageIconItemInfo)imageIconItems[i];
228
                       // Finds first item that it's patch matches and removes it
229
                    while (j < this.getItemCount()) {
230
                            iiii2 = (ImageIconItemInfo)this.getItemAt(j);
231
                            if ((iiii1.getPath().compareTo(iiii2.getPath()) == 0) &&
232
                                            (iiii1.getToolTipText().compareTo(iiii2.getToolTipText()) == 0) && 
233
                                            (((iiii1.getItemValue() == null) && (iiii2.getItemValue() == null)) || (iiii1.getItemValue().equals(iiii2.getItemValue())))) {
234
                                    this.removeItemAt(j);
235
                                    break; // Only remove the first match
236
                            }
237
                            else {
238
                                    j ++;
239
                            }
240
                    }
241
               }
242
    }
243

    
244
    /**
245
     * Removes some image icons that their paths, tool tip text and item value are stored in a Vector
246
     * (The image icon parameter is ignored)
247
     * 
248
     * @param imageIconPaths A Vector with the ImageIconItemInfo
249
     */
250
    public void removeImageIconItems(Vector imageIconItems) {
251
               int i, j;
252
               ImageIconItemInfo iiii1, iiii2;
253

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

    
274
    /*
275
     * (non-Javadoc)
276
     * @see javax.swing.JComponent#setPreferredSize(java.awt.Dimension)
277
     */
278
    public void setPreferredSize(Dimension preferredSize) {
279
            super.setPreferredSize(preferredSize);
280
            this.setPreferredSize(preferredSize);
281
    }
282

    
283
    /*
284
     * (non-Javadoc)
285
     * @see java.awt.Component#setSize(java.awt.Dimension)
286
     */
287
    public void setSize(Dimension d) {
288
            super.setSize(d);
289
            this.setSize(d);            
290
    }
291

    
292
    /*
293
     * (non-Javadoc)
294
     * @see java.awt.Component#setSize(int, int)
295
     */
296
    public void setSize(int width, int height) {
297
            super.setSize(width, height);
298
            this.setSize(width, height);
299
    }
300
    
301
        /**
302
         * @see ComboBoxImageIconModel#setShowImageIconAndTextProperty(boolean)
303
         */
304
        public void setShowImageIconAndTextProperty(boolean b) {
305
                ((ComboBoxImageIconModel)this.getModel()).setShowImageIconAndTextProperty(b);
306
        }
307
        
308
        /**
309
         * @see ComboBoxImageIconModel#getShowImageIconAndTextProperty()
310
         */
311
        public boolean getShowImageIconAndTextProperty() {
312
                return ((ComboBoxImageIconModel)this.getModel()).getShowImageIconAndTextProperty();
313
        }
314
    
315
        /**
316
         * @see ComboBoxRenderer#setPreferredSize(Dimension)
317
         */
318
        public void setJComboBoxRendererPreferredSize (Dimension d) {
319
                ((ComboBoxImageIconRenderer)this.getRenderer()).setPreferredSize(d);
320
        }
321
        
322
        /**
323
         * @see ComboBoxRenderer#getPreferredSize()
324
         */
325
        public Dimension getJComboBoxRendererPreferredSize () {
326
                return ((ComboBoxImageIconRenderer)this.getRenderer()).getPreferredSize();
327
        }
328
        
329
        /**
330
         * @see JComboBox#setMaximumRowCount(int)
331
         */
332
        public void setJComboBoxMaximumRowCount(int count) {
333
                this.setMaximumRowCount(count);
334
        }
335

    
336
        /**
337
         * @see JComboBox#getMaximumRowCount()
338
         */
339
        public int getJComboBoxMaximumRowCount() {
340
                return this.getMaximumRowCount();
341
        }
342

    
343
        /**
344
         * Model for 'JPanelWithJComboBoxImageIconItems', stores the attribute 'showImageIconAndText'
345
         * 
346
         * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
347
         */
348
    private class ComboBoxImageIconModel extends DefaultComboBoxModel implements Serializable {
349
                private static final long serialVersionUID = -775510419199115805L;
350

    
351
                public static final boolean DEFAULT_SHOW_IMAGE_ICON_AND_TEXT_PROPERTY = false;
352
            
353
            private boolean showImageIconAndText;
354

    
355
            /**
356
                 * @see DefaultComboBoxModel#DefaultComboBoxModel()
357
                 */
358
                public ComboBoxImageIconModel() {
359
                        super();
360
                        initialize();
361
                }
362

    
363
                /**
364
                 * @see DefaultComboBoxModel#DefaultComboBoxModel(Object[])
365
                 */
366
                public ComboBoxImageIconModel(Object[] items) {
367
                        super(items);
368
                        initialize();
369
                }
370

    
371
                /**
372
                 * @see DefaultComboBoxModel#DefaultComboBoxModel(Vector)
373
                 */
374
                public ComboBoxImageIconModel(Vector v) {
375
                        super(v);
376
                        initialize();
377
                }
378

    
379
                /**
380
                 * Initializes the new attribute of this model
381
                 */
382
                private void initialize() {
383
                        showImageIconAndText = DEFAULT_SHOW_IMAGE_ICON_AND_TEXT_PROPERTY;
384
                }
385

    
386
            /**
387
             * Sets the value of the inner attribute 'imageIconAndText'
388
             * 
389
             * @param b A boolean value
390
             */
391
            public void setShowImageIconAndTextProperty(boolean b) {
392
                    showImageIconAndText = b;
393
            }
394

    
395
            /**
396
             * Returns the value of the inner attribute 'imageIconAndText'
397
             * 
398
             * @return A boolean value
399
             */
400
            public boolean getShowImageIconAndTextProperty() {
401
                    return showImageIconAndText;
402
            }
403
    }
404

    
405
    /**
406
     * This code has been extracted and modified from the Java Sun Microsystems's example: 'CustomComboBoxDemo.java'
407
     *   in http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html
408
     * 
409
     * @author Sun Microsystems
410
     * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
411
     */
412
    private class ComboBoxImageIconRenderer extends JLabel implements ListCellRenderer, Serializable {
413
                private static final long serialVersionUID = -7247546606502171953L;
414

    
415
                private Font uhOhFont;
416

    
417
            /**
418
             * Default constructor
419
             */
420
            public ComboBoxImageIconRenderer() {
421
                    setOpaque(true);
422
                    setHorizontalAlignment(CENTER);
423
                    setVerticalAlignment(CENTER);
424
            }
425

    
426
            /**
427
             * This method finds the image and text corresponding
428
             * to the selected value and returns the label, set up
429
             * to display the text and image.
430
             * 
431
             * @see ListCellRenderer#getListCellRendererComponent(javax.swing.JList, java.lang.Object, int, boolean, boolean)
432
             */
433
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
434
                    if (value != null) {
435
                            if (isSelected) {
436
                                    setBackground(list.getSelectionBackground());
437
                                    setForeground(list.getSelectionForeground());
438
                            } else {
439
                                    setBackground(list.getBackground());
440
                                    setForeground(list.getForeground());
441
                            }
442
                            
443
                            ImageIcon icon = ((ImageIconItemInfo)value).getImageIcon();
444
                            String item = ((ImageIconItemInfo)value).getPath();
445
                            String toolTipText = ((ImageIconItemInfo)value).getToolTipText();
446
                            
447
                            setIcon(icon);
448
                            
449
                            if (toolTipText != null) {
450
                                    setToolTipText(toolTipText);
451
                                    referenceToThisJComboBoxWithImageIconItems.setToolTipText(toolTipText);
452
                            }
453
        
454
                            if (getShowImageIconAndTextProperty()) {
455
                                    if (icon != null) {
456
                                            setText(item);
457
                                            setFont(list.getFont());
458
                                    } else {
459
                                            setUhOhText(item + " (" + Messages.getText("no_image_available") + ")", list.getFont());
460
                                    }
461
                            }
462
                            else {
463
                                     /* In this way we force to show anything in the Popup list 
464
                                                 in order to user knew that no item has been loaded */
465
                                    setText(" ");
466
                            }
467
                    }
468
                    else {
469
                            // Empty previous information
470
                            setIcon(null);
471
                            setToolTipText("");
472
                            referenceToThisJComboBoxWithImageIconItems.setToolTipText("");
473
                            setText("");
474
                    }
475
                    
476
                    return this;
477
            }
478

    
479
            /**
480
             * Set the font and text when no image was found.
481
             * 
482
             * @param uhOhText Text
483
             * @param normalFont Font type
484
             */
485
            private void setUhOhText(String uhOhText, Font normalFont) {
486
                    if (uhOhFont == null) { //lazily create this font
487
                            uhOhFont = normalFont.deriveFont(Font.ITALIC);
488
                    }
489

    
490
                    setFont(uhOhFont);
491
                    setText(uhOhText);
492
            }
493
    }
494
}