[4293] | 1 | package org.greenstone.gatherer.gui;
|
---|
| 2 | /**
|
---|
| 3 | *#########################################################################
|
---|
| 4 | *
|
---|
| 5 | * A component of the Gatherer application, part of the Greenstone digital
|
---|
| 6 | * library suite from the New Zealand Digital Library Project at the
|
---|
| 7 | * University of Waikato, New Zealand.
|
---|
| 8 | *
|
---|
| 9 | * <BR><BR>
|
---|
| 10 | *
|
---|
| 11 | * Author: John Thompson, Greenstone Digital Library, University of Waikato
|
---|
| 12 | *
|
---|
| 13 | * <BR><BR>
|
---|
| 14 | *
|
---|
| 15 | * Copyright (C) 1999 New Zealand Digital Library Project
|
---|
| 16 | *
|
---|
| 17 | * <BR><BR>
|
---|
| 18 | *
|
---|
| 19 | * This program is free software; you can redistribute it and/or modify
|
---|
| 20 | * it under the terms of the GNU General Public License as published by
|
---|
| 21 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 22 | * (at your option) any later version.
|
---|
| 23 | *
|
---|
| 24 | * <BR><BR>
|
---|
| 25 | *
|
---|
| 26 | * This program is distributed in the hope that it will be useful,
|
---|
| 27 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 28 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 29 | * GNU General Public License for more details.
|
---|
| 30 | *
|
---|
| 31 | * <BR><BR>
|
---|
| 32 | *
|
---|
| 33 | * You should have received a copy of the GNU General Public License
|
---|
| 34 | * along with this program; if not, write to the Free Software
|
---|
| 35 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 36 | *########################################################################
|
---|
| 37 | */
|
---|
| 38 | import java.awt.*;
|
---|
| 39 | import java.util.*;
|
---|
| 40 | import javax.swing.*;
|
---|
| 41 | import javax.swing.plaf.basic.*;
|
---|
| 42 | import org.greenstone.gatherer.Gatherer;
|
---|
| 43 |
|
---|
| 44 | /**
|
---|
| 45 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
| 46 | * @version
|
---|
| 47 | */
|
---|
| 48 | public class GComboBox
|
---|
[4367] | 49 | extends JComboBox {
|
---|
[4293] | 50 |
|
---|
[4367] | 51 | private Color background = null;
|
---|
| 52 | private Color foreground = null;
|
---|
| 53 | private Color selection_background = null;
|
---|
| 54 | private Color selection_foreground = null;
|
---|
[4293] | 55 |
|
---|
[4367] | 56 | public GComboBox() {
|
---|
| 57 | super();
|
---|
| 58 | init();
|
---|
| 59 | }
|
---|
[4293] | 60 |
|
---|
[4367] | 61 | public GComboBox(ArrayList data) {
|
---|
| 62 | super(data.toArray());
|
---|
| 63 | init();
|
---|
| 64 | }
|
---|
[4293] | 65 |
|
---|
[4367] | 66 | public GComboBox(ComboBoxModel model) {
|
---|
| 67 | super(model);
|
---|
| 68 | init();
|
---|
| 69 | }
|
---|
[4293] | 70 |
|
---|
[4833] | 71 | /** Legacy constructor. */
|
---|
| 72 | public GComboBox(Gatherer gatherer) {
|
---|
| 73 | super();
|
---|
| 74 | init();
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[4367] | 77 | public GComboBox(Object data[]) {
|
---|
| 78 | super(data);
|
---|
| 79 | init();
|
---|
| 80 | }
|
---|
[4293] | 81 |
|
---|
[4367] | 82 | public GComboBox(Vector data) {
|
---|
| 83 | super(data);
|
---|
| 84 | init();
|
---|
| 85 | }
|
---|
[4293] | 86 |
|
---|
[4367] | 87 | public int add(Object object) {
|
---|
[4798] | 88 | if(dataModel instanceof Model) {
|
---|
| 89 | return ((Model)dataModel).add(object);
|
---|
| 90 | }
|
---|
| 91 | else {
|
---|
| 92 | return -1;
|
---|
| 93 | }
|
---|
[4367] | 94 | }
|
---|
[4293] | 95 |
|
---|
[4367] | 96 | public Object get(int index) {
|
---|
[4798] | 97 | return dataModel.getElementAt(index);
|
---|
[4367] | 98 | }
|
---|
[4293] | 99 |
|
---|
[4798] | 100 | public void clear() {
|
---|
| 101 | if(dataModel instanceof Model) {
|
---|
| 102 | ((Model)dataModel).clear();
|
---|
[4367] | 103 | }
|
---|
| 104 | }
|
---|
[4293] | 105 |
|
---|
[4367] | 106 | public int count() {
|
---|
[4798] | 107 | return dataModel.getSize();
|
---|
[4367] | 108 | }
|
---|
[4293] | 109 |
|
---|
[4367] | 110 | public void setBackgroundNonSelectionColor(Color background) {
|
---|
| 111 | this.background = background;
|
---|
| 112 | setEditor(new Editor());
|
---|
| 113 | setRenderer(new Renderer());
|
---|
| 114 | }
|
---|
[4293] | 115 |
|
---|
[4367] | 116 | public void setTextNonSelectionColor(Color foreground) {
|
---|
| 117 | this.foreground = foreground;
|
---|
| 118 | setEditor(new Editor());
|
---|
| 119 | setRenderer(new Renderer());
|
---|
| 120 | }
|
---|
[4293] | 121 |
|
---|
[4367] | 122 | public void setBackgroundSelectionColor(Color selection_background) {
|
---|
| 123 | this.selection_background = selection_background;
|
---|
| 124 | setEditor(new Editor());
|
---|
| 125 | setRenderer(new Renderer());
|
---|
| 126 | }
|
---|
[4293] | 127 |
|
---|
[4367] | 128 | public void setTextSelectionColor(Color selection_foreground) {
|
---|
| 129 | this.selection_foreground = selection_foreground;
|
---|
| 130 | setEditor(new Editor());
|
---|
| 131 | setRenderer(new Renderer());
|
---|
| 132 | }
|
---|
[4293] | 133 |
|
---|
[4367] | 134 | public void init() {
|
---|
[4798] | 135 | Model model = new Model();
|
---|
[4367] | 136 | ComboBoxModel old_model = (ComboBoxModel) getModel();
|
---|
| 137 | setModel(model);
|
---|
| 138 | // Restore any data given into our model
|
---|
| 139 | for(int i = 0; i < old_model.getSize(); i++) {
|
---|
| 140 | model.add(old_model.getElementAt(i));
|
---|
| 141 | }
|
---|
| 142 | // Change component
|
---|
| 143 | UI ui = new UI();
|
---|
| 144 | setUI(ui);
|
---|
[4675] | 145 | ui.setButtonBackground();
|
---|
[4367] | 146 | // Initialization
|
---|
| 147 | this.background = Gatherer.config.getColor("coloring.collection_tree_background", false);
|
---|
| 148 | this.foreground = Gatherer.config.getColor("coloring.collection_tree_foreground", false);
|
---|
| 149 | this.selection_background = Gatherer.config.getColor("coloring.collection_selection_background", false);
|
---|
| 150 | this.selection_foreground = Gatherer.config.getColor("coloring.collection_selection_foreground", false);
|
---|
| 151 | setEditor(new Editor());
|
---|
| 152 | setRenderer(new Renderer());
|
---|
| 153 | }
|
---|
[4293] | 154 |
|
---|
[4367] | 155 | private class Editor
|
---|
| 156 | extends JTextField
|
---|
| 157 | implements ComboBoxEditor {
|
---|
| 158 | public Editor() {
|
---|
| 159 | setBackground(background);
|
---|
| 160 | setForeground(foreground);
|
---|
[4675] | 161 | setSelectionColor(selection_background);
|
---|
[4367] | 162 | setSelectedTextColor(selection_foreground);
|
---|
| 163 | }
|
---|
[4293] | 164 |
|
---|
[4367] | 165 | public Component getEditorComponent() {
|
---|
| 166 | return this;
|
---|
| 167 | }
|
---|
[4293] | 168 |
|
---|
[4367] | 169 | public Object getItem() {
|
---|
| 170 | return getText();
|
---|
| 171 | }
|
---|
[4293] | 172 |
|
---|
[4367] | 173 | public void setItem(Object item) {
|
---|
| 174 | if(item != null) {
|
---|
| 175 | setText(item.toString());
|
---|
| 176 | }
|
---|
| 177 | else {
|
---|
| 178 | setText("");
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
[4293] | 182 |
|
---|
[4367] | 183 | private class Model
|
---|
| 184 | extends DefaultComboBoxModel {
|
---|
[4293] | 185 |
|
---|
[4367] | 186 | public int add(Object extension) {
|
---|
| 187 | int position = 0;
|
---|
| 188 | String extension_str = extension.toString().toLowerCase();
|
---|
[4798] | 189 | while(extension != null && position < getSize()) {
|
---|
[4367] | 190 | String sibling = getElementAt(position).toString().toLowerCase();
|
---|
| 191 | int order = extension_str.compareTo(sibling);
|
---|
| 192 | // If we are now less than the sibling, insert here.
|
---|
| 193 | if(order < 0) {
|
---|
| 194 | insertElementAt(extension, position);
|
---|
| 195 | extension = null;
|
---|
| 196 | }
|
---|
| 197 | // We are equal to the sibling, thus we already exist in list. Done.
|
---|
| 198 | else if(order == 0) {
|
---|
| 199 | extension = null;
|
---|
| 200 | }
|
---|
| 201 | // Continue searching.
|
---|
| 202 | else {
|
---|
| 203 | position++;
|
---|
| 204 | }
|
---|
| 205 | }
|
---|
| 206 | if(extension != null) {
|
---|
[4798] | 207 | position = getSize();
|
---|
[4367] | 208 | addElement(extension);
|
---|
| 209 | }
|
---|
| 210 | return position;
|
---|
| 211 | }
|
---|
[4293] | 212 |
|
---|
[4367] | 213 | public void clear() {
|
---|
| 214 | removeAllElements();
|
---|
| 215 | }
|
---|
| 216 | }
|
---|
[4293] | 217 |
|
---|
[4675] | 218 | private class Renderer
|
---|
| 219 | extends JLabel
|
---|
[4367] | 220 | implements ListCellRenderer {
|
---|
| 221 | public Renderer() {
|
---|
| 222 | }
|
---|
| 223 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
|
---|
| 224 | if(value != null) {
|
---|
| 225 | setText(value.toString());
|
---|
| 226 | }
|
---|
| 227 | else {
|
---|
| 228 | setText("");
|
---|
| 229 | }
|
---|
| 230 | setBackground(isSelected ? selection_background : background);
|
---|
| 231 | setForeground(isSelected ? selection_foreground : foreground);
|
---|
| 232 | setOpaque(true);
|
---|
| 233 | return this;
|
---|
| 234 | }
|
---|
| 235 | }
|
---|
[4293] | 236 |
|
---|
[4367] | 237 | private class UI
|
---|
| 238 | extends BasicComboBoxUI {
|
---|
| 239 | public UI() {
|
---|
| 240 | super();
|
---|
| 241 | }
|
---|
[4293] | 242 |
|
---|
[4367] | 243 | protected ComboPopup createPopup() {
|
---|
| 244 | BasicComboPopup popup = new BasicComboPopup( comboBox ) {
|
---|
| 245 | public void show() {
|
---|
| 246 | if(comboBox.getMaximumRowCount() > 0) {
|
---|
| 247 | Dimension popupSize = new Dimension( comboBox.getWidth(), getPopupHeightForRowCount( comboBox.getMaximumRowCount()));
|
---|
| 248 | Rectangle popupBounds = new Rectangle( 0, 0, popupSize.width, popupSize.height);
|
---|
| 249 | scroller.setMaximumSize( popupBounds.getSize() );
|
---|
| 250 | scroller.setPreferredSize( popupBounds.getSize() );
|
---|
| 251 | scroller.setMinimumSize( popupBounds.getSize() );
|
---|
[4675] | 252 | list.invalidate();
|
---|
[4367] | 253 | int selectedIndex = comboBox.getSelectedIndex();
|
---|
| 254 | if ( selectedIndex == -1 ) {
|
---|
| 255 | list.clearSelection();
|
---|
[4675] | 256 | }
|
---|
[4367] | 257 | else {
|
---|
| 258 | list.setSelectedIndex( selectedIndex );
|
---|
[4675] | 259 | }
|
---|
[4367] | 260 | list.ensureIndexIsVisible( list.getSelectedIndex() );
|
---|
| 261 | setLightWeightPopupEnabled( comboBox.isLightWeightPopupEnabled() );
|
---|
| 262 | show(arrowButton, arrowButton.getSize().width - (popupSize.width + 4), arrowButton.getSize().height);
|
---|
| 263 | }
|
---|
| 264 | }
|
---|
| 265 | };
|
---|
| 266 | popup.getAccessibleContext().setAccessibleParent(comboBox);
|
---|
| 267 | return popup;
|
---|
| 268 | }
|
---|
[4293] | 269 |
|
---|
[4675] | 270 | public void setButtonBackground() {
|
---|
| 271 | arrowButton.setBackground(Gatherer.config.getColor("coloring.button_background", false));
|
---|
[4367] | 272 | }
|
---|
| 273 | }
|
---|
[4293] | 274 | }
|
---|