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

Last change on this file since 6394 was 6389, checked in by jmt12, 20 years ago

Introduced the idea of detail modes - these have an effect on several parts of the gui, such as disabling or hiding all regular expression based controls, simplifying the output from perl scripts and (having been given yet another new last minute feature to implement) displays a completely different create pane. Mode is stored in the config.xml and is changable via the Preferences controls

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