source: trunk/gli/src/org/greenstone/gatherer/gui/GComboBox.java@ 5593

Last change on this file since 5593 was 5593, checked in by mdewsnip, 21 years ago

Changed calls to the Dictionary.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.1 KB
Line 
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 */
37package org.greenstone.gatherer.gui;
38
39import java.awt.*;
40import java.util.*;
41import javax.swing.*;
42import javax.swing.plaf.basic.*;
43import org.greenstone.gatherer.Gatherer;
44
45/**
46 * @author John Thompson, Greenstone Digital Library, University of Waikato
47 * @version
48 */
49public class GComboBox
50 extends JComboBox {
51
52 private Color background = null;
53 private Color foreground = null;
54 private Color selection_background = null;
55 private Color selection_foreground = null;
56
57 public GComboBox() {
58 super();
59 init();
60 }
61
62 public GComboBox(ArrayList data) {
63 super(data.toArray());
64 init();
65 }
66
67 public GComboBox(ComboBoxModel model) {
68 super(model);
69 init();
70 }
71
72 public GComboBox(Gatherer gatherer) {
73 super();
74 init();
75 }
76
77 public GComboBox(Object data[]) {
78 super(data);
79 init();
80 }
81
82 public GComboBox(Vector data) {
83 super(data);
84 init();
85 }
86
87 public int add(Object object) {
88 if(dataModel instanceof Model) {
89 return ((Model)dataModel).add(object);
90 }
91 else {
92 return -1;
93 }
94 }
95
96 public Object get(int index) {
97 return dataModel.getElementAt(index);
98 }
99
100 public void clear() {
101 if(dataModel instanceof Model) {
102 ((Model)dataModel).clear();
103 }
104 }
105
106 public int count() {
107 return dataModel.getSize();
108 }
109
110 public void setBackgroundNonSelectionColor(Color background) {
111 this.background = background;
112 setEditor(new Editor());
113 setRenderer(new Renderer());
114 }
115
116 public void setTextNonSelectionColor(Color foreground) {
117 this.foreground = foreground;
118 setEditor(new Editor());
119 setRenderer(new Renderer());
120 }
121
122 public void setBackgroundSelectionColor(Color selection_background) {
123 this.selection_background = selection_background;
124 setEditor(new Editor());
125 setRenderer(new Renderer());
126 }
127
128 public void setTextSelectionColor(Color selection_foreground) {
129 this.selection_foreground = selection_foreground;
130 setEditor(new Editor());
131 setRenderer(new Renderer());
132 }
133
134 public void init() {
135 Model model = new Model();
136 ComboBoxModel old_model = (ComboBoxModel) getModel();
137 setModel(model);
138 // Restore any data given into our model
139 for(int i = 0; i < old_model.getSize(); i++) {
140 model.add(old_model.getElementAt(i));
141 }
142 // Change component
143 UI ui = new UI();
144 setUI(ui);
145 ui.setButtonBackground();
146 // Initialization
147 this.background = Gatherer.config.getColor("coloring.collection_tree_background", false);
148 this.foreground = Gatherer.config.getColor("coloring.collection_tree_foreground", false);
149 this.selection_background = Gatherer.config.getColor("coloring.collection_selection_background", false);
150 this.selection_foreground = Gatherer.config.getColor("coloring.collection_selection_foreground", false);
151 setEditor(new Editor());
152 setRenderer(new Renderer());
153 }
154
155 private class Editor
156 extends JTextField
157 implements ComboBoxEditor {
158 public Editor() {
159 setBackground(background);
160 setForeground(foreground);
161 setSelectionColor(selection_background);
162 setSelectedTextColor(selection_foreground);
163 }
164
165 public Component getEditorComponent() {
166 return this;
167 }
168
169 public Object getItem() {
170 return getText();
171 }
172
173 public void setItem(Object item) {
174 if(item != null) {
175 setText(item.toString());
176 }
177 else {
178 setText("");
179 }
180 }
181 }
182
183 private class Model
184 extends DefaultComboBoxModel {
185
186 public int add(Object extension) {
187 int position = 0;
188 String extension_str = extension.toString().toLowerCase();
189 while(extension != null && position < getSize()) {
190 String sibling = getElementAt(position).toString().toLowerCase();
191 int order = extension_str.compareTo(sibling);
192 // If we are now less than the sibling, insert here.
193 if(order < 0) {
194 insertElementAt(extension, position);
195 extension = null;
196 }
197 // We are equal to the sibling, thus we already exist in list. Done.
198 else if(order == 0) {
199 extension = null;
200 }
201 // Continue searching.
202 else {
203 position++;
204 }
205 }
206 if(extension != null) {
207 position = getSize();
208 addElement(extension);
209 }
210 return position;
211 }
212
213 public void clear() {
214 removeAllElements();
215 }
216 }
217
218 private class Renderer
219 extends JLabel
220 implements ListCellRenderer {
221 public Renderer() {
222 }
223 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
224 if(value != null) {
225 setText(value.toString());
226 }
227 else {
228 setText("");
229 }
230 setBackground(isSelected ? selection_background : background);
231 setForeground(isSelected ? selection_foreground : foreground);
232 setOpaque(true);
233 return this;
234 }
235 }
236
237 private class UI
238 extends BasicComboBoxUI {
239 public UI() {
240 super();
241 }
242
243 protected ComboPopup createPopup() {
244 BasicComboPopup popup = new BasicComboPopup( comboBox ) {
245 public void show() {
246 if(comboBox.getMaximumRowCount() > 0) {
247 Dimension popupSize = new Dimension( comboBox.getWidth(), getPopupHeightForRowCount( comboBox.getMaximumRowCount()));
248 Rectangle popupBounds = new Rectangle( 0, 0, popupSize.width, popupSize.height);
249 scroller.setMaximumSize( popupBounds.getSize() );
250 scroller.setPreferredSize( popupBounds.getSize() );
251 scroller.setMinimumSize( popupBounds.getSize() );
252 list.invalidate();
253 int selectedIndex = comboBox.getSelectedIndex();
254 if ( selectedIndex == -1 ) {
255 list.clearSelection();
256 }
257 else {
258 list.setSelectedIndex( selectedIndex );
259 }
260 list.ensureIndexIsVisible( list.getSelectedIndex() );
261 setLightWeightPopupEnabled( comboBox.isLightWeightPopupEnabled() );
262 show(arrowButton, arrowButton.getSize().width - (popupSize.width + 4), arrowButton.getSize().height);
263 }
264 }
265 };
266 popup.getAccessibleContext().setAccessibleParent(comboBox);
267 return popup;
268 }
269
270 public void setButtonBackground() {
271 arrowButton.setBackground(Gatherer.config.getColor("coloring.button_background", false));
272 }
273 }
274}
Note: See TracBrowser for help on using the repository browser.