source: trunk/gli/src/org/greenstone/gatherer/gui/ComboArea.java@ 6394

Last change on this file since 6394 was 5589, checked in by mdewsnip, 21 years ago

Nearly finished adding tooltips (and thank goodness for that).

  • Property svn:keywords set to Author Date Id Revision
File size: 8.9 KB
Line 
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 */
37package org.greenstone.gatherer.gui;
38
39import java.awt.*;
40import java.awt.event.*;
41import javax.swing.*;
42import javax.swing.text.*;
43import jp.gr.java_conf.tame.swing.combobox.*;
44import org.greenstone.gatherer.Gatherer;
45import org.greenstone.gatherer.gui.SmarterTextArea;
46import org.greenstone.gatherer.util.Utility;
47
48/** Combines the idea of a combobox with a text area so that multiline entries can be displayed properly. Basically takes a JCombobox, (or in this case a SteppedComboBox that allows us to control the popup window that appears), butchers out the JTextField editor while retaining the button and the data model, then uses a JTextArea for the editor instead.
49 * @author John Thompson, Greenstone Digital Library, University of Waikato
50 * @version 2.3
51 */
52public class ComboArea
53 extends JPanel {
54 /** Should the listener ignore any change events fired (most likely as a result of the listener in the first place). */
55 private boolean ignore = false;
56 /** The combobox we are going to utilize. */
57 private SteppedComboBox combobox = null;
58 /** The text area involved. Not quite a JTextArea, the smarter part is that is allows coloring contrary to the UIManager settings. */
59 private SmarterTextArea text_area = null;
60 /** Create a new ComboArea demarked by a particular label. The label is given here so that it fits nicely with the text area and button.
61 * @param text The label text to be shown above text area as a <strong>String</strong>.
62 * @param label_size The <strong>Dimension</strong> to use for the label.
63 * @see jp.gr.java_conf.tame.swing.combobox.SteppedComboBox
64 * @see org.greenstone.gatherer.gui.ComboArea.ComboAreaListener
65 * @see org.greenstone.gatherer.gui.ComboArea.Renderer
66 */
67 public ComboArea(String text, Dimension label_size) {
68 // Creation
69 JPanel title_pane = new JPanel();
70 title_pane.setOpaque(false);
71 JLabel label = new JLabel(text);
72 label.setOpaque(false);
73 label.setPreferredSize(label_size);
74 JPanel text_pane = new JPanel();
75 text_area = new SmarterTextArea();
76 text_area.setRows(3);
77 JPanel button_pane = new JPanel();
78 Dimension button_size = new Dimension(label_size.height, label_size.height);
79 button_pane.setMaximumSize(button_size);
80 button_pane.setMinimumSize(button_size);
81 button_pane.setPreferredSize(button_size);
82 button_pane.setSize(button_size);
83 combobox = new SteppedComboBox(Gatherer.self, button_size);
84 combobox.setDependant(text_area);
85 combobox.setPreferredSize(label_size);
86 combobox.setRenderer(new Renderer());
87 // Connection
88 combobox.addActionListener(new ComboAreaListener());
89 // Layout
90 title_pane.setLayout(new BorderLayout());
91 title_pane.add(label, BorderLayout.WEST);
92
93 button_pane.setLayout(new BorderLayout());
94 button_pane.add(combobox.getButton(), BorderLayout.CENTER);
95
96 text_pane.setLayout(new BorderLayout());
97 text_pane.add(new JScrollPane(text_area), BorderLayout.CENTER);
98 text_pane.add(button_pane, BorderLayout.EAST);
99
100 setLayout(new BorderLayout());
101 add(title_pane, BorderLayout.NORTH);
102 add(text_pane, BorderLayout.CENTER);
103 }
104
105 /** Add an element to our combobox model. Remember to tell the listeners to ignore this action unless you want an infinite loop.
106 * @param item The <strong>Object</strong> to add.
107 */
108 public void add(Object item) {
109 ignore = true;
110 combobox.add(item.toString());
111 ignore = false;
112 }
113
114 /** Clear the combobox model.
115 */
116 public void clear() {
117 combobox.clear();
118 }
119
120 /** Retrieve the text currently displayed in the text area.
121 * @return A <strong>String</strong> containing the required text.
122 */
123 public String getText() {
124 return text_area.getText();
125 }
126
127 /** Retrieve a reference to the text area.
128 * @return A <strong>JTextComponent</strong> reference.
129 */
130 public JTextComponent getTextComponent() {
131 return text_area;
132 }
133
134 /** Change the selected item in the combobox to the one given. If such an object doesn't exist in the model, ignore it. Notice that this time we don't ignore, as we want a selection to update the text field.
135 * @param item The <strong>Object</strong> that we want to select from the combobox.
136 */
137 public void setSelectedItem(Object item) {
138 combobox.setSelectedItem(item);
139 }
140
141 /** Set the text shown in the text area to the value provided. Note that this has no effect on the combobox, nor will this value be stored in the model.
142 * @param text The <strong>String</strong> to display.
143 */
144 public void setText(String text) {
145 text_area.setText(text);
146 }
147
148 /** This clas listens for changes in the selected item in the combobox and if applicable updates the text area to match. */
149 private class ComboAreaListener
150 implements ActionListener {
151 /** Whenever a new object is selected from the combobox list, determine if this is an appropriate time to update the text area and if so do so.
152 * @param event An <strong>ActionEvent</strong> encompassing all the relevant data about an action having been performed.
153 */
154 public void actionPerformed(ActionEvent event) {
155 Object object = combobox.getSelectedItem();
156 if(object != null && !ignore) {
157 text_area.setText(object.toString());
158 }
159 }
160 }
161 /** This renderer is used to ensure that the labels shown in the combobox drop-down list are both correct in colouring and legible. The later is important as the entry itself may actually span several lines. */
162 private class Renderer
163 extends DefaultListCellRenderer {
164 /** Retrieve the component used to 'rubberstamp' the desired entry in the combobox.
165 * @param list The <strong>JList</strong> this component will be renderer in.
166 * @param value The <strong>Object</strong> whose value should be represented by the component returned.
167 * @param index An <i>int</i> giving the objects index in the list of objects.
168 * @param isSelected <i>true</i> if this object is currently selected, <i>false</i> otherwise.
169 * @param cellHasFocus <i>true</i> if this object is currently in focus, <i>false</i> otherwise.
170 * @see org.greenstone.gatherer.util.Utility
171 */
172 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
173 JLabel temp = null;
174 if(value != null) {
175 // Only show a single line by removing new lines.
176 StringBuffer text = new StringBuffer(Utility.stripNL((String)value));
177 // Generate prototype label
178 temp = (JLabel) super.getListCellRendererComponent(list, text.toString(), index, isSelected, cellHasFocus);
179 // Trim down the value string so it fits nicely. Start by shortening it to the number of characters determined from the smarter text area.
180 int preferred_characters = text_area.calculateColumnCount();
181 if(preferred_characters > 3 && text.length() > preferred_characters) {
182 text.delete(preferred_characters - 3, text.length());
183 text.append("...");
184 }
185 temp.setText(text.toString());
186 // If we are still too long, shorten by one character at a time until it fits
187 while(temp.getPreferredSize().width > text_area.getSize().width && text.length() > 4) {
188 // Remove the last four characters and mark as being chopped
189 text.delete(text.length() - 4, text.length());
190 text.append("...");
191 // Assign new string, rinse and repeat.
192 temp.setText(text.toString());
193 }
194 }
195 else {
196 temp = (JLabel) super.getListCellRendererComponent(list, "", index, isSelected, cellHasFocus);
197 }
198 // Colouring should be the same as the dependant we are based on.
199 if(isSelected) {
200 temp.setBackground(text_area.getSelectionColor());
201 temp.setForeground(text_area.getSelectedTextColor());
202 }
203 else {
204 temp.setBackground(text_area.getBackground());
205 temp.setForeground(text_area.getForeground());
206 }
207 return temp;
208 }
209 }
210}
Note: See TracBrowser for help on using the repository browser.