root / trunk / libraries / libUI / src / org / gvsig / gui / beans / comboBoxItemsSeeker / JComboBoxItemsSeekerConfigurable.java @ 6754
History | View | Annotate | Download (60.9 KB)
1 |
package org.gvsig.gui.beans.comboBoxItemsSeeker; |
---|---|
2 |
|
3 |
import java.awt.Color; |
4 |
import java.awt.event.ActionEvent; |
5 |
import java.awt.event.FocusAdapter; |
6 |
import java.awt.event.FocusEvent; |
7 |
import java.awt.event.FocusListener; |
8 |
import java.awt.event.KeyAdapter; |
9 |
import java.awt.event.KeyEvent; |
10 |
import java.awt.event.KeyListener; |
11 |
import java.beans.PropertyChangeEvent; |
12 |
import java.beans.PropertyChangeListener; |
13 |
|
14 |
import javax.swing.ComboBoxEditor; |
15 |
import javax.swing.ComboBoxModel; |
16 |
import javax.swing.JComboBox; |
17 |
import javax.swing.JOptionPane; |
18 |
import javax.swing.event.DocumentEvent; |
19 |
import javax.swing.event.DocumentListener; |
20 |
import javax.swing.event.ListDataEvent; |
21 |
import javax.swing.event.ListDataListener; |
22 |
import javax.swing.event.PopupMenuEvent; |
23 |
import javax.swing.event.PopupMenuListener; |
24 |
import javax.swing.text.AttributeSet; |
25 |
import javax.swing.text.BadLocationException; |
26 |
import javax.swing.text.JTextComponent; |
27 |
import javax.swing.text.PlainDocument; |
28 |
|
29 |
import org.apache.log4j.Logger; |
30 |
|
31 |
|
32 |
import com.iver.andami.PluginServices; |
33 |
|
34 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
35 |
*
|
36 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
37 |
*
|
38 |
* This program is free software; you can redistribute it and/or
|
39 |
* modify it under the terms of the GNU General Public License
|
40 |
* as published by the Free Software Foundation; either version 2
|
41 |
* of the License, or (at your option) any later version.
|
42 |
*
|
43 |
* This program is distributed in the hope that it will be useful,
|
44 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
45 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
46 |
* GNU General Public License for more details.
|
47 |
*
|
48 |
* You should have received a copy of the GNU General Public License
|
49 |
* along with this program; if not, write to the Free Software
|
50 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
51 |
*
|
52 |
* For more information, contact:
|
53 |
*
|
54 |
* Generalitat Valenciana
|
55 |
* Conselleria d'Infraestructures i Transport
|
56 |
* Av. Blasco Ib??ez, 50
|
57 |
* 46010 VALENCIA
|
58 |
* SPAIN
|
59 |
*
|
60 |
* +34 963862235
|
61 |
* gvsig@gva.es
|
62 |
* www.gvsig.gva.es
|
63 |
*
|
64 |
* or
|
65 |
*
|
66 |
* IVER T.I. S.A
|
67 |
* Salamanca 50
|
68 |
* 46005 Valencia
|
69 |
* Spain
|
70 |
*
|
71 |
* +34 963163400
|
72 |
* dac@iver.es
|
73 |
*/
|
74 |
|
75 |
/** VERSI?N EN DESARROLLO
|
76 |
* REVISAR COMENTARIOS Y C?DIGO QUE PUEDA SOBRAR
|
77 |
* This class allows users to insert a chain of characters and show all the sentences of the JComboBox that match its first ones characters with it.
|
78 |
* This class is also a Java Bean, and is made according the MVC (Model - View - Controller) pattern.
|
79 |
* This class has the View, the Control and can access to the Model.
|
80 |
* The behavior of this componet can be configurated by 7 flags: (Esto lo cambio!!!!!)
|
81 |
* + The appearence of the items showed at the beginning and when the user presses the ESCAPE key: (This is configured with 2 flags -> 3 different states are possible)
|
82 |
* -> maintainPositionItems -> When we see the list of all items, this are showed in the ordenation as they have been introduced (if true); (if false) each new item
|
83 |
* when we write on the TextField of this Component will be showed at the end (esto ?ltimo creo q. no -> revisarlo)
|
84 |
* -> all_Alphanumeric_Sorted -> All items we can see in this Component will be showed in alphanumeric ordenation (if true)
|
85 |
* + The list of the items showed when the user makes a search writting or pressing the BACK-SPACE key (This is configured with 1 flag -> 2 different states are possible)
|
86 |
* -> alphanumericSortedSearches -> When we write on the TextField of this Component all results (items) are showed in alphanumeric ordenation (if true)
|
87 |
* + Key Sensitive or not when the user writes
|
88 |
* -> key_Sensitive -> When we write on the TextField it can discriminate upper cases from down cases (if true) or not (if false) (This is configured with 1 flag -> 2 different states are possible)
|
89 |
* + Show all items always
|
90 |
* -> showAllItems -> If true -> this component shows all items always; if false -> this component only shows items that their first characters match with the string written by the user
|
91 |
* + Use one color (black) or 2 colors (black by default and red if the string written doesn't match with the beginning of all items)
|
92 |
* -> only_One_Color -> The text on the textField only will be showed on black color (if true); false -> if the text on the textField doesn't match with any current item of this component -> the text
|
93 |
* will be showed on red color
|
94 |
* + * @param boolean (completeMatchedItem (explicar))
|
95 |
* + @param boolean (useBeep (explicar))
|
96 |
*
|
97 |
* Default Values of the Flags:
|
98 |
* -> maintainPositionItems -> true
|
99 |
* -> all_Alphanumeric_Sorted -> true
|
100 |
* -> alphanumericSortedSearches -> false
|
101 |
* -> key_Sensitive -> false
|
102 |
* -> showAllItems -> false
|
103 |
* -> only_One_Color -> false
|
104 |
*
|
105 |
* The flags are distributed in two classes:
|
106 |
* -> ComboBoxSearcheableDynamicModel -> maintainPositionItems, all_Alphanumeric_Sorted, alphanumericSortedSearches, key_Sensitive, showAllItems
|
107 |
* -> JComboBoxSearcheableDynamic -> only_One_Color
|
108 |
*
|
109 |
* Combinations of flags not allowed:
|
110 |
* -> maintainPositionItems == allAlphanumericSorted == true
|
111 |
* -> ((maintain_Position_Items == false) && (all_Alphanumeric_Sorted == true) && (alphanumeric_Sorted_Searches == false))
|
112 |
* -> showAllItems == true && maintainPositionItems == false (Creo que esta mal)
|
113 |
*
|
114 |
* Limitations:
|
115 |
* -> When we add more than one item with the same name (string value) -> the behavior of this component only considers one item in most of the cases when it does a search
|
116 |
* -> If this component has Hundreds,Thousands or even Million/s of items is probably that had quite delay to respond the evens of the user; this also depends of the machine where it's executed and other factors
|
117 |
*
|
118 |
* @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
|
119 |
*/
|
120 |
public class JComboBoxItemsSeekerConfigurable extends JComboBox implements java.io.Serializable { |
121 |
//private static final long serialVersionUID = -1853812970336818959L;
|
122 |
// PONER SERIALIZACI?N
|
123 |
|
124 |
// eSTAS HAY QUE BORRARLAS!
|
125 |
/* private Vector defaultItems;
|
126 |
private Set notRemovedItems;
|
127 |
private Set removedItems;*/
|
128 |
|
129 |
private final int BEGINNING_VIEW_STATE = 0; |
130 |
private final int START_VIEW_STATE = 1; |
131 |
private final int SEARCH_VIEW_STATE = 2; |
132 |
private int currentShowState; // 0 -> at the beginning; 1 -> when is showing the start list of items; 2 -> when is showing a search list of items |
133 |
|
134 |
|
135 |
// CONFIGURATION FLAGS
|
136 |
private boolean onlyOneColor; |
137 |
private boolean completeMatchedItem; |
138 |
private boolean beepEnabled; |
139 |
// END FLAGS
|
140 |
|
141 |
// PUEDE QUE TB. sOBren
|
142 |
// private boolean allItemsAreSorted;
|
143 |
|
144 |
// Last selected item
|
145 |
private Object selectedItem; |
146 |
private Object lastSelectedItem; // This is used for only update (refresh) de popup when a different item is selected |
147 |
|
148 |
|
149 |
// pueDE QUE TB. SOBRE
|
150 |
private int num = 0; |
151 |
// private JTextField txtField;
|
152 |
|
153 |
private final int POPUP_LIST_ITEMS_HEIGHT = 8; |
154 |
// Reference to the ui
|
155 |
//private MetalComboBoxItemsSeekerUI ui;
|
156 |
|
157 |
// Reference to the model
|
158 |
private ComboBoxItemsSeekerConfigurableModel model;
|
159 |
|
160 |
// Some listeners for the Control between the View and de Model
|
161 |
private KeyListener editorKeyListener; |
162 |
private FocusListener editorFocusListener; |
163 |
private DocumentListener documentListener; |
164 |
private ListDataListener modelListDataListener; |
165 |
private PopupMenuListener popupMenuListener; |
166 |
|
167 |
// Reference to the document of the Editor of this component for optimize the seeks
|
168 |
private PlainDocumentSeeker document;
|
169 |
|
170 |
// private String writtenString;
|
171 |
|
172 |
private boolean continueOnStartViewState; |
173 |
// private boolean specialKeys; // ESC and TAB keys
|
174 |
private boolean textKeyPressed; |
175 |
private boolean lastWasTheBeginningState; |
176 |
// private boolean keyPressed;
|
177 |
// private boolean updatePopUp;
|
178 |
// private boolean upOrDownKeyPressed;
|
179 |
// private boolean deleteKeyPressed;
|
180 |
// private boolean escapeKeyPressed;
|
181 |
|
182 |
// OTRAS FLAGS que puede que sobren alguNAS
|
183 |
private boolean selecting=false; |
184 |
private boolean hidePopupOnFocusLoss; |
185 |
private boolean hitBackspace=false; |
186 |
private boolean hitBackspaceOnSelection; |
187 |
|
188 |
// TRAZA - DEBUG
|
189 |
private Logger logger = Logger.getLogger(JComboBoxItemsSeekerConfigurable.class.getClass()); |
190 |
|
191 |
|
192 |
/**
|
193 |
* Default Constructor
|
194 |
*/
|
195 |
public JComboBoxItemsSeekerConfigurable() {
|
196 |
// Invokes to the parent class constructor
|
197 |
super();
|
198 |
|
199 |
// Create attributes and set initial values
|
200 |
this.initialize();
|
201 |
|
202 |
// Set the default values of the flags
|
203 |
this.onlyOneColor = false; |
204 |
this.completeMatchedItem = false; //true; |
205 |
this.beepEnabled = false; //true; |
206 |
|
207 |
// Other configuration tasks
|
208 |
this.createDefaultListeners();
|
209 |
this.configure();
|
210 |
//this.configure();
|
211 |
} |
212 |
|
213 |
/**
|
214 |
* Default Constructor with four parameters: configure the 4 flags
|
215 |
*
|
216 |
* @param boolean (true -> maintains the position of the items; false -> don't maintains the position of the items)
|
217 |
* @param boolean (true -> all items showed when we write on the textfield will be showed in alphanumeric ordenation; false -> they don't have to be in alphanumeric ordenation)
|
218 |
* @param boolean (true -> all items showed will be in alphanumeric ordenation; false -> they don't have to be in alphanumeric ordenation)
|
219 |
* @param boolean (true -> discriminates capital letters from small letters; false -> don't discriminates capital letters from small letters)
|
220 |
*/
|
221 |
public JComboBoxItemsSeekerConfigurable(int start_Behavior, int search_Behavior, boolean key_Sensitive) { |
222 |
// Invokes to the parent class constructor
|
223 |
super();
|
224 |
|
225 |
// Create attributes and set initial values
|
226 |
this.initialize();
|
227 |
|
228 |
// Sets the options selected by the user
|
229 |
// this.model.setMaintainPositionItems(maintain_Position_Items);
|
230 |
// this.model.setAllAlphanumericSorted(all_Alphanumeric_Sorted);
|
231 |
// this.model.setAlphanumericSortedSearches(alphanumeric_Sorted_Searches);
|
232 |
this.model.setStartBehavior(start_Behavior);
|
233 |
this.model.setStartBehavior(search_Behavior);
|
234 |
this.model.setKeySensitive(key_Sensitive);
|
235 |
// this.model.setShowAllItems(false);
|
236 |
this.onlyOneColor = false; |
237 |
this.completeMatchedItem = false; //true; |
238 |
this.beepEnabled = true; |
239 |
|
240 |
if (!testFlagsConfigurationOK())
|
241 |
JOptionPane.showMessageDialog(this, PluginServices.getText(this, "errorMessageJComboBoxItemsSeekerConfigurable"), PluginServices.getText(this, "exportJOP2Title"), JOptionPane.ERROR_MESSAGE); |
242 |
else
|
243 |
this.configure();
|
244 |
} |
245 |
|
246 |
/**
|
247 |
* Default Constructor with six parameters: configure the 6 flags
|
248 |
*
|
249 |
* @param boolean (true -> maintains the position of the items; false -> don't maintains the position of the items)
|
250 |
* @param boolean (true -> all items showed when we write on the textfield will be showed in alphanumeric ordenation; false -> they don't have to be in alphanumeric ordenation)
|
251 |
* @param boolean (true -> all items showed will be in alphanumeric ordenation; false -> they don't have to be in alphanumeric ordenation)
|
252 |
* @param boolean (true -> discriminates capital letters from small letters; false -> don't discriminates capital letters from small letters)
|
253 |
* @param boolean (true -> the text on the textField only will be showed on black color (if true); false -> if the text on the textField doesn't match with any current item of this component -> the text
|
254 |
* will be showed on red color)
|
255 |
* @param boolean (true -> this component shows all items always; false -> this component only shows items that their first characters match with the string written by the user)
|
256 |
* @param boolean (completeMatchedItem (explicar))
|
257 |
* @param boolean (beepEnabled (explicar))
|
258 |
*/
|
259 |
public JComboBoxItemsSeekerConfigurable(int start_Behavior, int search_Behavior, boolean key_Sensitive, boolean only_One_Color, boolean show_All_Items, boolean complete_Matched_Item, boolean use_Beep) { |
260 |
// Invokes to the parent class constructor
|
261 |
super();
|
262 |
|
263 |
// Create attributes and set initial values
|
264 |
this.initialize();
|
265 |
|
266 |
// Sets the options selected by the user
|
267 |
// this.model.setMaintainPositionItems(maintain_Position_Items);
|
268 |
// this.model.setAllAlphanumericSorted(all_Alphanumeric_Sorted);
|
269 |
// this.model.setAlphanumericSortedSearches(alphanumeric_Sorted_Searches);
|
270 |
this.model.setStartBehavior(start_Behavior);
|
271 |
this.model.setStartBehavior(search_Behavior);
|
272 |
this.model.setKeySensitive(key_Sensitive);
|
273 |
// this.model.setShowAllItems(show_All_Items);
|
274 |
this.onlyOneColor = only_One_Color;
|
275 |
this.completeMatchedItem = complete_Matched_Item;
|
276 |
this.beepEnabled = use_Beep;
|
277 |
|
278 |
if (!testFlagsConfigurationOK())
|
279 |
JOptionPane.showMessageDialog(this, PluginServices.getText(this, "errorMessageJComboBoxItemsSeekerConfigurable"), PluginServices.getText(this, "exportJOP2Title"), JOptionPane.ERROR_MESSAGE); |
280 |
else
|
281 |
this.configure();
|
282 |
} |
283 |
|
284 |
|
285 |
/**
|
286 |
*
|
287 |
*/
|
288 |
private void initialize() { |
289 |
// // Creates the ui for this component and gets it reference
|
290 |
// super.setUI(new MetalComboBoxItemsSeekerUI());
|
291 |
// this.ui = (MetalComboBoxItemsSeekerUI) super.getUI();
|
292 |
// super.setUI(new MetalComboBoxItemsSeekerUI());
|
293 |
|
294 |
// Creates the model for this component and gets it reference
|
295 |
super.setModel(new ComboBoxItemsSeekerConfigurableModel()); |
296 |
this.model = (ComboBoxItemsSeekerConfigurableModel) super.getModel(); |
297 |
|
298 |
// Allows user to edit on the combobox
|
299 |
super.setEditable(true); |
300 |
|
301 |
// By default the last state of showign items was showing all the items (at the beginning of this component)
|
302 |
currentShowState = BEGINNING_VIEW_STATE; |
303 |
|
304 |
// Set the last selected item -> if there isn't any item -> -1; else -> this will be 0
|
305 |
super.setSelectedIndex(-1); |
306 |
this.selectedItem = null; |
307 |
// logger.debug("selectedItem: " + this.getSelectedIndex());
|
308 |
|
309 |
// By default the keys Up and Down aren't pressed
|
310 |
// this.upOrDownKeyPressed = false;
|
311 |
|
312 |
// By default the keys Delete aren't pressed
|
313 |
// this.escapeKeyPressed = false;
|
314 |
|
315 |
// By default any text key has been pressed
|
316 |
textKeyPressed = false;
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
// // By default any key has been pressed
|
322 |
// keyPressed = false;
|
323 |
|
324 |
// specialKeys = false;
|
325 |
// updatePopUp = false;
|
326 |
|
327 |
// Remove the default mouse listener
|
328 |
// super.removeMouseListener(super.getMouseListeners()[0]);
|
329 |
} |
330 |
|
331 |
/**
|
332 |
*
|
333 |
*/
|
334 |
private void createDefaultListeners() { |
335 |
// TODO Auto-generated method stub
|
336 |
|
337 |
// Define the key listener for the editor of this component
|
338 |
this.defineEditorKeyListener(this); |
339 |
|
340 |
// Define the focus listener for the editor of this component
|
341 |
this.defineEditorFocusListener(this); |
342 |
|
343 |
// Define a document listener for changes of text
|
344 |
this.defineDocumentListener();
|
345 |
|
346 |
// Define the list data listener for the model of this component
|
347 |
this.defineModelListDataListener();
|
348 |
|
349 |
// Define a popup menu listener for the popup of this component
|
350 |
this.definePopUpMenuListener(this); |
351 |
} |
352 |
|
353 |
/**
|
354 |
* Define a document listener for changes of text
|
355 |
*/
|
356 |
private void defineDocumentListener() { |
357 |
// TODO Auto-generated method stub
|
358 |
documentListener = new DocumentListener() { |
359 |
|
360 |
public void changedUpdate(DocumentEvent arg0) { |
361 |
// TODO Auto-generated method stub
|
362 |
} |
363 |
|
364 |
public void insertUpdate(DocumentEvent arg0) { |
365 |
// TODO Auto-generated method stub
|
366 |
logger.debug("NUM: " + num + " DocumentListener->insertUpdate"); |
367 |
try {
|
368 |
logger.debug("Texto insertado: ->" + document.getText(0, document.getLength()) + "<-"); |
369 |
|
370 |
} catch (BadLocationException e) { |
371 |
// TODO Auto-generated catch block
|
372 |
e.printStackTrace(); |
373 |
} |
374 |
num++; |
375 |
|
376 |
// Indicate to the model that now is disabled the start view
|
377 |
// This is used for the first text inserted to the document when the component is loaded and it does setSelectedIndex(0), to avoid use a search view
|
378 |
if (currentShowState == BEGINNING_VIEW_STATE)
|
379 |
{ |
380 |
currentShowState = START_VIEW_STATE; |
381 |
lastWasTheBeginningState = true;
|
382 |
continueOnStartViewState = true;
|
383 |
logger.debug("Pasa al estado 1 (de vista inicial)");
|
384 |
} |
385 |
} |
386 |
|
387 |
public void removeUpdate(DocumentEvent arg0) { |
388 |
// TODO Auto-generated method stub
|
389 |
logger.debug("NUM: " + num + " DocumentListener->removeUpdate"); |
390 |
num++; |
391 |
} |
392 |
}; |
393 |
} |
394 |
|
395 |
private void updateLastSelectionItem() { |
396 |
setSelectedItem(selectedItem); |
397 |
} |
398 |
|
399 |
/**
|
400 |
*
|
401 |
*/
|
402 |
private void configure() { |
403 |
// TODO Auto-generated method stub
|
404 |
|
405 |
// Configure the document of the editor of this component
|
406 |
this.configureDocument();
|
407 |
|
408 |
// Configure the editor (ComboBoxEditor) of this component
|
409 |
this.configureEditor(this.getEditor()); |
410 |
|
411 |
// Configure the model of this component
|
412 |
this.configureModel();
|
413 |
|
414 |
// Configure the popup of this component
|
415 |
this.configurePopUp();
|
416 |
|
417 |
} |
418 |
|
419 |
/**
|
420 |
*
|
421 |
*
|
422 |
*/
|
423 |
private void configurePopUp() { |
424 |
// TODO Auto-generated method stub
|
425 |
this.addPopupMenuListener(this.popupMenuListener); |
426 |
} |
427 |
|
428 |
// /**
|
429 |
// *
|
430 |
// */
|
431 |
// private void uninstallDefaultListeners() {
|
432 |
//
|
433 |
//
|
434 |
// }
|
435 |
|
436 |
/**
|
437 |
*
|
438 |
*/
|
439 |
private void configureModel() { |
440 |
// TODO Auto-generated method stub
|
441 |
this.model.addListDataListener(this.modelListDataListener); |
442 |
} |
443 |
|
444 |
/**
|
445 |
*
|
446 |
*/
|
447 |
private void configureDocument() { |
448 |
// TODO Auto-generated method stub
|
449 |
|
450 |
// Creates the document of the editor of this component
|
451 |
document = new PlainDocumentSeeker();
|
452 |
document.setJComboBoxItemsSeekerDynamicReference(this);
|
453 |
|
454 |
// Set the written String
|
455 |
// writtenString = "";
|
456 |
|
457 |
// Remove all Previous document Listeners
|
458 |
// DocumentListener documentListener[] = document.getDocumentListeners();
|
459 |
// for (int i=0; i < documentListener.length; i++)
|
460 |
// document.removeDocumentListener(documentListener[i]);
|
461 |
|
462 |
// Defines and add a Document listener for changes on the document of the editor of this component
|
463 |
document.addDocumentListener(this.documentListener);
|
464 |
} |
465 |
|
466 |
|
467 |
/**
|
468 |
* Define the list data listener for the model of this component
|
469 |
*/
|
470 |
public void defineModelListDataListener() { |
471 |
|
472 |
this.modelListDataListener = new ListDataListener() { |
473 |
|
474 |
public void contentsChanged(ListDataEvent e) { |
475 |
// TODO Auto-generated method stub
|
476 |
// logger.debug("ContentsChanged");
|
477 |
|
478 |
// This flag indicates to the 'insertString' and 'remove' methods of the PlainDocumentSeeker that the text of the item selected or written by the user
|
479 |
// hasn't been written on the TextEditor of ths component
|
480 |
selecting = false;
|
481 |
} |
482 |
|
483 |
public void intervalAdded(ListDataEvent e) { |
484 |
// TODO Auto-generated method stub
|
485 |
// logger.debug("intervalAdded");
|
486 |
} |
487 |
|
488 |
public void intervalRemoved(ListDataEvent e) { |
489 |
// TODO Auto-generated method stub
|
490 |
// logger.debug("intervalRemoved");
|
491 |
} |
492 |
|
493 |
}; |
494 |
|
495 |
} |
496 |
|
497 |
/**
|
498 |
* ?Sobra? Contiene c?digo interesante
|
499 |
*/
|
500 |
private void defineAndAddPropertyChangeListener() { |
501 |
// Define and add a Listener for changes on the editor or the model of the JComboBox
|
502 |
super.addPropertyChangeListener(new PropertyChangeListener() { |
503 |
public void propertyChange(PropertyChangeEvent ev) { |
504 |
if (ev.getPropertyName().equals("editor")) |
505 |
configureEditor((ComboBoxEditor) ev.getNewValue());
|
506 |
if (ev.getPropertyName().equals("model")) |
507 |
model = (ComboBoxItemsSeekerConfigurableModel) ev.getNewValue(); |
508 |
} |
509 |
}); |
510 |
} |
511 |
|
512 |
/**
|
513 |
* ?SOBRA? De momento la estoy usando, pero no s? si sobra o no (S? SE EST? USANDO)
|
514 |
*/
|
515 |
private void defineEditorFocusListener(JComboBoxItemsSeekerConfigurable combo_Box) { |
516 |
final JComboBoxItemsSeekerConfigurable comboBox = combo_Box;
|
517 |
|
518 |
// TODO Auto-generated method stub
|
519 |
// Highlight whole text when gaining focus
|
520 |
editorFocusListener = new FocusAdapter() { |
521 |
public void focusGained(FocusEvent e) { |
522 |
highlightCompletedText(0);
|
523 |
} |
524 |
|
525 |
public void focusLost(FocusEvent e) { |
526 |
// Workaround for Bug 5100422 - Hide Popup on focus loss
|
527 |
if (hidePopupOnFocusLoss) comboBox.setPopupVisible(false); |
528 |
} |
529 |
}; |
530 |
} |
531 |
|
532 |
/**
|
533 |
*
|
534 |
*/
|
535 |
private void defineEditorKeyListener(JComboBoxItemsSeekerConfigurable combo_Box) { |
536 |
final JComboBoxItemsSeekerConfigurable comboBoxReference = combo_Box;
|
537 |
|
538 |
// keyPressed = true;
|
539 |
|
540 |
|
541 |
// This flag indicates to the 'insertString' and 'remove' methods of the PlainDocumentSeeker that the text of the item selected or written by the user
|
542 |
// hasn't been written on the TextEditor of ths component
|
543 |
|
544 |
// TODO Auto-generated method stub
|
545 |
editorKeyListener = new KeyAdapter() |
546 |
{ |
547 |
public void keyPressed(KeyEvent ke) // Executed on the Start view state or Search view state |
548 |
{ |
549 |
logger.debug("Tecla apretada: ->" + ke.getKeyChar() + "<- ; Su keyCode es: " + ke.getKeyCode()); |
550 |
|
551 |
if (model.getParentSize() == 0) |
552 |
{ |
553 |
if (beepEnabled)
|
554 |
comboBoxReference.getToolkit().beep(); // when available use: UIManager.getLookAndFeel().provideErrorFeedback(comboBox);
|
555 |
} |
556 |
else
|
557 |
{ |
558 |
// This flag indicates to the 'insertString' and 'remove' methods of the PlainDocumentSeeker that the text of the item selected or written by the user
|
559 |
// hasn't been written on the TextEditor of ths component
|
560 |
selecting = false;
|
561 |
|
562 |
// This resolves the bug when the popup wasn't seen any time and the uses press a key that updates the view of the popup (the bug is that popup has the items but
|
563 |
// aren't seen
|
564 |
if (lastWasTheBeginningState)
|
565 |
{ |
566 |
// This maintains visible the popup
|
567 |
if (comboBoxReference.isDisplayable())
|
568 |
comboBoxReference.setPopupVisible(true);
|
569 |
lastWasTheBeginningState = false;
|
570 |
} |
571 |
|
572 |
hitBackspace=false;
|
573 |
|
574 |
JTextComponent editor = (JTextComponent) comboBoxReference.getEditor().getEditorComponent(); |
575 |
|
576 |
switch (ke.getKeyCode())
|
577 |
{ |
578 |
// determine if the pressed key is backspace (needed by the remove method)
|
579 |
case KeyEvent.VK_BACK_SPACE : |
580 |
hitBackspace=true;
|
581 |
hitBackspaceOnSelection=editor.getSelectionStart()!=editor.getSelectionEnd(); |
582 |
|
583 |
break;
|
584 |
// ignore delete key
|
585 |
case KeyEvent.VK_DELETE : // supr key in spanish keyboard |
586 |
// specialKeys = true;
|
587 |
// This avoids that popup listener could report about it detected item (Este no va)
|
588 |
// textKeyPressed = true;
|
589 |
|
590 |
|
591 |
// Remove selected text:
|
592 |
logger.debug("GETMARK: " + editor.getCaret().getMark() + "GETCARETPOSITION" + editor.getCaretPosition()); |
593 |
|
594 |
try {
|
595 |
// If no characters are selected
|
596 |
if (editor.getCaret().getMark() == editor.getCaretPosition())
|
597 |
{ |
598 |
// Remove the character which is on the right of the caret position
|
599 |
if (editor.getCaretPosition() < document.getLength())
|
600 |
document.remove(editor.getCaretPosition(), 1);
|
601 |
} |
602 |
else // If there are characters selected |
603 |
{ |
604 |
// Remove the characters which are selected
|
605 |
// int documentLength = document.getLength();
|
606 |
int length = Math.abs(editor.getCaret().getMark() - editor.getCaretPosition()); |
607 |
|
608 |
// Remove the characters which are selected
|
609 |
document.remove(Math.min(editor.getCaret().getMark(), editor.getCaretPosition()), length);
|
610 |
} |
611 |
} catch (BadLocationException e) { |
612 |
// TODO Auto-generated catch block
|
613 |
e.printStackTrace(); |
614 |
} |
615 |
|
616 |
switch(currentShowState)
|
617 |
{ |
618 |
case START_VIEW_STATE :
|
619 |
if (document.getLength() > 0) |
620 |
{ |
621 |
// If dynamic searches:
|
622 |
try {
|
623 |
// If the search will be dynamic:
|
624 |
if (isDynamicSearchConfiguration())
|
625 |
{ |
626 |
model.setWrittenText(document.getText(0, document.getLength()));
|
627 |
logger.debug("Texto que se le env?a al modelo desde SUPR: ->" + document.getText(0, document.getLength()) + "<-"); |
628 |
} |
629 |
} catch (BadLocationException e) { |
630 |
// TODO Auto-generated catch block
|
631 |
e.printStackTrace(); |
632 |
} |
633 |
|
634 |
// Indicate to the model that now is disabled the start view
|
635 |
if (currentShowState == START_VIEW_STATE)
|
636 |
{ |
637 |
model.setStartViewFlag(false);
|
638 |
currentShowState = SEARCH_VIEW_STATE; |
639 |
// updateLastSelectionItem();
|
640 |
} |
641 |
} |
642 |
break;
|
643 |
case SEARCH_VIEW_STATE :
|
644 |
// If there isn't characters
|
645 |
if (document.getLength() == 0) |
646 |
{ |
647 |
// This instruction avoids popup could failure when got invisible
|
648 |
hidePopup(); |
649 |
|
650 |
// If it's a dynamic search
|
651 |
if (isDynamicSearchConfiguration())
|
652 |
{ |
653 |
model.setWrittenText("");
|
654 |
logger.debug("Texto que se le env?a al modelo desde SUPR: ->\"\"<-");
|
655 |
} |
656 |
|
657 |
// Return to the start view state
|
658 |
model.setStartViewFlag(true);
|
659 |
currentShowState = START_VIEW_STATE; |
660 |
|
661 |
// Select the first Item
|
662 |
setSelectedIndex(0);
|
663 |
|
664 |
highlightCompletedText(0);
|
665 |
} |
666 |
else
|
667 |
{ |
668 |
// If dynamic searches:
|
669 |
try {
|
670 |
// If the search will be dynamic:
|
671 |
if (isDynamicSearchConfiguration())
|
672 |
{ |
673 |
model.setWrittenText(document.getText(0, document.getLength()));
|
674 |
logger.debug("Texto que se le env?a al modelo desde SUPR: ->" + document.getText(0, document.getLength()) + "<-"); |
675 |
} |
676 |
} catch (BadLocationException e) { |
677 |
// TODO Auto-generated catch block
|
678 |
e.printStackTrace(); |
679 |
} |
680 |
} |
681 |
break;
|
682 |
} |
683 |
|
684 |
// If we remove characters by default if we use two color -> set color red ; and if the item would match in the lookUp algorithm of the PlainDocumentSeeker
|
685 |
// text will be on black color
|
686 |
if (!isOnlyOneColor())
|
687 |
{ |
688 |
if (isKeySensitive())
|
689 |
{ |
690 |
try {
|
691 |
if (document.getText(0, document.getLength()).compareTo(getSelectedItem().toString()) == 0) |
692 |
comboBoxReference.getEditor().getEditorComponent().setForeground(Color.BLACK);
|
693 |
else
|
694 |
comboBoxReference.getEditor().getEditorComponent().setForeground(Color.RED);
|
695 |
} catch (BadLocationException e2) { |
696 |
// TODO Auto-generated catch block
|
697 |
e2.printStackTrace(); |
698 |
} |
699 |
} |
700 |
else
|
701 |
{ |
702 |
try {
|
703 |
if (document.getText(0, document.getLength()).compareToIgnoreCase(getSelectedItem().toString()) == 0) |
704 |
comboBoxReference.getEditor().getEditorComponent().setForeground(Color.BLACK);
|
705 |
else
|
706 |
comboBoxReference.getEditor().getEditorComponent().setForeground(Color.RED);
|
707 |
} catch (BadLocationException e2) { |
708 |
// TODO Auto-generated catch block
|
709 |
e2.printStackTrace(); |
710 |
} |
711 |
} |
712 |
} |
713 |
|
714 |
updatePopUpView(); |
715 |
|
716 |
break;
|
717 |
case KeyEvent.VK_ENTER : |
718 |
// try {
|
719 |
// specialKeys = true;
|
720 |
logger.debug("ENTRA EN EL C?DIGO DE ENTER");
|
721 |
|
722 |
// Reports about the written text to the model
|
723 |
try {
|
724 |
// Reports about the written text to the model
|
725 |
if (model.isDynamicSearch())
|
726 |
model.setWrittenText(document.getText(0, document.getLength()));
|
727 |
} catch (BadLocationException e1) { |
728 |
// TODO Auto-generated catch block
|
729 |
e1.printStackTrace(); |
730 |
} |
731 |
|
732 |
// Indicate to the model that now is disabled the start view
|
733 |
if (currentShowState == START_VIEW_STATE)
|
734 |
{ |
735 |
model.setStartViewFlag(false);
|
736 |
currentShowState = SEARCH_VIEW_STATE; |
737 |
// updateLastSelectionItem();
|
738 |
} |
739 |
|
740 |
|
741 |
// String text = document.getText(0, document.getLength());
|
742 |
// document.remove(0, document.getLength());
|
743 |
// document.insertString(0, text, null);
|
744 |
//
|
745 |
//
|
746 |
//
|
747 |
//// // If the text doesn't completely match with the selectedItem.toString() -> if allow 2 colors -> set foreground color to red
|
748 |
//// if (!onlyOneColor)
|
749 |
//// {
|
750 |
//// if (model.isKeySensitive())
|
751 |
//// {
|
752 |
//// if (text.compareTo(getSelectedItem().toString()) != 0)
|
753 |
//// editor.setForeground(Color.RED);
|
754 |
//// }
|
755 |
//// else
|
756 |
//// {
|
757 |
//// if (text.toUpperCase().compareTo(getSelectedItem().toString().toUpperCase()) != 0)
|
758 |
//// editor.setForeground(Color.RED);
|
759 |
//// }
|
760 |
//// }
|
761 |
// } catch (BadLocationException e1) {
|
762 |
// // TODO Auto-generated catch block
|
763 |
// e1.printStackTrace();
|
764 |
// }
|
765 |
hidePopup(); |
766 |
break;
|
767 |
case KeyEvent.VK_DOWN : |
768 |
// If the key (typed, pressed and) released has been the Down Key -> when there isn't any text (item of the list) selected, select the first
|
769 |
// ke.consume();
|
770 |
// selecting = true; // For indicate to the 'insertString' of the 'PlainDocumentSeeker' class that an item has been selected
|
771 |
// upOrDownKeyPressed = true;
|
772 |
// continueOnStartViewState = true;
|
773 |
|
774 |
// This allows user press the key DOWN for select another item of the start view list
|
775 |
// if ((model.isStartViewFlag()) && (lastWasStartShow))
|
776 |
// lastWasStartShow = true;
|
777 |
|
778 |
// // Reports about the written text to the model
|
779 |
// try {
|
780 |
// if (model.isDynamicSearch())
|
781 |
// model.setWrittenText(document.getText(0, document.getLength()));
|
782 |
// } catch (BadLocationException e2) {
|
783 |
// // TODO Auto-generated catch block
|
784 |
// e2.printStackTrace();
|
785 |
// }
|
786 |
|
787 |
// If items are disordered -> get the index of the selected item
|
788 |
if (model.nowItemsAreDisordered())
|
789 |
updateLastSelectionItem(); |
790 |
|
791 |
if (getItemCount() > 0) |
792 |
{ |
793 |
if (getSelectedIndex() == (getItemCount()-1)) |
794 |
((JComboBox)comboBoxReference).setSelectedIndex(0); |
795 |
else
|
796 |
((JComboBox)comboBoxReference).setSelectedIndex(getSelectedIndex()+1); |
797 |
} |
798 |
|
799 |
// model.setCurrentString(getSelectedItem().toString());
|
800 |
|
801 |
// lastSelectedIndex = getSelectedIndex();
|
802 |
selectedItem = getSelectedItem(); |
803 |
showPopup(); |
804 |
break;
|
805 |
case KeyEvent.VK_UP : |
806 |
// If the key (typed, pressed and) released has been the Up Key -> when there isn't any text (item of the list) selected, select the last
|
807 |
// ke.consume();
|
808 |
// selecting = true; // For indicate to the 'insertString' of the 'PlainDocumentSeeker' class that an item has been selected
|
809 |
// upOrDownKeyPressed = true;
|
810 |
// continueOnStartViewState = true;
|
811 |
|
812 |
// This allows user press the key UP for select another item of the start view list
|
813 |
// if ((model.isStartViewFlag()) && (lastWasStartShow))
|
814 |
// lastWasStartShow = true;
|
815 |
|
816 |
// // Reports about the written text to the model
|
817 |
// try {
|
818 |
// if (model.isDynamicSearch())
|
819 |
// model.setWrittenText(document.getText(0, document.getLength()));
|
820 |
// } catch (BadLocationException e2) {
|
821 |
// // TODO Auto-generated catch block
|
822 |
// e2.printStackTrace();
|
823 |
// }
|
824 |
|
825 |
// If items are disordered -> get the index of the selected item
|
826 |
if (model.nowItemsAreDisordered())
|
827 |
updateLastSelectionItem(); |
828 |
|
829 |
switch(getSelectedIndex())
|
830 |
{ |
831 |
case -1: case 0: |
832 |
((JComboBox)comboBoxReference).setSelectedIndex(getItemCount()-1); |
833 |
break;
|
834 |
default:
|
835 |
((JComboBox)comboBoxReference).setSelectedIndex(getSelectedIndex()-1); |
836 |
} |
837 |
|
838 |
// model.setCurrentString(getSelectedItem().toString());
|
839 |
selectedItem = getSelectedItem(); |
840 |
// lastSelectedIndex = getSelectedIndex();
|
841 |
showPopup(); |
842 |
break;
|
843 |
case KeyEvent.VK_LEFT : |
844 |
// Move the caret to left
|
845 |
if (editor.getCaretPosition() > 0) |
846 |
editor.setCaretPosition(editor.getCaretPosition() - 1);
|
847 |
else
|
848 |
{ |
849 |
// User hit backspace with the cursor positioned on the start => beep
|
850 |
if (beepEnabled)
|
851 |
getToolkit().beep(); // when available use: UIManager.getLookAndFeel().provideErrorFeedback(comboBox);
|
852 |
} |
853 |
//ke.consume();
|
854 |
break;
|
855 |
case KeyEvent.VK_RIGHT : |
856 |
// Move the caret to right
|
857 |
try {
|
858 |
if (editor.getCaretPosition() < (document.getText(0, document.getLength())).length()) |
859 |
editor.setCaretPosition(editor.getCaretPosition() + 1);
|
860 |
else
|
861 |
{ |
862 |
// User hit backspace with the cursor positioned on the start => beep
|
863 |
if (beepEnabled)
|
864 |
getToolkit().beep(); // when available use: UIManager.getLookAndFeel().provideErrorFeedback(comboBox);
|
865 |
} |
866 |
} catch (BadLocationException e) { |
867 |
// TODO Auto-generated catch block
|
868 |
e.printStackTrace(); |
869 |
} |
870 |
//ke.consume();
|
871 |
break;
|
872 |
case KeyEvent.VK_PAGE_DOWN : |
873 |
if (getSelectedIndex() != -1) |
874 |
{ |
875 |
// // Reports about the written text to the model
|
876 |
// try {
|
877 |
// if (model.isDynamicSearch())
|
878 |
// model.setWrittenText(document.getText(0, document.getLength()));
|
879 |
// } catch (BadLocationException e2) {
|
880 |
// // TODO Auto-generated catch block
|
881 |
// e2.printStackTrace();
|
882 |
// }
|
883 |
|
884 |
int index = getSelectedIndex() + POPUP_LIST_ITEMS_HEIGHT;
|
885 |
|
886 |
if (index < getItemCount())
|
887 |
setSelectedIndex(index); |
888 |
else
|
889 |
setSelectedIndex(getItemCount()-1);
|
890 |
|
891 |
selectedItem = getSelectedItem(); |
892 |
} |
893 |
|
894 |
// ke.consume();
|
895 |
break;
|
896 |
case KeyEvent.VK_PAGE_UP : |
897 |
if (getSelectedIndex() != -1) |
898 |
{ |
899 |
// // Reports about the written text to the model
|
900 |
// try {
|
901 |
// if (model.isDynamicSearch())
|
902 |
// model.setWrittenText(document.getText(0, document.getLength()));
|
903 |
// } catch (BadLocationException e2) {
|
904 |
// // TODO Auto-generated catch block
|
905 |
// e2.printStackTrace();
|
906 |
// }
|
907 |
|
908 |
int index = getSelectedIndex() - POPUP_LIST_ITEMS_HEIGHT;
|
909 |
|
910 |
if (index > -1) |
911 |
setSelectedIndex(index); |
912 |
else
|
913 |
setSelectedIndex(0);
|
914 |
|
915 |
selectedItem = getSelectedItem(); |
916 |
} |
917 |
|
918 |
// ke.consume();
|
919 |
break;
|
920 |
case KeyEvent.VK_HOME : // 'Inicio' key on the spanish keyboard |
921 |
editor.setCaretPosition(0);
|
922 |
break;
|
923 |
case KeyEvent.VK_END : // 'Fin' key on the spanish keyboard |
924 |
editor.setCaretPosition(document.getLength()); |
925 |
break;
|
926 |
case KeyEvent.VK_TAB : |
927 |
// specialKeys = true;
|
928 |
|
929 |
// This avoids that popup listener could report about it detected item
|
930 |
textKeyPressed = true;
|
931 |
|
932 |
if (model.getSize() > 0) |
933 |
{ |
934 |
// logger.debug("SelectedIndex: " + getSelectedIndex());
|
935 |
// boolean update = false;
|
936 |
|
937 |
if (((JComboBox)comboBoxReference).getSelectedIndex() == -1) |
938 |
{ |
939 |
// lastSelectedIndex = 0;
|
940 |
// ((JComboBox)comboBoxReference).setSelectedIndex(0);
|
941 |
//updatePopUp = true; // Indicate to the document.insertString that has update the popup after insert
|
942 |
// updatePopUpView();
|
943 |
// comboBoxReference.setSelectedIndex(0);
|
944 |
selectedItem = comboBoxReference.getItemAt(0); //.getSelectedItem(); |
945 |
} |
946 |
else
|
947 |
{ |
948 |
// lastSelectedIndex = getSelectedIndex();
|
949 |
// updatePopUp = true; // Indicate to the document.insertString that has update the popup after insert
|
950 |
// updatePopUpView();
|
951 |
selectedItem = comboBoxReference.getSelectedItem(); |
952 |
|
953 |
// setSelectedIndex()); // Complete the item string value in the document of the editor
|
954 |
// update = true;
|
955 |
} |
956 |
|
957 |
document.setText(selectedItem.toString()); |
958 |
|
959 |
// Reports about the written text to the model
|
960 |
// try {
|
961 |
if (model.isDynamicSearch()) {
|
962 |
model.setWrittenText(selectedItem.toString()); |
963 |
logger.debug("Texto que se le env?a al modelo: ->" + selectedItem.toString() + "<-"); |
964 |
} |
965 |
|
966 |
// } catch (BadLocationException e2) {
|
967 |
// // TODO Auto-generated catch block
|
968 |
// e2.printStackTrace();
|
969 |
// }
|
970 |
//
|
971 |
if (currentShowState == START_VIEW_STATE)
|
972 |
{ |
973 |
model.setStartViewFlag(false);
|
974 |
currentShowState = SEARCH_VIEW_STATE; |
975 |
// updatePopUpView(); // Update to the new popup view
|
976 |
} |
977 |
|
978 |
|
979 |
// It's supposed that the text on the document is equal as the string value of the selected item -> then text must be on black color
|
980 |
comboBoxReference.getEditor().getEditorComponent().setForeground(Color.BLACK);
|
981 |
|
982 |
updatePopUpView(); |
983 |
|
984 |
|
985 |
// document.setText(selectedItem.toString());
|
986 |
|
987 |
// continueOnStartViewState = false;
|
988 |
|
989 |
// HidePopUp + ShowPopUp to force update the popup list of items
|
990 |
// hidePopup();
|
991 |
// showPopup();
|
992 |
//// hidePopup();
|
993 |
} |
994 |
break;
|
995 |
case KeyEvent.VK_ESCAPE : |
996 |
// Reset to the Start View State
|
997 |
// model.setStartViewFlag(true);
|
998 |
|
999 |
// Force update the popup
|
1000 |
if (isPopupVisible())
|
1001 |
hidePopup(); |
1002 |
|
1003 |
model.setWrittenText("");
|
1004 |
selectedItem = null;
|
1005 |
|
1006 |
// currentShowState = START_VIEW_STATE;
|
1007 |
if (currentShowState == SEARCH_VIEW_STATE)
|
1008 |
{ |
1009 |
try {
|
1010 |
document.remove(0, document.getLength());
|
1011 |
} catch (BadLocationException e) { |
1012 |
// TODO Auto-generated catch block
|
1013 |
e.printStackTrace(); |
1014 |
} |
1015 |
|
1016 |
// setSelectedItem(selectedItem);
|
1017 |
// setSelectedIndex(model.getIndexOf(selectedItem));
|
1018 |
|
1019 |
// Return to the start view state
|
1020 |
model.setStartViewFlag(true);
|
1021 |
currentShowState = START_VIEW_STATE; |
1022 |
// escapeKeyPressed = true;
|
1023 |
// continueOnStartViewState = true;
|
1024 |
|
1025 |
// If items are disordered -> get the index of the selected item
|
1026 |
// if (model.nowItemsAreDisordered())
|
1027 |
// updateLastSelectionItem();
|
1028 |
|
1029 |
// Select the first Item
|
1030 |
setSelectedIndex(0);
|
1031 |
// selectFirstItemAddedToModel();
|
1032 |
|
1033 |
// updatePopUp = true;
|
1034 |
// updatePopUpView();
|
1035 |
|
1036 |
//updateLastSelectionItem();
|
1037 |
// setSelectedIndex(lastSelectedIndex);
|
1038 |
highlightCompletedText(0);
|
1039 |
// HidePopUp + ShowPopUp to force update the popup list of items
|
1040 |
// hidePopup();
|
1041 |
// showPopup();
|
1042 |
} |
1043 |
else
|
1044 |
{ |
1045 |
// Select the first Item
|
1046 |
setSelectedIndex(0);
|
1047 |
// selectFirstItemAddedToModel();
|
1048 |
|
1049 |
highlightCompletedText(0);
|
1050 |
} |
1051 |
updatePopUpView(); |
1052 |
break;
|
1053 |
case KeyEvent.VK_SHIFT : // Do nothing |
1054 |
break;
|
1055 |
case KeyEvent.VK_ALT_GRAPH : // Do nothing |
1056 |
break;
|
1057 |
case KeyEvent.VK_ALT : // Do nothing |
1058 |
break;
|
1059 |
case KeyEvent.VK_CONTROL : // Do nothing |
1060 |
break;
|
1061 |
case KeyEvent.VK_NUM_LOCK : // Do nothing |
1062 |
break;
|
1063 |
case KeyEvent.VK_CAPS_LOCK : // Do nothing |
1064 |
break;
|
1065 |
default:
|
1066 |
logger.debug("Actualiza view flag a false");
|
1067 |
|
1068 |
logger.debug("LASTITEM1 :" + lastSelectedItem);
|
1069 |
|
1070 |
logger.debug("GETMARK: " + editor.getCaret().getMark() + "GETCARETPOSITION" + editor.getCaretPosition()); |
1071 |
|
1072 |
if (!isOnlyOneColor())
|
1073 |
comboBoxReference.getEditor().getEditorComponent().setForeground(Color.RED);
|
1074 |
|
1075 |
if (model.isDynamicSearch())
|
1076 |
{ |
1077 |
// Force update the popup
|
1078 |
// hidePopup();
|
1079 |
|
1080 |
String text;
|
1081 |
if (editor.getCaretPosition() == editor.getCaret().getMark())
|
1082 |
text = editor.getText().substring(0, editor.getCaretPosition()) + ke.getKeyChar() + editor.getText().substring(editor.getCaretPosition(), editor.getText().length());
|
1083 |
else
|
1084 |
{ |
1085 |
text = "" + editor.getText().substring(0, (int) Math.min(editor.getCaretPosition(), editor.getCaret().getMark())) + ke.getKeyChar(); |
1086 |
text += editor.getText().substring((int) Math.max(editor.getCaretPosition(), editor.getCaret().getMark()), editor.getText().length()); |
1087 |
} |
1088 |
|
1089 |
// Reports about the written text to the model
|
1090 |
logger.debug("Texto que se le env?a KEY al modelo: ->" + text + "<-"); |
1091 |
model.setWrittenText(text); |
1092 |
|
1093 |
|
1094 |
// Indicate to the model that now is disabled the start view
|
1095 |
if (currentShowState == START_VIEW_STATE)
|
1096 |
{ |
1097 |
model.setStartViewFlag(false);
|
1098 |
currentShowState = SEARCH_VIEW_STATE; |
1099 |
// updateLastSelectionItem();
|
1100 |
} |
1101 |
|
1102 |
|
1103 |
// textKeyPressed = false;
|
1104 |
} |
1105 |
|
1106 |
updatePopUpView(); |
1107 |
textKeyPressed = true;
|
1108 |
|
1109 |
|
1110 |
logger.debug("Key Pressed:" + ke.getKeyCode());
|
1111 |
} |
1112 |
|
1113 |
// Consume the key event for not to use the handler of the keyboard of other components
|
1114 |
ke.consume(); |
1115 |
} |
1116 |
} |
1117 |
}; |
1118 |
|
1119 |
// keyPressed = false;
|
1120 |
} |
1121 |
|
1122 |
/**
|
1123 |
*
|
1124 |
*/
|
1125 |
private void definePopUpMenuListener(JComboBoxItemsSeekerConfigurable combo_Box) { |
1126 |
final JComboBoxItemsSeekerConfigurable comboBoxReference = combo_Box;
|
1127 |
|
1128 |
this.popupMenuListener = new PopupMenuListener() { |
1129 |
|
1130 |
/*
|
1131 |
* (non-Javadoc)
|
1132 |
* @see javax.swing.event.PopupMenuListener#popupMenuCanceled(javax.swing.event.PopupMenuEvent)
|
1133 |
*/
|
1134 |
public void popupMenuCanceled(PopupMenuEvent e) { |
1135 |
// TODO Auto-generated method stub
|
1136 |
logger.debug("MenuCANCELED");
|
1137 |
} |
1138 |
|
1139 |
/*
|
1140 |
* (non-Javadoc)
|
1141 |
* @see javax.swing.event.PopupMenuListener#popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent)
|
1142 |
*/
|
1143 |
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { |
1144 |
// TODO Auto-generated method stub
|
1145 |
logger.debug("MenuBECOMEINVISIBLE");
|
1146 |
|
1147 |
if (!textKeyPressed)
|
1148 |
{ |
1149 |
if (lastSelectedItem != null) |
1150 |
{ |
1151 |
// If the user has select a different item with the mouse, and this is in the Start View State -> change to the Search View State
|
1152 |
if (!getSelectedItem().equals(lastSelectedItem))
|
1153 |
{ |
1154 |
logger.debug("POPUP: NO SON IGUALES");
|
1155 |
|
1156 |
// Reports about the written text to the model
|
1157 |
if (model.isDynamicSearch())
|
1158 |
{ |
1159 |
model.setWrittenText(getSelectedItem().toString()); |
1160 |
logger.debug("Texto que le envia POPUP al modelo: ->" + getSelectedItem().toString() + "<-"); |
1161 |
} |
1162 |
|
1163 |
if (currentShowState == START_VIEW_STATE)
|
1164 |
{ |
1165 |
model.setStartViewFlag(false);
|
1166 |
currentShowState = SEARCH_VIEW_STATE; |
1167 |
// updateLastSelectionItem();
|
1168 |
} |
1169 |
logger.debug("Selected index of item: " + lastSelectedItem.toString() + " is " + getSelectedIndex()); |
1170 |
// updatePopUpView();
|
1171 |
// setSelectedIndex(1);
|
1172 |
} |
1173 |
else
|
1174 |
logger.debug("POPUP: SON IGUALES");
|
1175 |
} |
1176 |
} |
1177 |
} |
1178 |
|
1179 |
/*
|
1180 |
* (non-Javadoc)
|
1181 |
* @see javax.swing.event.PopupMenuListener#popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent)
|
1182 |
*/
|
1183 |
public void popupMenuWillBecomeVisible(PopupMenuEvent e) { |
1184 |
// TODO Auto-generated method stub
|
1185 |
logger.debug("MenuBECOMEVISIBLE");
|
1186 |
lastSelectedItem = selectedItem; // This object is used for know if the user has select a different item with the mouse
|
1187 |
} |
1188 |
}; |
1189 |
} |
1190 |
|
1191 |
|
1192 |
/**
|
1193 |
* Returns true if the model has a dynamic search configuration
|
1194 |
*
|
1195 |
* @return
|
1196 |
*/
|
1197 |
private boolean isDynamicSearchConfiguration() { |
1198 |
switch(model.getSearchBehavior())
|
1199 |
{ |
1200 |
case ComboBoxItemsSeekerConfigurableModel.MAINTAIN_ORIGINAL_POSITION_DYNAMIC_SEARCH :
|
1201 |
case ComboBoxItemsSeekerConfigurableModel.ORDERED_DYNAMIC_SEARCH :
|
1202 |
case ComboBoxItemsSeekerConfigurableModel.DISORDERED_DYNAMIC_SEARCH :
|
1203 |
return true; |
1204 |
} |
1205 |
|
1206 |
return false; |
1207 |
} |
1208 |
|
1209 |
|
1210 |
// comboBoxReference.getUI().
|
1211 |
// for (int i=0; i<comboBoxReference.getComponentCount(); i++) {
|
1212 |
// logger.debug("Componente: " + comboBoxReference.getComponent(i).getName());
|
1213 |
// comboBoxReference.getComponent(i).addMouseListener(editorMouseListener);
|
1214 |
//
|
1215 |
// }
|
1216 |
// this.addMouseListener(editorMouseListener);
|
1217 |
// }
|
1218 |
|
1219 |
/**
|
1220 |
* Selects the first item added to the model
|
1221 |
*/
|
1222 |
public void selectFirstItemAddedToModel() { |
1223 |
// Select the first Item added
|
1224 |
Object item = model.getFirstItemAdded();
|
1225 |
if (item != null) |
1226 |
setSelectedItem(item); |
1227 |
} |
1228 |
|
1229 |
private void configureEditor(ComboBoxEditor newEditor) { |
1230 |
JTextComponent editor = (JTextComponent) this.getEditor().getEditorComponent(); |
1231 |
|
1232 |
// If there isn't any change on the editor
|
1233 |
if (editor == newEditor)
|
1234 |
return;
|
1235 |
|
1236 |
// Remove the default or last keyListener and FocusListener of the editor of this component
|
1237 |
if (editor != null) { |
1238 |
editor.removeKeyListener(this.editorKeyListener);
|
1239 |
// editor.removeMouseListener(this.editorMouseListener);
|
1240 |
editor.removeFocusListener(this.editorFocusListener);
|
1241 |
} |
1242 |
|
1243 |
// Add the new keyListener and FocusListener for this editor and sets
|
1244 |
// it document as a PlainDocument to optimize the searches of items
|
1245 |
// when user writes on this editor
|
1246 |
if (newEditor != null) { |
1247 |
editor = (JTextComponent) newEditor.getEditorComponent();
|
1248 |
editor.addKeyListener(this.editorKeyListener);
|
1249 |
// editor.addMouseListener(this.editorMouseListener);
|
1250 |
editor.addFocusListener(this.editorFocusListener);
|
1251 |
editor.setDocument(this.document);
|
1252 |
|
1253 |
// Disable the focus transversal keys (Example: CTRL+TAB) for enable the TAB key
|
1254 |
editor.setFocusTraversalKeysEnabled(false);
|
1255 |
} |
1256 |
} |
1257 |
|
1258 |
/**
|
1259 |
* Marks text since 'start' position to its end on a grey color
|
1260 |
*
|
1261 |
* @param start Start position for mark the text
|
1262 |
*/
|
1263 |
private void highlightCompletedText(int start) { |
1264 |
JTextComponent editor = (JTextComponent) this.getEditor().getEditorComponent(); |
1265 |
|
1266 |
editor.setCaretPosition(document.getLength()); |
1267 |
editor.moveCaretPosition(start); |
1268 |
} |
1269 |
|
1270 |
/**
|
1271 |
*
|
1272 |
*/
|
1273 |
private void updatePopUpView() { |
1274 |
// HidePopUp + ShowPopUp to force update the popup list of items
|
1275 |
hidePopup(); |
1276 |
showPopup(); |
1277 |
this.setVisible(true); |
1278 |
} |
1279 |
|
1280 |
////// REDEFINITION OF SOME METHODS OF JCOMBOBOX //////
|
1281 |
|
1282 |
|
1283 |
/*
|
1284 |
* (non-Javadoc)
|
1285 |
* @see javax.swing.JComboBox#actionPerformed(java.awt.event.ActionEvent)
|
1286 |
*/
|
1287 |
public void actionPerformed(ActionEvent e) |
1288 |
{ |
1289 |
super.actionPerformed(e);
|
1290 |
// logger.debug("Action-Performed");
|
1291 |
|
1292 |
// This flag indicates to the 'insertString' method of the PlainDocumentSeeker that the text of the item selected or written by the user
|
1293 |
// hasn't been written on the TextEditor of ths component
|
1294 |
selecting = false;
|
1295 |
} |
1296 |
|
1297 |
|
1298 |
/*
|
1299 |
* (non-Javadoc)
|
1300 |
* @see javax.swing.JComboBox#addItem(java.lang.Object)
|
1301 |
*/
|
1302 |
public void addItem(Object anObject) { |
1303 |
// Adds the item to this component
|
1304 |
model.addElement(anObject); |
1305 |
|
1306 |
// logger.debug("LastSelectedIndexWas: " + this.lastSelectedIndex);
|
1307 |
|
1308 |
// Set the last selected item -> by default select the first item:
|
1309 |
// This is used to show the first item that model returns when we add a new item. This also
|
1310 |
// corrects the 'mistake' produced by 'ComboBoxModel' when is added a new item and there wasn't
|
1311 |
// any selected -> that the 'ComboBoxModel' selects by default the first added (that in our case
|
1312 |
// might not be the same as the first item seen in the popup list)
|
1313 |
if (this.getSelectedIndex() != 0) |
1314 |
{ |
1315 |
this.setSelectedIndex(0); |
1316 |
this.selectedItem = this.getSelectedItem(); |
1317 |
} |
1318 |
} |
1319 |
|
1320 |
/*
|
1321 |
* (non-Javadoc)
|
1322 |
* @see javax.swing.JComboBox#removeAllItems()
|
1323 |
*/
|
1324 |
public void removeAllItems() { |
1325 |
// Removes all items from the model
|
1326 |
model.removeAllElements(); |
1327 |
|
1328 |
currentShowState = BEGINNING_VIEW_STATE; |
1329 |
} |
1330 |
|
1331 |
/*
|
1332 |
* (non-Javadoc)
|
1333 |
* @see javax.swing.JComboBox#removeItem(java.lang.Object)
|
1334 |
*/
|
1335 |
public void removeItem(Object anObject) { |
1336 |
// Removes an object from the model
|
1337 |
model.removeElement(anObject); |
1338 |
|
1339 |
if (model.getSize() == 0) |
1340 |
currentShowState = BEGINNING_VIEW_STATE; |
1341 |
} |
1342 |
|
1343 |
/*
|
1344 |
* (non-Javadoc)
|
1345 |
* @see javax.swing.JComboBox#removeItemAt(int)
|
1346 |
*/
|
1347 |
public void removeItemAt(int anIndex) { |
1348 |
// Removes an object, which is in a determinated position (index), from the model
|
1349 |
model.removeElementAt(anIndex); |
1350 |
|
1351 |
if (model.getSize() == 0) |
1352 |
currentShowState = BEGINNING_VIEW_STATE; |
1353 |
} |
1354 |
|
1355 |
// /*
|
1356 |
// * (non-Javadoc)
|
1357 |
// * @see javax.swing.JComboBox#getSelectedIndex()
|
1358 |
// */
|
1359 |
// public int getSelectedIndex() {
|
1360 |
// // ??Modificarlo??
|
1361 |
// return super.getSelectedIndex();
|
1362 |
// }
|
1363 |
|
1364 |
/*
|
1365 |
* (non-Javadoc)
|
1366 |
* @see javax.swing.JComboBox#getItemCount()
|
1367 |
*/
|
1368 |
public int getItemCount() { |
1369 |
return model.getSize();
|
1370 |
} |
1371 |
|
1372 |
/*
|
1373 |
* (non-Javadoc)
|
1374 |
* @see javax.swing.JComboBox#getItemAt(int)
|
1375 |
*/
|
1376 |
public Object getItemAt(int index) { |
1377 |
return model.getElementAt(index);
|
1378 |
} |
1379 |
|
1380 |
/*
|
1381 |
* (non-Javadoc)
|
1382 |
* @see javax.swing.JComboBox#setModel(javax.swing.ComboBoxModel)
|
1383 |
*/
|
1384 |
public void setModel(ComboBoxModel aModel) { //PROBLEMA |
1385 |
|
1386 |
// Depending on the type of class of the current object, this object will execute different code
|
1387 |
if (aModel instanceof ComboBoxItemsSeekerConfigurableModel) |
1388 |
{ |
1389 |
// logger.debug("A");
|
1390 |
//super.setModel(aModel);
|
1391 |
super.setModel(aModel);
|
1392 |
model = (ComboBoxItemsSeekerConfigurableModel)super.getModel();
|
1393 |
|
1394 |
// If we insert a model with items -> select by default the first
|
1395 |
if (aModel.getSize() > 0) |
1396 |
{ |
1397 |
this.setSelectedIndex(0); |
1398 |
this.selectedItem = this.getSelectedItem(); |
1399 |
// model.setCurrentString(this.getSelectedItem().toString());
|
1400 |
} |
1401 |
else
|
1402 |
{ |
1403 |
// Else -> set the default index (-1) and current showed string ("")
|
1404 |
this.selectedItem = null; |
1405 |
this.setSelectedIndex(-1); |
1406 |
// model.setCurrentString("");
|
1407 |
} |
1408 |
|
1409 |
this.configureModel();
|
1410 |
} |
1411 |
else
|
1412 |
{ |
1413 |
// If it's an instance of DefaultComboBoxModel:
|
1414 |
|
1415 |
// logger.debug("B");
|
1416 |
super.setModel(aModel);
|
1417 |
} |
1418 |
} |
1419 |
|
1420 |
/*
|
1421 |
* (non-Javadoc)
|
1422 |
* @see javax.swing.JComboBox#setSelectedIndex(int)
|
1423 |
*/
|
1424 |
public void setSelectedIndex(int anIndex) { |
1425 |
super.setSelectedIndex(anIndex);
|
1426 |
this.selectedItem = super.getSelectedItem(); |
1427 |
|
1428 |
// this.lastSelectedIndex = anIndex;
|
1429 |
|
1430 |
try {
|
1431 |
document.remove(0, document.getLength());
|
1432 |
if (getSelectedIndex() > -1) |
1433 |
document.insertString(0, getSelectedItem().toString(), null); |
1434 |
// else
|
1435 |
// this.lastSelectedIndex = -1;
|
1436 |
|
1437 |
} catch (BadLocationException e) { |
1438 |
// TODO Auto-generated catch block
|
1439 |
e.printStackTrace(); |
1440 |
} |
1441 |
} |
1442 |
|
1443 |
/*
|
1444 |
* (non-Javadoc)
|
1445 |
* @see javax.swing.JComboBox#getSelectedIndex()
|
1446 |
*/
|
1447 |
public int getSelectedIndex() { |
1448 |
Object item = this.getSelectedItem(); |
1449 |
|
1450 |
if (item == null) |
1451 |
return -1; |
1452 |
else
|
1453 |
return model.getIndexOf(item);
|
1454 |
} |
1455 |
|
1456 |
/*
|
1457 |
* (non-Javadoc)
|
1458 |
* @see javax.swing.JComboBox#setSelectedItem(java.lang.Object)
|
1459 |
*/
|
1460 |
public void setSelectedItem(Object anObject) { |
1461 |
super.setSelectedItem(anObject);
|
1462 |
this.selectedItem = super.getSelectedItem(); |
1463 |
// this.lastSelectedIndex = model.getIndexOf(anObject);
|
1464 |
// logger.debug("Posici?n: " + this.lastSelectedIndex);
|
1465 |
|
1466 |
//super.setSelectedIndex(this.lastSelectedIndex);
|
1467 |
|
1468 |
// try {
|
1469 |
// document.remove(0, document.getLength());
|
1470 |
// if (getSelectedIndex() > -1)
|
1471 |
// document.insertString(0, getSelectedItem().toString(), null);
|
1472 |
// else
|
1473 |
// this.lastSelectedIndex = -1;
|
1474 |
//
|
1475 |
// } catch (BadLocationException e) {
|
1476 |
// // TODO Auto-generated catch block
|
1477 |
// e.printStackTrace();
|
1478 |
// }
|
1479 |
} |
1480 |
|
1481 |
//
|
1482 |
////// END REDEFINITION OF SOME METHODS OF JCOMBOBOX //////
|
1483 |
|
1484 |
|
1485 |
|
1486 |
|
1487 |
|
1488 |
////// METHODS FOR THE BEHAVIOR FLAGS //////
|
1489 |
|
1490 |
/**
|
1491 |
* This method tests the configuration of the flags and returns true or false if its ok or not with the
|
1492 |
* logical behavior of this component
|
1493 |
*
|
1494 |
* @return boolean True if the configuration of the flags is oks, false if not
|
1495 |
*/
|
1496 |
public boolean testFlagsConfigurationOK() { |
1497 |
return this.model.testFlagsConfigurationOK(); |
1498 |
} |
1499 |
|
1500 |
public int getStartBehavior() { |
1501 |
return this.model.getStartBehavior(); |
1502 |
} |
1503 |
|
1504 |
public void setStartBehavior(int start_Behavior) { |
1505 |
this.model.setStartBehavior(start_Behavior);
|
1506 |
|
1507 |
if (!this.model.testFlagsConfigurationOK()) |
1508 |
JOptionPane.showMessageDialog(this, PluginServices.getText(this, "errorMessageJComboBoxItemsSeekerConfigurable"), PluginServices.getText(this, "exportJOP2Title"), JOptionPane.ERROR_MESSAGE); |
1509 |
else
|
1510 |
updateLastSelectionItem(); |
1511 |
} |
1512 |
|
1513 |
public int getSearchBehavior() { |
1514 |
return this.model.getSearchBehavior(); |
1515 |
} |
1516 |
|
1517 |
public void setSearchBehavior(int search_Behavior) { |
1518 |
this.model.setSearchBehavior(search_Behavior);
|
1519 |
|
1520 |
if (!this.model.testFlagsConfigurationOK()) |
1521 |
JOptionPane.showMessageDialog(this, PluginServices.getText(this, "errorMessageJComboBoxItemsSeekerConfigurable"), PluginServices.getText(this, "exportJOP2Title"), JOptionPane.ERROR_MESSAGE); |
1522 |
else
|
1523 |
updateLastSelectionItem(); |
1524 |
} |
1525 |
|
1526 |
|
1527 |
/**
|
1528 |
* Gets the flag of the configuration of the keySensitive flag of this component
|
1529 |
* (true -> discriminates capital letters from small letters ,false -> don't discriminates capital letters from small letters)
|
1530 |
*
|
1531 |
* @return boolean The value of the flag
|
1532 |
*/
|
1533 |
public boolean isKeySensitive() { |
1534 |
return this.model.isKeySensitive(); |
1535 |
} |
1536 |
|
1537 |
/**
|
1538 |
* Sets the flag of the configuration of the keySensitive flag of this component
|
1539 |
* (true -> discriminates capital letters from small letters ,false -> don't discriminates capital letters from small letters)
|
1540 |
*
|
1541 |
* @param boolean The value of the flag
|
1542 |
*/
|
1543 |
public void setKeySensitive(boolean key_Sensitive) { |
1544 |
this.model.setKeySensitive(key_Sensitive);
|
1545 |
|
1546 |
if (!this.model.testFlagsConfigurationOK()) |
1547 |
JOptionPane.showMessageDialog(this, PluginServices.getText(this, "errorMessageJComboBoxItemsSeekerConfigurable"), PluginServices.getText(this, "exportJOP2Title"), JOptionPane.ERROR_MESSAGE); |
1548 |
} |
1549 |
|
1550 |
/**
|
1551 |
* Gets the flag of the configuration of the onlyOneColor flag of this component
|
1552 |
* (true -> The text on the textField only will be showed on black color; false -> if the text on the textField doesn't match with any current item of this
|
1553 |
* component -> the text will be showed on red color)
|
1554 |
*
|
1555 |
* @return boolean The value of the flag
|
1556 |
*/
|
1557 |
public boolean isOnlyOneColor() { |
1558 |
return onlyOneColor;
|
1559 |
} |
1560 |
|
1561 |
|
1562 |
/**
|
1563 |
* Sets the flag of the configuration of the onlyOneColor flag of this component
|
1564 |
* (true -> discriminates capital letters from small letters ,false -> don't discriminates capital letters from small letters)
|
1565 |
*
|
1566 |
* @param boolean The value of the flag
|
1567 |
*/
|
1568 |
public void setOnlyOneColor(boolean only_One_Color) { |
1569 |
this.onlyOneColor = only_One_Color;
|
1570 |
|
1571 |
if (!this.model.testFlagsConfigurationOK()) |
1572 |
JOptionPane.showMessageDialog(this, PluginServices.getText(this, "errorMessageJComboBoxItemsSeekerConfigurable"), PluginServices.getText(this, "exportJOP2Title"), JOptionPane.ERROR_MESSAGE); |
1573 |
} |
1574 |
|
1575 |
|
1576 |
public boolean isBeepEnabled() { |
1577 |
return this.beepEnabled; |
1578 |
} |
1579 |
|
1580 |
public void setBeepEnabled(boolean beep_Enabled) { |
1581 |
this.beepEnabled = beep_Enabled;
|
1582 |
} |
1583 |
|
1584 |
public boolean isCompleteMatchedItem() { |
1585 |
return this.completeMatchedItem; |
1586 |
} |
1587 |
|
1588 |
public void setCompleteMatchedItem(boolean Complete_Matched_Item) { |
1589 |
this.completeMatchedItem = Complete_Matched_Item;
|
1590 |
} |
1591 |
|
1592 |
|
1593 |
////// END METHODS FOR THE BEHAVIOR FLAGS //////
|
1594 |
|
1595 |
|
1596 |
/** REVISAR COMENTARIOS Y C?DIGO
|
1597 |
* Inner class for optimize the seek of items
|
1598 |
*
|
1599 |
* @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
|
1600 |
*
|
1601 |
*/
|
1602 |
private class PlainDocumentSeeker extends PlainDocument{ |
1603 |
private JComboBoxItemsSeekerConfigurable comboBox;
|
1604 |
|
1605 |
/**
|
1606 |
* Default Constructor
|
1607 |
*/
|
1608 |
public PlainDocumentSeeker() {
|
1609 |
super();
|
1610 |
} |
1611 |
|
1612 |
/**
|
1613 |
* Sets a reference of the combo_Box
|
1614 |
*
|
1615 |
* @param combo_Box
|
1616 |
*/
|
1617 |
public void setJComboBoxItemsSeekerDynamicReference(JComboBoxItemsSeekerConfigurable combo_Box) { |
1618 |
comboBox = combo_Box; |
1619 |
} |
1620 |
|
1621 |
/* REVISAR
|
1622 |
* (non-Javadoc)
|
1623 |
* @see javax.swing.text.Document#remove(int, int)
|
1624 |
*/
|
1625 |
public void remove(int offs, int len) throws BadLocationException { |
1626 |
// return immediately when selecting an item
|
1627 |
if (selecting) return; |
1628 |
|
1629 |
if (hitBackspace)
|
1630 |
{ |
1631 |
// user hit backspace => move the selection backwards
|
1632 |
// old item keeps being selected
|
1633 |
if (offs>0) |
1634 |
{ |
1635 |
if (hitBackspaceOnSelection)
|
1636 |
offs--; |
1637 |
} |
1638 |
else
|
1639 |
{ |
1640 |
// User hit backspace with the cursor positioned on the start => beep
|
1641 |
if (beepEnabled)
|
1642 |
comboBox.getToolkit().beep(); // when available use: UIManager.getLookAndFeel().provideErrorFeedback(comboBox);
|
1643 |
} |
1644 |
|
1645 |
highlightCompletedText(offs); |
1646 |
} |
1647 |
else
|
1648 |
{ |
1649 |
super.remove(offs, len);
|
1650 |
} |
1651 |
//
|
1652 |
// if (updatePopUp)
|
1653 |
// {
|
1654 |
// updatePopUpView();
|
1655 |
// updatePopUp = false;
|
1656 |
// }
|
1657 |
|
1658 |
} |
1659 |
|
1660 |
/*REVISAR
|
1661 |
* (non-Javadoc)
|
1662 |
* @see javax.swing.text.Document#insertString(int, java.lang.String, javax.swing.text.AttributeSet)
|
1663 |
*/
|
1664 |
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { |
1665 |
// super.insertString(offs, str, a);
|
1666 |
logger.debug("INSERTSTRING: " + str);
|
1667 |
// model.setCurrentString(str);
|
1668 |
|
1669 |
if (model.getParentSize() > 0) |
1670 |
{ |
1671 |
// Insert the string into the document
|
1672 |
|
1673 |
// If we aren't in the particular case of show al items at the beginning and is enabled the flag for complete the matched item:
|
1674 |
// this is for not insert two strings to the document of the editor of this component
|
1675 |
if (!((completeMatchedItem) && (model.isStartViewFlag())))
|
1676 |
// if (!completeMatchedItem)
|
1677 |
super.insertString(offs, str, a);
|
1678 |
|
1679 |
|
1680 |
// Lookup and select a matching item
|
1681 |
Object item = lookupItem(getText(0, getLength())); |
1682 |
|
1683 |
if (item != null) |
1684 |
{ |
1685 |
logger.debug("ITEM NOT NULL");
|
1686 |
if ((completeMatchedItem) || (getSearchBehavior() == ComboBoxItemsSeekerConfigurableModel.MAINTAIN_ORIGINAL_POSITION_ALL_ITEMS_SEARCH))
|
1687 |
setSelectedItem(item); |
1688 |
|
1689 |
|
1690 |
// model.setCurrentString(getSelectedItem().toString());
|
1691 |
// lastSelectedIndex = getSelectedIndex();
|
1692 |
} |
1693 |
else
|
1694 |
{ |
1695 |
logger.debug("ITEM NULL");
|
1696 |
// Keep old item selected if there is no match
|
1697 |
item = comboBox.getSelectedItem(); |
1698 |
logger.debug("ITEM QUE SELECCIONA CUANDO NULL: ->" + item.toString() + "<-"); |
1699 |
|
1700 |
// Imitate no insert (later on offs will be incremented by str.length(): selection won't move forward)
|
1701 |
offs = offs-str.length(); |
1702 |
|
1703 |
// Provide feedback to the user that his input has been received but can not be accepted
|
1704 |
if (beepEnabled)
|
1705 |
comboBox.getToolkit().beep(); // when available use: UIManager.getLookAndFeel().provideErrorFeedback(comboBox);
|
1706 |
} |
1707 |
|
1708 |
// Set the complete item value matched
|
1709 |
if (completeMatchedItem)
|
1710 |
setText(item.toString()); |
1711 |
|
1712 |
// Select the completed part
|
1713 |
highlightCompletedText(offs+str.length()); |
1714 |
|
1715 |
if (textKeyPressed)
|
1716 |
{ |
1717 |
// updatePopUpView();
|
1718 |
|
1719 |
textKeyPressed = false;
|
1720 |
} |
1721 |
} |
1722 |
} |
1723 |
|
1724 |
/** ?Sobra?
|
1725 |
* This method sets the string value of the item to the PlainDocument removing the previous value of the PlainDocument
|
1726 |
*
|
1727 |
* @param text The string value of the item
|
1728 |
*/
|
1729 |
private void setText(String text) { |
1730 |
try
|
1731 |
{ |
1732 |
// remove all text and insert the completed string
|
1733 |
super.remove(0, super.getLength()); |
1734 |
super.insertString(0, text, null); |
1735 |
} |
1736 |
catch (BadLocationException e) |
1737 |
{ |
1738 |
throw new RuntimeException(e.toString()); |
1739 |
} |
1740 |
} |
1741 |
|
1742 |
|
1743 |
|
1744 |
|
1745 |
|
1746 |
|
1747 |
|
1748 |
|
1749 |
|
1750 |
|
1751 |
|
1752 |
|
1753 |
|
1754 |
|
1755 |
|
1756 |
|
1757 |
|
1758 |
|
1759 |
/**
|
1760 |
*
|
1761 |
*/
|
1762 |
private Object lookupItem(String pattern) { |
1763 |
logger.debug("LOOKUP -> patr?n ->" + pattern + "<-"); |
1764 |
// Reset the color to the default
|
1765 |
// editor.getEditorComponent().setForeground(Color.BLACK);
|
1766 |
|
1767 |
// If no item starts with the pattern => return null and set the text written on red color if it's allowed
|
1768 |
|
1769 |
Object selectedItem = model.getSelectedItem();
|
1770 |
|
1771 |
// if (selectedItem.toString().length() >= pattern.length())
|
1772 |
// {
|
1773 |
// only search for a different item if the currently selected does not match
|
1774 |
if ((selectedItem != null) && (startsWithIgnoreCase(selectedItem.toString(), pattern))) |
1775 |
{ |
1776 |
editor.getEditorComponent().setForeground(Color.BLACK);
|
1777 |
return selectedItem;
|
1778 |
} |
1779 |
else
|
1780 |
{ |
1781 |
// iterate over all items
|
1782 |
for (int i=0, n=model.getSize(); i < n; i++) |
1783 |
{ |
1784 |
Object currentItem = model.getElementAt(i);
|
1785 |
|
1786 |
// current item starts with the pattern?
|
1787 |
if ((currentItem != null) && (startsWithIgnoreCase(currentItem.toString(), pattern))) |
1788 |
{ |
1789 |
editor.getEditorComponent().setForeground(Color.BLACK);
|
1790 |
return currentItem;
|
1791 |
} |
1792 |
} |
1793 |
} |
1794 |
|
1795 |
return null; |
1796 |
} |
1797 |
|
1798 |
|
1799 |
/**
|
1800 |
* Checks if the starts of two strings agrees
|
1801 |
*/
|
1802 |
private boolean startsWithIgnoreCase(String str1, String str2) { |
1803 |
if (comboBox.isKeySensitive())
|
1804 |
return str1.startsWith(str2);
|
1805 |
else
|
1806 |
return str1.toUpperCase().startsWith(str2.toUpperCase());
|
1807 |
} |
1808 |
} |
1809 |
} |