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