source: trunk/gli/src/org/greenstone/gatherer/gems/ComboArea.java@ 8978

Last change on this file since 8978 was 8978, checked in by mdewsnip, 19 years ago

Replaced all "dependant" with "dependent" (mgw5).

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