source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/properties/PropertiesDialog.java@ 6336

Last change on this file since 6336 was 6336, checked in by Don-vip, 11 years ago

code cleanup / robustness in edit layer handling

  • Property svn:eol-style set to native
File size: 51.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs.properties;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.Container;
8import java.awt.Font;
9import java.awt.GridBagLayout;
10import java.awt.Point;
11import java.awt.event.ActionEvent;
12import java.awt.event.InputEvent;
13import java.awt.event.KeyEvent;
14import java.awt.event.MouseAdapter;
15import java.awt.event.MouseEvent;
16import java.net.HttpURLConnection;
17import java.net.URI;
18import java.net.URLEncoder;
19import java.util.ArrayList;
20import java.util.Arrays;
21import java.util.Collection;
22import java.util.Collections;
23import java.util.Comparator;
24import java.util.EnumSet;
25import java.util.HashMap;
26import java.util.LinkedList;
27import java.util.List;
28import java.util.Map;
29import java.util.Map.Entry;
30import java.util.Set;
31import java.util.TreeMap;
32import java.util.TreeSet;
33
34import javax.swing.AbstractAction;
35import javax.swing.JComponent;
36import javax.swing.JLabel;
37import javax.swing.JPanel;
38import javax.swing.JPopupMenu;
39import javax.swing.JScrollPane;
40import javax.swing.JTable;
41import javax.swing.KeyStroke;
42import javax.swing.ListSelectionModel;
43import javax.swing.event.ListSelectionEvent;
44import javax.swing.event.ListSelectionListener;
45import javax.swing.table.DefaultTableCellRenderer;
46import javax.swing.table.DefaultTableModel;
47import javax.swing.table.TableColumnModel;
48import javax.swing.table.TableModel;
49
50import org.openstreetmap.josm.Main;
51import org.openstreetmap.josm.actions.JosmAction;
52import org.openstreetmap.josm.actions.relation.DownloadMembersAction;
53import org.openstreetmap.josm.actions.relation.DownloadSelectedIncompleteMembersAction;
54import org.openstreetmap.josm.actions.relation.SelectInRelationListAction;
55import org.openstreetmap.josm.actions.relation.SelectMembersAction;
56import org.openstreetmap.josm.actions.relation.SelectRelationAction;
57import org.openstreetmap.josm.actions.search.SearchAction.SearchMode;
58import org.openstreetmap.josm.actions.search.SearchAction.SearchSetting;
59import org.openstreetmap.josm.command.ChangeCommand;
60import org.openstreetmap.josm.command.ChangePropertyCommand;
61import org.openstreetmap.josm.command.Command;
62import org.openstreetmap.josm.data.Preferences.PreferenceChangeEvent;
63import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener;
64import org.openstreetmap.josm.data.SelectionChangedListener;
65import org.openstreetmap.josm.data.osm.IRelation;
66import org.openstreetmap.josm.data.osm.Node;
67import org.openstreetmap.josm.data.osm.OsmPrimitive;
68import org.openstreetmap.josm.data.osm.Relation;
69import org.openstreetmap.josm.data.osm.RelationMember;
70import org.openstreetmap.josm.data.osm.Tag;
71import org.openstreetmap.josm.data.osm.Way;
72import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
73import org.openstreetmap.josm.data.osm.event.DataSetListenerAdapter;
74import org.openstreetmap.josm.data.osm.event.DatasetEventManager;
75import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode;
76import org.openstreetmap.josm.data.osm.event.SelectionEventManager;
77import org.openstreetmap.josm.gui.DefaultNameFormatter;
78import org.openstreetmap.josm.gui.ExtendedDialog;
79import org.openstreetmap.josm.gui.MapFrame;
80import org.openstreetmap.josm.gui.MapView;
81import org.openstreetmap.josm.gui.PopupMenuHandler;
82import org.openstreetmap.josm.gui.SideButton;
83import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
84import org.openstreetmap.josm.gui.dialogs.properties.PresetListPanel.PresetHandler;
85import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor;
86import org.openstreetmap.josm.gui.layer.OsmDataLayer;
87import org.openstreetmap.josm.gui.tagging.TaggingPreset;
88import org.openstreetmap.josm.gui.tagging.TaggingPresetType;
89import org.openstreetmap.josm.gui.util.GuiHelper;
90import org.openstreetmap.josm.gui.util.HighlightHelper;
91import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
92import org.openstreetmap.josm.tools.GBC;
93import org.openstreetmap.josm.tools.ImageProvider;
94import org.openstreetmap.josm.tools.InputMapUtils;
95import org.openstreetmap.josm.tools.LanguageInfo;
96import org.openstreetmap.josm.tools.OpenBrowser;
97import org.openstreetmap.josm.tools.Shortcut;
98import org.openstreetmap.josm.tools.Utils;
99
100/**
101 * This dialog displays the tags of the current selected primitives.
102 *
103 * If no object is selected, the dialog list is empty.
104 * If only one is selected, all tags of this object are selected.
105 * If more than one object are selected, the sum of all tags are displayed. If the
106 * different objects share the same tag, the shared value is displayed. If they have
107 * different values, all of them are put in a combo box and the string "<different>"
108 * is displayed in italic.
109 *
110 * Below the list, the user can click on an add, modify and delete tag button to
111 * edit the table selection value.
112 *
113 * The command is applied to all selected entries.
114 *
115 * @author imi
116 */
117public class PropertiesDialog extends ToggleDialog implements SelectionChangedListener, MapView.EditLayerChangeListener, DataSetListenerAdapter.Listener, PreferenceChangedListener {
118
119 /**
120 * hook for roadsigns plugin to display a small button in the upper right corner of this dialog
121 */
122 public static final JPanel pluginHook = new JPanel();
123
124 /**
125 * The tag data of selected objects.
126 */
127 private final DefaultTableModel tagData = new ReadOnlyTableModel();
128
129 /**
130 * The membership data of selected objects.
131 */
132 private final DefaultTableModel membershipData = new ReadOnlyTableModel();
133
134 /**
135 * The tags table.
136 */
137 private final JTable tagTable = new JTable(tagData);
138
139 /**
140 * The membership table.
141 */
142 private final JTable membershipTable = new JTable(membershipData);
143
144 // Popup menus
145 private final JPopupMenu tagMenu = new JPopupMenu();
146 private final JPopupMenu membershipMenu = new JPopupMenu();
147
148 // Popup menu handlers
149 private final PopupMenuHandler tagMenuHandler = new PopupMenuHandler(tagMenu);
150 private final PopupMenuHandler membershipMenuHandler = new PopupMenuHandler(membershipMenu);
151
152 private final Map<String, Map<String, Integer>> valueCount = new TreeMap<String, Map<String, Integer>>();
153 /**
154 * This sub-object is responsible for all adding and editing of tags
155 */
156 private final TagEditHelper editHelper = new TagEditHelper(tagData, valueCount);
157
158 private final DataSetListenerAdapter dataChangedAdapter = new DataSetListenerAdapter(this);
159 private final HelpAction helpAction = new HelpAction();
160 private final PasteValueAction pasteValueAction = new PasteValueAction();
161 private final CopyValueAction copyValueAction = new CopyValueAction();
162 private final CopyKeyValueAction copyKeyValueAction = new CopyKeyValueAction();
163 private final CopyAllKeyValueAction copyAllKeyValueAction = new CopyAllKeyValueAction();
164 private final SearchAction searchActionSame = new SearchAction(true);
165 private final SearchAction searchActionAny = new SearchAction(false);
166 private final AddAction addAction = new AddAction();
167 private final EditAction editAction = new EditAction();
168 private final DeleteAction deleteAction = new DeleteAction();
169 private final JosmAction[] josmActions = new JosmAction[]{addAction, editAction, deleteAction};
170
171 // relation actions
172 private final SelectInRelationListAction setRelationSelectionAction = new SelectInRelationListAction();
173 private final SelectRelationAction selectRelationAction = new SelectRelationAction(false);
174 private final SelectRelationAction addRelationToSelectionAction = new SelectRelationAction(true);
175
176 private final DownloadMembersAction downloadMembersAction = new DownloadMembersAction();
177 private final DownloadSelectedIncompleteMembersAction downloadSelectedIncompleteMembersAction = new DownloadSelectedIncompleteMembersAction();
178
179 private final SelectMembersAction selectMembersAction = new SelectMembersAction(false);
180 private final SelectMembersAction addMembersToSelectionAction = new SelectMembersAction(true);
181
182 private final HighlightHelper highlightHelper= new HighlightHelper();
183
184 /**
185 * The Add button (needed to be able to disable it)
186 */
187 private final SideButton btnAdd = new SideButton(addAction);
188 /**
189 * The Edit button (needed to be able to disable it)
190 */
191 private final SideButton btnEdit = new SideButton(editAction);
192 /**
193 * The Delete button (needed to be able to disable it)
194 */
195 private final SideButton btnDel = new SideButton(deleteAction);
196 /**
197 * Matching preset display class
198 */
199 private final PresetListPanel presets = new PresetListPanel();
200
201 /**
202 * Text to display when nothing selected.
203 */
204 private final JLabel selectSth = new JLabel("<html><p>"
205 + tr("Select objects for which to change tags.") + "</p></html>");
206
207 private PresetHandler presetHandler = new PresetHandler() {
208 @Override public void updateTags(List<Tag> tags) {
209 Command command = TaggingPreset.createCommand(getSelection(), tags);
210 if (command != null) Main.main.undoRedo.add(command);
211 }
212
213 @Override public Collection<OsmPrimitive> getSelection() {
214 if (Main.main == null) return null;
215 if (Main.main.getCurrentDataSet() == null) return null;
216 return Main.main.getCurrentDataSet().getSelected();
217 }
218 };
219
220 // <editor-fold defaultstate="collapsed" desc="Dialog construction and helper methods">
221
222 /**
223 * Create a new PropertiesDialog
224 * @param mapFrame The parent map fram
225 */
226 public PropertiesDialog(MapFrame mapFrame) {
227 super(tr("Tags/Memberships"), "propertiesdialog", tr("Tags for selected objects."),
228 Shortcut.registerShortcut("subwindow:properties", tr("Toggle: {0}", tr("Tags/Memberships")), KeyEvent.VK_P,
229 Shortcut.ALT_SHIFT), 150, true);
230
231 setupTagsMenu();
232 buildTagsTable();
233
234 setupMembershipMenu();
235 buildMembershipTable();
236
237 // combine both tables and wrap them in a scrollPane
238 JPanel bothTables = new JPanel();
239 boolean top = Main.pref.getBoolean("properties.presets.top", true);
240 bothTables.setLayout(new GridBagLayout());
241 if(top) {
242 bothTables.add(presets, GBC.std().fill(GBC.HORIZONTAL).insets(5, 2, 5, 2).anchor(GBC.NORTHWEST));
243 double epsilon = Double.MIN_VALUE; // need to set a weight or else anchor value is ignored
244 bothTables.add(pluginHook, GBC.eol().insets(0,1,1,1).anchor(GBC.NORTHEAST).weight(epsilon, epsilon));
245 }
246 bothTables.add(selectSth, GBC.eol().fill().insets(10, 10, 10, 10));
247 bothTables.add(tagTable.getTableHeader(), GBC.eol().fill(GBC.HORIZONTAL));
248 bothTables.add(tagTable, GBC.eol().fill(GBC.BOTH));
249 bothTables.add(membershipTable.getTableHeader(), GBC.eol().fill(GBC.HORIZONTAL));
250 bothTables.add(membershipTable, GBC.eol().fill(GBC.BOTH));
251 if(!top) {
252 bothTables.add(presets, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 2, 5, 2));
253 }
254
255 setupKeyboardShortcuts();
256
257 // Let the action know when selection in the tables change
258 tagTable.getSelectionModel().addListSelectionListener(editAction);
259 membershipTable.getSelectionModel().addListSelectionListener(editAction);
260 tagTable.getSelectionModel().addListSelectionListener(deleteAction);
261 membershipTable.getSelectionModel().addListSelectionListener(deleteAction);
262
263
264 JScrollPane scrollPane = (JScrollPane) createLayout(bothTables, true, Arrays.asList(new SideButton[] {
265 this.btnAdd, this.btnEdit, this.btnDel
266 }));
267
268 MouseClickWatch mouseClickWatch = new MouseClickWatch();
269 tagTable.addMouseListener(mouseClickWatch);
270 membershipTable.addMouseListener(mouseClickWatch);
271 scrollPane.addMouseListener(mouseClickWatch);
272
273 selectSth.setPreferredSize(scrollPane.getSize());
274 presets.setSize(scrollPane.getSize());
275
276 editHelper.loadTagsIfNeeded();
277
278 Main.pref.addPreferenceChangeListener(this);
279 }
280
281 private void buildTagsTable() {
282 // setting up the tags table
283
284 tagData.setColumnIdentifiers(new String[]{tr("Key"),tr("Value")});
285 tagTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
286 tagTable.getTableHeader().setReorderingAllowed(false);
287
288 PropertiesCellRenderer cellRenderer = new PropertiesCellRenderer();
289 tagTable.getColumnModel().getColumn(0).setCellRenderer(cellRenderer);
290 tagTable.getColumnModel().getColumn(1).setCellRenderer(cellRenderer);
291 }
292
293 private void buildMembershipTable() {
294 membershipData.setColumnIdentifiers(new String[]{tr("Member Of"),tr("Role"),tr("Position")});
295 membershipTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
296
297 TableColumnModel mod = membershipTable.getColumnModel();
298 membershipTable.getTableHeader().setReorderingAllowed(false);
299 mod.getColumn(0).setCellRenderer(new DefaultTableCellRenderer() {
300 @Override public Component getTableCellRendererComponent(JTable table, Object value,
301 boolean isSelected, boolean hasFocus, int row, int column) {
302 Component c = super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
303 if (value == null)
304 return this;
305 if (c instanceof JLabel) {
306 JLabel label = (JLabel)c;
307 Relation r = (Relation)value;
308 label.setText(r.getDisplayName(DefaultNameFormatter.getInstance()));
309 if (r.isDisabledAndHidden()) {
310 label.setFont(label.getFont().deriveFont(Font.ITALIC));
311 }
312 }
313 return c;
314 }
315 });
316
317 mod.getColumn(1).setCellRenderer(new DefaultTableCellRenderer() {
318 @Override public Component getTableCellRendererComponent(JTable table, Object value,
319 boolean isSelected, boolean hasFocus, int row, int column) {
320 if (value == null)
321 return this;
322 Component c = super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
323 boolean isDisabledAndHidden = (((Relation)table.getValueAt(row, 0))).isDisabledAndHidden();
324 if (c instanceof JLabel) {
325 JLabel label = (JLabel)c;
326 MemberInfo col = (MemberInfo) value;
327
328 String text = null;
329 for (RelationMember r : col.role) {
330 if (text == null) {
331 text = r.getRole();
332 }
333 else if (!text.equals(r.getRole())) {
334 text = tr("<different>");
335 break;
336 }
337 }
338
339 label.setText(text);
340 if (isDisabledAndHidden) {
341 label.setFont(label.getFont().deriveFont(Font.ITALIC));
342 }
343 }
344 return c;
345 }
346 });
347
348 mod.getColumn(2).setCellRenderer(new DefaultTableCellRenderer() {
349 @Override public Component getTableCellRendererComponent(JTable table, Object value,
350 boolean isSelected, boolean hasFocus, int row, int column) {
351 Component c = super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
352 boolean isDisabledAndHidden = (((Relation)table.getValueAt(row, 0))).isDisabledAndHidden();
353 if (c instanceof JLabel) {
354 JLabel label = (JLabel)c;
355 label.setText(((MemberInfo) table.getValueAt(row, 1)).getPositionString());
356 if (isDisabledAndHidden) {
357 label.setFont(label.getFont().deriveFont(Font.ITALIC));
358 }
359 }
360 return c;
361 }
362 });
363 mod.getColumn(2).setPreferredWidth(20);
364 mod.getColumn(1).setPreferredWidth(40);
365 mod.getColumn(0).setPreferredWidth(200);
366 }
367
368 /**
369 * creates the popup menu @field membershipMenu and its launcher on membership table
370 */
371 private void setupMembershipMenu() {
372 // setting up the membership table
373 membershipMenuHandler.addAction(setRelationSelectionAction);
374 membershipMenuHandler.addAction(selectRelationAction);
375 membershipMenuHandler.addAction(addRelationToSelectionAction);
376 membershipMenuHandler.addAction(selectMembersAction);
377 membershipMenuHandler.addAction(addMembersToSelectionAction);
378 membershipMenu.addSeparator();
379 membershipMenuHandler.addAction(downloadMembersAction);
380 membershipMenuHandler.addAction(downloadSelectedIncompleteMembersAction);
381 membershipMenu.addSeparator();
382 membershipMenu.add(helpAction);
383
384 membershipTable.addMouseListener(new PopupMenuLauncher(membershipMenu) {
385 @Override
386 protected int checkTableSelection(JTable table, Point p) {
387 int row = super.checkTableSelection(table, p);
388 List<Relation> rels = new ArrayList<Relation>();
389 for (int i: table.getSelectedRows()) {
390 rels.add((Relation) table.getValueAt(i, 0));
391 }
392 membershipMenuHandler.setPrimitives(rels);
393 return row;
394 }
395
396 @Override
397 public void mouseClicked(MouseEvent e) {
398 //update highlights
399 if (Main.isDisplayingMapView()) {
400 int row = membershipTable.rowAtPoint(e.getPoint());
401 if (row>=0) {
402 if (highlightHelper.highlightOnly((Relation) membershipTable.getValueAt(row, 0))) {
403 Main.map.mapView.repaint();
404 }
405 }
406 }
407 super.mouseClicked(e);
408 }
409
410 @Override
411 public void mouseExited(MouseEvent me) {
412 highlightHelper.clear();
413 }
414 });
415 }
416
417 /**
418 * creates the popup menu @field tagMenu and its launcher on tag table
419 */
420 private void setupTagsMenu() {
421 tagMenu.add(pasteValueAction);
422 tagMenu.add(copyValueAction);
423 tagMenu.add(copyKeyValueAction);
424 tagMenu.add(copyAllKeyValueAction);
425 tagMenu.addSeparator();
426 tagMenu.add(searchActionAny);
427 tagMenu.add(searchActionSame);
428 tagMenu.addSeparator();
429 tagMenu.add(helpAction);
430 tagTable.addMouseListener(new PopupMenuLauncher(tagMenu));
431 }
432
433 /**
434 * Assignas all needed keys like Enter and Spacebar to most important actions
435 */
436 private void setupKeyboardShortcuts() {
437
438 // ENTER = editAction, open "edit" dialog
439 tagTable.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
440 .put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),"onTableEnter");
441 tagTable.getActionMap().put("onTableEnter",editAction);
442 membershipTable.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
443 .put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),"onTableEnter");
444 membershipTable.getActionMap().put("onTableEnter",editAction);
445
446 // INSERT button = addAction, open "add tag" dialog
447 tagTable.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
448 .put(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, 0),"onTableInsert");
449 tagTable.getActionMap().put("onTableInsert",addAction);
450
451 // unassign some standard shortcuts for JTable to allow upload / download
452 InputMapUtils.unassignCtrlShiftUpDown(tagTable, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
453
454 // unassign some standard shortcuts for correct copy-pasting, fix #8508
455 tagTable.setTransferHandler(null);
456
457 tagTable.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
458 .put(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK),"onCopy");
459 tagTable.getActionMap().put("onCopy",copyKeyValueAction);
460
461 // allow using enter to add tags for all look&feel configurations
462 InputMapUtils.enableEnter(this.btnAdd);
463
464 // DEL button = deleteAction
465 getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
466 KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0),"delete"
467 );
468 getActionMap().put("delete", deleteAction);
469
470 // F1 button = custom help action
471 getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
472 KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0), "onHelp");
473 getActionMap().put("onHelp", helpAction);
474 }
475
476 /**
477 * This simply fires up an {@link RelationEditor} for the relation shown; everything else
478 * is the editor's business.
479 *
480 * @param row
481 */
482 private void editMembership(int row) {
483 Relation relation = (Relation)membershipData.getValueAt(row, 0);
484 Main.map.relationListDialog.selectRelation(relation);
485 RelationEditor.getEditor(
486 Main.main.getEditLayer(),
487 relation,
488 ((MemberInfo) membershipData.getValueAt(row, 1)).role).setVisible(true);
489 }
490
491 private int findRow(TableModel model, Object value) {
492 for (int i=0; i<model.getRowCount(); i++) {
493 if (model.getValueAt(i, 0).equals(value))
494 return i;
495 }
496 return -1;
497 }
498
499 /**
500 * Update selection status, call @{link #selectionChanged} function.
501 */
502 private void updateSelection() {
503 if (Main.main.getCurrentDataSet() == null) {
504 selectionChanged(Collections.<OsmPrimitive>emptyList());
505 } else {
506 selectionChanged(Main.main.getCurrentDataSet().getSelected());
507 }
508 }
509
510 // </editor-fold>
511
512 // <editor-fold defaultstate="collapsed" desc="Event listeners methods">
513
514 @Override
515 public void showNotify() {
516 DatasetEventManager.getInstance().addDatasetListener(dataChangedAdapter, FireMode.IN_EDT_CONSOLIDATED);
517 SelectionEventManager.getInstance().addSelectionListener(this, FireMode.IN_EDT_CONSOLIDATED);
518 MapView.addEditLayerChangeListener(this);
519 for (JosmAction action : josmActions) {
520 Main.registerActionShortcut(action);
521 }
522 updateSelection();
523 }
524
525 @Override
526 public void hideNotify() {
527 DatasetEventManager.getInstance().removeDatasetListener(dataChangedAdapter);
528 SelectionEventManager.getInstance().removeSelectionListener(this);
529 MapView.removeEditLayerChangeListener(this);
530 for (JosmAction action : josmActions) {
531 Main.unregisterActionShortcut(action);
532 }
533 }
534
535 @Override
536 public void setVisible(boolean b) {
537 super.setVisible(b);
538 if (b && Main.main.getCurrentDataSet() != null) {
539 selectionChanged(Main.main.getCurrentDataSet().getSelected());
540 }
541 }
542
543 @Override
544 public void destroy() {
545 super.destroy();
546 Main.pref.removePreferenceChangeListener(this);
547 for (JosmAction action : josmActions) {
548 action.destroy();
549 }
550 Container parent = pluginHook.getParent();
551 if (parent != null) {
552 parent.remove(pluginHook);
553 }
554 }
555
556 @Override
557 public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
558 if (!isVisible())
559 return;
560 if (tagTable == null)
561 return; // selection changed may be received in base class constructor before init
562 if (tagTable.getCellEditor() != null) {
563 tagTable.getCellEditor().cancelCellEditing();
564 }
565
566 String selectedTag;
567 Relation selectedRelation = null;
568 selectedTag = editHelper.getChangedKey(); // select last added or last edited key by default
569 if (selectedTag == null && tagTable.getSelectedRowCount() == 1) {
570 selectedTag = (String)tagData.getValueAt(tagTable.getSelectedRow(), 0);
571 }
572 if (membershipTable.getSelectedRowCount() == 1) {
573 selectedRelation = (Relation)membershipData.getValueAt(membershipTable.getSelectedRow(), 0);
574 }
575
576 // re-load tag data
577 tagData.setRowCount(0);
578
579 final boolean displayDiscardableKeys = Main.pref.getBoolean("display.discardable-keys", false);
580 final Map<String, Integer> keyCount = new HashMap<String, Integer>();
581 final Map<String, String> tags = new HashMap<String, String>();
582 valueCount.clear();
583 EnumSet<TaggingPresetType> types = EnumSet.noneOf(TaggingPresetType.class);
584 for (OsmPrimitive osm : newSelection) {
585 types.add(TaggingPresetType.forPrimitive(osm));
586 for (String key : osm.keySet()) {
587 if (displayDiscardableKeys || !OsmPrimitive.getDiscardableKeys().contains(key)) {
588 String value = osm.get(key);
589 keyCount.put(key, keyCount.containsKey(key) ? keyCount.get(key) + 1 : 1);
590 if (valueCount.containsKey(key)) {
591 Map<String, Integer> v = valueCount.get(key);
592 v.put(value, v.containsKey(value) ? v.get(value) + 1 : 1);
593 } else {
594 TreeMap<String, Integer> v = new TreeMap<String, Integer>();
595 v.put(value, 1);
596 valueCount.put(key, v);
597 }
598 }
599 }
600 }
601 for (Entry<String, Map<String, Integer>> e : valueCount.entrySet()) {
602 int count = 0;
603 for (Entry<String, Integer> e1 : e.getValue().entrySet()) {
604 count += e1.getValue();
605 }
606 if (count < newSelection.size()) {
607 e.getValue().put("", newSelection.size() - count);
608 }
609 tagData.addRow(new Object[]{e.getKey(), e.getValue()});
610 tags.put(e.getKey(), e.getValue().size() == 1
611 ? e.getValue().keySet().iterator().next() : tr("<different>"));
612 }
613
614 membershipData.setRowCount(0);
615
616 Map<Relation, MemberInfo> roles = new HashMap<Relation, MemberInfo>();
617 for (OsmPrimitive primitive: newSelection) {
618 for (OsmPrimitive ref: primitive.getReferrers()) {
619 if (ref instanceof Relation && !ref.isIncomplete() && !ref.isDeleted()) {
620 Relation r = (Relation) ref;
621 MemberInfo mi = roles.get(r);
622 if(mi == null) {
623 mi = new MemberInfo();
624 }
625 roles.put(r, mi);
626 int i = 1;
627 for (RelationMember m : r.getMembers()) {
628 if (m.getMember() == primitive) {
629 mi.add(m, i);
630 }
631 ++i;
632 }
633 }
634 }
635 }
636
637 List<Relation> sortedRelations = new ArrayList<Relation>(roles.keySet());
638 Collections.sort(sortedRelations, new Comparator<Relation>() {
639 @Override public int compare(Relation o1, Relation o2) {
640 int comp = Boolean.valueOf(o1.isDisabledAndHidden()).compareTo(o2.isDisabledAndHidden());
641 if (comp == 0) {
642 comp = o1.getDisplayName(DefaultNameFormatter.getInstance()).compareTo(o2.getDisplayName(DefaultNameFormatter.getInstance()));
643 }
644 return comp;
645 }}
646 );
647
648 for (Relation r: sortedRelations) {
649 membershipData.addRow(new Object[]{r, roles.get(r)});
650 }
651
652 presets.updatePresets(types, tags, presetHandler);
653
654 membershipTable.getTableHeader().setVisible(membershipData.getRowCount() > 0);
655 membershipTable.setVisible(membershipData.getRowCount() > 0);
656
657 boolean hasSelection = !newSelection.isEmpty();
658 boolean hasTags = hasSelection && tagData.getRowCount() > 0;
659 boolean hasMemberships = hasSelection && membershipData.getRowCount() > 0;
660 btnAdd.setEnabled(hasSelection);
661 btnEdit.setEnabled(hasTags || hasMemberships);
662 btnDel.setEnabled(hasTags || hasMemberships);
663 tagTable.setVisible(hasTags);
664 tagTable.getTableHeader().setVisible(hasTags);
665 selectSth.setVisible(!hasSelection);
666 pluginHook.setVisible(hasSelection);
667
668 int selectedIndex;
669 if (selectedTag != null && (selectedIndex = findRow(tagData, selectedTag)) != -1) {
670 tagTable.changeSelection(selectedIndex, 0, false, false);
671 } else if (selectedRelation != null && (selectedIndex = findRow(membershipData, selectedRelation)) != -1) {
672 membershipTable.changeSelection(selectedIndex, 0, false, false);
673 } else if(hasTags) {
674 tagTable.changeSelection(0, 0, false, false);
675 } else if(hasMemberships) {
676 membershipTable.changeSelection(0, 0, false, false);
677 }
678
679 if(tagData.getRowCount() != 0 || membershipData.getRowCount() != 0) {
680 setTitle(tr("Tags: {0} / Memberships: {1}",
681 tagData.getRowCount(), membershipData.getRowCount()));
682 } else {
683 setTitle(tr("Tags / Memberships"));
684 }
685 }
686
687 /* ---------------------------------------------------------------------------------- */
688 /* EditLayerChangeListener */
689 /* ---------------------------------------------------------------------------------- */
690 @Override
691 public void editLayerChanged(OsmDataLayer oldLayer, OsmDataLayer newLayer) {
692 if (newLayer == null) editHelper.saveTagsIfNeeded();
693 // it is time to save history of tags
694 GuiHelper.runInEDT(new Runnable() {
695 @Override public void run() {
696 updateSelection();
697 }
698 });
699 }
700
701 @Override
702 public void processDatasetEvent(AbstractDatasetChangedEvent event) {
703 updateSelection();
704 }
705
706 // </editor-fold>
707
708 // <editor-fold defaultstate="collapsed" desc="Methods that are called by plugins to extend fuctionality ">
709
710 /**
711 * Replies the tag popup menu handler.
712 * @return The tag popup menu handler
713 */
714 public PopupMenuHandler getPropertyPopupMenuHandler() {
715 return tagMenuHandler;
716 }
717
718 @SuppressWarnings("unchecked")
719 public Tag getSelectedProperty() {
720 int row = tagTable.getSelectedRow();
721 if (row == -1) return null;
722 TreeMap<String, Integer> map = (TreeMap<String, Integer>) tagData.getValueAt(row, 1);
723 return new Tag(
724 tagData.getValueAt(row, 0).toString(),
725 map.size() > 1 ? "" : map.keySet().iterator().next());
726 }
727
728 /**
729 * Replies the membership popup menu handler.
730 * @return The membership popup menu handler
731 */
732 public PopupMenuHandler getMembershipPopupMenuHandler() {
733 return membershipMenuHandler;
734 }
735
736 public IRelation getSelectedMembershipRelation() {
737 int row = membershipTable.getSelectedRow();
738 return row > -1 ? (IRelation) membershipData.getValueAt(row, 0) : null;
739 }
740
741 // </editor-fold>
742
743 /**
744 * Class that watches for mouse clicks
745 * @author imi
746 */
747 public class MouseClickWatch extends MouseAdapter {
748 @Override public void mouseClicked(MouseEvent e) {
749 if (e.getClickCount() < 2)
750 {
751 // single click, clear selection in other table not clicked in
752 if (e.getSource() == tagTable) {
753 membershipTable.clearSelection();
754 } else if (e.getSource() == membershipTable) {
755 tagTable.clearSelection();
756 }
757 }
758 // double click, edit or add tag
759 else if (e.getSource() == tagTable)
760 {
761 int row = tagTable.rowAtPoint(e.getPoint());
762 if (row > -1) {
763 boolean focusOnKey = (tagTable.columnAtPoint(e.getPoint()) == 0);
764 editHelper.editTag(row, focusOnKey);
765 } else {
766 editHelper.addTag();
767 btnAdd.requestFocusInWindow();
768 }
769 } else if (e.getSource() == membershipTable) {
770 int row = membershipTable.rowAtPoint(e.getPoint());
771 if (row > -1) {
772 editMembership(row);
773 }
774 }
775 else
776 {
777 editHelper.addTag();
778 btnAdd.requestFocusInWindow();
779 }
780 }
781 @Override public void mousePressed(MouseEvent e) {
782 if (e.getSource() == tagTable) {
783 membershipTable.clearSelection();
784 } else if (e.getSource() == membershipTable) {
785 tagTable.clearSelection();
786 }
787 }
788
789 }
790
791 static class MemberInfo {
792 List<RelationMember> role = new ArrayList<RelationMember>();
793 List<Integer> position = new ArrayList<Integer>();
794 private String positionString = null;
795 void add(RelationMember r, Integer p) {
796 role.add(r);
797 position.add(p);
798 }
799 String getPositionString() {
800 if (positionString == null) {
801 Collections.sort(position);
802 positionString = String.valueOf(position.get(0));
803 int cnt = 0;
804 int last = position.get(0);
805 for (int i = 1; i < position.size(); ++i) {
806 int cur = position.get(i);
807 if (cur == last + 1) {
808 ++cnt;
809 } else if (cnt == 0) {
810 positionString += "," + cur;
811 } else {
812 positionString += "-" + last;
813 positionString += "," + cur;
814 cnt = 0;
815 }
816 last = cur;
817 }
818 if (cnt >= 1) {
819 positionString += "-" + last;
820 }
821 }
822 if (positionString.length() > 20) {
823 positionString = positionString.substring(0, 17) + "...";
824 }
825 return positionString;
826 }
827 }
828
829 /**
830 * Class that allows fast creation of read-only table model with String columns
831 */
832 public static class ReadOnlyTableModel extends DefaultTableModel {
833 @Override public boolean isCellEditable(int row, int column) {
834 return false;
835 }
836 @Override public Class<?> getColumnClass(int columnIndex) {
837 return String.class;
838 }
839 }
840
841 /**
842 * Action handling delete button press in properties dialog.
843 */
844 class DeleteAction extends JosmAction implements ListSelectionListener {
845
846 public DeleteAction() {
847 super(tr("Delete"), "dialogs/delete", tr("Delete the selected key in all objects"),
848 Shortcut.registerShortcut("properties:delete", tr("Delete Tags"), KeyEvent.VK_D,
849 Shortcut.ALT_CTRL_SHIFT), false);
850 updateEnabledState();
851 }
852
853 protected void deleteTags(int[] rows){
854 // convert list of rows to HashMap (and find gap for nextKey)
855 HashMap<String, String> tags = new HashMap<String, String>(rows.length);
856 int nextKeyIndex = rows[0];
857 for (int row : rows) {
858 String key = tagData.getValueAt(row, 0).toString();
859 if (row == nextKeyIndex + 1) {
860 nextKeyIndex = row; // no gap yet
861 }
862 tags.put(key, null);
863 }
864
865 // find key to select after deleting other tags
866 String nextKey = null;
867 int rowCount = tagData.getRowCount();
868 if (rowCount > rows.length) {
869 if (nextKeyIndex == rows[rows.length-1]) {
870 // no gap found, pick next or previous key in list
871 nextKeyIndex = (nextKeyIndex + 1 < rowCount ? nextKeyIndex + 1 : rows[0] - 1);
872 } else {
873 // gap found
874 nextKeyIndex++;
875 }
876 nextKey = (String)tagData.getValueAt(nextKeyIndex, 0);
877 }
878
879 Collection<OsmPrimitive> sel = Main.main.getCurrentDataSet().getSelected();
880 Main.main.undoRedo.add(new ChangePropertyCommand(sel, tags));
881
882 membershipTable.clearSelection();
883 if (nextKey != null) {
884 tagTable.changeSelection(findRow(tagData, nextKey), 0, false, false);
885 }
886 }
887
888 protected void deleteFromRelation(int row) {
889 Relation cur = (Relation)membershipData.getValueAt(row, 0);
890
891 Relation nextRelation = null;
892 int rowCount = membershipTable.getRowCount();
893 if (rowCount > 1) {
894 nextRelation = (Relation)membershipData.getValueAt((row + 1 < rowCount ? row + 1 : row - 1), 0);
895 }
896
897 ExtendedDialog ed = new ExtendedDialog(Main.parent,
898 tr("Change relation"),
899 new String[] {tr("Delete from relation"), tr("Cancel")});
900 ed.setButtonIcons(new String[] {"dialogs/delete.png", "cancel.png"});
901 ed.setContent(tr("Really delete selection from relation {0}?", cur.getDisplayName(DefaultNameFormatter.getInstance())));
902 ed.toggleEnable("delete_from_relation");
903 ed.showDialog();
904
905 if(ed.getValue() != 1)
906 return;
907
908 Relation rel = new Relation(cur);
909 Collection<OsmPrimitive> sel = Main.main.getCurrentDataSet().getSelected();
910 for (OsmPrimitive primitive: sel) {
911 rel.removeMembersFor(primitive);
912 }
913 Main.main.undoRedo.add(new ChangeCommand(cur, rel));
914
915 tagTable.clearSelection();
916 if (nextRelation != null) {
917 membershipTable.changeSelection(findRow(membershipData, nextRelation), 0, false, false);
918 }
919 }
920
921 @Override
922 public void actionPerformed(ActionEvent e) {
923 if (tagTable.getSelectedRowCount() > 0) {
924 int[] rows = tagTable.getSelectedRows();
925 deleteTags(rows);
926 } else if (membershipTable.getSelectedRowCount() > 0) {
927 int[] rows = membershipTable.getSelectedRows();
928 // delete from last relation to convserve row numbers in the table
929 for (int i=rows.length-1; i>=0; i--) {
930 deleteFromRelation(rows[i]);
931 }
932 }
933 }
934
935 @Override
936 protected void updateEnabledState() {
937 setEnabled(
938 (tagTable != null && tagTable.getSelectedRowCount() >= 1)
939 || (membershipTable != null && membershipTable.getSelectedRowCount() > 0)
940 );
941 }
942
943 @Override
944 public void valueChanged(ListSelectionEvent e) {
945 updateEnabledState();
946 }
947 }
948
949 /**
950 * Action handling add button press in properties dialog.
951 */
952 class AddAction extends JosmAction {
953 public AddAction() {
954 super(tr("Add"), "dialogs/add", tr("Add a new key/value pair to all objects"),
955 Shortcut.registerShortcut("properties:add", tr("Add Tag"), KeyEvent.VK_A,
956 Shortcut.ALT), false);
957 }
958
959 @Override
960 public void actionPerformed(ActionEvent e) {
961 editHelper.addTag();
962 btnAdd.requestFocusInWindow();
963 }
964 }
965
966 /**
967 * Action handling edit button press in properties dialog.
968 */
969 class EditAction extends JosmAction implements ListSelectionListener {
970 public EditAction() {
971 super(tr("Edit"), "dialogs/edit", tr("Edit the value of the selected key for all objects"),
972 Shortcut.registerShortcut("properties:edit", tr("Edit Tags"), KeyEvent.VK_S,
973 Shortcut.ALT), false);
974 updateEnabledState();
975 }
976
977 @Override
978 public void actionPerformed(ActionEvent e) {
979 if (!isEnabled())
980 return;
981 if (tagTable.getSelectedRowCount() == 1) {
982 int row = tagTable.getSelectedRow();
983 editHelper.editTag(row, false);
984 } else if (membershipTable.getSelectedRowCount() == 1) {
985 int row = membershipTable.getSelectedRow();
986 editMembership(row);
987 }
988 }
989
990 @Override
991 protected void updateEnabledState() {
992 setEnabled(
993 (tagTable != null && tagTable.getSelectedRowCount() == 1)
994 ^ (membershipTable != null && membershipTable.getSelectedRowCount() == 1)
995 );
996 }
997
998 @Override
999 public void valueChanged(ListSelectionEvent e) {
1000 updateEnabledState();
1001 }
1002 }
1003
1004 class HelpAction extends AbstractAction {
1005 public HelpAction() {
1006 putValue(NAME, tr("Go to OSM wiki for tag help (F1)"));
1007 putValue(SHORT_DESCRIPTION, tr("Launch browser with wiki help for selected object"));
1008 putValue(SMALL_ICON, ImageProvider.get("dialogs", "search"));
1009 }
1010
1011 @Override
1012 public void actionPerformed(ActionEvent e) {
1013 try {
1014 String base = Main.pref.get("url.openstreetmap-wiki", "http://wiki.openstreetmap.org/wiki/");
1015 String lang = LanguageInfo.getWikiLanguagePrefix();
1016 final List<URI> uris = new ArrayList<URI>();
1017 int row;
1018 if (tagTable.getSelectedRowCount() == 1) {
1019 row = tagTable.getSelectedRow();
1020 String key = URLEncoder.encode(tagData.getValueAt(row, 0).toString(), "UTF-8");
1021 @SuppressWarnings("unchecked")
1022 Map<String, Integer> m = (Map<String, Integer>) tagData.getValueAt(row, 1);
1023 String val = URLEncoder.encode(m.entrySet().iterator().next().getKey(), "UTF-8");
1024
1025 uris.add(new URI(String.format("%s%sTag:%s=%s", base, lang, key, val)));
1026 uris.add(new URI(String.format("%sTag:%s=%s", base, key, val)));
1027 uris.add(new URI(String.format("%s%sKey:%s", base, lang, key)));
1028 uris.add(new URI(String.format("%sKey:%s", base, key)));
1029 uris.add(new URI(String.format("%s%sMap_Features", base, lang)));
1030 uris.add(new URI(String.format("%sMap_Features", base)));
1031 } else if (membershipTable.getSelectedRowCount() == 1) {
1032 row = membershipTable.getSelectedRow();
1033 String type = URLEncoder.encode(
1034 ((Relation)membershipData.getValueAt(row, 0)).get("type"), "UTF-8"
1035 );
1036
1037 if (type != null && !type.isEmpty()) {
1038 uris.add(new URI(String.format("%s%sRelation:%s", base, lang, type)));
1039 uris.add(new URI(String.format("%sRelation:%s", base, type)));
1040 }
1041
1042 uris.add(new URI(String.format("%s%sRelations", base, lang)));
1043 uris.add(new URI(String.format("%sRelations", base)));
1044 } else {
1045 // give the generic help page, if more than one element is selected
1046 uris.add(new URI(String.format("%s%sMap_Features", base, lang)));
1047 uris.add(new URI(String.format("%sMap_Features", base)));
1048 }
1049
1050 Main.worker.execute(new Runnable(){
1051 @Override public void run() {
1052 try {
1053 // find a page that actually exists in the wiki
1054 HttpURLConnection conn;
1055 for (URI u : uris) {
1056 conn = Utils.openHttpConnection(u.toURL());
1057 conn.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000);
1058
1059 if (conn.getResponseCode() != 200) {
1060 Main.info("{0} does not exist", u);
1061 conn.disconnect();
1062 } else {
1063 int osize = conn.getContentLength();
1064 if (osize > -1) {
1065 conn.disconnect();
1066
1067 conn = Utils.openHttpConnection(new URI(u.toString()
1068 .replace("=", "%3D") /* do not URLencode whole string! */
1069 .replaceFirst("/wiki/", "/w/index.php?redirect=no&title=")
1070 ).toURL());
1071 conn.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000);
1072 }
1073
1074 /* redirect pages have different content length, but retrieving a "nonredirect"
1075 * page using index.php and the direct-link method gives slightly different
1076 * content lengths, so we have to be fuzzy.. (this is UGLY, recode if u know better)
1077 */
1078 if (conn.getContentLength() != -1 && osize > -1 && Math.abs(conn.getContentLength() - osize) > 200) {
1079 Main.info("{0} is a mediawiki redirect", u);
1080 conn.disconnect();
1081 } else {
1082 Main.info("browsing to {0}", u);
1083 conn.disconnect();
1084
1085 OpenBrowser.displayUrl(u.toString());
1086 break;
1087 }
1088 }
1089 }
1090 } catch (Exception e) {
1091 e.printStackTrace();
1092 }
1093 }
1094 });
1095 } catch (Exception e1) {
1096 e1.printStackTrace();
1097 }
1098 }
1099 }
1100
1101 class PasteValueAction extends AbstractAction {
1102 public PasteValueAction() {
1103 putValue(NAME, tr("Paste Value"));
1104 putValue(SHORT_DESCRIPTION, tr("Paste the value of the selected tag from clipboard"));
1105 }
1106
1107 @Override
1108 public void actionPerformed(ActionEvent ae) {
1109 if (tagTable.getSelectedRowCount() != 1)
1110 return;
1111 String key = tagData.getValueAt(tagTable.getSelectedRow(), 0).toString();
1112 Collection<OsmPrimitive> sel = Main.main.getCurrentDataSet().getSelected();
1113 String clipboard = Utils.getClipboardContent();
1114 if (sel.isEmpty() || clipboard == null)
1115 return;
1116 Main.main.undoRedo.add(new ChangePropertyCommand(sel, key, Utils.strip(clipboard)));
1117 }
1118 }
1119
1120 abstract class AbstractCopyAction extends AbstractAction {
1121
1122 protected abstract Collection<String> getString(OsmPrimitive p, String key);
1123
1124 @Override
1125 public void actionPerformed(ActionEvent ae) {
1126 int[] rows = tagTable.getSelectedRows();
1127 Set<String> values = new TreeSet<String>();
1128 Collection<OsmPrimitive> sel = Main.main.getCurrentDataSet().getSelected();
1129 if (rows.length == 0 || sel.isEmpty()) return;
1130
1131 for (int row: rows) {
1132 String key = tagData.getValueAt(row, 0).toString();
1133 if (sel.isEmpty())
1134 return;
1135 for (OsmPrimitive p : sel) {
1136 Collection<String> s = getString(p,key);
1137 if (s != null) {
1138 values.addAll(s);
1139 }
1140 }
1141 }
1142 if (!values.isEmpty()) {
1143 Utils.copyToClipboard(Utils.join("\n", values));
1144 }
1145 }
1146 }
1147
1148 class CopyValueAction extends AbstractCopyAction {
1149
1150 public CopyValueAction() {
1151 putValue(NAME, tr("Copy Value"));
1152 putValue(SHORT_DESCRIPTION, tr("Copy the value of the selected tag to clipboard"));
1153 }
1154
1155 @Override
1156 protected Collection<String> getString(OsmPrimitive p, String key) {
1157 String v = p.get(key);
1158 return v == null ? null : Collections.singleton(v);
1159 }
1160 }
1161
1162 class CopyKeyValueAction extends AbstractCopyAction {
1163
1164 public CopyKeyValueAction() {
1165 putValue(NAME, tr("Copy Key/Value"));
1166 putValue(SHORT_DESCRIPTION, tr("Copy the key and value of the selected tag to clipboard"));
1167 }
1168
1169 @Override
1170 protected Collection<String> getString(OsmPrimitive p, String key) {
1171 String v = p.get(key);
1172 return v == null ? null : Collections.singleton(new Tag(key, v).toString());
1173 }
1174 }
1175
1176 class CopyAllKeyValueAction extends AbstractCopyAction {
1177
1178 public CopyAllKeyValueAction() {
1179 putValue(NAME, tr("Copy all Keys/Values"));
1180 putValue(SHORT_DESCRIPTION, tr("Copy the key and value of the all tags to clipboard"));
1181 }
1182
1183 @Override
1184 protected Collection<String> getString(OsmPrimitive p, String key) {
1185 List<String> r = new LinkedList<String>();
1186 for (Entry<String, String> kv : p.getKeys().entrySet()) {
1187 r.add(new Tag(kv.getKey(), kv.getValue()).toString());
1188 }
1189 return r;
1190 }
1191 }
1192
1193 class SearchAction extends AbstractAction {
1194 final boolean sameType;
1195
1196 public SearchAction(boolean sameType) {
1197 this.sameType = sameType;
1198 if (sameType) {
1199 putValue(NAME, tr("Search Key/Value/Type"));
1200 putValue(SHORT_DESCRIPTION, tr("Search with the key and value of the selected tag, restrict to type (i.e., node/way/relation)"));
1201 } else {
1202 putValue(NAME, tr("Search Key/Value"));
1203 putValue(SHORT_DESCRIPTION, tr("Search with the key and value of the selected tag"));
1204 }
1205 }
1206
1207 @Override
1208 public void actionPerformed(ActionEvent e) {
1209 if (tagTable.getSelectedRowCount() != 1)
1210 return;
1211 String key = tagData.getValueAt(tagTable.getSelectedRow(), 0).toString();
1212 Collection<OsmPrimitive> sel = Main.main.getCurrentDataSet().getSelected();
1213 if (sel.isEmpty())
1214 return;
1215 String sep = "";
1216 StringBuilder s = new StringBuilder();
1217 for (OsmPrimitive p : sel) {
1218 String val = p.get(key);
1219 if (val == null) {
1220 continue;
1221 }
1222 String t = "";
1223 if (!sameType) {
1224 t = "";
1225 } else if (p instanceof Node) {
1226 t = "type:node ";
1227 } else if (p instanceof Way) {
1228 t = "type:way ";
1229 } else if (p instanceof Relation) {
1230 t = "type:relation ";
1231 }
1232 s.append(sep).append("(").append(t).append("\"").append(
1233 org.openstreetmap.josm.actions.search.SearchAction.escapeStringForSearch(key)).append("\"=\"").append(
1234 org.openstreetmap.josm.actions.search.SearchAction.escapeStringForSearch(val)).append("\")");
1235 sep = " OR ";
1236 }
1237
1238 SearchSetting ss = new SearchSetting(s.toString(), SearchMode.replace, true, false, false);
1239 org.openstreetmap.josm.actions.search.SearchAction.searchWithoutHistory(ss);
1240 }
1241 }
1242
1243 @Override
1244 public void preferenceChanged(PreferenceChangeEvent e) {
1245 if ("display.discardable-keys".equals(e.getKey())) {
1246 if (Main.main.getCurrentDataSet() != null) {
1247 // Re-load data when display preference change
1248 selectionChanged(Main.main.getCurrentDataSet().getSelected());
1249 }
1250 }
1251 }
1252}
Note: See TracBrowser for help on using the repository browser.