svn-gvsig-desktop / trunk / libraries / libUI / src / org / gvsig / gui / beans / comboBoxItemsSeeker / JComboBoxItemsSeekerConfigurable.java @ 6792
History | View | Annotate | Download (74.4 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 |
*
|
78 |
* POR DEFECTO USA UN MODELO QUE NO FUNCIONA BIEN SI HAY ITEMS REPETIDOS
|
79 |
*
|
80 |
* 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.
|
81 |
* This class is also a Java Bean, and is made according the MVC (Model - View - Controller) pattern.
|
82 |
* This class has the View, the Control and can access to the Model.
|
83 |
* The behavior of this componet can be configurated by 7 flags: (Esto lo cambio!!!!!)
|
84 |
* + 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)
|
85 |
* -> 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
|
86 |
* when we write on the TextField of this Component will be showed at the end (esto ?ltimo creo q. no -> revisarlo)
|
87 |
* -> all_Alphanumeric_Sorted -> All items we can see in this Component will be showed in alphanumeric ordenation (if true)
|
88 |
* + 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)
|
89 |
* -> alphanumericSortedSearches -> When we write on the TextField of this Component all results (items) are showed in alphanumeric ordenation (if true)
|
90 |
* + case Sensitive or not when the user writes
|
91 |
* -> case_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)
|
92 |
* + Show all items always
|
93 |
* -> 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
|
94 |
* + 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)
|
95 |
* -> 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
|
96 |
* will be showed on red color
|
97 |
* + * @param boolean (completeMatchedItem (explicar))
|
98 |
* + @param boolean (useBeep (explicar))
|
99 |
*
|
100 |
* Default Values of the Flags:
|
101 |
* -> maintainPositionItems -> true
|
102 |
* -> all_Alphanumeric_Sorted -> true
|
103 |
* -> alphanumericSortedSearches -> false
|
104 |
* -> case_Sensitive -> false
|
105 |
* -> showAllItems -> false
|
106 |
* -> only_One_Color -> false
|
107 |
*
|
108 |
* The flags are distributed in two classes:
|
109 |
* -> ComboBoxSearcheableDynamicModel -> maintainPositionItems, all_Alphanumeric_Sorted, alphanumericSortedSearches, case_Sensitive, showAllItems
|
110 |
* -> JComboBoxSearcheableDynamic -> only_One_Color
|
111 |
*
|
112 |
* Combinations of flags not allowed:
|
113 |
* -> maintainPositionItems == allAlphanumericSorted == true
|
114 |
* -> ((maintain_Position_Items == false) && (all_Alphanumeric_Sorted == true) && (alphanumeric_Sorted_Searches == false))
|
115 |
* -> showAllItems == true && maintainPositionItems == false (Creo que esta mal)
|
116 |
*
|
117 |
* Limitations:
|
118 |
* -> 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
|
119 |
* -> 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
|
120 |
*
|
121 |
* @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
|
122 |
*/
|
123 |
public class JComboBoxItemsSeekerConfigurable extends JComboBox implements java.io.Serializable { |
124 |
//private static final long serialVersionUID = -1853812970336818959L;
|
125 |
// PONER SERIALIZACI?N
|
126 |
|
127 |
// eSTAS HAY QUE BORRARLAS!
|
128 |
/* private Vector defaultItems;
|
129 |
private Set notRemovedItems;
|
130 |
private Set removedItems;*/
|
131 |
|
132 |
private final int BEGINNING_VIEW_STATE = 0; |
133 |
private final int START_VIEW_STATE = 1; |
134 |
private final int SEARCH_VIEW_STATE = 2; |
135 |
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 |
136 |
|
137 |
|
138 |
// CONFIGURATION FLAGS
|
139 |
private boolean onlyOneColor; |
140 |
private boolean completeMatchedItem; |
141 |
private boolean beepEnabled; |
142 |
// END FLAGS
|
143 |
|
144 |
// PUEDE QUE TB. sOBren
|
145 |
// private boolean allItemsAreSorted;
|
146 |
|
147 |
// Last selected item
|
148 |
private Object selectedItem; |
149 |
private Object lastSelectedItem; // This is used for only update (refresh) de popup when a different item is selected |
150 |
private String lastDocumentText; |
151 |
|
152 |
// pueDE QUE TB. SOBRE
|
153 |
private int num = 0; |
154 |
// private JTextField txtField;
|
155 |
|
156 |
private final int POPUP_LIST_ITEMS_HEIGHT = 8; |
157 |
// Reference to the ui
|
158 |
//private MetalComboBoxItemsSeekerUI ui;
|
159 |
|
160 |
// Reference to the model
|
161 |
private AbstractDefaultComboBoxItemsSeekerConfigurableModel model;
|
162 |
|
163 |
// Some listeners for the Control between the View and de Model
|
164 |
private KeyListener editorKeyListener; |
165 |
private FocusListener editorFocusListener; |
166 |
private DocumentListener documentListener; |
167 |
private ListDataListener modelListDataListener; |
168 |
private PopupMenuListener popupMenuListener; |
169 |
|
170 |
// Reference to the document of the Editor of this component for optimize the seeks
|
171 |
private PlainDocumentSeeker document;
|
172 |
|
173 |
// private String writtenString;
|
174 |
|
175 |
private boolean continueOnStartViewState; |
176 |
// private boolean specialKeys; // ESC and TAB keys
|
177 |
private boolean textKeyPressed; |
178 |
private boolean lastWasTheBeginningState; |
179 |
private boolean lastWasPressedAKeyModifier; |
180 |
private boolean popupWillBeVisible; |
181 |
// private boolean keyPressed;
|
182 |
// private boolean updatePopUp;
|
183 |
// private boolean upOrDownKeyPressed;
|
184 |
// private boolean deleteKeyPressed;
|
185 |
// private boolean escapeKeyPressed;
|
186 |
|
187 |
// OTRAS FLAGS que puede que sobren alguNAS
|
188 |
private boolean selecting=false; |
189 |
private boolean hidePopupOnFocusLoss; |
190 |
private boolean hitBackspace; |
191 |
private boolean hitBackspaceOnSelection; |
192 |
private boolean allowedRepeatedItems; |
193 |
|
194 |
// TRAZA - DEBUG
|
195 |
private Logger logger = Logger.getLogger(JComboBoxItemsSeekerConfigurable.class.getClass()); |
196 |
|
197 |
|
198 |
/**
|
199 |
* Default Constructor
|
200 |
*/
|
201 |
public JComboBoxItemsSeekerConfigurable() {
|
202 |
// Invokes to the parent class constructor
|
203 |
super();
|
204 |
|
205 |
// Create attributes and set initial values
|
206 |
this.initialize();
|
207 |
|
208 |
// Set the default values of the flags
|
209 |
this.onlyOneColor = false; |
210 |
this.completeMatchedItem = false; //true; |
211 |
this.beepEnabled = false; //true; |
212 |
|
213 |
// Other configuration tasks
|
214 |
this.createDefaultListeners();
|
215 |
this.configure();
|
216 |
//this.configure();
|
217 |
} |
218 |
|
219 |
/**
|
220 |
* Default Constructor with four parameters: configure the 4 flags
|
221 |
*
|
222 |
* @param boolean (true -> maintains the position of the items; false -> don't maintains the position of the items)
|
223 |
* @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)
|
224 |
* @param boolean (true -> all items showed will be in alphanumeric ordenation; false -> they don't have to be in alphanumeric ordenation)
|
225 |
* @param boolean (true -> discriminates capital letters from small letters; false -> don't discriminates capital letters from small letters)
|
226 |
*/
|
227 |
public JComboBoxItemsSeekerConfigurable(int start_Behavior, int search_Behavior, boolean case_Sensitive) { |
228 |
// Invokes to the parent class constructor
|
229 |
super();
|
230 |
|
231 |
// Create attributes and set initial values
|
232 |
this.initialize();
|
233 |
|
234 |
// Sets the options selected by the user
|
235 |
// this.model.setMaintainPositionItems(maintain_Position_Items);
|
236 |
// this.model.setAllAlphanumericSorted(all_Alphanumeric_Sorted);
|
237 |
// this.model.setAlphanumericSortedSearches(alphanumeric_Sorted_Searches);
|
238 |
this.model.setStartBehavior(start_Behavior);
|
239 |
this.model.setStartBehavior(search_Behavior);
|
240 |
this.model.setCaseSensitive(case_Sensitive);
|
241 |
// this.model.setShowAllItems(false);
|
242 |
this.onlyOneColor = false; |
243 |
this.completeMatchedItem = false; //true; |
244 |
this.beepEnabled = true; |
245 |
|
246 |
if (!testFlagsConfigurationOK())
|
247 |
JOptionPane.showMessageDialog(this, PluginServices.getText(this, "errorMessageJComboBoxItemsSeekerConfigurable"), PluginServices.getText(this, "exportJOP2Title"), JOptionPane.ERROR_MESSAGE); |
248 |
else
|
249 |
this.configure();
|
250 |
} |
251 |
|
252 |
/**
|
253 |
* Default Constructor with six parameters: configure the 6 flags
|
254 |
*
|
255 |
* @param boolean (true -> maintains the position of the items; false -> don't maintains the position of the items)
|
256 |
* @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)
|
257 |
* @param boolean (true -> all items showed will be in alphanumeric ordenation; false -> they don't have to be in alphanumeric ordenation)
|
258 |
* @param boolean (true -> discriminates capital letters from small letters; false -> don't discriminates capital letters from small letters)
|
259 |
* @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
|
260 |
* will be showed on red color)
|
261 |
* @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)
|
262 |
* @param boolean (completeMatchedItem (explicar))
|
263 |
* @param boolean (beepEnabled (explicar))
|
264 |
* @param boolean (allowedRepeatedItems (explicar))
|
265 |
*/
|
266 |
public JComboBoxItemsSeekerConfigurable(int start_Behavior, int search_Behavior, boolean case_Sensitive, boolean only_One_Color, boolean show_All_Items, boolean complete_Matched_Item, boolean use_Beep, boolean allowed_Repeated_Items) { |
267 |
// Invokes to the parent class constructor
|
268 |
super();
|
269 |
|
270 |
// Create attributes and set initial values
|
271 |
this.initialize();
|
272 |
|
273 |
// Sets the options selected by the user
|
274 |
// this.model.setMaintainPositionItems(maintain_Position_Items);
|
275 |
// this.model.setAllAlphanumericSorted(all_Alphanumeric_Sorted);
|
276 |
// this.model.setAlphanumericSortedSearches(alphanumeric_Sorted_Searches);
|
277 |
this.model.setStartBehavior(start_Behavior);
|
278 |
this.model.setStartBehavior(search_Behavior);
|
279 |
this.model.setCaseSensitive(case_Sensitive);
|
280 |
// this.model.setShowAllItems(show_All_Items);
|
281 |
this.onlyOneColor = only_One_Color;
|
282 |
this.completeMatchedItem = complete_Matched_Item;
|
283 |
this.beepEnabled = use_Beep;
|
284 |
this.allowedRepeatedItems = allowed_Repeated_Items;
|
285 |
|
286 |
if (!testFlagsConfigurationOK())
|
287 |
JOptionPane.showMessageDialog(this, PluginServices.getText(this, "errorMessageJComboBoxItemsSeekerConfigurable"), PluginServices.getText(this, "exportJOP2Title"), JOptionPane.ERROR_MESSAGE); |
288 |
else
|
289 |
this.configure();
|
290 |
} |
291 |
|
292 |
|
293 |
/**
|
294 |
*
|
295 |
*/
|
296 |
private void initialize() { |
297 |
// // Creates the ui for this component and gets it reference
|
298 |
// super.setUI(new MetalComboBoxItemsSeekerUI());
|
299 |
// this.ui = (MetalComboBoxItemsSeekerUI) super.getUI();
|
300 |
// super.setUI(new MetalComboBoxItemsSeekerUI());
|
301 |
|
302 |
// Creates the model for this component and gets it reference
|
303 |
if (this.isAllowedRepeatedItems()) |
304 |
{ |
305 |
super.setModel(new ComboBoxItemsSeekerConfigurableModel()); |
306 |
this.model = (ComboBoxItemsSeekerConfigurableModel) super.getModel(); |
307 |
} |
308 |
else
|
309 |
{ |
310 |
super.setModel(new ComboBoxSingularItemsSeekerConfigurableModel()); |
311 |
this.model = (ComboBoxSingularItemsSeekerConfigurableModel) super.getModel(); |
312 |
} |
313 |
|
314 |
// Allows user to edit on the combobox
|
315 |
super.setEditable(true); |
316 |
|
317 |
// By default the last state of showign items was showing all the items (at the beginning of this component)
|
318 |
currentShowState = BEGINNING_VIEW_STATE; |
319 |
|
320 |
// Set the last selected item -> if there isn't any item -> -1; else -> this will be 0
|
321 |
super.setSelectedIndex(-1); |
322 |
this.selectedItem = null; |
323 |
// logger.debug("selectedItem: " + this.getSelectedIndex());
|
324 |
|
325 |
// By default the keys Up and Down aren't pressed
|
326 |
// this.upOrDownKeyPressed = false;
|
327 |
|
328 |
// By default the keys Delete aren't pressed
|
329 |
// this.escapeKeyPressed = false;
|
330 |
|
331 |
// By default any text key has been pressed
|
332 |
textKeyPressed = false;
|
333 |
|
334 |
allowedRepeatedItems = false;
|
335 |
|
336 |
lastWasPressedAKeyModifier = false;
|
337 |
|
338 |
// By default popup won't be visible when gained the focus the editor associated
|
339 |
popupWillBeVisible = false;
|
340 |
|
341 |
|
342 |
hitBackspace=false;
|
343 |
|
344 |
// // By default any key has been pressed
|
345 |
// keyPressed = false;
|
346 |
|
347 |
// specialKeys = false;
|
348 |
// updatePopUp = false;
|
349 |
|
350 |
// Remove the default mouse listener
|
351 |
// super.removeMouseListener(super.getMouseListeners()[0]);
|
352 |
} |
353 |
|
354 |
/**
|
355 |
*
|
356 |
*/
|
357 |
private void createDefaultListeners() { |
358 |
// TODO Auto-generated method stub
|
359 |
|
360 |
// Define the key listener for the editor of this component
|
361 |
this.defineEditorKeyListener(this); |
362 |
|
363 |
// Define the focus listener for the editor of this component
|
364 |
this.defineEditorFocusListener(this); |
365 |
|
366 |
// Define a document listener for changes of text
|
367 |
this.defineDocumentListener();
|
368 |
|
369 |
// Define the list data listener for the model of this component
|
370 |
this.defineModelListDataListener();
|
371 |
|
372 |
// Define a popup menu listener for the popup of this component
|
373 |
this.definePopUpMenuListener(this); |
374 |
} |
375 |
|
376 |
/**
|
377 |
* Define a document listener for changes of text
|
378 |
*/
|
379 |
private void defineDocumentListener() { |
380 |
// TODO Auto-generated method stub
|
381 |
documentListener = new DocumentListener() { |
382 |
|
383 |
public void changedUpdate(DocumentEvent arg0) { |
384 |
// TODO Auto-generated method stub
|
385 |
} |
386 |
|
387 |
public void insertUpdate(DocumentEvent arg0) { |
388 |
// TODO Auto-generated method stub
|
389 |
|
390 |
|
391 |
logger.debug("NUM: " + num + " DocumentListener->insertUpdate"); |
392 |
try {
|
393 |
// lastDocumentText = document.getText(0, document.getLength())
|
394 |
logger.debug("Texto insertado: ->" + document.getText(0, document.getLength()) + "<-"); |
395 |
|
396 |
} catch (BadLocationException e) { |
397 |
// TODO Auto-generated catch block
|
398 |
e.printStackTrace(); |
399 |
} |
400 |
num++; |
401 |
|
402 |
// Indicate to the model that now is disabled the start view
|
403 |
// 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
|
404 |
if (currentShowState == BEGINNING_VIEW_STATE)
|
405 |
{ |
406 |
currentShowState = START_VIEW_STATE; |
407 |
lastWasTheBeginningState = true;
|
408 |
continueOnStartViewState = true;
|
409 |
logger.debug("Pasa al estado 1 (de vista inicial)");
|
410 |
} |
411 |
} |
412 |
|
413 |
public void removeUpdate(DocumentEvent arg0) { |
414 |
// TODO Auto-generated method stub
|
415 |
logger.debug("NUM: " + num + " DocumentListener->removeUpdate"); |
416 |
num++; |
417 |
} |
418 |
}; |
419 |
} |
420 |
|
421 |
private void updateLastSelectionItem() { |
422 |
setSelectedItem(selectedItem); |
423 |
} |
424 |
|
425 |
/**
|
426 |
*
|
427 |
*/
|
428 |
private void configure() { |
429 |
// TODO Auto-generated method stub
|
430 |
|
431 |
// Configure the document of the editor of this component
|
432 |
this.configureDocument();
|
433 |
|
434 |
// Configure the editor (ComboBoxEditor) of this component
|
435 |
this.configureEditor(this.getEditor()); |
436 |
|
437 |
// Configure the model of this component
|
438 |
this.configureModel();
|
439 |
|
440 |
// Configure the popup of this component
|
441 |
this.configurePopUp();
|
442 |
|
443 |
} |
444 |
|
445 |
/**
|
446 |
*
|
447 |
*
|
448 |
*/
|
449 |
private void configurePopUp() { |
450 |
// TODO Auto-generated method stub
|
451 |
this.addPopupMenuListener(this.popupMenuListener); |
452 |
} |
453 |
|
454 |
// /**
|
455 |
// *
|
456 |
// */
|
457 |
// private void uninstallDefaultListeners() {
|
458 |
//
|
459 |
//
|
460 |
// }
|
461 |
|
462 |
/**
|
463 |
*
|
464 |
*/
|
465 |
private void configureModel() { |
466 |
// TODO Auto-generated method stub
|
467 |
this.model.addListDataListener(this.modelListDataListener); |
468 |
} |
469 |
|
470 |
/**
|
471 |
*
|
472 |
*/
|
473 |
private void configureDocument() { |
474 |
// TODO Auto-generated method stub
|
475 |
|
476 |
// Creates the document of the editor of this component
|
477 |
document = new PlainDocumentSeeker();
|
478 |
document.setJComboBoxItemsSeekerDynamicReference(this);
|
479 |
|
480 |
// Set the written String
|
481 |
// writtenString = "";
|
482 |
|
483 |
// Remove all Previous document Listeners
|
484 |
// DocumentListener documentListener[] = document.getDocumentListeners();
|
485 |
// for (int i=0; i < documentListener.length; i++)
|
486 |
// document.removeDocumentListener(documentListener[i]);
|
487 |
|
488 |
// Defines and add a Document listener for changes on the document of the editor of this component
|
489 |
document.addDocumentListener(this.documentListener);
|
490 |
} |
491 |
|
492 |
|
493 |
/**
|
494 |
* Define the list data listener for the model of this component
|
495 |
*/
|
496 |
public void defineModelListDataListener() { |
497 |
|
498 |
this.modelListDataListener = new ListDataListener() { |
499 |
|
500 |
public void contentsChanged(ListDataEvent e) { |
501 |
// TODO Auto-generated method stub
|
502 |
// logger.debug("ContentsChanged");
|
503 |
|
504 |
// This flag indicates to the 'insertString' and 'remove' methods of the PlainDocumentSeeker that the text of the item selected or written by the user
|
505 |
// hasn't been written on the TextEditor of ths component
|
506 |
selecting = false;
|
507 |
} |
508 |
|
509 |
public void intervalAdded(ListDataEvent e) { |
510 |
// TODO Auto-generated method stub
|
511 |
// logger.debug("intervalAdded");
|
512 |
} |
513 |
|
514 |
public void intervalRemoved(ListDataEvent e) { |
515 |
// TODO Auto-generated method stub
|
516 |
// logger.debug("intervalRemoved");
|
517 |
} |
518 |
|
519 |
}; |
520 |
|
521 |
} |
522 |
|
523 |
/**
|
524 |
* ?Sobra? Contiene c?digo interesante
|
525 |
*/
|
526 |
private void defineAndAddPropertyChangeListener() { |
527 |
// Define and add a Listener for changes on the editor or the model of the JComboBox
|
528 |
super.addPropertyChangeListener(new PropertyChangeListener() { |
529 |
public void propertyChange(PropertyChangeEvent ev) { |
530 |
if (ev.getPropertyName().equals("editor")) |
531 |
configureEditor((ComboBoxEditor) ev.getNewValue());
|
532 |
if (ev.getPropertyName().equals("model")) |
533 |
{ |
534 |
if (isAllowedRepeatedItems())
|
535 |
model = (ComboBoxItemsSeekerConfigurableModel) ev.getNewValue(); |
536 |
else
|
537 |
model = (ComboBoxSingularItemsSeekerConfigurableModel) ev.getNewValue(); |
538 |
} |
539 |
} |
540 |
}); |
541 |
} |
542 |
|
543 |
/**
|
544 |
* (S? SE EST? USANDO)
|
545 |
*/
|
546 |
private void defineEditorFocusListener(JComboBoxItemsSeekerConfigurable combo_Box) { |
547 |
final JComboBoxItemsSeekerConfigurable comboBoxReference = combo_Box;
|
548 |
|
549 |
// Bug 5100422 on Java 1.5: Editable JComboBox won't hide popup when tabbing out
|
550 |
hidePopupOnFocusLoss=System.getProperty("java.version").startsWith("1.5"); |
551 |
|
552 |
// TODO Auto-generated method stub
|
553 |
// Highlight whole text when gaining focus
|
554 |
editorFocusListener = new FocusAdapter() { |
555 |
public void focusGained(FocusEvent e) { |
556 |
highlightCompletedText(0);
|
557 |
logger.debug("currentShowState: " + currentShowState);
|
558 |
logger.debug("EDITOR FOCUS GAINED");
|
559 |
|
560 |
// This is used when the component has lost the focus, the popup whas in show state and is restored the focus -> update the popup
|
561 |
//if (() && (currentShowState != BEGINNING_VIEW_STATE))
|
562 |
if (popupWillBeVisible)
|
563 |
{ |
564 |
logger.debug("UPDATE POPUP DESDE EL FOCUSGAINED");
|
565 |
comboBoxReference.updatePopUpView(); |
566 |
} |
567 |
} |
568 |
|
569 |
public void focusLost(FocusEvent e) { |
570 |
logger.debug("EDITOR FOCUS LOST");
|
571 |
|
572 |
if (isPopupVisible())
|
573 |
popupWillBeVisible = true;
|
574 |
else
|
575 |
popupWillBeVisible = false;
|
576 |
|
577 |
// Workaround for Bug 5100422 - Hide Popup on focus loss
|
578 |
if (hidePopupOnFocusLoss) comboBoxReference.setPopupVisible(false); |
579 |
} |
580 |
}; |
581 |
} |
582 |
|
583 |
/**
|
584 |
*
|
585 |
*/
|
586 |
private void defineEditorKeyListener(JComboBoxItemsSeekerConfigurable combo_Box) { |
587 |
final JComboBoxItemsSeekerConfigurable comboBoxReference = combo_Box;
|
588 |
|
589 |
// keyPressed = true;
|
590 |
|
591 |
|
592 |
// This flag indicates to the 'insertString' and 'remove' methods of the PlainDocumentSeeker that the text of the item selected or written by the user
|
593 |
// hasn't been written on the TextEditor of ths component
|
594 |
|
595 |
// TODO Auto-generated method stub
|
596 |
editorKeyListener = new KeyAdapter() |
597 |
{ |
598 |
// public void keyTyped(KeyEvent ke) {
|
599 |
// logger.debug("Tecla tecleada: ->" + ke.getKeyChar() + "<- ; Su keyCode es: " + ke.getKeyCode());
|
600 |
// }
|
601 |
|
602 |
public void keyPressed(KeyEvent ke) // Executed on the Start view state or Search view state |
603 |
{ |
604 |
// DISTINGUIR SI: s?lo tienen keyChar ? s?lo keyCode ? ambas!!!
|
605 |
|
606 |
logger.debug("Tecla apretada: ->" + ke.getKeyChar() + "<- ; Su keyCode es: " + ke.getKeyCode() + " TIPO: " + Character.getType(ke.getKeyChar())); |
607 |
|
608 |
if (model.getParentSize() == 0) |
609 |
{ |
610 |
if (beepEnabled)
|
611 |
comboBoxReference.getToolkit().beep(); // when available use: UIManager.getLookAndFeel().provideErrorFeedback(comboBox);
|
612 |
} |
613 |
else
|
614 |
{ |
615 |
// This flag indicates to the 'insertString' and 'remove' methods of the PlainDocumentSeeker that the text of the item selected or written by the user
|
616 |
// hasn't been written on the TextEditor of ths component
|
617 |
selecting = false;
|
618 |
|
619 |
// 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
|
620 |
// aren't seen
|
621 |
if (lastWasTheBeginningState)
|
622 |
{ |
623 |
// This maintains visible the popup
|
624 |
if (comboBoxReference.isDisplayable())
|
625 |
comboBoxReference.setPopupVisible(true);
|
626 |
lastWasTheBeginningState = false;
|
627 |
} |
628 |
|
629 |
if (Character.isLetterOrDigit(ke.getKeyChar())) { |
630 |
logger.debug("IS LETTER OR DIGIT");
|
631 |
} |
632 |
else
|
633 |
logger.debug("ISN'T LETTER OR DIGIT");
|
634 |
|
635 |
|
636 |
// hitBackspace=false;
|
637 |
JTextComponent editor = (JTextComponent) comboBoxReference.getEditor().getEditorComponent(); |
638 |
|
639 |
// if (ke.getKeyCode() == 0)
|
640 |
// {
|
641 |
// logger.debug("CASO -> KEYCHAR");
|
642 |
//
|
643 |
// switch (ke.getKeyChar())
|
644 |
// {
|
645 |
//
|
646 |
// }
|
647 |
// }
|
648 |
// else
|
649 |
// {
|
650 |
// logger.debug("CASO -> KEYCODE");
|
651 |
|
652 |
|
653 |
|
654 |
|
655 |
switch (ke.getKeyCode())
|
656 |
{ |
657 |
// determine if the pressed key is backspace (needed by the remove method)
|
658 |
case KeyEvent.VK_BACK_SPACE : |
659 |
hitBackspace=true;
|
660 |
hitBackspaceOnSelection=editor.getSelectionStart()!=editor.getSelectionEnd(); |
661 |
|
662 |
break;
|
663 |
// ignore delete key
|
664 |
case KeyEvent.VK_DELETE : // supr key in spanish keyboard |
665 |
// specialKeys = true;
|
666 |
// This avoids that popup listener could report about it detected item (Este no va)
|
667 |
// textKeyPressed = true;
|
668 |
|
669 |
|
670 |
// Remove selected text:
|
671 |
logger.debug("GETMARK: " + editor.getCaret().getMark() + "GETCARETPOSITION" + editor.getCaretPosition()); |
672 |
|
673 |
try {
|
674 |
// If no characters are selected
|
675 |
if (editor.getCaret().getMark() == editor.getCaretPosition())
|
676 |
{ |
677 |
// Remove the character which is on the right of the caret position
|
678 |
if (editor.getCaretPosition() < document.getLength())
|
679 |
document.remove(editor.getCaretPosition(), 1);
|
680 |
} |
681 |
else // If there are characters selected |
682 |
{ |
683 |
// Remove the characters which are selected
|
684 |
// int documentLength = document.getLength();
|
685 |
int length = Math.abs(editor.getCaret().getMark() - editor.getCaretPosition()); |
686 |
|
687 |
// Remove the characters which are selected
|
688 |
document.remove(Math.min(editor.getCaret().getMark(), editor.getCaretPosition()), length);
|
689 |
} |
690 |
} catch (BadLocationException e) { |
691 |
// TODO Auto-generated catch block
|
692 |
e.printStackTrace(); |
693 |
} |
694 |
|
695 |
switch(currentShowState)
|
696 |
{ |
697 |
case START_VIEW_STATE :
|
698 |
if (document.getLength() > 0) |
699 |
{ |
700 |
// If dynamic searches:
|
701 |
try {
|
702 |
// If the search will be dynamic:
|
703 |
if (isDynamicSearchConfiguration())
|
704 |
{ |
705 |
model.setWrittenText(document.getText(0, document.getLength()));
|
706 |
logger.debug("Texto que se le env?a al modelo desde SUPR: ->" + document.getText(0, document.getLength()) + "<-"); |
707 |
} |
708 |
} catch (BadLocationException e) { |
709 |
// TODO Auto-generated catch block
|
710 |
e.printStackTrace(); |
711 |
} |
712 |
|
713 |
// Indicate to the model that now is disabled the start view
|
714 |
if (currentShowState == START_VIEW_STATE)
|
715 |
{ |
716 |
model.setStartViewFlag(false);
|
717 |
currentShowState = SEARCH_VIEW_STATE; |
718 |
// updateLastSelectionItem();
|
719 |
} |
720 |
} |
721 |
break;
|
722 |
case SEARCH_VIEW_STATE :
|
723 |
// If there isn't characters
|
724 |
if (document.getLength() == 0) |
725 |
{ |
726 |
// This instruction avoids popup could failure when got invisible
|
727 |
hidePopup(); |
728 |
|
729 |
// If it's a dynamic search
|
730 |
if (isDynamicSearchConfiguration())
|
731 |
{ |
732 |
model.setWrittenText("");
|
733 |
logger.debug("Texto que se le env?a al modelo desde SUPR: ->\"\"<-");
|
734 |
} |
735 |
|
736 |
// Return to the start view state
|
737 |
model.setStartViewFlag(true);
|
738 |
currentShowState = START_VIEW_STATE; |
739 |
|
740 |
// Select the first Item
|
741 |
setSelectedIndex(0);
|
742 |
|
743 |
highlightCompletedText(0);
|
744 |
} |
745 |
else
|
746 |
{ |
747 |
// If dynamic searches:
|
748 |
try {
|
749 |
// If the search will be dynamic:
|
750 |
if (isDynamicSearchConfiguration())
|
751 |
{ |
752 |
model.setWrittenText(document.getText(0, document.getLength()));
|
753 |
logger.debug("Texto que se le env?a al modelo desde SUPR: ->" + document.getText(0, document.getLength()) + "<-"); |
754 |
} |
755 |
} catch (BadLocationException e) { |
756 |
// TODO Auto-generated catch block
|
757 |
e.printStackTrace(); |
758 |
} |
759 |
} |
760 |
break;
|
761 |
} |
762 |
|
763 |
// // Update the selected item
|
764 |
// try {
|
765 |
// document.insertString(0, document.getText(0, document.getLength()), null);
|
766 |
// } catch (BadLocationException e3) {
|
767 |
// // TODO Auto-generated catch block
|
768 |
// e3.printStackTrace();
|
769 |
// }
|
770 |
|
771 |
// // 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
|
772 |
// // text will be on black color
|
773 |
// if (!isOnlyOneColor())
|
774 |
// {
|
775 |
// if (isCaseSensitive())
|
776 |
// {
|
777 |
//// try {
|
778 |
//// if (document.getText(0, document.getLength()).compareTo(getSelectedItem().toString()) == 0)
|
779 |
// if (getSelectedIndex() != -1)
|
780 |
// comboBoxReference.getEditor().getEditorComponent().setForeground(Color.BLACK);
|
781 |
// else
|
782 |
// comboBoxReference.getEditor().getEditorComponent().setForeground(Color.RED);
|
783 |
//// } catch (BadLocationException e2) {
|
784 |
//// // TODO Auto-generated catch block
|
785 |
//// e2.printStackTrace();
|
786 |
//// }
|
787 |
// }
|
788 |
// else
|
789 |
// {
|
790 |
//// try {
|
791 |
//// if (document.getText(0, document.getLength()).compareToIgnoreCase(getSelectedItem().toString()) == 0)
|
792 |
// if (getSelectedIndex() != -1)
|
793 |
// comboBoxReference.getEditor().getEditorComponent().setForeground(Color.BLACK);
|
794 |
// else
|
795 |
// comboBoxReference.getEditor().getEditorComponent().setForeground(Color.RED);
|
796 |
//// } catch (BadLocationException e2) {
|
797 |
//// // TODO Auto-generated catch block
|
798 |
//// e2.printStackTrace();
|
799 |
//// }
|
800 |
// }
|
801 |
// }
|
802 |
|
803 |
updatePopUpView(); |
804 |
|
805 |
break;
|
806 |
case KeyEvent.VK_ENTER : |
807 |
// try {
|
808 |
// specialKeys = true;
|
809 |
logger.debug("ENTRA EN EL C?DIGO DE ENTER");
|
810 |
|
811 |
// Reports about the written text to the model
|
812 |
try {
|
813 |
// Reports about the written text to the model
|
814 |
if (model.isDynamicSearch())
|
815 |
model.setWrittenText(document.getText(0, document.getLength()));
|
816 |
} catch (BadLocationException e1) { |
817 |
// TODO Auto-generated catch block
|
818 |
e1.printStackTrace(); |
819 |
} |
820 |
|
821 |
// Indicate to the model that now is disabled the start view
|
822 |
if (currentShowState == START_VIEW_STATE)
|
823 |
{ |
824 |
model.setStartViewFlag(false);
|
825 |
currentShowState = SEARCH_VIEW_STATE; |
826 |
// updateLastSelectionItem();
|
827 |
} |
828 |
|
829 |
|
830 |
// String text = document.getText(0, document.getLength());
|
831 |
// document.remove(0, document.getLength());
|
832 |
// document.insertString(0, text, null);
|
833 |
//
|
834 |
//
|
835 |
//
|
836 |
//// // If the text doesn't completely match with the selectedItem.toString() -> if allow 2 colors -> set foreground color to red
|
837 |
//// if (!onlyOneColor)
|
838 |
//// {
|
839 |
//// if (model.isCaseSensitive())
|
840 |
//// {
|
841 |
//// if (text.compareTo(getSelectedItem().toString()) != 0)
|
842 |
//// editor.setForeground(Color.RED);
|
843 |
//// }
|
844 |
//// else
|
845 |
//// {
|
846 |
//// if (text.toUpperCase().compareTo(getSelectedItem().toString().toUpperCase()) != 0)
|
847 |
//// editor.setForeground(Color.RED);
|
848 |
//// }
|
849 |
//// }
|
850 |
// } catch (BadLocationException e1) {
|
851 |
// // TODO Auto-generated catch block
|
852 |
// e1.printStackTrace();
|
853 |
// }
|
854 |
hidePopup(); |
855 |
break;
|
856 |
case KeyEvent.VK_DOWN : |
857 |
// 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
|
858 |
// ke.consume();
|
859 |
// selecting = true; // For indicate to the 'insertString' of the 'PlainDocumentSeeker' class that an item has been selected
|
860 |
// upOrDownKeyPressed = true;
|
861 |
// continueOnStartViewState = true;
|
862 |
|
863 |
// This allows user press the key DOWN for select another item of the start view list
|
864 |
// if ((model.isStartViewFlag()) && (lastWasStartShow))
|
865 |
// lastWasStartShow = true;
|
866 |
|
867 |
// // Reports about the written text to the model
|
868 |
// try {
|
869 |
// if (model.isDynamicSearch())
|
870 |
// model.setWrittenText(document.getText(0, document.getLength()));
|
871 |
// } catch (BadLocationException e2) {
|
872 |
// // TODO Auto-generated catch block
|
873 |
// e2.printStackTrace();
|
874 |
// }
|
875 |
|
876 |
// If items are disordered -> get the index of the selected item
|
877 |
if (model.nowItemsAreDisordered())
|
878 |
updateLastSelectionItem(); |
879 |
|
880 |
if (getItemCount() > 0) |
881 |
{ |
882 |
if (getSelectedIndex() == (getItemCount()-1)) |
883 |
((JComboBox)comboBoxReference).setSelectedIndex(0); |
884 |
else
|
885 |
((JComboBox)comboBoxReference).setSelectedIndex(getSelectedIndex()+1); |
886 |
} |
887 |
|
888 |
// model.setCurrentString(getSelectedItem().toString());
|
889 |
|
890 |
// lastSelectedIndex = getSelectedIndex();
|
891 |
selectedItem = getSelectedItem(); |
892 |
showPopup(); |
893 |
break;
|
894 |
case KeyEvent.VK_UP : |
895 |
// 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
|
896 |
// ke.consume();
|
897 |
// selecting = true; // For indicate to the 'insertString' of the 'PlainDocumentSeeker' class that an item has been selected
|
898 |
// upOrDownKeyPressed = true;
|
899 |
// continueOnStartViewState = true;
|
900 |
|
901 |
// This allows user press the key UP for select another item of the start view list
|
902 |
// if ((model.isStartViewFlag()) && (lastWasStartShow))
|
903 |
// lastWasStartShow = true;
|
904 |
|
905 |
// // Reports about the written text to the model
|
906 |
// try {
|
907 |
// if (model.isDynamicSearch())
|
908 |
// model.setWrittenText(document.getText(0, document.getLength()));
|
909 |
// } catch (BadLocationException e2) {
|
910 |
// // TODO Auto-generated catch block
|
911 |
// e2.printStackTrace();
|
912 |
// }
|
913 |
|
914 |
// If items are disordered -> get the index of the selected item
|
915 |
if (model.nowItemsAreDisordered())
|
916 |
updateLastSelectionItem(); |
917 |
|
918 |
switch(getSelectedIndex())
|
919 |
{ |
920 |
case -1: case 0: |
921 |
((JComboBox)comboBoxReference).setSelectedIndex(getItemCount()-1); |
922 |
break;
|
923 |
default:
|
924 |
((JComboBox)comboBoxReference).setSelectedIndex(getSelectedIndex()-1); |
925 |
} |
926 |
|
927 |
// model.setCurrentString(getSelectedItem().toString());
|
928 |
selectedItem = getSelectedItem(); |
929 |
// lastSelectedIndex = getSelectedIndex();
|
930 |
showPopup(); |
931 |
break;
|
932 |
case KeyEvent.VK_LEFT : |
933 |
// Move the caret to left
|
934 |
if (editor.getCaretPosition() > 0) |
935 |
editor.setCaretPosition(editor.getCaretPosition() - 1);
|
936 |
else
|
937 |
{ |
938 |
// User hit backspace with the cursor positioned on the start => beep
|
939 |
if (beepEnabled)
|
940 |
getToolkit().beep(); // when available use: UIManager.getLookAndFeel().provideErrorFeedback(comboBox);
|
941 |
} |
942 |
//ke.consume();
|
943 |
break;
|
944 |
case KeyEvent.VK_RIGHT : |
945 |
// Move the caret to right
|
946 |
try {
|
947 |
if (editor.getCaretPosition() < (document.getText(0, document.getLength())).length()) |
948 |
editor.setCaretPosition(editor.getCaretPosition() + 1);
|
949 |
else
|
950 |
{ |
951 |
// User hit backspace with the cursor positioned on the start => beep
|
952 |
if (beepEnabled)
|
953 |
getToolkit().beep(); // when available use: UIManager.getLookAndFeel().provideErrorFeedback(comboBox);
|
954 |
} |
955 |
} catch (BadLocationException e) { |
956 |
// TODO Auto-generated catch block
|
957 |
e.printStackTrace(); |
958 |
} |
959 |
//ke.consume();
|
960 |
break;
|
961 |
case KeyEvent.VK_PAGE_DOWN : |
962 |
if (getItemCount() > 0) |
963 |
// if (getSelectedIndex() != -1)
|
964 |
{ |
965 |
// // Reports about the written text to the model
|
966 |
// try {
|
967 |
// if (model.isDynamicSearch())
|
968 |
// model.setWrittenText(document.getText(0, document.getLength()));
|
969 |
// } catch (BadLocationException e2) {
|
970 |
// // TODO Auto-generated catch block
|
971 |
// e2.printStackTrace();
|
972 |
// }
|
973 |
|
974 |
int index;
|
975 |
|
976 |
if (getSelectedIndex() == -1) |
977 |
index = 0;
|
978 |
else
|
979 |
index = getSelectedIndex() + POPUP_LIST_ITEMS_HEIGHT; |
980 |
|
981 |
if (index < getItemCount())
|
982 |
setSelectedIndex(index); |
983 |
else
|
984 |
setSelectedIndex(getItemCount()-1);
|
985 |
|
986 |
selectedItem = getSelectedItem(); |
987 |
} |
988 |
|
989 |
// ke.consume();
|
990 |
break;
|
991 |
case KeyEvent.VK_PAGE_UP : |
992 |
if (getItemCount() > 0) |
993 |
// if (getSelectedIndex() != -1)
|
994 |
{ |
995 |
// // Reports about the written text to the model
|
996 |
// try {
|
997 |
// if (model.isDynamicSearch())
|
998 |
// model.setWrittenText(document.getText(0, document.getLength()));
|
999 |
// } catch (BadLocationException e2) {
|
1000 |
// // TODO Auto-generated catch block
|
1001 |
// e2.printStackTrace();
|
1002 |
// }
|
1003 |
|
1004 |
int index;
|
1005 |
|
1006 |
if (getSelectedIndex() == -1) |
1007 |
index = getItemCount() - 1;
|
1008 |
else
|
1009 |
index = getSelectedIndex() - POPUP_LIST_ITEMS_HEIGHT; |
1010 |
|
1011 |
if (index > -1) |
1012 |
setSelectedIndex(index); |
1013 |
else
|
1014 |
setSelectedIndex(0);
|
1015 |
|
1016 |
selectedItem = getSelectedItem(); |
1017 |
} |
1018 |
|
1019 |
// ke.consume();
|
1020 |
break;
|
1021 |
case KeyEvent.VK_HOME : // 'Inicio' key on the spanish keyboard |
1022 |
editor.setCaretPosition(0);
|
1023 |
break;
|
1024 |
case KeyEvent.VK_END : // 'Fin' key on the spanish keyboard |
1025 |
editor.setCaretPosition(document.getLength()); |
1026 |
break;
|
1027 |
case KeyEvent.VK_TAB : |
1028 |
// specialKeys = true;
|
1029 |
|
1030 |
// This avoids that popup listener could report about it detected item
|
1031 |
textKeyPressed = true;
|
1032 |
|
1033 |
if (model.getSize() > 0) |
1034 |
{ |
1035 |
// logger.debug("SelectedIndex: " + getSelectedIndex());
|
1036 |
// boolean update = false;
|
1037 |
|
1038 |
if (((JComboBox)comboBoxReference).getSelectedIndex() == -1) |
1039 |
{ |
1040 |
// lastSelectedIndex = 0;
|
1041 |
// ((JComboBox)comboBoxReference).setSelectedIndex(0);
|
1042 |
//updatePopUp = true; // Indicate to the document.insertString that has update the popup after insert
|
1043 |
// updatePopUpView();
|
1044 |
// comboBoxReference.setSelectedIndex(0);
|
1045 |
selectedItem = comboBoxReference.getItemAt(0); //.getSelectedItem(); |
1046 |
} |
1047 |
else
|
1048 |
{ |
1049 |
// lastSelectedIndex = getSelectedIndex();
|
1050 |
// updatePopUp = true; // Indicate to the document.insertString that has update the popup after insert
|
1051 |
// updatePopUpView();
|
1052 |
selectedItem = comboBoxReference.getSelectedItem(); |
1053 |
|
1054 |
// setSelectedIndex()); // Complete the item string value in the document of the editor
|
1055 |
// update = true;
|
1056 |
} |
1057 |
|
1058 |
document.setText(selectedItem.toString()); |
1059 |
|
1060 |
// Reports about the written text to the model
|
1061 |
// try {
|
1062 |
if (model.isDynamicSearch()) {
|
1063 |
model.setWrittenText(selectedItem.toString()); |
1064 |
logger.debug("Texto que se le env?a al modelo: ->" + selectedItem.toString() + "<-"); |
1065 |
} |
1066 |
|
1067 |
// } catch (BadLocationException e2) {
|
1068 |
// // TODO Auto-generated catch block
|
1069 |
// e2.printStackTrace();
|
1070 |
// }
|
1071 |
//
|
1072 |
if (currentShowState == START_VIEW_STATE)
|
1073 |
{ |
1074 |
model.setStartViewFlag(false);
|
1075 |
currentShowState = SEARCH_VIEW_STATE; |
1076 |
// updatePopUpView(); // Update to the new popup view
|
1077 |
} |
1078 |
|
1079 |
|
1080 |
// 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
|
1081 |
comboBoxReference.getEditor().getEditorComponent().setForeground(Color.BLACK);
|
1082 |
|
1083 |
updatePopUpView(); |
1084 |
|
1085 |
|
1086 |
// document.setText(selectedItem.toString());
|
1087 |
|
1088 |
// continueOnStartViewState = false;
|
1089 |
|
1090 |
// HidePopUp + ShowPopUp to force update the popup list of items
|
1091 |
// hidePopup();
|
1092 |
// showPopup();
|
1093 |
//// hidePopup();
|
1094 |
} |
1095 |
break;
|
1096 |
case KeyEvent.VK_ESCAPE : |
1097 |
// Reset to the Start View State
|
1098 |
// model.setStartViewFlag(true);
|
1099 |
|
1100 |
// Force update the popup
|
1101 |
if (isPopupVisible())
|
1102 |
hidePopup(); |
1103 |
|
1104 |
model.setWrittenText("");
|
1105 |
selectedItem = null;
|
1106 |
|
1107 |
// currentShowState = START_VIEW_STATE;
|
1108 |
if (currentShowState == SEARCH_VIEW_STATE)
|
1109 |
{ |
1110 |
try {
|
1111 |
document.remove(0, document.getLength());
|
1112 |
} catch (BadLocationException e) { |
1113 |
// TODO Auto-generated catch block
|
1114 |
e.printStackTrace(); |
1115 |
} |
1116 |
|
1117 |
// setSelectedItem(selectedItem);
|
1118 |
// setSelectedIndex(model.getIndexOf(selectedItem));
|
1119 |
|
1120 |
// Return to the start view state
|
1121 |
model.setStartViewFlag(true);
|
1122 |
currentShowState = START_VIEW_STATE; |
1123 |
// escapeKeyPressed = true;
|
1124 |
// continueOnStartViewState = true;
|
1125 |
|
1126 |
// If items are disordered -> get the index of the selected item
|
1127 |
// if (model.nowItemsAreDisordered())
|
1128 |
// updateLastSelectionItem();
|
1129 |
|
1130 |
// Select the first Item
|
1131 |
setSelectedIndex(0);
|
1132 |
// selectFirstItemAddedToModel();
|
1133 |
|
1134 |
// updatePopUp = true;
|
1135 |
// updatePopUpView();
|
1136 |
|
1137 |
//updateLastSelectionItem();
|
1138 |
// setSelectedIndex(lastSelectedIndex);
|
1139 |
highlightCompletedText(0);
|
1140 |
// HidePopUp + ShowPopUp to force update the popup list of items
|
1141 |
// hidePopup();
|
1142 |
// showPopup();
|
1143 |
} |
1144 |
else
|
1145 |
{ |
1146 |
// Select the first Item
|
1147 |
setSelectedIndex(0);
|
1148 |
// selectFirstItemAddedToModel();
|
1149 |
|
1150 |
highlightCompletedText(0);
|
1151 |
} |
1152 |
updatePopUpView(); |
1153 |
break;
|
1154 |
case KeyEvent.VK_SHIFT : // Do nothing |
1155 |
break;
|
1156 |
case KeyEvent.VK_ALT_GRAPH : // Do nothing |
1157 |
break;
|
1158 |
case KeyEvent.VK_ALT : // Do nothing |
1159 |
break;
|
1160 |
case KeyEvent.VK_CONTROL : // Do nothing |
1161 |
break;
|
1162 |
case KeyEvent.VK_NUM_LOCK : // Do nothing |
1163 |
break;
|
1164 |
case KeyEvent.VK_CAPS_LOCK : // Do nothing |
1165 |
break;
|
1166 |
default:
|
1167 |
logger.debug("Actualiza view flag a false");
|
1168 |
|
1169 |
logger.debug("LASTITEM1 :" + lastSelectedItem);
|
1170 |
|
1171 |
logger.debug("GETMARK: " + editor.getCaret().getMark() + "GETCARETPOSITION" + editor.getCaretPosition()); |
1172 |
|
1173 |
// If it has been pressed a modifier key (Examples: ^ ` ? ?), not consume the key pressed
|
1174 |
if ((Character.getType(ke.getKeyChar()) == Character.MODIFIER_LETTER) || (Character.getType(ke.getKeyChar()) == Character.MODIFIER_SYMBOL)) |
1175 |
{ |
1176 |
logger.debug("CAR?CTER MODIFICADOR");
|
1177 |
lastWasPressedAKeyModifier = true;
|
1178 |
} |
1179 |
else
|
1180 |
{ |
1181 |
logger.debug("CAR?CTER NO MODIFICADOR");
|
1182 |
|
1183 |
if (lastWasPressedAKeyModifier)
|
1184 |
{ |
1185 |
logger.debug("PRESSED: " + editor.getText().substring(0)); |
1186 |
lastWasPressedAKeyModifier = false;
|
1187 |
} |
1188 |
|
1189 |
// if (!isOnlyOneColor())
|
1190 |
// comboBoxReference.getEditor().getEditorComponent().setForeground(Color.RED);
|
1191 |
//
|
1192 |
if (model.isDynamicSearch())
|
1193 |
{ |
1194 |
// Force update the popup
|
1195 |
|
1196 |
String text;
|
1197 |
if (editor.getCaretPosition() == editor.getCaret().getMark())
|
1198 |
text = editor.getText().substring(0, editor.getCaretPosition()) + ke.getKeyChar() + editor.getText().substring(editor.getCaretPosition(), editor.getText().length());
|
1199 |
else
|
1200 |
{ |
1201 |
text = "" + editor.getText().substring(0, (int) Math.min(editor.getCaretPosition(), editor.getCaret().getMark())) + ke.getKeyChar(); |
1202 |
text += editor.getText().substring((int) Math.max(editor.getCaretPosition(), editor.getCaret().getMark()), editor.getText().length()); |
1203 |
} |
1204 |
|
1205 |
// Reports about the written text to the model
|
1206 |
logger.debug("Texto que se le env?a KEY al modelo: ->" + text + "<-"); |
1207 |
model.setWrittenText(text); |
1208 |
|
1209 |
// Indicate to the model that now is disabled the start view
|
1210 |
if (currentShowState == START_VIEW_STATE)
|
1211 |
{ |
1212 |
model.setStartViewFlag(false);
|
1213 |
currentShowState = SEARCH_VIEW_STATE; |
1214 |
// updateLastSelectionItem();
|
1215 |
} |
1216 |
|
1217 |
|
1218 |
// textKeyPressed = false;
|
1219 |
} |
1220 |
|
1221 |
updatePopUpView(); |
1222 |
textKeyPressed = true;
|
1223 |
|
1224 |
|
1225 |
logger.debug("Key Pressed:" + ke.getKeyCode());
|
1226 |
} |
1227 |
} |
1228 |
|
1229 |
// Consume the key event for not to use the handler of the keyboard of other components
|
1230 |
// Only if it has been pressed a modifier key, not consume the key pressed
|
1231 |
if (!lastWasPressedAKeyModifier)
|
1232 |
{ |
1233 |
ke.consume(); |
1234 |
} |
1235 |
} |
1236 |
} |
1237 |
// }
|
1238 |
}; |
1239 |
|
1240 |
// keyPressed = false;
|
1241 |
} |
1242 |
|
1243 |
/**
|
1244 |
*
|
1245 |
*/
|
1246 |
private void definePopUpMenuListener(JComboBoxItemsSeekerConfigurable combo_Box) { |
1247 |
final JComboBoxItemsSeekerConfigurable comboBoxReference = combo_Box;
|
1248 |
|
1249 |
this.popupMenuListener = new PopupMenuListener() { |
1250 |
|
1251 |
/*
|
1252 |
* (non-Javadoc)
|
1253 |
* @see javax.swing.event.PopupMenuListener#popupMenuCanceled(javax.swing.event.PopupMenuEvent)
|
1254 |
*/
|
1255 |
public void popupMenuCanceled(PopupMenuEvent e) { |
1256 |
// TODO Auto-generated method stub
|
1257 |
logger.debug("MenuCANCELED");
|
1258 |
|
1259 |
// // This is used when the component has lost the focus, the popup whas in show state and is restored the focus -> update the popup
|
1260 |
// if (isPopupVisible())
|
1261 |
// popupVisible = true;
|
1262 |
// else
|
1263 |
// popupVisible = false;
|
1264 |
} |
1265 |
|
1266 |
/*
|
1267 |
* (non-Javadoc)
|
1268 |
* @see javax.swing.event.PopupMenuListener#popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent)
|
1269 |
*/
|
1270 |
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { |
1271 |
// TODO Auto-generated method stub
|
1272 |
logger.debug("MenuBECOMEINVISIBLE");
|
1273 |
// popupVisible = false;
|
1274 |
|
1275 |
if (!textKeyPressed)
|
1276 |
{ |
1277 |
if (lastSelectedItem != null) |
1278 |
{ |
1279 |
// 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
|
1280 |
if (!getSelectedItem().equals(lastSelectedItem))
|
1281 |
{ |
1282 |
logger.debug("POPUP: NO SON IGUALES");
|
1283 |
|
1284 |
// Reports about the written text to the model
|
1285 |
if (model.isDynamicSearch())
|
1286 |
{ |
1287 |
model.setWrittenText(getSelectedItem().toString()); |
1288 |
logger.debug("Texto que le envia POPUP al modelo: ->" + getSelectedItem().toString() + "<-"); |
1289 |
} |
1290 |
|
1291 |
if (currentShowState == START_VIEW_STATE)
|
1292 |
{ |
1293 |
model.setStartViewFlag(false);
|
1294 |
currentShowState = SEARCH_VIEW_STATE; |
1295 |
// updateLastSelectionItem();
|
1296 |
} |
1297 |
logger.debug("Selected index of item: " + lastSelectedItem.toString() + " is " + getSelectedIndex()); |
1298 |
// updatePopUpView();
|
1299 |
// setSelectedIndex(1);
|
1300 |
} |
1301 |
else
|
1302 |
logger.debug("POPUP: SON IGUALES");
|
1303 |
} |
1304 |
} |
1305 |
} |
1306 |
|
1307 |
/*
|
1308 |
* (non-Javadoc)
|
1309 |
* @see javax.swing.event.PopupMenuListener#popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent)
|
1310 |
*/
|
1311 |
public void popupMenuWillBecomeVisible(PopupMenuEvent e) { |
1312 |
// TODO Auto-generated method stub
|
1313 |
logger.debug("MenuBECOMEVISIBLE");
|
1314 |
// popupVisible = true;
|
1315 |
lastSelectedItem = selectedItem; // This object is used for know if the user has select a different item with the mouse
|
1316 |
} |
1317 |
}; |
1318 |
} |
1319 |
|
1320 |
|
1321 |
/**
|
1322 |
* Returns true if the model has a dynamic search configuration
|
1323 |
*
|
1324 |
* @return
|
1325 |
*/
|
1326 |
private boolean isDynamicSearchConfiguration() { |
1327 |
switch(model.getSearchBehavior())
|
1328 |
{ |
1329 |
case AbstractDefaultComboBoxItemsSeekerConfigurableModel.MAINTAIN_ORIGINAL_POSITION_DYNAMIC_SEARCH :
|
1330 |
case AbstractDefaultComboBoxItemsSeekerConfigurableModel.ORDERED_DYNAMIC_SEARCH :
|
1331 |
case AbstractDefaultComboBoxItemsSeekerConfigurableModel.DISORDERED_DYNAMIC_SEARCH :
|
1332 |
return true; |
1333 |
} |
1334 |
|
1335 |
return false; |
1336 |
} |
1337 |
|
1338 |
|
1339 |
// comboBoxReference.getUI().
|
1340 |
// for (int i=0; i<comboBoxReference.getComponentCount(); i++) {
|
1341 |
// logger.debug("Componente: " + comboBoxReference.getComponent(i).getName());
|
1342 |
// comboBoxReference.getComponent(i).addMouseListener(editorMouseListener);
|
1343 |
//
|
1344 |
// }
|
1345 |
// this.addMouseListener(editorMouseListener);
|
1346 |
// }
|
1347 |
|
1348 |
/**
|
1349 |
* Selects the first item added to the model
|
1350 |
*/
|
1351 |
public void selectFirstItemAddedToModel() { |
1352 |
// Select the first Item added
|
1353 |
Object item = model.getFirstItemAdded();
|
1354 |
if (item != null) |
1355 |
setSelectedItem(item); |
1356 |
} |
1357 |
|
1358 |
private void configureEditor(ComboBoxEditor newEditor) { |
1359 |
JTextComponent editor = (JTextComponent) this.getEditor().getEditorComponent(); |
1360 |
|
1361 |
// If there isn't any change on the editor
|
1362 |
if (editor == newEditor)
|
1363 |
return;
|
1364 |
|
1365 |
// Remove the default or last keyListener and FocusListener of the editor of this component
|
1366 |
if (editor != null) { |
1367 |
editor.removeKeyListener(this.editorKeyListener);
|
1368 |
// editor.removeMouseListener(this.editorMouseListener);
|
1369 |
editor.removeFocusListener(this.editorFocusListener);
|
1370 |
} |
1371 |
|
1372 |
// Add the new keyListener and FocusListener for this editor and sets
|
1373 |
// it document as a PlainDocument to optimize the searches of items
|
1374 |
// when user writes on this editor
|
1375 |
if (newEditor != null) { |
1376 |
editor = (JTextComponent) newEditor.getEditorComponent();
|
1377 |
editor.addKeyListener(this.editorKeyListener);
|
1378 |
// editor.addMouseListener(this.editorMouseListener);
|
1379 |
editor.addFocusListener(this.editorFocusListener);
|
1380 |
editor.setDocument(this.document);
|
1381 |
|
1382 |
// Disable the focus transversal keys (Example: CTRL+TAB) for enable the TAB key
|
1383 |
editor.setFocusTraversalKeysEnabled(false);
|
1384 |
} |
1385 |
} |
1386 |
|
1387 |
/**
|
1388 |
* Marks text since 'start' position to its end on a grey color
|
1389 |
*
|
1390 |
* @param start Start position for mark the text
|
1391 |
*/
|
1392 |
private void highlightCompletedText(int start) { |
1393 |
JTextComponent editor = (JTextComponent) this.getEditor().getEditorComponent(); |
1394 |
|
1395 |
editor.setCaretPosition(document.getLength()); |
1396 |
editor.moveCaretPosition(start); |
1397 |
} |
1398 |
|
1399 |
/**
|
1400 |
*
|
1401 |
*/
|
1402 |
private void updatePopUpView() { |
1403 |
// HidePopUp + ShowPopUp to force update the popup list of items
|
1404 |
hidePopup(); |
1405 |
showPopup(); |
1406 |
this.setVisible(true); |
1407 |
} |
1408 |
|
1409 |
////// REDEFINITION OF SOME METHODS OF JCOMBOBOX //////
|
1410 |
|
1411 |
|
1412 |
/*
|
1413 |
* (non-Javadoc)
|
1414 |
* @see javax.swing.JComboBox#actionPerformed(java.awt.event.ActionEvent)
|
1415 |
*/
|
1416 |
public void actionPerformed(ActionEvent e) |
1417 |
{ |
1418 |
super.actionPerformed(e);
|
1419 |
// logger.debug("Action-Performed");
|
1420 |
|
1421 |
// This flag indicates to the 'insertString' method of the PlainDocumentSeeker that the text of the item selected or written by the user
|
1422 |
// hasn't been written on the TextEditor of ths component
|
1423 |
selecting = false;
|
1424 |
} |
1425 |
|
1426 |
|
1427 |
/*
|
1428 |
* (non-Javadoc)
|
1429 |
* @see javax.swing.JComboBox#addItem(java.lang.Object)
|
1430 |
*/
|
1431 |
public void addItem(Object anObject) { |
1432 |
// Adds the item to this component
|
1433 |
model.addElement(anObject); |
1434 |
|
1435 |
// logger.debug("LastSelectedIndexWas: " + this.lastSelectedIndex);
|
1436 |
|
1437 |
// Set the last selected item -> by default select the first item:
|
1438 |
// This is used to show the first item that model returns when we add a new item. This also
|
1439 |
// corrects the 'mistake' produced by 'ComboBoxModel' when is added a new item and there wasn't
|
1440 |
// any selected -> that the 'ComboBoxModel' selects by default the first added (that in our case
|
1441 |
// might not be the same as the first item seen in the popup list)
|
1442 |
if (this.getSelectedIndex() != 0) |
1443 |
{ |
1444 |
this.setSelectedIndex(0); |
1445 |
this.selectedItem = this.getSelectedItem(); |
1446 |
} |
1447 |
} |
1448 |
|
1449 |
/*
|
1450 |
* (non-Javadoc)
|
1451 |
* @see javax.swing.JComboBox#removeAllItems()
|
1452 |
*/
|
1453 |
public void removeAllItems() { |
1454 |
// Removes all items from the model
|
1455 |
model.removeAllElements(); |
1456 |
|
1457 |
currentShowState = BEGINNING_VIEW_STATE; |
1458 |
} |
1459 |
|
1460 |
/*
|
1461 |
* (non-Javadoc)
|
1462 |
* @see javax.swing.JComboBox#removeItem(java.lang.Object)
|
1463 |
*/
|
1464 |
public void removeItem(Object anObject) { |
1465 |
// Removes an object from the model
|
1466 |
model.removeElement(anObject); |
1467 |
|
1468 |
if (model.getSize() == 0) |
1469 |
currentShowState = BEGINNING_VIEW_STATE; |
1470 |
} |
1471 |
|
1472 |
/*
|
1473 |
* (non-Javadoc)
|
1474 |
* @see javax.swing.JComboBox#removeItemAt(int)
|
1475 |
*/
|
1476 |
public void removeItemAt(int anIndex) { |
1477 |
// Removes an object, which is in a determinated position (index), from the model
|
1478 |
model.removeElementAt(anIndex); |
1479 |
|
1480 |
if (model.getSize() == 0) |
1481 |
currentShowState = BEGINNING_VIEW_STATE; |
1482 |
} |
1483 |
|
1484 |
// /*
|
1485 |
// * (non-Javadoc)
|
1486 |
// * @see javax.swing.JComboBox#getSelectedIndex()
|
1487 |
// */
|
1488 |
// public int getSelectedIndex() {
|
1489 |
// // ??Modificarlo??
|
1490 |
// return super.getSelectedIndex();
|
1491 |
// }
|
1492 |
|
1493 |
/*
|
1494 |
* (non-Javadoc)
|
1495 |
* @see javax.swing.JComboBox#getItemCount()
|
1496 |
*/
|
1497 |
public int getItemCount() { |
1498 |
return model.getSize();
|
1499 |
} |
1500 |
|
1501 |
/*
|
1502 |
* (non-Javadoc)
|
1503 |
* @see javax.swing.JComboBox#getItemAt(int)
|
1504 |
*/
|
1505 |
public Object getItemAt(int index) { |
1506 |
return model.getElementAt(index);
|
1507 |
} |
1508 |
|
1509 |
/*
|
1510 |
* (non-Javadoc)
|
1511 |
* @see javax.swing.JComboBox#setModel(javax.swing.ComboBoxModel)
|
1512 |
*/
|
1513 |
public void setModel(ComboBoxModel aModel) { //PROBLEMA |
1514 |
|
1515 |
// Depending on the type of class of the current object, this object will execute different code
|
1516 |
if ( (aModel instanceof ComboBoxSingularItemsSeekerConfigurableModel) || (aModel instanceof ComboBoxItemsSeekerConfigurableModel) ) |
1517 |
{ |
1518 |
// logger.debug("A");
|
1519 |
//super.setModel(aModel);
|
1520 |
super.setModel(aModel);
|
1521 |
|
1522 |
if (aModel instanceof ComboBoxSingularItemsSeekerConfigurableModel) |
1523 |
model = (ComboBoxSingularItemsSeekerConfigurableModel)super.getModel();
|
1524 |
else
|
1525 |
model = (ComboBoxItemsSeekerConfigurableModel)super.getModel();
|
1526 |
|
1527 |
// If we insert a model with items -> select by default the first
|
1528 |
if (aModel.getSize() > 0) |
1529 |
{ |
1530 |
this.setSelectedIndex(0); |
1531 |
this.selectedItem = this.getSelectedItem(); |
1532 |
// model.setCurrentString(this.getSelectedItem().toString());
|
1533 |
} |
1534 |
else
|
1535 |
{ |
1536 |
// Else -> set the default index (-1) and current showed string ("")
|
1537 |
this.selectedItem = null; |
1538 |
this.setSelectedIndex(-1); |
1539 |
// model.setCurrentString("");
|
1540 |
} |
1541 |
|
1542 |
this.configureModel();
|
1543 |
} |
1544 |
else
|
1545 |
{ |
1546 |
// If it's an instance of DefaultComboBoxModel:
|
1547 |
|
1548 |
// logger.debug("B");
|
1549 |
super.setModel(aModel);
|
1550 |
} |
1551 |
} |
1552 |
|
1553 |
/*
|
1554 |
* (non-Javadoc)
|
1555 |
* @see javax.swing.JComboBox#setSelectedIndex(int)
|
1556 |
*/
|
1557 |
public void setSelectedIndex(int anIndex) { |
1558 |
super.setSelectedIndex(anIndex);
|
1559 |
this.selectedItem = super.getSelectedItem(); |
1560 |
|
1561 |
// this.lastSelectedIndex = anIndex;
|
1562 |
|
1563 |
try {
|
1564 |
document.remove(0, document.getLength());
|
1565 |
if (getSelectedIndex() > -1) |
1566 |
document.insertString(0, getSelectedItem().toString(), null); |
1567 |
// else
|
1568 |
// this.lastSelectedIndex = -1;
|
1569 |
|
1570 |
} catch (BadLocationException e) { |
1571 |
// TODO Auto-generated catch block
|
1572 |
e.printStackTrace(); |
1573 |
} |
1574 |
} |
1575 |
|
1576 |
/*
|
1577 |
* (non-Javadoc)
|
1578 |
* @see javax.swing.JComboBox#getSelectedIndex()
|
1579 |
*/
|
1580 |
public int getSelectedIndex() { |
1581 |
Object item = this.getSelectedItem(); |
1582 |
|
1583 |
if (item == null) |
1584 |
return -1; |
1585 |
else
|
1586 |
return model.getIndexOf(item);
|
1587 |
} |
1588 |
|
1589 |
/*
|
1590 |
* (non-Javadoc)
|
1591 |
* @see javax.swing.JComboBox#setSelectedItem(java.lang.Object)
|
1592 |
*/
|
1593 |
public void setSelectedItem(Object anObject) { |
1594 |
super.setSelectedItem(anObject);
|
1595 |
this.selectedItem = super.getSelectedItem(); |
1596 |
// this.lastSelectedIndex = model.getIndexOf(anObject);
|
1597 |
// logger.debug("Posici?n: " + this.lastSelectedIndex);
|
1598 |
|
1599 |
//super.setSelectedIndex(this.lastSelectedIndex);
|
1600 |
|
1601 |
// try {
|
1602 |
// document.remove(0, document.getLength());
|
1603 |
// if (getSelectedIndex() > -1)
|
1604 |
// document.insertString(0, getSelectedItem().toString(), null);
|
1605 |
// else
|
1606 |
// this.lastSelectedIndex = -1;
|
1607 |
//
|
1608 |
// } catch (BadLocationException e) {
|
1609 |
// // TODO Auto-generated catch block
|
1610 |
// e.printStackTrace();
|
1611 |
// }
|
1612 |
} |
1613 |
|
1614 |
//
|
1615 |
////// END REDEFINITION OF SOME METHODS OF JCOMBOBOX //////
|
1616 |
|
1617 |
|
1618 |
|
1619 |
|
1620 |
|
1621 |
////// METHODS FOR THE BEHAVIOR FLAGS //////
|
1622 |
|
1623 |
/**
|
1624 |
* This method tests the configuration of the flags and returns true or false if its ok or not with the
|
1625 |
* logical behavior of this component
|
1626 |
*
|
1627 |
* @return boolean True if the configuration of the flags is oks, false if not
|
1628 |
*/
|
1629 |
public boolean testFlagsConfigurationOK() { |
1630 |
return this.model.testFlagsConfigurationOK(); |
1631 |
} |
1632 |
|
1633 |
public int getStartBehavior() { |
1634 |
return this.model.getStartBehavior(); |
1635 |
} |
1636 |
|
1637 |
public void setStartBehavior(int start_Behavior) { |
1638 |
this.model.setStartBehavior(start_Behavior);
|
1639 |
|
1640 |
if (!this.model.testFlagsConfigurationOK()) |
1641 |
JOptionPane.showMessageDialog(this, PluginServices.getText(this, "errorMessageJComboBoxItemsSeekerConfigurable"), PluginServices.getText(this, "exportJOP2Title"), JOptionPane.ERROR_MESSAGE); |
1642 |
else
|
1643 |
updateLastSelectionItem(); |
1644 |
} |
1645 |
|
1646 |
public int getSearchBehavior() { |
1647 |
return this.model.getSearchBehavior(); |
1648 |
} |
1649 |
|
1650 |
public void setSearchBehavior(int search_Behavior) { |
1651 |
this.model.setSearchBehavior(search_Behavior);
|
1652 |
|
1653 |
if (!this.model.testFlagsConfigurationOK()) |
1654 |
JOptionPane.showMessageDialog(this, PluginServices.getText(this, "errorMessageJComboBoxItemsSeekerConfigurable"), PluginServices.getText(this, "exportJOP2Title"), JOptionPane.ERROR_MESSAGE); |
1655 |
else
|
1656 |
updateLastSelectionItem(); |
1657 |
} |
1658 |
|
1659 |
|
1660 |
/**
|
1661 |
* Gets the flag of the configuration of the caseSensitive flag of this component
|
1662 |
* (true -> discriminates capital letters from small letters ,false -> don't discriminates capital letters from small letters)
|
1663 |
*
|
1664 |
* @return boolean The value of the flag
|
1665 |
*/
|
1666 |
public boolean isCaseSensitive() { |
1667 |
return this.model.isCaseSensitive(); |
1668 |
} |
1669 |
|
1670 |
/**
|
1671 |
* Sets the flag of the configuration of the caseSensitive flag of this component
|
1672 |
* (true -> discriminates capital letters from small letters ,false -> don't discriminates capital letters from small letters)
|
1673 |
*
|
1674 |
* @param boolean The value of the flag
|
1675 |
*/
|
1676 |
public void setCaseSensitive(boolean case_Sensitive) { |
1677 |
this.model.setCaseSensitive(case_Sensitive);
|
1678 |
|
1679 |
if (!this.model.testFlagsConfigurationOK()) |
1680 |
JOptionPane.showMessageDialog(this, PluginServices.getText(this, "errorMessageJComboBoxItemsSeekerConfigurable"), PluginServices.getText(this, "exportJOP2Title"), JOptionPane.ERROR_MESSAGE); |
1681 |
} |
1682 |
|
1683 |
/**
|
1684 |
* Gets the flag of the configuration of the onlyOneColor flag of this component
|
1685 |
* (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
|
1686 |
* component -> the text will be showed on red color)
|
1687 |
*
|
1688 |
* @return boolean The value of the flag
|
1689 |
*/
|
1690 |
public boolean isOnlyOneColor() { |
1691 |
return onlyOneColor;
|
1692 |
} |
1693 |
|
1694 |
|
1695 |
/**
|
1696 |
* Sets the flag of the configuration of the onlyOneColor flag of this component
|
1697 |
* (true -> discriminates capital letters from small letters ,false -> don't discriminates capital letters from small letters)
|
1698 |
*
|
1699 |
* @param boolean The value of the flag
|
1700 |
*/
|
1701 |
public void setOnlyOneColor(boolean only_One_Color) { |
1702 |
this.onlyOneColor = only_One_Color;
|
1703 |
|
1704 |
if (!this.model.testFlagsConfigurationOK()) |
1705 |
JOptionPane.showMessageDialog(this, PluginServices.getText(this, "errorMessageJComboBoxItemsSeekerConfigurable"), PluginServices.getText(this, "exportJOP2Title"), JOptionPane.ERROR_MESSAGE); |
1706 |
} |
1707 |
|
1708 |
|
1709 |
public boolean isBeepEnabled() { |
1710 |
return this.beepEnabled; |
1711 |
} |
1712 |
|
1713 |
public void setBeepEnabled(boolean beep_Enabled) { |
1714 |
this.beepEnabled = beep_Enabled;
|
1715 |
} |
1716 |
|
1717 |
public boolean isCompleteMatchedItem() { |
1718 |
return this.completeMatchedItem; |
1719 |
} |
1720 |
|
1721 |
public void setCompleteMatchedItem(boolean Complete_Matched_Item) { |
1722 |
this.completeMatchedItem = Complete_Matched_Item;
|
1723 |
} |
1724 |
|
1725 |
public boolean isAllowedRepeatedItems() { |
1726 |
return this.allowedRepeatedItems; |
1727 |
} |
1728 |
|
1729 |
public void setAllowedRepeatedItems(boolean allowed_Repeated_Items) { |
1730 |
this.allowedRepeatedItems = allowed_Repeated_Items;
|
1731 |
|
1732 |
try {
|
1733 |
AbstractDefaultComboBoxItemsSeekerConfigurableModel model_temp; |
1734 |
// if (model.getSize() > 0)
|
1735 |
// {
|
1736 |
// Add the items to the new model
|
1737 |
if (allowed_Repeated_Items)
|
1738 |
model_temp = new ComboBoxItemsSeekerConfigurableModel(model.getParentAllData());
|
1739 |
else
|
1740 |
model_temp = new ComboBoxSingularItemsSeekerConfigurableModel(model.getParentAllData());
|
1741 |
|
1742 |
// Copy the behavior of the model
|
1743 |
model_temp.setStartBehavior(this.getStartBehavior());
|
1744 |
model_temp.setSearchBehavior(this.getSearchBehavior());
|
1745 |
model_temp.setCaseSensitive(this.isCaseSensitive());
|
1746 |
|
1747 |
// Set the new model
|
1748 |
this.setModel(model_temp);
|
1749 |
// }
|
1750 |
} |
1751 |
catch(Exception e) |
1752 |
{ |
1753 |
e.printStackTrace(); |
1754 |
} |
1755 |
} |
1756 |
|
1757 |
|
1758 |
|
1759 |
////// END METHODS FOR THE BEHAVIOR FLAGS //////
|
1760 |
|
1761 |
|
1762 |
/** REVISAR COMENTARIOS Y C?DIGO
|
1763 |
* Inner class for optimize the seek of items
|
1764 |
*
|
1765 |
* @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
|
1766 |
*
|
1767 |
*/
|
1768 |
private class PlainDocumentSeeker extends PlainDocument{ |
1769 |
private JComboBoxItemsSeekerConfigurable comboBox;
|
1770 |
|
1771 |
/**
|
1772 |
* Default Constructor
|
1773 |
*/
|
1774 |
public PlainDocumentSeeker() {
|
1775 |
super();
|
1776 |
} |
1777 |
|
1778 |
/**
|
1779 |
* Sets a reference of the combo_Box
|
1780 |
*
|
1781 |
* @param combo_Box
|
1782 |
*/
|
1783 |
public void setJComboBoxItemsSeekerDynamicReference(JComboBoxItemsSeekerConfigurable combo_Box) { |
1784 |
comboBox = combo_Box; |
1785 |
} |
1786 |
|
1787 |
/* REVISAR
|
1788 |
* (non-Javadoc)
|
1789 |
* @see javax.swing.text.Document#remove(int, int)
|
1790 |
*/
|
1791 |
public void remove(int offs, int len) throws BadLocationException { |
1792 |
// return immediately when selecting an item
|
1793 |
if (selecting) return; |
1794 |
|
1795 |
if (hitBackspace)
|
1796 |
{ |
1797 |
// user hit backspace => move the selection backwards
|
1798 |
// old item keeps being selected
|
1799 |
if (offs>0) |
1800 |
{ |
1801 |
if (hitBackspaceOnSelection)
|
1802 |
offs--; |
1803 |
} |
1804 |
else
|
1805 |
{ |
1806 |
// User hit backspace with the cursor positioned on the start => beep
|
1807 |
if (beepEnabled)
|
1808 |
comboBox.getToolkit().beep(); // when available use: UIManager.getLookAndFeel().provideErrorFeedback(comboBox);
|
1809 |
} |
1810 |
|
1811 |
highlightCompletedText(offs); |
1812 |
hitBackspace=false;
|
1813 |
} |
1814 |
else
|
1815 |
{ |
1816 |
super.remove(offs, len);
|
1817 |
} |
1818 |
|
1819 |
lookupItem(document.getText(0, document.getLength()));
|
1820 |
|
1821 |
|
1822 |
// if ((!isOnlyOneColor()) && (selectedItem == null))
|
1823 |
// comboBox.getEditor().getEditorComponent().setForeground(Color.RED);
|
1824 |
|
1825 |
//
|
1826 |
// if (updatePopUp)
|
1827 |
// {
|
1828 |
// updatePopUpView();
|
1829 |
// updatePopUp = false;
|
1830 |
// }
|
1831 |
|
1832 |
} |
1833 |
|
1834 |
/*VERSI?N NUEVA
|
1835 |
* (non-Javadoc)
|
1836 |
* @see javax.swing.text.Document#insertString(int, java.lang.String, javax.swing.text.AttributeSet)
|
1837 |
*/
|
1838 |
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { |
1839 |
// super.insertString(offs, str, a);
|
1840 |
logger.debug("INSERTSTRING: " + str);
|
1841 |
// model.setCurrentString(str);
|
1842 |
|
1843 |
if (model.getParentSize() > 0) |
1844 |
{ |
1845 |
// Insert the string into the document
|
1846 |
|
1847 |
// If we aren't in the particular case of show all items at the beginning and is enabled the flag for complete the matched item:
|
1848 |
// this is for not insert two strings to the document of the editor of this component
|
1849 |
if (!((completeMatchedItem) && (model.isStartViewFlag())))
|
1850 |
// if (!completeMatchedItem)
|
1851 |
{ |
1852 |
super.insertString(offs, str, a);
|
1853 |
logger.debug("INSERT STRING DE PLAINDOCUMENTSEEKER A?ADE : " + str);
|
1854 |
} |
1855 |
|
1856 |
String text = getText(0, getLength()); |
1857 |
// Lookup and select a matching item
|
1858 |
Object item = lookupItem(text);
|
1859 |
|
1860 |
if (item != null) |
1861 |
{ |
1862 |
logger.debug("ITEM NOT NULL");
|
1863 |
|
1864 |
setSelectedItem(item); |
1865 |
|
1866 |
if (!completeMatchedItem)
|
1867 |
|
1868 |
|
1869 |
// Set the complete item value matched
|
1870 |
if (completeMatchedItem)
|
1871 |
setText(item.toString()); |
1872 |
else
|
1873 |
{ |
1874 |
editor.getEditorComponent().setForeground(Color.BLACK);
|
1875 |
setText(text); |
1876 |
} |
1877 |
|
1878 |
// Imitate no insert (later on offs will be incremented by str.length(): selection won't move forward)
|
1879 |
offs+=str.length(); |
1880 |
} |
1881 |
else
|
1882 |
{ |
1883 |
logger.debug("ITEM NULL");
|
1884 |
|
1885 |
// If no item matches -> default selection (no item selection)
|
1886 |
selectedItem = null;
|
1887 |
setSelectedIndex(-1);
|
1888 |
super.insertString(0, text, null); |
1889 |
|
1890 |
// Provide feedback to the user that his input has been received but can not be accepted
|
1891 |
if (beepEnabled)
|
1892 |
comboBox.getToolkit().beep(); // when available use: UIManager.getLookAndFeel().provideErrorFeedback(comboBox);
|
1893 |
|
1894 |
// Update the popup view for not see any item selected
|
1895 |
updatePopUpView(); |
1896 |
} |
1897 |
|
1898 |
// Select the completed part
|
1899 |
highlightCompletedText(offs); |
1900 |
|
1901 |
if (textKeyPressed)
|
1902 |
{ |
1903 |
// updatePopUpView();
|
1904 |
|
1905 |
textKeyPressed = false;
|
1906 |
} |
1907 |
} |
1908 |
} |
1909 |
|
1910 |
|
1911 |
// /*VERSI?N ANTIGUA
|
1912 |
// * (non-Javadoc)
|
1913 |
// * @see javax.swing.text.Document#insertString(int, java.lang.String, javax.swing.text.AttributeSet)
|
1914 |
// */
|
1915 |
// public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
|
1916 |
//// super.insertString(offs, str, a);
|
1917 |
// logger.debug("INSERTSTRING: " + str);
|
1918 |
//// model.setCurrentString(str);
|
1919 |
//
|
1920 |
// if (model.getParentSize() > 0)
|
1921 |
// {
|
1922 |
// // Insert the string into the document
|
1923 |
//
|
1924 |
// // 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:
|
1925 |
// // this is for not insert two strings to the document of the editor of this component
|
1926 |
// if (!((completeMatchedItem) && (model.isStartViewFlag())))
|
1927 |
// // if (!completeMatchedItem)
|
1928 |
// {
|
1929 |
// super.insertString(offs, str, a);
|
1930 |
// logger.debug("INSERT STRING DE PLAINDOCUMENTSEEKER A?ADE : " + str);
|
1931 |
// }
|
1932 |
//
|
1933 |
// // Lookup and select a matching item
|
1934 |
// Object item = lookupItem(getText(0, getLength()));
|
1935 |
//
|
1936 |
// if (item != null)
|
1937 |
// {
|
1938 |
// logger.debug("ITEM NOT NULL");
|
1939 |
// if ((completeMatchedItem) || (getSearchBehavior() == AbstractDefaultComboBoxItemsSeekerConfigurableModel.MAINTAIN_ORIGINAL_POSITION_ALL_ITEMS_SEARCH))
|
1940 |
// setSelectedItem(item);
|
1941 |
//
|
1942 |
//
|
1943 |
// // model.setCurrentString(getSelectedItem().toString());
|
1944 |
// // lastSelectedIndex = getSelectedIndex();
|
1945 |
// }
|
1946 |
// else
|
1947 |
// {
|
1948 |
// logger.debug("ITEM NULL");
|
1949 |
// // Keep old item selected if there is no match
|
1950 |
// item = comboBox.getSelectedItem();
|
1951 |
// logger.debug("ITEM QUE SELECCIONA CUANDO NULL: ->" + item.toString() + "<-");
|
1952 |
//
|
1953 |
// // Imitate no insert (later on offs will be incremented by str.length(): selection won't move forward)
|
1954 |
// offs = offs-str.length();
|
1955 |
//
|
1956 |
// // Provide feedback to the user that his input has been received but can not be accepted
|
1957 |
// if (beepEnabled)
|
1958 |
// comboBox.getToolkit().beep(); // when available use: UIManager.getLookAndFeel().provideErrorFeedback(comboBox);
|
1959 |
// }
|
1960 |
//
|
1961 |
// // Set the complete item value matched
|
1962 |
// if (completeMatchedItem)
|
1963 |
// setText(item.toString());
|
1964 |
//
|
1965 |
// // Select the completed part
|
1966 |
// highlightCompletedText(offs+str.length());
|
1967 |
//
|
1968 |
// if (textKeyPressed)
|
1969 |
// {
|
1970 |
//// updatePopUpView();
|
1971 |
//
|
1972 |
// textKeyPressed = false;
|
1973 |
// }
|
1974 |
// }
|
1975 |
// }
|
1976 |
|
1977 |
/** ?Sobra?
|
1978 |
* This method sets the string value of the item to the PlainDocument removing the previous value of the PlainDocument
|
1979 |
*
|
1980 |
* @param text The string value of the item
|
1981 |
*/
|
1982 |
private void setText(String text) { |
1983 |
try
|
1984 |
{ |
1985 |
// remove all text and insert the completed string
|
1986 |
super.remove(0, super.getLength()); |
1987 |
super.insertString(0, text, null); |
1988 |
logger.debug("SET TEXT DE PLAINDOCUMENTSEEKER A?ADE : " + text);
|
1989 |
} |
1990 |
catch (BadLocationException e) |
1991 |
{ |
1992 |
throw new RuntimeException(e.toString()); |
1993 |
} |
1994 |
} |
1995 |
|
1996 |
/** ANTIGUO
|
1997 |
* Only in red color if is allowed by the configuration and any item starts with this pattern
|
1998 |
*/
|
1999 |
private Object lookupItem(String pattern) { |
2000 |
logger.debug("LOOKUP -> patr?n ->" + pattern + "<-"); |
2001 |
// Reset the color to the default
|
2002 |
// editor.getEditorComponent().setForeground(Color.BLACK);
|
2003 |
|
2004 |
// If no item starts with the pattern => return null and set the text written on red color if it's allowed
|
2005 |
|
2006 |
Object selectedItem = model.getSelectedItem();
|
2007 |
|
2008 |
// if (selectedItem.toString().length() >= pattern.length())
|
2009 |
// {
|
2010 |
// only search for a different item if the currently selected does not match
|
2011 |
if ((selectedItem != null) && (str1StartsWithStr2(selectedItem.toString(), pattern))) |
2012 |
{ |
2013 |
editor.getEditorComponent().setForeground(Color.BLACK);
|
2014 |
|
2015 |
this.setRedColorToTextIfDoesntMatch(selectedItem.toString(), pattern);
|
2016 |
|
2017 |
return selectedItem;
|
2018 |
} |
2019 |
else
|
2020 |
{ |
2021 |
// iterate over all items
|
2022 |
for (int i=0, n=model.getSize(); i < n; i++) |
2023 |
{ |
2024 |
Object currentItem = model.getElementAt(i);
|
2025 |
|
2026 |
// current item starts with the pattern?
|
2027 |
if ((currentItem != null) && (str1StartsWithStr2(currentItem.toString(), pattern))) |
2028 |
{ |
2029 |
editor.getEditorComponent().setForeground(Color.BLACK);
|
2030 |
|
2031 |
this.setRedColorToTextIfDoesntMatch(currentItem.toString(), pattern);
|
2032 |
|
2033 |
return currentItem;
|
2034 |
} |
2035 |
} |
2036 |
} |
2037 |
|
2038 |
if (!comboBox.isOnlyOneColor()){
|
2039 |
editor.getEditorComponent().setForeground(Color.RED);
|
2040 |
logger.debug("Pone a Rojo Lookup");
|
2041 |
} |
2042 |
|
2043 |
return null; |
2044 |
} |
2045 |
|
2046 |
// /** NUEVO
|
2047 |
// * Only in red color if is allowed by the configuration and any item starts with this pattern
|
2048 |
// */
|
2049 |
// private Object lookupItem(String pattern) {
|
2050 |
// logger.debug("LOOKUP -> patr?n ->" + pattern + "<-");
|
2051 |
// // Reset the color to the default
|
2052 |
//// editor.getEditorComponent().setForeground(Color.BLACK);
|
2053 |
//
|
2054 |
// // If no item starts with the pattern => return null and set the text written on red color if it's allowed
|
2055 |
//
|
2056 |
// Object selectedItem = model.getSelectedItem();
|
2057 |
//
|
2058 |
// if (isCaseSensitive())
|
2059 |
// {
|
2060 |
// // only search for a different item if the currently selected does not match
|
2061 |
// if ((selectedItem != null) && (startsWith(selectedItem.toString(), pattern)))
|
2062 |
// {
|
2063 |
// editor.getEditorComponent().setForeground(Color.BLACK);
|
2064 |
//
|
2065 |
// if (!isOnlyOneColor()){
|
2066 |
// editor.getEditorComponent().setForeground(Color.RED);
|
2067 |
// logger.debug("Pone a Rojo Lookup");
|
2068 |
// }
|
2069 |
// return selectedItem;
|
2070 |
// }
|
2071 |
// else
|
2072 |
// {
|
2073 |
// // iterate over all items
|
2074 |
// for (int i=0, n=model.getSize(); i < n; i++)
|
2075 |
// {
|
2076 |
// Object currentItem = model.getElementAt(i);
|
2077 |
//
|
2078 |
// // current item starts with the pattern?
|
2079 |
// if ((currentItem != null) && (startsWith(currentItem.toString(), pattern)))
|
2080 |
// {
|
2081 |
// editor.getEditorComponent().setForeground(Color.BLACK);
|
2082 |
//
|
2083 |
// if (!isOnlyOneColor()){
|
2084 |
// editor.getEditorComponent().setForeground(Color.RED);
|
2085 |
// logger.debug("Pone a Rojo Lookup");
|
2086 |
// }
|
2087 |
// return currentItem;
|
2088 |
// }
|
2089 |
// }
|
2090 |
// }
|
2091 |
// }
|
2092 |
// else
|
2093 |
// {
|
2094 |
// // only search for a different item if the currently selected does not match
|
2095 |
// if ((selectedItem != null) && (startsWithIgnoreCase(selectedItem.toString(), pattern)))
|
2096 |
// {
|
2097 |
// editor.getEditorComponent().setForeground(Color.BLACK);
|
2098 |
//
|
2099 |
// if (!isOnlyOneColor()){
|
2100 |
// editor.getEditorComponent().setForeground(Color.RED);
|
2101 |
// logger.debug("Pone a Rojo Lookup");
|
2102 |
// }
|
2103 |
// return selectedItem;
|
2104 |
// }
|
2105 |
// else
|
2106 |
// {
|
2107 |
// // iterate over all items
|
2108 |
// for (int i=0, n=model.getSize(); i < n; i++)
|
2109 |
// {
|
2110 |
// Object currentItem = model.getElementAt(i);
|
2111 |
//
|
2112 |
// // current item starts with the pattern?
|
2113 |
// if ((currentItem != null) && (startsWithIgnoreCase(currentItem.toString(), pattern)))
|
2114 |
// {
|
2115 |
// editor.getEditorComponent().setForeground(Color.BLACK);
|
2116 |
//
|
2117 |
// if (!isOnlyOneColor()){
|
2118 |
// editor.getEditorComponent().setForeground(Color.RED);
|
2119 |
// logger.debug("Pone a Rojo Lookup");
|
2120 |
// }
|
2121 |
// return currentItem;
|
2122 |
// }
|
2123 |
// }
|
2124 |
// }
|
2125 |
// }
|
2126 |
//
|
2127 |
// if (!isOnlyOneColor()){
|
2128 |
// editor.getEditorComponent().setForeground(Color.RED);
|
2129 |
// logger.debug("Pone a Rojo Lookup");
|
2130 |
// }
|
2131 |
//
|
2132 |
// return null;
|
2133 |
// }
|
2134 |
|
2135 |
|
2136 |
private void setRedColorToTextIfDoesntMatch(String str1, String str2) { |
2137 |
if (!isOnlyOneColor()){
|
2138 |
if (comboBox.isCaseSensitive())
|
2139 |
{ |
2140 |
if (str1.compareTo(str2) != 0) |
2141 |
{ |
2142 |
editor.getEditorComponent().setForeground(Color.RED);
|
2143 |
logger.debug("Pone a Rojo Lookup");
|
2144 |
} |
2145 |
} |
2146 |
else
|
2147 |
{ |
2148 |
if (str1.compareToIgnoreCase(str2) != 0) |
2149 |
{ |
2150 |
editor.getEditorComponent().setForeground(Color.RED);
|
2151 |
logger.debug("Pone a Rojo Lookup");
|
2152 |
} |
2153 |
} |
2154 |
} |
2155 |
} |
2156 |
|
2157 |
|
2158 |
/**
|
2159 |
* Checks if the starts of two strings agrees
|
2160 |
*/
|
2161 |
private boolean str1StartsWithStr2(String str1, String str2) { |
2162 |
if (comboBox.isCaseSensitive())
|
2163 |
return str1.startsWith(str2);
|
2164 |
else
|
2165 |
return str1.toUpperCase().startsWith(str2.toUpperCase());
|
2166 |
} |
2167 |
} |
2168 |
} |