[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] | 37 | package org.greenstone.gatherer.gui;
|
---|
| 38 |
|
---|
[4293] | 39 | import java.awt.*;
|
---|
| 40 | import java.util.*;
|
---|
| 41 | import javax.swing.*;
|
---|
| 42 | import javax.swing.plaf.basic.*;
|
---|
[8231] | 43 | import org.greenstone.gatherer.Configuration;
|
---|
[13195] | 44 | import 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 | */
|
---|
| 50 | public 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();
|
---|
| 64 | init();
|
---|
[13195] | 65 | setOpaque(!Utility.isMac());
|
---|
[4367] | 66 | }
|
---|
[4293] | 67 |
|
---|
[6321] | 68 | public GComboBox(boolean editable) {
|
---|
| 69 | super();
|
---|
[13195] | 70 | setOpaque(!Utility.isMac());
|
---|
[8169] | 71 | setEditable(editable);
|
---|
[6321] | 72 | init();
|
---|
[13195] | 73 |
|
---|
[6321] | 74 | }
|
---|
| 75 |
|
---|
[4367] | 76 | public GComboBox(ArrayList data) {
|
---|
| 77 | super(data.toArray());
|
---|
[13195] | 78 | setOpaque(!Utility.isMac());
|
---|
[4367] | 79 | init();
|
---|
| 80 | }
|
---|
[4293] | 81 |
|
---|
[6321] | 82 | public GComboBox(ArrayList data, boolean editable) {
|
---|
| 83 | super(data.toArray());
|
---|
[13195] | 84 | setOpaque(!Utility.isMac());
|
---|
[8169] | 85 | setEditable(editable);
|
---|
[6321] | 86 | init();
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[12823] | 89 | public GComboBox(ArrayList data, boolean editable, boolean sorted) {
|
---|
| 90 | super(data.toArray());
|
---|
[13195] | 91 | setOpaque(!Utility.isMac());
|
---|
[12823] | 92 | setEditable(editable);
|
---|
| 93 | setSorted(sorted);
|
---|
| 94 | init();
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[4367] | 97 | public GComboBox(ComboBoxModel model) {
|
---|
| 98 | super(model);
|
---|
[13195] | 99 | setOpaque(!Utility.isMac());
|
---|
[4367] | 100 | init();
|
---|
| 101 | }
|
---|
[4293] | 102 |
|
---|
[6321] | 103 | public GComboBox(ComboBoxModel model, boolean editable) {
|
---|
| 104 | super(model);
|
---|
[13195] | 105 | setOpaque(!Utility.isMac());
|
---|
[8169] | 106 | setEditable(editable);
|
---|
[6321] | 107 | init();
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[4367] | 110 | public GComboBox(Object data[]) {
|
---|
| 111 | super(data);
|
---|
[13195] | 112 | setOpaque(!Utility.isMac());
|
---|
[4367] | 113 | init();
|
---|
| 114 | }
|
---|
[4293] | 115 |
|
---|
[6321] | 116 | public GComboBox(Object data[], boolean editable) {
|
---|
| 117 | super(data);
|
---|
[13195] | 118 | setOpaque(!Utility.isMac());
|
---|
[8169] | 119 | setEditable(editable);
|
---|
[6321] | 120 | init();
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[12823] | 123 | public GComboBox(Object data[], boolean editable, boolean sorted) {
|
---|
| 124 | super(data);
|
---|
[13195] | 125 | setOpaque(!Utility.isMac());
|
---|
[12823] | 126 | setEditable(editable);
|
---|
| 127 | setSorted(sorted);
|
---|
| 128 | init();
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[4367] | 131 | public GComboBox(Vector data) {
|
---|
| 132 | super(data);
|
---|
[13195] | 133 | setOpaque(!Utility.isMac());
|
---|
[4367] | 134 | init();
|
---|
| 135 | }
|
---|
[4293] | 136 |
|
---|
[6321] | 137 | public GComboBox(Vector data, boolean editable) {
|
---|
| 138 | super(data);
|
---|
[13195] | 139 | setOpaque(!Utility.isMac());
|
---|
[8169] | 140 | setEditable(editable);
|
---|
[6321] | 141 | init();
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[12823] | 144 | public void setSorted(boolean sort) {
|
---|
| 145 | sort_objects = sort;
|
---|
| 146 | }
|
---|
[4367] | 147 | public int add(Object object) {
|
---|
[8169] | 148 | if (dataModel instanceof Model) {
|
---|
| 149 | return ((Model) dataModel).add(object);
|
---|
[4798] | 150 | }
|
---|
| 151 | else {
|
---|
| 152 | return -1;
|
---|
| 153 | }
|
---|
[4367] | 154 | }
|
---|
[4293] | 155 |
|
---|
[4367] | 156 | public Object get(int index) {
|
---|
[4798] | 157 | return dataModel.getElementAt(index);
|
---|
[4367] | 158 | }
|
---|
[4293] | 159 |
|
---|
[4798] | 160 | public void clear() {
|
---|
[8169] | 161 | if (dataModel instanceof Model) {
|
---|
| 162 | ((Model) dataModel).clear();
|
---|
[4367] | 163 | }
|
---|
| 164 | }
|
---|
[4293] | 165 |
|
---|
[8169] | 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
|
---|
[8231] | 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);
|
---|
[8169] | 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 |
|
---|
[6321] | 201 | /** Overridden to do nothing.
|
---|
| 202 | * @param background
|
---|
| 203 | */
|
---|
| 204 | public void setBackground(Color background) {
|
---|
| 205 | }
|
---|
| 206 |
|
---|
[8169] | 207 | public void setBackgroundEditableColor(Color editable_background) {
|
---|
| 208 | this.editable_background = editable_background;
|
---|
| 209 | }
|
---|
[4293] | 210 |
|
---|
[6321] | 211 | public void setBackgroundNonSelectionColor(Color background) {
|
---|
| 212 | this.background = background;
|
---|
[4367] | 213 | }
|
---|
[4293] | 214 |
|
---|
[4367] | 215 | public void setBackgroundSelectionColor(Color selection_background) {
|
---|
| 216 | this.selection_background = selection_background;
|
---|
| 217 | }
|
---|
[4293] | 218 |
|
---|
[6321] | 219 | public void setEditable(boolean editable) {
|
---|
[8169] | 220 | setEditor(new Editor());
|
---|
[6321] | 221 | super.setEditable(editable);
|
---|
[8169] | 222 | if (isEditable()) {
|
---|
[6389] | 223 | this.setBackground(editable_background);
|
---|
| 224 | this.setForeground(editable_foreground);
|
---|
| 225 | }
|
---|
| 226 | else {
|
---|
| 227 | this.setBackground(background);
|
---|
| 228 | this.setForeground(foreground);
|
---|
| 229 | }
|
---|
[6321] | 230 | }
|
---|
| 231 |
|
---|
[8169] | 232 | public void setTextEditableColor(Color editable_foreground) {
|
---|
| 233 | this.editable_foreground = editable_foreground;
|
---|
| 234 | }
|
---|
[6321] | 235 |
|
---|
| 236 | public void setTextNonSelectionColor(Color foreground) {
|
---|
| 237 | this.foreground = foreground;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
[4367] | 240 | public void setTextSelectionColor(Color selection_foreground) {
|
---|
| 241 | this.selection_foreground = selection_foreground;
|
---|
| 242 | }
|
---|
[4293] | 243 |
|
---|
| 244 |
|
---|
[4367] | 245 | private class Editor
|
---|
| 246 | extends JTextField
|
---|
| 247 | implements ComboBoxEditor {
|
---|
| 248 | public Editor() {
|
---|
[6321] | 249 | setOpaque(true);
|
---|
[8169] | 250 | if (isEditable()) {
|
---|
[6389] | 251 | setBackground(editable_background);
|
---|
| 252 | setForeground(editable_foreground);
|
---|
[6321] | 253 | }
|
---|
[6389] | 254 | else {
|
---|
| 255 | setBackground(background);
|
---|
| 256 | setForeground(foreground);
|
---|
| 257 | }
|
---|
| 258 | setSelectionColor(selection_background);
|
---|
| 259 | setSelectedTextColor(selection_foreground);
|
---|
[4367] | 260 | }
|
---|
[4293] | 261 |
|
---|
[4367] | 262 | public Component getEditorComponent() {
|
---|
| 263 | return this;
|
---|
| 264 | }
|
---|
[4293] | 265 |
|
---|
[4367] | 266 | public Object getItem() {
|
---|
| 267 | return getText();
|
---|
| 268 | }
|
---|
[4293] | 269 |
|
---|
[4367] | 270 | public void setItem(Object item) {
|
---|
| 271 | if(item != null) {
|
---|
| 272 | setText(item.toString());
|
---|
| 273 | }
|
---|
| 274 | else {
|
---|
| 275 | setText("");
|
---|
| 276 | }
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
[4293] | 279 |
|
---|
[4367] | 280 | private class Model
|
---|
| 281 | extends DefaultComboBoxModel {
|
---|
[12823] | 282 |
|
---|
| 283 |
|
---|
[4367] | 284 | public int add(Object extension) {
|
---|
| 285 | int position = 0;
|
---|
| 286 | String extension_str = extension.toString().toLowerCase();
|
---|
[4798] | 287 | while(extension != null && position < getSize()) {
|
---|
[4367] | 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.
|
---|
[12823] | 291 | if(sort_objects && order < 0) {
|
---|
[4367] | 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) {
|
---|
[4798] | 305 | position = getSize();
|
---|
[4367] | 306 | addElement(extension);
|
---|
| 307 | }
|
---|
| 308 | return position;
|
---|
| 309 | }
|
---|
[4293] | 310 |
|
---|
[4367] | 311 | public void clear() {
|
---|
| 312 | removeAllElements();
|
---|
| 313 | }
|
---|
| 314 | }
|
---|
[4293] | 315 |
|
---|
[4675] | 316 | private class Renderer
|
---|
| 317 | extends JLabel
|
---|
[4367] | 318 | implements ListCellRenderer {
|
---|
| 319 | public Renderer() {
|
---|
[6321] | 320 | super("");
|
---|
| 321 | this.setOpaque(true);
|
---|
[4367] | 322 | }
|
---|
| 323 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
|
---|
| 324 | if(value != null) {
|
---|
[6321] | 325 | this.setText(value.toString());
|
---|
[4367] | 326 | }
|
---|
| 327 | else {
|
---|
[6321] | 328 | this.setText("");
|
---|
[4367] | 329 | }
|
---|
[6389] | 330 | if(isSelected) {
|
---|
| 331 | this.setBackground(selection_background);
|
---|
| 332 | this.setForeground(selection_foreground);
|
---|
| 333 | }
|
---|
[8169] | 334 | else if (isEditable()) {
|
---|
[6321] | 335 | this.setBackground(editable_background);
|
---|
| 336 | this.setForeground(editable_foreground);
|
---|
| 337 | }
|
---|
| 338 | else {
|
---|
| 339 | this.setBackground(background);
|
---|
| 340 | this.setForeground(foreground);
|
---|
| 341 | }
|
---|
[4367] | 342 | return this;
|
---|
| 343 | }
|
---|
| 344 | }
|
---|
[4293] | 345 |
|
---|
[4367] | 346 | private class UI
|
---|
| 347 | extends BasicComboBoxUI {
|
---|
| 348 | public UI() {
|
---|
| 349 | super();
|
---|
| 350 | }
|
---|
[4293] | 351 |
|
---|
[4367] | 352 | protected ComboPopup createPopup() {
|
---|
[9184] | 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 | // };
|
---|
[4367] | 377 | popup.getAccessibleContext().setAccessibleParent(comboBox);
|
---|
| 378 | return popup;
|
---|
| 379 | }
|
---|
[4293] | 380 |
|
---|
[4675] | 381 | public void setButtonBackground() {
|
---|
[8231] | 382 | arrowButton.setBackground(Configuration.getColor("coloring.button_background", false));
|
---|
[4367] | 383 | }
|
---|
| 384 | }
|
---|
[4293] | 385 | }
|
---|