Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / simplecombobox / SimpleComboBox.java @ 40561

History | View | Annotate | Download (4.7 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.simplecombobox;
25

    
26
import javax.swing.JComboBox;
27

    
28

    
29

    
30
/**
31
 * <p>Simple ComboBox component to add items with an associated code.
32
 * Typical usage would be adding Strings items and associated action
33
 * codes. Example</p>
34
 * <pre>SimpleComboBox combo = new SimpleComboBox();
35
 * combo.addItem("Left", TextComponent.ALIGN_LEFT);
36
 * combo.addItem("Right", TextComponent.ALIGN_RIGHT;
37
 * combo.addItem("Center", TextComponent.ALIGN_CENTER);
38
 * combo.addItem("Justify", TextComponent.ALIGN_JUSTIFY);
39
 * . . .
40
 * int textAlign = combo.getSelectedCode();
41
 * textComponent.align(textAlign);
42
 * </pre>
43
 * <p>This component provides a basic selection autocompletion when the user
44
 * presses a key (while the combo has the focus).</p>
45
 * 
46
 * @author Cesar Martinez Izquierdo <cesar.martinez@iver.es>
47
 *
48
 */
49
public class SimpleComboBox extends JComboBox {
50
        private static final long serialVersionUID = 5805274299238455334L;
51

    
52
        public SimpleComboBox() {
53
        super();
54
    }
55
    
56
        /**
57
         * <p>Invalid method, do not use it. Use
58
         * {@link #addItem(Object, int)} instead.</p>
59
         */
60
        public void addItem(Object anObject) {
61
                throw new RuntimeException("Invalid method: use addItem(Object anObject, int code) instead or regular Swing JComboBox");
62
        }
63

    
64
    public void addItem(Object anObject, int code) {
65
            ComboItem item = new ComboItem(anObject, code);
66
            super.addItem(item);
67
    }
68

    
69
    public Object getItemAt(int index) {
70
            ComboItem item = (ComboItem) super.getItemAt(index);
71
            if (item!=null) {
72
                    return item.getObject();
73
            }
74
            return null;
75
    }
76

    
77
    public Object getSelectedItem() {
78
            ComboItem item = (ComboItem) super.getSelectedItem();
79
            if (item!=null) {
80
                    return item.getObject();
81
            }
82
            return null;
83
    }
84

    
85
    public void setSelectedIndex(int anIndex) {
86
        int size = dataModel.getSize();
87
        if ( anIndex == -1 ) {
88
            setSelectedItem( null );
89
        } else if ( anIndex < -1 || anIndex >= size ) {
90
            throw new IllegalArgumentException("setSelectedIndex: " + anIndex + " out of bounds");
91
        } else {
92
                Object item = dataModel.getElementAt(anIndex);
93
            super.setSelectedItem(item);
94
        }
95
    }
96

    
97
    public void setSelectedItem(Object anObject) {
98
            ComboItem item;
99
            for (int i=0; i<getItemCount(); i++) {
100
                    item = (ComboItem) super.getItemAt(i);
101
                    if (item.getObject().equals(anObject)) {
102
                            super.setSelectedItem(item);                            
103
                    }
104
            }
105
    }
106

    
107
    public void setSelectedCode(int code) {
108
            ComboItem item;
109
            for (int i=0; i<getItemCount(); i++) {
110
                    item = (ComboItem) super.getItemAt(i);
111
                    if (item.getCode()==code) {
112
                            super.setSelectedItem(item);                            
113
                    }
114
            }
115
    }
116

    
117
    public Object[] getSelectedObjects() {
118
            Object obj = getSelectedItem();
119
            if (obj!=null) {
120
                    return new Object[]{obj};
121
            }
122
            else {
123
                    return new Object[0];
124
            }
125
    }
126

    
127
    /**
128
     * Gets the code associated to the selected item
129
     * @return
130
     */
131
    public int getSelectedCode() {
132
            ComboItem item = (ComboItem) super.getSelectedItem();
133
            if (item!=null) {
134
                    return item.getCode();
135
            }
136
            return -1;
137
    }
138

    
139
    private class ComboItem {
140
            private Object theObject;
141
            private int code;
142

    
143
            public ComboItem(Object object, int code) {
144
                    this.theObject = object;
145
                    this.code = code;
146
            }
147
 
148
                public void setObject(Object theObject) {
149
                        this.theObject = theObject;
150
                }
151
                public Object getObject() {
152
                        return theObject;
153
                }
154
                public void setCode(int code) {
155
                        this.code = code;
156
                }
157
                public int getCode() {
158
                        return code;
159
                }
160

    
161
                public String toString() {
162
                        return theObject.toString();
163
                }
164
    }
165
}