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