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