Changeset 17714 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2021-04-07T23:41:13+02:00 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/dialogs/NotesDialog.java
r17712 r17714 17 17 import java.util.List; 18 18 import java.util.Objects; 19 import java.util.function.Predicate; 20 import java.util.regex.Pattern; 19 21 20 22 import javax.swing.AbstractAction; … … 49 51 import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent; 50 52 import org.openstreetmap.josm.gui.layer.NoteLayer; 53 import org.openstreetmap.josm.gui.util.DocumentAdapter; 54 import org.openstreetmap.josm.gui.widgets.DisableShortcutsOnFocusGainedTextField; 55 import org.openstreetmap.josm.gui.widgets.JosmTextField; 51 56 import org.openstreetmap.josm.spi.preferences.Config; 52 57 import org.openstreetmap.josm.tools.ImageProvider; … … 64 69 private NoteTableModel model; 65 70 private JList<Note> displayList; 71 private final JosmTextField filter = setupFilter(); 66 72 private final AddCommentAction addCommentAction; 67 73 private final CloseAction closeAction; … … 114 120 115 121 JPanel pane = new JPanel(new BorderLayout()); 122 pane.add(filter, BorderLayout.NORTH); 116 123 pane.add(new JScrollPane(displayList), BorderLayout.CENTER); 117 124 … … 222 229 public Note getSelectedNote() { 223 230 return noteData != null ? noteData.getSelectedNote() : null; 231 } 232 233 private JosmTextField setupFilter() { 234 final JosmTextField f = new DisableShortcutsOnFocusGainedTextField(); 235 f.setToolTipText(tr("Note filter")); 236 f.getDocument().addDocumentListener(DocumentAdapter.create(ignore -> { 237 String text = f.getText(); 238 model.setFilter(note -> matchesNote(text, note)); 239 })); 240 return f; 241 } 242 243 static boolean matchesNote(String filter, Note note) { 244 if (filter == null || filter.isEmpty()) { 245 return true; 246 } 247 return Pattern.compile("\\s+").splitAsStream(filter).allMatch(string -> { 248 switch (string) { 249 case "open": 250 return note.getState() == State.OPEN; 251 case "closed": 252 return note.getState() == State.CLOSED; 253 case "reopened": 254 return note.getLastComment().getNoteAction() == NoteComment.Action.REOPENED; 255 case "new": 256 return note.getId() < 0; 257 case "modified": 258 return note.getLastComment().isNew(); 259 default: 260 return note.getComments().toString().contains(string); 261 } 262 }); 224 263 } 225 264 … … 270 309 271 310 class NoteTableModel extends AbstractListModel<Note> { 272 private final transient List<Note> data; 273 274 /** 275 * Constructs a new {@code NoteTableModel}. 276 */ 277 NoteTableModel() { 278 data = new ArrayList<>(); 279 } 311 private final transient List<Note> data = new ArrayList<>(); 312 private final transient List<Note> filteredData = new ArrayList<>(); 313 private transient Predicate<Note> filter; 280 314 281 315 @Override 282 316 public int getSize() { 283 if (data == null) { 284 return 0; 285 } 286 return data.size(); 317 return filteredData.size(); 287 318 } 288 319 289 320 @Override 290 321 public Note getElementAt(int index) { 291 return data.get(index); 322 return filteredData.get(index); 323 } 324 325 public void setFilter(Predicate<Note> filter) { 326 this.filter = filter; 327 filteredData.clear(); 328 if (filter == null) { 329 filteredData.addAll(data); 330 } else { 331 data.stream().filter(filter).forEach(filteredData::add); 332 } 333 fireContentsChanged(this, 0, getSize()); 334 setTitle(data.isEmpty() 335 ? tr("Notes") 336 : tr("Notes: {0}/{1}", filteredData.size(), data.size())); 292 337 } 293 338 … … 295 340 data.clear(); 296 341 data.addAll(noteList); 297 fireContentsChanged(this, 0, noteList.size());342 setFilter(filter); 298 343 } 299 344 … … 301 346 displayList.clearSelection(); 302 347 data.clear(); 303 fireIntervalRemoved(this, 0, getSize());348 setFilter(filter); 304 349 } 305 350 }
Note:
See TracChangeset
for help on using the changeset viewer.