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 | */
|
---|
37 | package org.greenstone.gatherer.gui;
|
---|
38 |
|
---|
39 | import java.awt.*;
|
---|
40 | import java.util.*;
|
---|
41 | import javax.swing.*;
|
---|
42 | import javax.swing.plaf.basic.*;
|
---|
43 | import org.greenstone.gatherer.Gatherer;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
47 | * @version
|
---|
48 | */
|
---|
49 | public class GComboBox
|
---|
50 | extends JComboBox {
|
---|
51 |
|
---|
52 | private Color background = null;
|
---|
53 | private Color foreground = null;
|
---|
54 | private Color selection_background = null;
|
---|
55 | private Color selection_foreground = null;
|
---|
56 |
|
---|
57 | public GComboBox() {
|
---|
58 | super();
|
---|
59 | init();
|
---|
60 | }
|
---|
61 |
|
---|
62 | public GComboBox(ArrayList data) {
|
---|
63 | super(data.toArray());
|
---|
64 | init();
|
---|
65 | }
|
---|
66 |
|
---|
67 | public GComboBox(ComboBoxModel model) {
|
---|
68 | super(model);
|
---|
69 | init();
|
---|
70 | }
|
---|
71 |
|
---|
72 | public GComboBox(Object data[]) {
|
---|
73 | super(data);
|
---|
74 | init();
|
---|
75 | }
|
---|
76 |
|
---|
77 | public GComboBox(Vector data) {
|
---|
78 | super(data);
|
---|
79 | init();
|
---|
80 | }
|
---|
81 |
|
---|
82 | public int add(Object object) {
|
---|
83 | if(dataModel instanceof Model) {
|
---|
84 | return ((Model)dataModel).add(object);
|
---|
85 | }
|
---|
86 | else {
|
---|
87 | return -1;
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | public Object get(int index) {
|
---|
92 | return dataModel.getElementAt(index);
|
---|
93 | }
|
---|
94 |
|
---|
95 | public void clear() {
|
---|
96 | if(dataModel instanceof Model) {
|
---|
97 | ((Model)dataModel).clear();
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | public int count() {
|
---|
102 | return dataModel.getSize();
|
---|
103 | }
|
---|
104 |
|
---|
105 | public void setBackgroundNonSelectionColor(Color background) {
|
---|
106 | this.background = background;
|
---|
107 | setEditor(new Editor());
|
---|
108 | setRenderer(new Renderer());
|
---|
109 | }
|
---|
110 |
|
---|
111 | public void setTextNonSelectionColor(Color foreground) {
|
---|
112 | this.foreground = foreground;
|
---|
113 | setEditor(new Editor());
|
---|
114 | setRenderer(new Renderer());
|
---|
115 | }
|
---|
116 |
|
---|
117 | public void setBackgroundSelectionColor(Color selection_background) {
|
---|
118 | this.selection_background = selection_background;
|
---|
119 | setEditor(new Editor());
|
---|
120 | setRenderer(new Renderer());
|
---|
121 | }
|
---|
122 |
|
---|
123 | public void setTextSelectionColor(Color selection_foreground) {
|
---|
124 | this.selection_foreground = selection_foreground;
|
---|
125 | setEditor(new Editor());
|
---|
126 | setRenderer(new Renderer());
|
---|
127 | }
|
---|
128 |
|
---|
129 | public void init() {
|
---|
130 | Model model = new Model();
|
---|
131 | ComboBoxModel old_model = (ComboBoxModel) getModel();
|
---|
132 | setModel(model);
|
---|
133 | // Restore any data given into our model
|
---|
134 | for(int i = 0; i < old_model.getSize(); i++) {
|
---|
135 | model.add(old_model.getElementAt(i));
|
---|
136 | }
|
---|
137 | // Change component
|
---|
138 | UI ui = new UI();
|
---|
139 | setUI(ui);
|
---|
140 | ui.setButtonBackground();
|
---|
141 | // Initialization
|
---|
142 | this.background = Gatherer.config.getColor("coloring.collection_tree_background", false);
|
---|
143 | this.foreground = Gatherer.config.getColor("coloring.collection_tree_foreground", false);
|
---|
144 | this.selection_background = Gatherer.config.getColor("coloring.collection_selection_background", false);
|
---|
145 | this.selection_foreground = Gatherer.config.getColor("coloring.collection_selection_foreground", false);
|
---|
146 | setEditor(new Editor());
|
---|
147 | setRenderer(new Renderer());
|
---|
148 | }
|
---|
149 |
|
---|
150 | private class Editor
|
---|
151 | extends JTextField
|
---|
152 | implements ComboBoxEditor {
|
---|
153 | public Editor() {
|
---|
154 | setBackground(background);
|
---|
155 | setForeground(foreground);
|
---|
156 | setSelectionColor(selection_background);
|
---|
157 | setSelectedTextColor(selection_foreground);
|
---|
158 | }
|
---|
159 |
|
---|
160 | public Component getEditorComponent() {
|
---|
161 | return this;
|
---|
162 | }
|
---|
163 |
|
---|
164 | public Object getItem() {
|
---|
165 | return getText();
|
---|
166 | }
|
---|
167 |
|
---|
168 | public void setItem(Object item) {
|
---|
169 | if(item != null) {
|
---|
170 | setText(item.toString());
|
---|
171 | }
|
---|
172 | else {
|
---|
173 | setText("");
|
---|
174 | }
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | private class Model
|
---|
179 | extends DefaultComboBoxModel {
|
---|
180 |
|
---|
181 | public int add(Object extension) {
|
---|
182 | int position = 0;
|
---|
183 | String extension_str = extension.toString().toLowerCase();
|
---|
184 | while(extension != null && position < getSize()) {
|
---|
185 | String sibling = getElementAt(position).toString().toLowerCase();
|
---|
186 | int order = extension_str.compareTo(sibling);
|
---|
187 | // If we are now less than the sibling, insert here.
|
---|
188 | if(order < 0) {
|
---|
189 | insertElementAt(extension, position);
|
---|
190 | extension = null;
|
---|
191 | }
|
---|
192 | // We are equal to the sibling, thus we already exist in list. Done.
|
---|
193 | else if(order == 0) {
|
---|
194 | extension = null;
|
---|
195 | }
|
---|
196 | // Continue searching.
|
---|
197 | else {
|
---|
198 | position++;
|
---|
199 | }
|
---|
200 | }
|
---|
201 | if(extension != null) {
|
---|
202 | position = getSize();
|
---|
203 | addElement(extension);
|
---|
204 | }
|
---|
205 | return position;
|
---|
206 | }
|
---|
207 |
|
---|
208 | public void clear() {
|
---|
209 | removeAllElements();
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | private class Renderer
|
---|
214 | extends JLabel
|
---|
215 | implements ListCellRenderer {
|
---|
216 | public Renderer() {
|
---|
217 | }
|
---|
218 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
|
---|
219 | if(value != null) {
|
---|
220 | setText(value.toString());
|
---|
221 | }
|
---|
222 | else {
|
---|
223 | setText("");
|
---|
224 | }
|
---|
225 | setBackground(isSelected ? selection_background : background);
|
---|
226 | setForeground(isSelected ? selection_foreground : foreground);
|
---|
227 | setOpaque(true);
|
---|
228 | return this;
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | private class UI
|
---|
233 | extends BasicComboBoxUI {
|
---|
234 | public UI() {
|
---|
235 | super();
|
---|
236 | }
|
---|
237 |
|
---|
238 | protected ComboPopup createPopup() {
|
---|
239 | BasicComboPopup popup = new BasicComboPopup( comboBox ) {
|
---|
240 | public void show() {
|
---|
241 | if(comboBox.getMaximumRowCount() > 0) {
|
---|
242 | Dimension popupSize = new Dimension( comboBox.getWidth(), getPopupHeightForRowCount( comboBox.getMaximumRowCount()));
|
---|
243 | Rectangle popupBounds = new Rectangle( 0, 0, popupSize.width, popupSize.height);
|
---|
244 | scroller.setMaximumSize( popupBounds.getSize() );
|
---|
245 | scroller.setPreferredSize( popupBounds.getSize() );
|
---|
246 | scroller.setMinimumSize( popupBounds.getSize() );
|
---|
247 | list.invalidate();
|
---|
248 | int selectedIndex = comboBox.getSelectedIndex();
|
---|
249 | if ( selectedIndex == -1 ) {
|
---|
250 | list.clearSelection();
|
---|
251 | }
|
---|
252 | else {
|
---|
253 | list.setSelectedIndex( selectedIndex );
|
---|
254 | }
|
---|
255 | list.ensureIndexIsVisible( list.getSelectedIndex() );
|
---|
256 | setLightWeightPopupEnabled( comboBox.isLightWeightPopupEnabled() );
|
---|
257 | show(arrowButton, arrowButton.getSize().width - (popupSize.width + 4), arrowButton.getSize().height);
|
---|
258 | }
|
---|
259 | }
|
---|
260 | };
|
---|
261 | popup.getAccessibleContext().setAccessibleParent(comboBox);
|
---|
262 | return popup;
|
---|
263 | }
|
---|
264 |
|
---|
265 | public void setButtonBackground() {
|
---|
266 | arrowButton.setBackground(Gatherer.config.getColor("coloring.button_background", false));
|
---|
267 | }
|
---|
268 | }
|
---|
269 | }
|
---|