Changeset 12609 in josm for trunk/src/org/openstreetmap/josm/gui/download/OverpassQueryList.java
- Timestamp:
- 2017-08-16T22:08:22+02:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/download/OverpassQueryList.java
r12578 r12609 16 16 import java.time.LocalDateTime; 17 17 import java.time.format.DateTimeFormatter; 18 import java.time.format.DateTimeParseException; 18 19 import java.util.ArrayList; 19 20 import java.util.Collection; 20 21 import java.util.Collections; 21 22 import java.util.HashMap; 23 import java.util.List; 22 24 import java.util.Locale; 23 25 import java.util.Map; … … 74 76 private static final String KEY_KEY = "key"; 75 77 private static final String QUERY_KEY = "query"; 76 private static final String USE_COUNT_KEY = "useCount";78 private static final String LAST_EDIT_KEY = "lastEdit"; 77 79 private static final String PREFERENCE_ITEMS = "download.overpass.query"; 80 81 private static final String TRANSLATED_HISTORY = tr("history"); 78 82 79 83 /** … … 103 107 public synchronized Optional<SelectorItem> getSelectedItem() { 104 108 int idx = lsResult.getSelectedIndex(); 105 if (lsResultModel.getSize() == 0|| idx == -1) {109 if (lsResultModel.getSize() <= idx || idx == -1) { 106 110 return Optional.empty(); 107 111 } 108 112 109 113 SelectorItem item = lsResultModel.getElementAt(idx); 110 item.increaseUsageCount();111 112 this.items.values().stream()113 .filter(it -> !it.getKey().equals(item.getKey()))114 .forEach(SelectorItem::decreaseUsageCount);115 114 116 115 filterItems(); … … 128 127 public synchronized void saveHistoricItem(String query) { 129 128 boolean historicExist = this.items.values().stream() 130 .filter(it -> it.getKey().contains("history"))131 129 .map(SelectorItem::getQuery) 132 130 .anyMatch(q -> q.equals(query)); … … 134 132 if (!historicExist) { 135 133 SelectorItem item = new SelectorItem( 136 "history " + LocalDateTime.now().format(FORMAT), 137 query); 134 TRANSLATED_HISTORY + " " + LocalDateTime.now().format(FORMAT), query); 138 135 139 136 this.items.put(item.getKey(), item); … … 148 145 * updates the view. 149 146 */ 150 p rivatesynchronized void removeSelectedItem() {147 public synchronized void removeSelectedItem() { 151 148 Optional<SelectorItem> it = this.getSelectedItem(); 152 149 … … 160 157 SelectorItem item = it.get(); 161 158 if (this.items.remove(item.getKey(), item)) { 159 clearSelection(); 162 160 savePreferences(); 163 161 filterItems(); … … 169 167 * to preferences and updates the view. 170 168 */ 171 p rivatesynchronized void editSelectedItem() {169 public synchronized void editSelectedItem() { 172 170 Optional<SelectorItem> it = this.getSelectedItem(); 173 171 … … 185 183 tr("Edit item"), 186 184 item, 187 tr("Save") );185 tr("Save"), tr("Cancel")); 188 186 dialog.showDialog(); 189 187 … … 202 200 * and updates the view. 203 201 */ 204 p rivatesynchronized void createNewItem() {202 public synchronized void createNewItem() { 205 203 EditItemDialog dialog = new EditItemDialog(componentParent, tr("Add snippet"), tr("Add")); 206 204 dialog.showDialog(); … … 208 206 Optional<SelectorItem> newItem = dialog.getOutputItem(); 209 207 newItem.ifPresent(i -> { 210 items.put(i.getKey(), new SelectorItem(i.getKey(), i.getQuery()));208 items.put(i.getKey(), i); 211 209 savePreferences(); 212 210 filterItems(); … … 222 220 protected void filterItems() { 223 221 String text = edSearchText.getText().toLowerCase(Locale.ENGLISH); 224 225 super.lsResultModel.setItems(this.items.values().stream()222 List<SelectorItem> matchingItems = this.items.values().stream() 223 .sorted((i1, i2) -> i2.getLastEdit().compareTo(i1.getLastEdit())) 226 224 .filter(item -> item.getKey().contains(text)) 227 .collect(Collectors.toList())); 225 .collect(Collectors.toList()); 226 227 super.lsResultModel.setItems(matchingItems); 228 228 } 229 229 … … 248 248 it.put(KEY_KEY, item.getKey()); 249 249 it.put(QUERY_KEY, item.getQuery()); 250 it.put( USE_COUNT_KEY, Integer.toString(item.getUsageCount()));250 it.put(LAST_EDIT_KEY, item.getLastEdit().format(FORMAT)); 251 251 252 252 toSave.add(it); … … 266 266 267 267 for (Map<String, String> entry : toRetrieve) { 268 String key = entry.get(KEY_KEY); 269 String query = entry.get(QUERY_KEY); 270 int usageCount = Integer.parseInt(entry.get(USE_COUNT_KEY)); 271 272 result.put(key, new SelectorItem(key, query, usageCount)); 268 try { 269 String key = entry.get(KEY_KEY); 270 String query = entry.get(QUERY_KEY); 271 String lastEditText = entry.get(LAST_EDIT_KEY); 272 // Compatibility: Some entries may not have a last edit set. 273 LocalDateTime lastEdit = lastEditText == null ? LocalDateTime.MIN : LocalDateTime.parse(lastEditText, FORMAT); 274 275 result.put(key, new SelectorItem(key, query, lastEdit)); 276 } catch (IllegalArgumentException | DateTimeParseException e) { 277 // skip any corrupted item 278 Main.error(e); 279 } 273 280 } 274 281 … … 412 419 private final JTextField name; 413 420 private final JosmTextArea query; 414 private final int initialNameHash;415 421 416 422 private final transient AbstractTextComponentValidator queryValidator; … … 419 425 private static final int SUCCESS_BTN = 0; 420 426 private static final int CANCEL_BTN = 1; 427 428 private final transient SelectorItem itemToEdit; 421 429 422 430 /** … … 437 445 super(parent, title, buttonTexts); 438 446 439 String nameToEdit = itemToEdit != null ? itemToEdit.getKey() : ""; 440 String queryToEdit = itemToEdit != null ? itemToEdit.getQuery() : ""; 441 this.initialNameHash = nameToEdit.hashCode(); 447 this.itemToEdit = itemToEdit; 448 449 String nameToEdit = itemToEdit == null ? "" : itemToEdit.getKey(); 450 String queryToEdit = itemToEdit == null ? "" : itemToEdit.getQuery(); 442 451 443 452 this.name = new JTextField(nameToEdit); … … 458 467 public boolean isValid() { 459 468 String currentName = name.getText(); 460 int currentHash = currentName.hashCode(); 461 462 return !Utils.isStripEmpty(currentName) && 463 !(currentHash != initialNameHash && 464 items.containsKey(currentName)); 469 470 boolean notEmpty = !Utils.isStripEmpty(currentName); 471 boolean exist = !currentName.equals(nameToEdit) && 472 items.containsKey(currentName); 473 474 return notEmpty && !exist; 465 475 } 466 476 }; … … 478 488 panel.add(queryScrollPane, constraint); 479 489 480 setDefaultButton(SUCCESS_BTN );481 setCancelButton(CANCEL_BTN );490 setDefaultButton(SUCCESS_BTN + 1); 491 setCancelButton(CANCEL_BTN + 1); 482 492 setPreferredSize(new Dimension(400, 400)); 483 493 setContent(panel, false); … … 501 511 tr("Warning"), 502 512 JOptionPane.WARNING_MESSAGE); 513 514 return; 503 515 } else if (!this.queryValidator.isValid()) { 504 516 JOptionPane.showMessageDialog( … … 507 519 tr("Warning"), 508 520 JOptionPane.WARNING_MESSAGE); 509 } else { 510 this.outputItem = Optional.of(new SelectorItem(this.name.getText(), this.query.getText())); 511 super.buttonAction(buttonIndex, evt); 512 } 513 } else { 514 super.buttonAction(buttonIndex, evt); 515 } 521 522 return; 523 } else if (this.itemToEdit != null) { // editing the item 524 String newKey = this.name.getText(); 525 String newQuery = this.query.getText(); 526 527 String itemKey = this.itemToEdit.getKey(); 528 String itemQuery = this.itemToEdit.getQuery(); 529 530 this.outputItem = Optional.of(new SelectorItem( 531 this.name.getText(), 532 this.query.getText(), 533 !newKey.equals(itemKey) || !newQuery.equals(itemQuery) 534 ? LocalDateTime.now() 535 : this.itemToEdit.getLastEdit())); 536 537 } else { // creating new 538 this.outputItem = Optional.of(new SelectorItem( 539 this.name.getText(), 540 this.query.getText())); 541 } 542 } 543 544 super.buttonAction(buttonIndex, evt); 516 545 } 517 546 } … … 524 553 private final String itemKey; 525 554 private final String query; 526 private int usageCount;555 private final LocalDateTime lastEdit; 527 556 528 557 /** … … 534 563 */ 535 564 public SelectorItem(String key, String query) { 536 this(key, query, 1);565 this(key, query, LocalDateTime.now()); 537 566 } 538 567 … … 541 570 * @param key The key of this item. 542 571 * @param query The query of the item. 543 * @param usageCount The number of times this query was used.572 * @param lastEdit The latest when the item was 544 573 * @exception NullPointerException if any parameter is {@code null}. 545 574 * @exception IllegalArgumentException if any parameter is empty. 546 575 */ 547 public SelectorItem(String key, String query, int usageCount) { 548 Objects.requireNonNull(key); 549 Objects.requireNonNull(query); 576 public SelectorItem(String key, String query, LocalDateTime lastEdit) { 577 Objects.requireNonNull(key, "The name of the item cannot be null"); 578 Objects.requireNonNull(query, "The query of the item cannot be null"); 579 Objects.requireNonNull(lastEdit, "The last edit date time cannot be null"); 550 580 551 581 if (Utils.isStripEmpty(key)) { … … 558 588 this.itemKey = key; 559 589 this.query = query; 560 this. usageCount = usageCount;590 this.lastEdit = lastEdit; 561 591 } 562 592 … … 578 608 579 609 /** 580 * Gets the number of times the query was used by the user. 581 * @return The usage count of this item. 582 */ 583 public int getUsageCount() { 584 return this.usageCount; 585 } 586 587 /** 588 * Increments the {@link SelectorItem#usageCount} by one till 589 * it reaches {@link Integer#MAX_VALUE}. 590 */ 591 public void increaseUsageCount() { 592 if (this.usageCount < Integer.MAX_VALUE) { 593 this.usageCount++; 594 } 595 } 596 597 /** 598 * Decrements the {@link SelectorItem#usageCount} ny one till 599 * it reaches 0. 600 */ 601 public void decreaseUsageCount() { 602 if (this.usageCount > 0) { 603 this.usageCount--; 604 } 610 * Gets the latest date time when the item was created/changed. 611 * @return The latest date time when the item was created/changed. 612 */ 613 public LocalDateTime getLastEdit() { 614 return lastEdit; 605 615 } 606 616
Note:
See TracChangeset
for help on using the changeset viewer.