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

Last change on this file since 4448 was 4367, checked in by mdewsnip, 21 years ago

Fixed tabbing.

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