1 | /**
|
---|
2 | *#########################################################################
|
---|
3 | *
|
---|
4 | * A component of the Gatherer application, part of the Greenstone digital
|
---|
5 | * library suite from the New Zealand Digital Library Project at the
|
---|
6 | * University of Waikato, New Zealand.
|
---|
7 | *
|
---|
8 | * <BR><BR>
|
---|
9 | *
|
---|
10 | * Author: John Thompson, Greenstone Digital Library, University of Waikato
|
---|
11 | *
|
---|
12 | * <BR><BR>
|
---|
13 | *
|
---|
14 | * Copyright (C) 1999 New Zealand Digital Library Project
|
---|
15 | *
|
---|
16 | * <BR><BR>
|
---|
17 | *
|
---|
18 | * This program is free software; you can redistribute it and/or modify
|
---|
19 | * it under the terms of the GNU General Public License as published by
|
---|
20 | * the Free Software Foundation; either version 2 of the License, or
|
---|
21 | * (at your option) any later version.
|
---|
22 | *
|
---|
23 | * <BR><BR>
|
---|
24 | *
|
---|
25 | * This program is distributed in the hope that it will be useful,
|
---|
26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
28 | * GNU General Public License for more details.
|
---|
29 | *
|
---|
30 | * <BR><BR>
|
---|
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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
35 | *########################################################################
|
---|
36 | */
|
---|
37 |
|
---|
38 |
|
---|
39 |
|
---|
40 |
|
---|
41 |
|
---|
42 |
|
---|
43 | package org.greenstone.gatherer.gui.combobox;
|
---|
44 |
|
---|
45 | import java.util.Collections;
|
---|
46 | import java.util.Hashtable;
|
---|
47 | import java.util.Vector;
|
---|
48 |
|
---|
49 | import javax.swing.AbstractListModel;
|
---|
50 | import javax.swing.MutableComboBoxModel;
|
---|
51 | import org.greenstone.gatherer.msm.ElementWrapper;
|
---|
52 | import org.w3c.dom.Element;
|
---|
53 |
|
---|
54 | public class GComboBoxModel
|
---|
55 | extends AbstractListModel
|
---|
56 | implements MutableComboBoxModel {
|
---|
57 |
|
---|
58 | // Data.
|
---|
59 | private boolean isMetadataElement = false;
|
---|
60 |
|
---|
61 | private Hashtable name_to_element = new Hashtable();
|
---|
62 |
|
---|
63 | private Object selected_item = null;
|
---|
64 |
|
---|
65 | private Vector contents = new Vector();
|
---|
66 |
|
---|
67 | public GComboBoxModel() {
|
---|
68 | }
|
---|
69 |
|
---|
70 | public GComboBoxModel(Vector contents) {
|
---|
71 | this.contents = contents;
|
---|
72 | Collections.sort(contents);
|
---|
73 | }
|
---|
74 |
|
---|
75 | public GComboBoxModel(Vector elements, boolean isMetadataElement) {
|
---|
76 | this.isMetadataElement = isMetadataElement;
|
---|
77 | for(int i = 0; i < elements.size(); i++) {
|
---|
78 | ElementWrapper element = (ElementWrapper)elements.get(i);
|
---|
79 | contents.add(element.toString());
|
---|
80 | name_to_element.put(element.toString(), element);
|
---|
81 | }
|
---|
82 | Collections.sort(contents);
|
---|
83 | }
|
---|
84 |
|
---|
85 | private boolean contains(Vector vector, Object key) {
|
---|
86 | for(int i = 0; i < vector.size(); i++) {
|
---|
87 | if(vector.get(i).toString().equals(key.toString())) {
|
---|
88 | return true;
|
---|
89 | }
|
---|
90 | }
|
---|
91 | return false;
|
---|
92 | }
|
---|
93 |
|
---|
94 | // Methods.
|
---|
95 | public void addElement(Object obj) {
|
---|
96 | if(!contents.contains(obj)) {
|
---|
97 | contents.add(obj);
|
---|
98 | Collections.sort(contents);
|
---|
99 | refresh();
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | public Object getElementAt(int index) {
|
---|
104 | return contents.get(index);
|
---|
105 | }
|
---|
106 |
|
---|
107 | public Object getSelectedItem() {
|
---|
108 | return selected_item;
|
---|
109 | }
|
---|
110 |
|
---|
111 | public ElementWrapper getSelectedMetadata() {
|
---|
112 | if(isMetadataElement) {
|
---|
113 | return (ElementWrapper)name_to_element.get(selected_item);
|
---|
114 | }
|
---|
115 | return null;
|
---|
116 | }
|
---|
117 |
|
---|
118 | public int getSize() {
|
---|
119 | return contents.size();
|
---|
120 | }
|
---|
121 |
|
---|
122 | public void insertElementAt(Object obj, int index) {
|
---|
123 | contents.add(index, obj);
|
---|
124 | fireIntervalAdded(this, index, index);
|
---|
125 | }
|
---|
126 |
|
---|
127 | public void refresh() {
|
---|
128 | fireContentsChanged(this, 0, getSize());
|
---|
129 | }
|
---|
130 |
|
---|
131 | public void removeElement(Object obj) {
|
---|
132 | int index = contents.indexOf(obj);
|
---|
133 | contents.remove(index);
|
---|
134 | fireIntervalRemoved(this, index, index);
|
---|
135 | }
|
---|
136 |
|
---|
137 | public void removeElementAt(int index) {
|
---|
138 | contents.remove(index);
|
---|
139 | fireIntervalRemoved(this, index, index);
|
---|
140 | }
|
---|
141 |
|
---|
142 | public void setSelectedItem(Object obj) {
|
---|
143 | selected_item = obj;
|
---|
144 | int index = contents.indexOf(obj);
|
---|
145 | fireContentsChanged(this, index, index);
|
---|
146 | }
|
---|
147 | }
|
---|