- Timestamp:
- 2014-08-16T03:15:37+02:00 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPresetSelector.java
r7324 r7412 16 16 import java.awt.event.MouseEvent; 17 17 import java.util.ArrayList; 18 import java.util.Arrays; 18 19 import java.util.Collection; 19 20 import java.util.Collections; … … 22 23 import java.util.Iterator; 23 24 import java.util.List; 25 import java.util.Objects; 24 26 25 27 import javax.swing.AbstractAction; … … 69 71 private static final BooleanProperty ONLY_APPLICABLE = new BooleanProperty("taggingpreset.dialog.only-applicable-to-selection", true); 70 72 71 private JosmTextField edSearchText;72 private JList<TaggingPreset> lsResult;73 private JCheckBox ckOnlyApplicable;74 private JCheckBox ckSearchInTags;73 private final JosmTextField edSearchText; 74 private final JList<TaggingPreset> lsResult; 75 private final JCheckBox ckOnlyApplicable; 76 private final JCheckBox ckSearchInTags; 75 77 private final EnumSet<TaggingPresetType> typesInSelection = EnumSet.noneOf(TaggingPresetType.class); 76 78 private boolean typesInSelectionDirty = true; 77 79 private final PresetClassifications classifications = new PresetClassifications(); 78 private ResultListModel lsResultModel = new ResultListModel(); 80 private final ResultListModel lsResultModel = new ResultListModel(); 81 82 private final List<ListSelectionListener> listSelectionListeners = new ArrayList<>(); 79 83 80 84 private ActionListener dblClickListener; … … 96 100 private List<PresetClassification> presets = new ArrayList<>(); 97 101 98 public void setPresets(List<PresetClassification> presets) {102 public synchronized void setPresets(List<PresetClassification> presets) { 99 103 this.presets = presets; 100 104 fireContentsChanged(this, 0, Integer.MAX_VALUE); 101 105 } 102 106 103 public List<PresetClassification> getPresets() {104 return presets;105 }106 107 107 @Override 108 public TaggingPreset getElementAt(int index) {108 public synchronized TaggingPreset getElementAt(int index) { 109 109 return presets.get(index).preset; 110 110 } 111 111 112 112 @Override 113 public int getSize() {113 public synchronized int getSize() { 114 114 return presets.size(); 115 } 116 117 public synchronized boolean isEmpty() { 118 return presets.isEmpty(); 115 119 } 116 120 } … … 246 250 add(edSearchText, BorderLayout.NORTH); 247 251 248 lsResult = new JList<>(); 249 lsResult.setModel(lsResultModel); 252 lsResult = new JList<>(lsResultModel); 250 253 lsResult.setCellRenderer(new ResultListCellRenderer()); 251 254 lsResult.addMouseListener(new MouseAdapter() { … … 276 279 } 277 280 }); 281 } else { 282 ckOnlyApplicable = null; 278 283 } 279 284 … … 289 294 }); 290 295 pnChecks.add(ckSearchInTags); 296 } else { 297 ckSearchInTags = null; 291 298 } 292 299 … … 306 313 } 307 314 308 private void selectPreset(int newIndex) {315 private synchronized void selectPreset(int newIndex) { 309 316 if (newIndex < 0) { 310 317 newIndex = 0; … … 320 327 * Search expression can be in form: "group1/group2/name" where names can contain multiple words 321 328 */ 322 private void filterPresets() {329 private synchronized void filterPresets() { 323 330 //TODO Save favorites to file 324 331 String text = edSearchText.getText().toLowerCase(); … … 331 338 text, onlyApplicable, inTags, getTypesInSelection(), selected); 332 339 340 TaggingPreset oldPreset = getSelectedPreset(); 333 341 lsResultModel.setPresets(result); 334 342 TaggingPreset newPreset = getSelectedPreset(); 343 if (!Objects.equals(oldPreset, newPreset)) { 344 int[] indices = lsResult.getSelectedIndices(); 345 System.out.println(newPreset + " / "+Arrays.toString(indices)); 346 for (ListSelectionListener listener : listSelectionListeners) { 347 listener.valueChanged(new ListSelectionEvent(lsResult, lsResult.getSelectedIndex(), 348 indices.length > 0 ? indices[indices.length-1] : -1, false)); 349 } 350 } 335 351 } 336 352 … … 456 472 } 457 473 458 public void init() {474 public synchronized void init() { 459 475 if (ckOnlyApplicable != null) { 460 476 ckOnlyApplicable.setEnabled(!getTypesInSelection().isEmpty()); 461 477 ckOnlyApplicable.setSelected(!getTypesInSelection().isEmpty() && ONLY_APPLICABLE.get()); 462 478 } 479 listSelectionListeners.clear(); 463 480 edSearchText.setText(""); 464 481 filterPresets(); … … 471 488 } 472 489 473 public void clearSelection() {490 public synchronized void clearSelection() { 474 491 lsResult.getSelectionModel().clearSelection(); 475 492 } … … 491 508 * @return selected preset (as action) 492 509 */ 493 public TaggingPreset getSelectedPreset() { 494 List<PresetClassification> presets = lsResultModel.getPresets(); 495 if (presets.isEmpty()) return null; 510 public synchronized TaggingPreset getSelectedPreset() { 511 if (lsResultModel.isEmpty()) return null; 496 512 int idx = lsResult.getSelectedIndex(); 497 if (idx == -1) {513 if (idx < 0 || idx >= lsResultModel.getSize()) { 498 514 idx = 0; 499 515 } 500 TaggingPreset preset = presets.get(idx).preset;516 TaggingPreset preset = lsResultModel.getElementAt(idx); 501 517 for (PresetClassification pc: classifications) { 502 518 if (pc.preset == preset) { … … 509 525 } 510 526 511 public void setSelectedPreset(TaggingPreset p) {527 public synchronized void setSelectedPreset(TaggingPreset p) { 512 528 lsResult.setSelectedValue(p, true); 513 529 } 514 530 515 public int getItemCount() {531 public synchronized int getItemCount() { 516 532 return lsResultModel.getSize(); 517 533 } … … 525 541 } 526 542 527 public void addSelectionListener(final ActionListener selectListener) { 528 lsResult.getSelectionModel().addListSelectionListener(new ListSelectionListener() { 529 @Override 530 public void valueChanged(ListSelectionEvent e) { 531 if (!e.getValueIsAdjusting()) 532 selectListener.actionPerformed(null); 533 } 534 }); 543 /** 544 * Adds a selection listener to the presets list. 545 * @param selectListener The list selection listener 546 * @since 7412 547 */ 548 public synchronized void addSelectionListener(ListSelectionListener selectListener) { 549 lsResult.getSelectionModel().addListSelectionListener(selectListener); 550 listSelectionListeners.add(selectListener); 551 } 552 553 /** 554 * Removes a selection listener from the presets list. 555 * @param selectListener The list selection listener 556 * @since 7412 557 */ 558 public synchronized void removeSelectionListener(ListSelectionListener selectListener) { 559 listSelectionListeners.remove(selectListener); 560 lsResult.getSelectionModel().removeListSelectionListener(selectListener); 535 561 } 536 562 }
Note:
See TracChangeset
for help on using the changeset viewer.