|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
TitledPickList.java | 38.9% | 73.5% | 76.5% | 66.7% |
|
1 |
/*
|
|
2 |
* SimplyHTML, a word processor based on Java, HTML and CSS
|
|
3 |
* Copyright (C) 2002 Ulrich Hilger
|
|
4 |
*
|
|
5 |
* This program is free software; you can redistribute it and/or
|
|
6 |
* modify it under the terms of the GNU General Public License
|
|
7 |
* as published by the Free Software Foundation; either version 2
|
|
8 |
* of the License, or (at your option) any later version.
|
|
9 |
*
|
|
10 |
* This program is distributed in the hope that it will be useful,
|
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
* GNU General Public License for more details.
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License
|
|
16 |
* along with this program; if not, write to the Free Software
|
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
18 |
*/
|
|
19 |
|
|
20 |
import javax.swing.event.ListSelectionListener;
|
|
21 |
import javax.swing.event.ListSelectionEvent;
|
|
22 |
import javax.swing.event.CaretListener;
|
|
23 |
import javax.swing.event.CaretEvent;
|
|
24 |
import java.awt.event.FocusListener;
|
|
25 |
import java.awt.event.FocusEvent;
|
|
26 |
import java.awt.event.KeyListener;
|
|
27 |
import java.awt.event.KeyEvent;
|
|
28 |
import javax.swing.*;
|
|
29 |
import java.awt.BorderLayout;
|
|
30 |
import java.util.Vector;
|
|
31 |
import java.util.EventObject;
|
|
32 |
import java.util.EventListener;
|
|
33 |
import java.util.Enumeration;
|
|
34 |
|
|
35 |
/**
|
|
36 |
* A pick list typically being used in font dialogs, consisting
|
|
37 |
* of a list title, a text field for the currently selected
|
|
38 |
* value and the actual pick list containing all possible values.
|
|
39 |
*
|
|
40 |
* As three different lists are needed in our font panel for
|
|
41 |
* family, style and size, its quite handy to have the code
|
|
42 |
* making up such a component in a separate class only once.
|
|
43 |
*
|
|
44 |
* As well in a separate class it is easier to implement the
|
|
45 |
* special 'behaviour', i.e. when list is clicked, the value
|
|
46 |
* of the text field is set accordingly and when a value is
|
|
47 |
* typed in the text field, the value in the list changes
|
|
48 |
* accordingly.
|
|
49 |
*
|
|
50 |
* @author Ulrich Hilger
|
|
51 |
* @author Light Development
|
|
52 |
* @author <a href="http://www.lightdev.com">http://www.lightdev.com</a>
|
|
53 |
* @author <a href="mailto:info@lightdev.com">info@lightdev.com</a>
|
|
54 |
* @author published under the terms and conditions of the
|
|
55 |
* GNU General Public License,
|
|
56 |
* for details see file gpl.txt in the distribution
|
|
57 |
* package of this software
|
|
58 |
*
|
|
59 |
* @version stage 11, April 27, 2003
|
|
60 |
*/
|
|
61 |
class TitledPickList extends JPanel |
|
62 |
implements ListSelectionListener, CaretListener, FocusListener, KeyListener
|
|
63 |
{ |
|
64 |
/** the chosen list entry */
|
|
65 |
private JTextField choice;
|
|
66 |
|
|
67 |
boolean ignoreTextChanges = false; |
|
68 |
|
|
69 |
/** the list having all possible entries */
|
|
70 |
private JList optionsList;
|
|
71 |
|
|
72 |
/**
|
|
73 |
* constructor
|
|
74 |
*
|
|
75 |
* @param options the options to be selectable in this list
|
|
76 |
* @param titleText the title for the pick list
|
|
77 |
*/
|
|
78 | 12 |
public TitledPickList(String[] options, String titleText) {
|
79 | 12 |
super(new BorderLayout()); |
80 |
|
|
81 | 12 |
choice = new JTextField();
|
82 | 12 |
choice.addCaretListener(this);
|
83 | 12 |
choice.addFocusListener(this);
|
84 | 12 |
choice.addKeyListener(this);
|
85 |
|
|
86 | 12 |
optionsList = new JList(options);
|
87 | 12 |
optionsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
88 | 12 |
optionsList.addListSelectionListener(this);
|
89 |
|
|
90 | 12 |
JScrollPane scrollableList = new JScrollPane(optionsList);
|
91 |
|
|
92 | 12 |
JPanel pickListPanel = new JPanel(new BorderLayout()); |
93 | 12 |
pickListPanel.add(choice, BorderLayout.NORTH); |
94 | 12 |
pickListPanel.add(scrollableList, BorderLayout.CENTER); |
95 |
|
|
96 | 12 |
JLabel title = new JLabel();
|
97 | 12 |
title.setText(titleText); |
98 |
|
|
99 | 12 |
add(title, BorderLayout.NORTH); |
100 | 12 |
add(pickListPanel, BorderLayout.CENTER); |
101 |
} |
|
102 |
|
|
103 |
/**
|
|
104 |
* if the caret was updated, i.e. the user typed into
|
|
105 |
* the text field, try to find a font family name starting
|
|
106 |
* with what was typed so far, set the list to that family
|
|
107 |
*/
|
|
108 | 8 |
public void caretUpdate(CaretEvent ce) { |
109 | 8 |
if(!ignoreTextChanges) {
|
110 | 8 |
if(choice.hasFocus()) {
|
111 | 0 |
ListModel model = optionsList.getModel(); |
112 | 0 |
String key = choice.getText().toLowerCase(); |
113 | 0 |
if(key != null) { |
114 | 0 |
int i = 0;
|
115 | 0 |
int modelSize = model.getSize();
|
116 | 0 |
String listEntry = (String) model.getElementAt(i); |
117 | 0 |
while(++i < modelSize && !listEntry.toLowerCase().startsWith(key)) {
|
118 | 0 |
listEntry = (String) model.getElementAt(i); |
119 |
} |
|
120 | 0 |
if(i < modelSize) {
|
121 | 0 |
optionsList.setSelectedValue(listEntry, true);
|
122 |
} |
|
123 |
} |
|
124 |
} |
|
125 |
} |
|
126 |
} |
|
127 |
|
|
128 |
/** for FocusListener implementation, but unused here */
|
|
129 | 2 |
public void focusGained(FocusEvent e) { |
130 |
} |
|
131 |
|
|
132 |
/**
|
|
133 |
* put the currently selected value in the list
|
|
134 |
* into the text field when user leaves field
|
|
135 |
*/
|
|
136 | 2 |
public void focusLost(FocusEvent e) { |
137 | 2 |
updateTextFromList(); |
138 |
} |
|
139 |
|
|
140 |
/**
|
|
141 |
* put the currently selected value in the list
|
|
142 |
* into the text field when user pressed enter in field
|
|
143 |
*/
|
|
144 | 0 |
public void keyPressed(KeyEvent e) { |
145 | 0 |
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
|
146 | 0 |
updateTextFromList(); |
147 |
} |
|
148 |
} |
|
149 |
|
|
150 |
/** for KeyListener implementation, but unused here */
|
|
151 | 0 |
public void keyReleased(KeyEvent e) { |
152 |
} |
|
153 |
|
|
154 |
/** for KeyListener implementation, but unused here */
|
|
155 | 0 |
public void keyTyped(KeyEvent e) { |
156 |
} |
|
157 |
|
|
158 |
/** put selected value into text field */
|
|
159 | 16 |
private void updateTextFromList() { |
160 | 16 |
Object value = optionsList.getSelectedValue(); |
161 | 16 |
if(value != null) { |
162 | 16 |
choice.setText(value.toString()); |
163 |
} |
|
164 |
} |
|
165 |
|
|
166 |
/**
|
|
167 |
* get the value selected from the pick list
|
|
168 |
*
|
|
169 |
* @return the selected value or null if nothing is selected
|
|
170 |
*/
|
|
171 | 20 |
public Object getSelection() {
|
172 | 20 |
return optionsList.getSelectedValue();
|
173 |
} |
|
174 |
|
|
175 |
/**
|
|
176 |
* set the value selected in the pick list
|
|
177 |
*
|
|
178 |
* @param value the value to be selected in the list
|
|
179 |
*/
|
|
180 | 8 |
public void setSelection(Object value) { |
181 | 8 |
optionsList.setSelectedValue(value.toString(), true);
|
182 | 8 |
updateTextFromList(); |
183 |
} |
|
184 |
|
|
185 |
/**
|
|
186 |
* set the selected index in the pick list
|
|
187 |
*
|
|
188 |
* @param index the index of the value to be selected in the list
|
|
189 |
*/
|
|
190 | 4 |
public void setSelection(int index) { |
191 | 4 |
optionsList.setSelectedIndex(index); |
192 | 4 |
updateTextFromList(); |
193 |
} |
|
194 |
|
|
195 |
/**
|
|
196 |
* get the index of the value selected in the pick list
|
|
197 |
*
|
|
198 |
* @return the index of the selected value or -1 if none is selected
|
|
199 |
*/
|
|
200 | 7 |
public int getIndex() { |
201 | 7 |
return optionsList.getSelectedIndex();
|
202 |
} |
|
203 |
|
|
204 |
/**
|
|
205 |
* if another value was picked from the list, set the
|
|
206 |
* textfield according to the choice and update the
|
|
207 |
* sample
|
|
208 |
*/
|
|
209 | 16 |
public void valueChanged(ListSelectionEvent e) { |
210 | 16 |
if(optionsList.hasFocus()) {
|
211 | 2 |
updateTextFromList(); |
212 |
} |
|
213 | 16 |
fireValueChanged(); |
214 |
} |
|
215 |
|
|
216 |
/* ------------- event handling start ------------ */
|
|
217 |
|
|
218 |
/** the listeners for TitledPickkListEvents */
|
|
219 |
private Vector listeners = new Vector(0); |
|
220 |
|
|
221 |
/**
|
|
222 |
* add an event listener.
|
|
223 |
*
|
|
224 |
* @param listener the event listener to add
|
|
225 |
*/
|
|
226 | 12 |
public void addTitledPickListListener(TitledPickListListener listener) { |
227 | 12 |
listeners.addElement(listener); |
228 |
} |
|
229 |
|
|
230 |
/**
|
|
231 |
* remove an event listener.
|
|
232 |
*
|
|
233 |
* @param listener the event listener to remove
|
|
234 |
*/
|
|
235 | 0 |
public void removeTitledPickListListener(TitledPickListListener listener) { |
236 | 0 |
listeners.removeElement(listener); |
237 |
} |
|
238 |
|
|
239 |
/** fire a value changed event to all registered listeners */
|
|
240 | 16 |
void fireValueChanged() {
|
241 | 16 |
Enumeration listenerList = listeners.elements(); |
242 | 16 |
while(listenerList.hasMoreElements()) {
|
243 | 16 |
((TitledPickListListener) listenerList.nextElement()).valueChanged( |
244 |
new TitledPickListEvent(this)); |
|
245 |
} |
|
246 |
} |
|
247 |
|
|
248 |
/** the event object definition for ColorPanels */
|
|
249 |
class TitledPickListEvent extends EventObject { |
|
250 | 16 |
public TitledPickListEvent(Object source) {
|
251 | 16 |
super(source);
|
252 |
} |
|
253 |
} |
|
254 |
|
|
255 |
/** the event listener definition for ColorPanels */
|
|
256 |
interface TitledPickListListener extends EventListener { |
|
257 |
public void valueChanged(TitledPickListEvent e); |
|
258 |
} |
|
259 |
|
|
260 |
/* ------------- event handling end ------------ */
|
|
261 |
|
|
262 |
} |
|