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

Last change on this file since 18376 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
RevLine 
[4293]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 */
[5589]37package org.greenstone.gatherer.gui;
38
[4293]39import java.awt.*;
40import java.util.*;
41import javax.swing.*;
42import javax.swing.plaf.basic.*;
[8231]43import org.greenstone.gatherer.Configuration;
[13195]44import org.greenstone.gatherer.util.Utility;
[4293]45
46/**
47 * @author John Thompson, Greenstone Digital Library, University of Waikato
[6389]48 * @version 2.3
[4293]49 */
50public class GComboBox
[4367]51 extends JComboBox {
[4293]52
[4367]53 private Color background = null;
54 private Color foreground = null;
[6321]55 private Color editable_background = null;
56 private Color editable_foreground = null;
[4367]57 private Color selection_background = null;
58 private Color selection_foreground = null;
[4293]59
[12823]60 private boolean sort_objects = true;
61
[4367]62 public GComboBox() {
63 super();
[18370]64 this.setComponentOrientation(org.greenstone.gatherer.Dictionary.getOrientation());
[4367]65 init();
[13195]66 setOpaque(!Utility.isMac());
[4367]67 }
[4293]68
[6321]69 public GComboBox(boolean editable) {
70 super();
[18370]71 this.setComponentOrientation(org.greenstone.gatherer.Dictionary.getOrientation());
[13195]72 setOpaque(!Utility.isMac());
[8169]73 setEditable(editable);
[6321]74 init();
[13195]75
[6321]76 }
77
[4367]78 public GComboBox(ArrayList data) {
79 super(data.toArray());
[18370]80 this.setComponentOrientation(org.greenstone.gatherer.Dictionary.getOrientation());
[13195]81 setOpaque(!Utility.isMac());
[4367]82 init();
83 }
[4293]84
[6321]85 public GComboBox(ArrayList data, boolean editable) {
86 super(data.toArray());
[18370]87 this.setComponentOrientation(org.greenstone.gatherer.Dictionary.getOrientation());
[13195]88 setOpaque(!Utility.isMac());
[8169]89 setEditable(editable);
[6321]90 init();
91 }
92
[12823]93 public GComboBox(ArrayList data, boolean editable, boolean sorted) {
94 super(data.toArray());
[18370]95 this.setComponentOrientation(org.greenstone.gatherer.Dictionary.getOrientation());
[13195]96 setOpaque(!Utility.isMac());
[12823]97 setEditable(editable);
98 setSorted(sorted);
99 init();
100 }
101
[4367]102 public GComboBox(ComboBoxModel model) {
103 super(model);
[18370]104 this.setComponentOrientation(org.greenstone.gatherer.Dictionary.getOrientation());
[13195]105 setOpaque(!Utility.isMac());
[4367]106 init();
107 }
[4293]108
[6321]109 public GComboBox(ComboBoxModel model, boolean editable) {
110 super(model);
[18370]111 this.setComponentOrientation(org.greenstone.gatherer.Dictionary.getOrientation());
[13195]112 setOpaque(!Utility.isMac());
[8169]113 setEditable(editable);
[6321]114 init();
115 }
116
[4367]117 public GComboBox(Object data[]) {
118 super(data);
[18370]119 this.setComponentOrientation(org.greenstone.gatherer.Dictionary.getOrientation());
[13195]120 setOpaque(!Utility.isMac());
[4367]121 init();
122 }
[4293]123
[6321]124 public GComboBox(Object data[], boolean editable) {
125 super(data);
[18370]126 this.setComponentOrientation(org.greenstone.gatherer.Dictionary.getOrientation());
[13195]127 setOpaque(!Utility.isMac());
[8169]128 setEditable(editable);
[6321]129 init();
130 }
131
[12823]132 public GComboBox(Object data[], boolean editable, boolean sorted) {
133 super(data);
[18370]134 this.setComponentOrientation(org.greenstone.gatherer.Dictionary.getOrientation());
[13195]135 setOpaque(!Utility.isMac());
[12823]136 setEditable(editable);
137 setSorted(sorted);
138 init();
139 }
140
[4367]141 public GComboBox(Vector data) {
142 super(data);
[18370]143 this.setComponentOrientation(org.greenstone.gatherer.Dictionary.getOrientation());
[13195]144 setOpaque(!Utility.isMac());
[4367]145 init();
146 }
[4293]147
[6321]148 public GComboBox(Vector data, boolean editable) {
149 super(data);
[18370]150 this.setComponentOrientation(org.greenstone.gatherer.Dictionary.getOrientation());
[13195]151 setOpaque(!Utility.isMac());
[8169]152 setEditable(editable);
[6321]153 init();
154 }
155
[12823]156 public void setSorted(boolean sort) {
157 sort_objects = sort;
158 }
[4367]159 public int add(Object object) {
[8169]160 if (dataModel instanceof Model) {
161 return ((Model) dataModel).add(object);
[4798]162 }
163 else {
164 return -1;
165 }
[4367]166 }
[4293]167
[4367]168 public Object get(int index) {
[4798]169 return dataModel.getElementAt(index);
[4367]170 }
[4293]171
[4798]172 public void clear() {
[8169]173 if (dataModel instanceof Model) {
174 ((Model) dataModel).clear();
[4367]175 }
176 }
[4293]177
[8169]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
[8231]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);
[8169]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
[6321]213 /** Overridden to do nothing.
214 * @param background
215 */
216 public void setBackground(Color background) {
217 }
218
[8169]219 public void setBackgroundEditableColor(Color editable_background) {
220 this.editable_background = editable_background;
221 }
[4293]222
[6321]223 public void setBackgroundNonSelectionColor(Color background) {
224 this.background = background;
[4367]225 }
[4293]226
[4367]227 public void setBackgroundSelectionColor(Color selection_background) {
228 this.selection_background = selection_background;
229 }
[4293]230
[6321]231 public void setEditable(boolean editable) {
[8169]232 setEditor(new Editor());
[6321]233 super.setEditable(editable);
[8169]234 if (isEditable()) {
[6389]235 this.setBackground(editable_background);
236 this.setForeground(editable_foreground);
237 }
238 else {
239 this.setBackground(background);
240 this.setForeground(foreground);
241 }
[6321]242 }
243
[8169]244 public void setTextEditableColor(Color editable_foreground) {
245 this.editable_foreground = editable_foreground;
246 }
[6321]247
248 public void setTextNonSelectionColor(Color foreground) {
249 this.foreground = foreground;
250 }
251
[4367]252 public void setTextSelectionColor(Color selection_foreground) {
253 this.selection_foreground = selection_foreground;
254 }
[4293]255
256
[4367]257 private class Editor
258 extends JTextField
259 implements ComboBoxEditor {
260 public Editor() {
[6321]261 setOpaque(true);
[8169]262 if (isEditable()) {
[6389]263 setBackground(editable_background);
264 setForeground(editable_foreground);
[6321]265 }
[6389]266 else {
267 setBackground(background);
268 setForeground(foreground);
269 }
270 setSelectionColor(selection_background);
271 setSelectedTextColor(selection_foreground);
[4367]272 }
[4293]273
[4367]274 public Component getEditorComponent() {
275 return this;
276 }
[4293]277
[4367]278 public Object getItem() {
279 return getText();
280 }
[4293]281
[4367]282 public void setItem(Object item) {
283 if(item != null) {
284 setText(item.toString());
285 }
286 else {
287 setText("");
288 }
289 }
290 }
[4293]291
[4367]292 private class Model
293 extends DefaultComboBoxModel {
[12823]294
295
[4367]296 public int add(Object extension) {
297 int position = 0;
298 String extension_str = extension.toString().toLowerCase();
[4798]299 while(extension != null && position < getSize()) {
[4367]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.
[12823]303 if(sort_objects && order < 0) {
[4367]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) {
[4798]317 position = getSize();
[4367]318 addElement(extension);
319 }
320 return position;
321 }
[4293]322
[4367]323 public void clear() {
324 removeAllElements();
325 }
326 }
[4293]327
[4675]328 private class Renderer
329 extends JLabel
[4367]330 implements ListCellRenderer {
331 public Renderer() {
[6321]332 super("");
333 this.setOpaque(true);
[18370]334 this.setComponentOrientation(org.greenstone.gatherer.Dictionary.getOrientation());
[4367]335 }
336 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
337 if(value != null) {
[6321]338 this.setText(value.toString());
[4367]339 }
340 else {
[6321]341 this.setText("");
[4367]342 }
[6389]343 if(isSelected) {
344 this.setBackground(selection_background);
345 this.setForeground(selection_foreground);
346 }
[8169]347 else if (isEditable()) {
[6321]348 this.setBackground(editable_background);
349 this.setForeground(editable_foreground);
350 }
351 else {
352 this.setBackground(background);
353 this.setForeground(foreground);
354 }
[4367]355 return this;
356 }
357 }
[4293]358
[4367]359 private class UI
360 extends BasicComboBoxUI {
361 public UI() {
362 super();
363 }
[4293]364
[4367]365 protected ComboPopup createPopup() {
[9184]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// };
[4367]390 popup.getAccessibleContext().setAccessibleParent(comboBox);
391 return popup;
392 }
[4293]393
[4675]394 public void setButtonBackground() {
[8231]395 arrowButton.setBackground(Configuration.getColor("coloring.button_background", false));
[4367]396 }
397 }
[4293]398}
Note: See TracBrowser for help on using the repository browser.