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

Last change on this file since 18370 was 18370, checked in by kjdon, 15 years ago

committed code submitted by Amin Hedjazi for making the GLI right to left. I worked on this code on the rtl-gli branch, then merged the branch back to the trunk at revision 18368. The branch code was slightly different in a couple of places where it shouldn't have been. So don't use the branch code next time. Start a new branch.

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