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

Last change on this file since 13195 was 13195, checked in by kjdon, 17 years ago

quan's changes to remove blue borders around buttons and comboboxes on macs

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