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

Last change on this file since 12561 was 9184, checked in by mdewsnip, 19 years ago

Fixes for Java 1.5.0.

  • Property svn:keywords set to Author Date Id Revision
File size: 9.0 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.Configuration;
44
45/**
46 * @author John Thompson, Greenstone Digital Library, University of Waikato
47 * @version 2.3
48 */
49public class GComboBox
50 extends JComboBox {
51
52 private Color background = null;
53 private Color foreground = null;
54 private Color editable_background = null;
55 private Color editable_foreground = null;
56 private Color selection_background = null;
57 private Color selection_foreground = null;
58
59 public GComboBox() {
60 super();
61 init();
62 }
63
64 public GComboBox(boolean editable) {
65 super();
66 setEditable(editable);
67 init();
68 }
69
70 public GComboBox(ArrayList data) {
71 super(data.toArray());
72 init();
73 }
74
75 public GComboBox(ArrayList data, boolean editable) {
76 super(data.toArray());
77 setEditable(editable);
78 init();
79 }
80
81 public GComboBox(ComboBoxModel model) {
82 super(model);
83 init();
84 }
85
86 public GComboBox(ComboBoxModel model, boolean editable) {
87 super(model);
88 setEditable(editable);
89 init();
90 }
91
92 public GComboBox(Object data[]) {
93 super(data);
94 init();
95 }
96
97 public GComboBox(Object data[], boolean editable) {
98 super(data);
99 setEditable(editable);
100 init();
101 }
102
103 public GComboBox(Vector data) {
104 super(data);
105 init();
106 }
107
108 public GComboBox(Vector data, boolean editable) {
109 super(data);
110 setEditable(editable);
111 init();
112 }
113
114 public int add(Object object) {
115 if (dataModel instanceof Model) {
116 return ((Model) dataModel).add(object);
117 }
118 else {
119 return -1;
120 }
121 }
122
123 public Object get(int index) {
124 return dataModel.getElementAt(index);
125 }
126
127 public void clear() {
128 if (dataModel instanceof Model) {
129 ((Model) dataModel).clear();
130 }
131 }
132
133 public void init() {
134 Model model = new Model();
135 ComboBoxModel old_model = (ComboBoxModel) getModel();
136 setModel(model);
137 setOpaque(true);
138
139 // Restore any data given into our model
140 for(int i = 0; i < old_model.getSize(); i++) {
141 model.add(old_model.getElementAt(i));
142 }
143
144 // Change component
145 UI ui = new UI();
146 setUI(ui);
147 ui.setButtonBackground();
148
149 // Initialization
150 this.background = Configuration.getColor("coloring.collection_tree_background", false);
151 this.foreground = Configuration.getColor("coloring.collection_tree_foreground", false);
152 this.editable_background = Configuration.getColor("coloring.editable_background", false);
153 this.editable_foreground = Configuration.getColor("coloring.editable_foreground", false);
154 this.selection_background = Configuration.getColor("coloring.collection_selection_background", false);
155 this.selection_foreground = Configuration.getColor("coloring.collection_selection_foreground", false);
156 if (isEditable()) {
157 this.setBackground(editable_background);
158 this.setForeground(editable_foreground);
159 }
160 else {
161 this.setBackground(background);
162 this.setForeground(foreground);
163 }
164 setBorder(BorderFactory.createLoweredBevelBorder());
165 setRenderer(new Renderer());
166 }
167
168 /** Overridden to do nothing.
169 * @param background
170 */
171 public void setBackground(Color background) {
172 }
173
174 public void setBackgroundEditableColor(Color editable_background) {
175 this.editable_background = editable_background;
176 }
177
178 public void setBackgroundNonSelectionColor(Color background) {
179 this.background = background;
180 }
181
182 public void setBackgroundSelectionColor(Color selection_background) {
183 this.selection_background = selection_background;
184 }
185
186 public void setEditable(boolean editable) {
187 setEditor(new Editor());
188 super.setEditable(editable);
189 if (isEditable()) {
190 this.setBackground(editable_background);
191 this.setForeground(editable_foreground);
192 }
193 else {
194 this.setBackground(background);
195 this.setForeground(foreground);
196 }
197 }
198
199 public void setTextEditableColor(Color editable_foreground) {
200 this.editable_foreground = editable_foreground;
201 }
202
203 public void setTextNonSelectionColor(Color foreground) {
204 this.foreground = foreground;
205 }
206
207 public void setTextSelectionColor(Color selection_foreground) {
208 this.selection_foreground = selection_foreground;
209 }
210
211
212 private class Editor
213 extends JTextField
214 implements ComboBoxEditor {
215 public Editor() {
216 setOpaque(true);
217 if (isEditable()) {
218 setBackground(editable_background);
219 setForeground(editable_foreground);
220 }
221 else {
222 setBackground(background);
223 setForeground(foreground);
224 }
225 setSelectionColor(selection_background);
226 setSelectedTextColor(selection_foreground);
227 }
228
229 public Component getEditorComponent() {
230 return this;
231 }
232
233 public Object getItem() {
234 return getText();
235 }
236
237 public void setItem(Object item) {
238 if(item != null) {
239 setText(item.toString());
240 }
241 else {
242 setText("");
243 }
244 }
245 }
246
247 private class Model
248 extends DefaultComboBoxModel {
249
250 public int add(Object extension) {
251 int position = 0;
252 String extension_str = extension.toString().toLowerCase();
253 while(extension != null && position < getSize()) {
254 String sibling = getElementAt(position).toString().toLowerCase();
255 int order = extension_str.compareTo(sibling);
256 // If we are now less than the sibling, insert here.
257 if(order < 0) {
258 insertElementAt(extension, position);
259 extension = null;
260 }
261 // We are equal to the sibling, thus we already exist in list. Done.
262 else if(order == 0) {
263 extension = null;
264 }
265 // Continue searching.
266 else {
267 position++;
268 }
269 }
270 if(extension != null) {
271 position = getSize();
272 addElement(extension);
273 }
274 return position;
275 }
276
277 public void clear() {
278 removeAllElements();
279 }
280 }
281
282 private class Renderer
283 extends JLabel
284 implements ListCellRenderer {
285 public Renderer() {
286 super("");
287 this.setOpaque(true);
288 }
289 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
290 if(value != null) {
291 this.setText(value.toString());
292 }
293 else {
294 this.setText("");
295 }
296 if(isSelected) {
297 this.setBackground(selection_background);
298 this.setForeground(selection_foreground);
299 }
300 else if (isEditable()) {
301 this.setBackground(editable_background);
302 this.setForeground(editable_foreground);
303 }
304 else {
305 this.setBackground(background);
306 this.setForeground(foreground);
307 }
308 return this;
309 }
310 }
311
312 private class UI
313 extends BasicComboBoxUI {
314 public UI() {
315 super();
316 }
317
318 protected ComboPopup createPopup() {
319 BasicComboPopup popup = new BasicComboPopup(comboBox);
320 // ---- I don't know why this code is here... maybe it is needed for the Mac? ----
321// {
322// public void show() {
323// if(comboBox.getMaximumRowCount() > 0) {
324// Dimension popupSize = new Dimension( comboBox.getWidth(), getPopupHeightForRowCount( comboBox.getMaximumRowCount()));
325// Rectangle popupBounds = new Rectangle( 0, 0, popupSize.width, popupSize.height);
326// scroller.setMaximumSize( popupBounds.getSize() );
327// scroller.setPreferredSize( popupBounds.getSize() );
328// scroller.setMinimumSize( popupBounds.getSize() );
329// list.invalidate();
330// int selectedIndex = comboBox.getSelectedIndex();
331// if ( selectedIndex == -1 ) {
332// list.clearSelection();
333// }
334// else {
335// list.setSelectedIndex( selectedIndex );
336// }
337// list.ensureIndexIsVisible( list.getSelectedIndex() );
338// setLightWeightPopupEnabled( comboBox.isLightWeightPopupEnabled() );
339// show(arrowButton, arrowButton.getSize().width - (popupSize.width + 4), arrowButton.getSize().height);
340// }
341// }
342// };
343 popup.getAccessibleContext().setAccessibleParent(comboBox);
344 return popup;
345 }
346
347 public void setButtonBackground() {
348 arrowButton.setBackground(Configuration.getColor("coloring.button_background", false));
349 }
350 }
351}
Note: See TracBrowser for help on using the repository browser.