summaryrefslogtreecommitdiffstats
path: root/pki/base/console/src/com/netscape/admin/certsrv/CustomComboBoxModel.java
blob: c97beeb5bdeaf7ea4df69eb101af2c5958873b9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.admin.certsrv;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

/**
 * Custom Combo Box Model
 * Let you specify an icon and title to be displayed.
 *
 * @author  jpanchen
 * @version $Revision$, $Date$
 * @see     com.netscape.admin.certsrv
 * @see     CustomComboBox
 */
class CustomComboBoxModel extends AbstractListModel implements ComboBoxModel {
    
    /*==========================================================
     * constructors
     *==========================================================*/    
    public CustomComboBoxModel() {
        _cache = new Vector();
        _index = new Vector();
    }

	/*==========================================================
	 * public methods
     *==========================================================*/
     
    /**
     * set selected item
     * DO NOT USE!!!
     * use JComboBox.setSelectedIndex()
     */
    public void setSelectedItem(Object anObject) {
        _currentValue = anObject;
        fireContentsChanged(this,-1,-1);
    }

    /**
     * Get selected Item.
     * DO NOT USE !!!
     * use JComboBox.getItemAt(JComboBox.getSelectedIndex())
     */
    public Object getSelectedItem() {
        return _currentValue;
    }

    /**
     * Return size
     * @return size
     */
    public int getSize() {
        return _cache.size();
    }

    /**
     * Retrieve element at index position
     * @param index location
     * @Object Hashtable obejct with "icon" and "title" field
     */
    public Object getElementAt(int index) {
        try {
            return _cache.elementAt(index);
        } catch(ArrayIndexOutOfBoundsException e) {
            return null;
        }
    }
    
    /**
     * set default icon
     * @param icon new icon to be used
     */
    public void setIcon(ImageIcon icon) {
        _icon = icon;
    }

    /**
     * Add new list entry into model
     * @param icon new icon associated
     * @param title text associated
     */
    public void addItem(ImageIcon icon, String title, Object data) {
        Hashtable newItem = new Hashtable();
        newItem.put(SELECTION_ICON,icon);
        newItem.put(SELECTION_TITLE, title);
        newItem.put(SELECTION_DATA, data);
        _cache.addElement(newItem);
        _index.addElement(title.toUpperCase());
    }    
    
    /**
     * Add new list entry into model
     * @param icon new icon associated
     * @param title text associated
     */
    public void addItem(ImageIcon icon, String title) {
        Hashtable newItem = new Hashtable();
        newItem.put(SELECTION_ICON,icon);
        newItem.put(SELECTION_TITLE, title);
        _cache.addElement(newItem);
        _index.addElement(title.toUpperCase());
    }
    
    /**
     * Add new list entry into model.
     * Default icon used
     * @param title text associated
     */
    public void addItem(String title) {
        Hashtable newItem = new Hashtable();
        newItem.put(SELECTION_ICON,_icon);
        newItem.put(SELECTION_TITLE, title);
        _cache.addElement(newItem);
        _index.addElement(title.toUpperCase());
    }
    
    /**
     * Remove all entries from the model
     */
    public void removeAll() {
        _cache.removeAllElements();
    }
    
    /**
     * Remove specific entry from the model
     * @param key key string associated with the entry
     */
    public void removeEntry(String key) {
        int x = _index.indexOf(key.toUpperCase());
        if ((x != -1) && (x < _cache.size()) ) {
            _cache.removeElementAt(x);
            _index.removeElementAt(x);
        }
    }
    
    /*==========================================================
     * variables
     *==========================================================*/
     
    public static final String SELECTION_TITLE = "title";
    public static final String SELECTION_ICON = "icon";
    public static final String SELECTION_DATA = "data";
    
    private Object _currentValue;
    private Vector _cache;
    private Vector _index;
    private ImageIcon _icon;
}