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.net.HttpURLConnection;
|
---|
17 | import java.net.URI;
|
---|
18 | import java.net.URLEncoder;
|
---|
19 | import java.util.ArrayList;
|
---|
20 | import java.util.Arrays;
|
---|
21 | import java.util.Collection;
|
---|
22 | import java.util.Collections;
|
---|
23 | import java.util.Comparator;
|
---|
24 | import java.util.EnumSet;
|
---|
25 | import java.util.HashMap;
|
---|
26 | import java.util.LinkedList;
|
---|
27 | import java.util.List;
|
---|
28 | import java.util.Map;
|
---|
29 | import java.util.Map.Entry;
|
---|
30 | import java.util.Set;
|
---|
31 | import java.util.TreeMap;
|
---|
32 | import java.util.TreeSet;
|
---|
33 |
|
---|
34 | import javax.swing.AbstractAction;
|
---|
35 | import javax.swing.JComponent;
|
---|
36 | import javax.swing.JLabel;
|
---|
37 | import javax.swing.JPanel;
|
---|
38 | import javax.swing.JPopupMenu;
|
---|
39 | import javax.swing.JScrollPane;
|
---|
40 | import javax.swing.JTable;
|
---|
41 | import javax.swing.KeyStroke;
|
---|
42 | import javax.swing.ListSelectionModel;
|
---|
43 | import javax.swing.event.ListSelectionEvent;
|
---|
44 | import javax.swing.event.ListSelectionListener;
|
---|
45 | import javax.swing.table.DefaultTableCellRenderer;
|
---|
46 | import javax.swing.table.DefaultTableModel;
|
---|
47 | import javax.swing.table.TableColumnModel;
|
---|
48 | import javax.swing.table.TableModel;
|
---|
49 |
|
---|
50 | import org.openstreetmap.josm.Main;
|
---|
51 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
52 | import org.openstreetmap.josm.actions.relation.DownloadMembersAction;
|
---|
53 | import org.openstreetmap.josm.actions.relation.DownloadSelectedIncompleteMembersAction;
|
---|
54 | import org.openstreetmap.josm.actions.relation.SelectInRelationListAction;
|
---|
55 | import org.openstreetmap.josm.actions.relation.SelectMembersAction;
|
---|
56 | import org.openstreetmap.josm.actions.relation.SelectRelationAction;
|
---|
57 | import org.openstreetmap.josm.actions.search.SearchAction.SearchMode;
|
---|
58 | import org.openstreetmap.josm.actions.search.SearchAction.SearchSetting;
|
---|
59 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
60 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
61 | import org.openstreetmap.josm.command.Command;
|
---|
62 | import org.openstreetmap.josm.data.Preferences.PreferenceChangeEvent;
|
---|
63 | import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener;
|
---|
64 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
65 | import org.openstreetmap.josm.data.osm.IRelation;
|
---|
66 | import org.openstreetmap.josm.data.osm.Node;
|
---|
67 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
68 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
69 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
70 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
71 | import org.openstreetmap.josm.data.osm.Way;
|
---|
72 | import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
|
---|
73 | import org.openstreetmap.josm.data.osm.event.DataSetListenerAdapter;
|
---|
74 | import org.openstreetmap.josm.data.osm.event.DatasetEventManager;
|
---|
75 | import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode;
|
---|
76 | import org.openstreetmap.josm.data.osm.event.SelectionEventManager;
|
---|
77 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
78 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
79 | import org.openstreetmap.josm.gui.MapView;
|
---|
80 | import org.openstreetmap.josm.gui.PopupMenuHandler;
|
---|
81 | import org.openstreetmap.josm.gui.SideButton;
|
---|
82 | import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
|
---|
83 | import org.openstreetmap.josm.gui.dialogs.properties.PresetListPanel.PresetHandler;
|
---|
84 | import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor;
|
---|
85 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
86 | import org.openstreetmap.josm.gui.tagging.TaggingPreset;
|
---|
87 | import org.openstreetmap.josm.gui.tagging.TaggingPresetType;
|
---|
88 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
89 | import org.openstreetmap.josm.gui.util.HighlightHelper;
|
---|
90 | import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
|
---|
91 | import org.openstreetmap.josm.tools.GBC;
|
---|
92 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
93 | import org.openstreetmap.josm.tools.InputMapUtils;
|
---|
94 | import org.openstreetmap.josm.tools.LanguageInfo;
|
---|
95 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
96 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
97 | import org.openstreetmap.josm.tools.Utils;
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * This dialog displays the tags of the current selected primitives.
|
---|
101 | *
|
---|
102 | * If no object is selected, the dialog list is empty.
|
---|
103 | * If only one is selected, all tags of this object are selected.
|
---|
104 | * If more than one object are selected, the sum of all tags are displayed. If the
|
---|
105 | * different objects share the same tag, the shared value is displayed. If they have
|
---|
106 | * different values, all of them are put in a combo box and the string "<different>"
|
---|
107 | * is displayed in italic.
|
---|
108 | *
|
---|
109 | * Below the list, the user can click on an add, modify and delete tag button to
|
---|
110 | * edit the table selection value.
|
---|
111 | *
|
---|
112 | * The command is applied to all selected entries.
|
---|
113 | *
|
---|
114 | * @author imi
|
---|
115 | */
|
---|
116 | public class PropertiesDialog extends ToggleDialog implements SelectionChangedListener, MapView.EditLayerChangeListener, DataSetListenerAdapter.Listener, PreferenceChangedListener {
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * hook for roadsigns plugin to display a small button in the upper right corner of this dialog
|
---|
120 | */
|
---|
121 | public static final JPanel pluginHook = new JPanel();
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * The tag data of selected objects.
|
---|
125 | */
|
---|
126 | private final DefaultTableModel tagData = new ReadOnlyTableModel();
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * The membership data of selected objects.
|
---|
130 | */
|
---|
131 | private final DefaultTableModel membershipData = new ReadOnlyTableModel();
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * The tags table.
|
---|
135 | */
|
---|
136 | private final JTable tagTable = new JTable(tagData);
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * The membership table.
|
---|
140 | */
|
---|
141 | private final JTable membershipTable = new JTable(membershipData);
|
---|
142 |
|
---|
143 | // Popup menus
|
---|
144 | private final JPopupMenu tagMenu = new JPopupMenu();
|
---|
145 | private final JPopupMenu membershipMenu = new JPopupMenu();
|
---|
146 |
|
---|
147 | // Popup menu handlers
|
---|
148 | private final PopupMenuHandler tagMenuHandler = new PopupMenuHandler(tagMenu);
|
---|
149 | private final PopupMenuHandler membershipMenuHandler = new PopupMenuHandler(membershipMenu);
|
---|
150 |
|
---|
151 | private final Map<String, Map<String, Integer>> valueCount = new TreeMap<String, Map<String, Integer>>();
|
---|
152 | /**
|
---|
153 | * This sub-object is responsible for all adding and editing of tags
|
---|
154 | */
|
---|
155 | private final TagEditHelper editHelper = new TagEditHelper(tagData, valueCount);
|
---|
156 |
|
---|
157 | private final DataSetListenerAdapter dataChangedAdapter = new DataSetListenerAdapter(this);
|
---|
158 | private final HelpAction helpAction = new HelpAction();
|
---|
159 | private final PasteValueAction pasteValueAction = new PasteValueAction();
|
---|
160 | private final CopyValueAction copyValueAction = new CopyValueAction();
|
---|
161 | private final CopyKeyValueAction copyKeyValueAction = new CopyKeyValueAction();
|
---|
162 | private final CopyAllKeyValueAction copyAllKeyValueAction = new CopyAllKeyValueAction();
|
---|
163 | private final SearchAction searchActionSame = new SearchAction(true);
|
---|
164 | private final SearchAction searchActionAny = new SearchAction(false);
|
---|
165 | private final AddAction addAction = new AddAction();
|
---|
166 | private final EditAction editAction = new EditAction();
|
---|
167 | private final DeleteAction deleteAction = new DeleteAction();
|
---|
168 | private final JosmAction[] josmActions = new JosmAction[]{addAction, editAction, deleteAction};
|
---|
169 |
|
---|
170 | // relation actions
|
---|
171 | private final SelectInRelationListAction setRelationSelectionAction = new SelectInRelationListAction();
|
---|
172 | private final SelectRelationAction selectRelationAction = new SelectRelationAction(false);
|
---|
173 | private final SelectRelationAction addRelationToSelectionAction = new SelectRelationAction(true);
|
---|
174 |
|
---|
175 | private final DownloadMembersAction downloadMembersAction = new DownloadMembersAction();
|
---|
176 | private final DownloadSelectedIncompleteMembersAction downloadSelectedIncompleteMembersAction = new DownloadSelectedIncompleteMembersAction();
|
---|
177 |
|
---|
178 | private final SelectMembersAction selectMembersAction = new SelectMembersAction(false);
|
---|
179 | private final SelectMembersAction addMembersToSelectionAction = new SelectMembersAction(true);
|
---|
180 |
|
---|
181 | private final HighlightHelper highlightHelper= new HighlightHelper();
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * The Add button (needed to be able to disable it)
|
---|
185 | */
|
---|
186 | private final SideButton btnAdd = new SideButton(addAction);
|
---|
187 | /**
|
---|
188 | * The Edit button (needed to be able to disable it)
|
---|
189 | */
|
---|
190 | private final SideButton btnEdit = new SideButton(editAction);
|
---|
191 | /**
|
---|
192 | * The Delete button (needed to be able to disable it)
|
---|
193 | */
|
---|
194 | private final SideButton btnDel = new SideButton(deleteAction);
|
---|
195 | /**
|
---|
196 | * Matching preset display class
|
---|
197 | */
|
---|
198 | private final PresetListPanel presets = new PresetListPanel();
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Text to display when nothing selected.
|
---|
202 | */
|
---|
203 | private final JLabel selectSth = new JLabel("<html><p>"
|
---|
204 | + tr("Select objects for which to change tags.") + "</p></html>");
|
---|
205 |
|
---|
206 | private PresetHandler presetHandler = new PresetHandler() {
|
---|
207 | @Override public void updateTags(List<Tag> tags) {
|
---|
208 | Command command = TaggingPreset.createCommand(getSelection(), tags);
|
---|
209 | if (command != null) Main.main.undoRedo.add(command);
|
---|
210 | }
|
---|
211 |
|
---|
212 | @Override public Collection<OsmPrimitive> getSelection() {
|
---|
213 | if (Main.main == null) return null;
|
---|
214 | return Main.main.getInProgressSelection();
|
---|
215 | }
|
---|
216 | };
|
---|
217 |
|
---|
218 | // <editor-fold defaultstate="collapsed" desc="Dialog construction and helper methods">
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Create a new PropertiesDialog
|
---|
222 | */
|
---|
223 | public PropertiesDialog() {
|
---|
224 | super(tr("Tags/Memberships"), "propertiesdialog", tr("Tags for selected objects."),
|
---|
225 | Shortcut.registerShortcut("subwindow:properties", tr("Toggle: {0}", tr("Tags/Memberships")), KeyEvent.VK_P,
|
---|
226 | Shortcut.ALT_SHIFT), 150, true);
|
---|
227 |
|
---|
228 | setupTagsMenu();
|
---|
229 | buildTagsTable();
|
---|
230 |
|
---|
231 | setupMembershipMenu();
|
---|
232 | buildMembershipTable();
|
---|
233 |
|
---|
234 | // combine both tables and wrap them in a scrollPane
|
---|
235 | JPanel bothTables = new JPanel();
|
---|
236 | boolean top = Main.pref.getBoolean("properties.presets.top", true);
|
---|
237 | bothTables.setLayout(new GridBagLayout());
|
---|
238 | if(top) {
|
---|
239 | bothTables.add(presets, GBC.std().fill(GBC.HORIZONTAL).insets(5, 2, 5, 2).anchor(GBC.NORTHWEST));
|
---|
240 | double epsilon = Double.MIN_VALUE; // need to set a weight or else anchor value is ignored
|
---|
241 | bothTables.add(pluginHook, GBC.eol().insets(0,1,1,1).anchor(GBC.NORTHEAST).weight(epsilon, epsilon));
|
---|
242 | }
|
---|
243 | bothTables.add(selectSth, GBC.eol().fill().insets(10, 10, 10, 10));
|
---|
244 | bothTables.add(tagTable.getTableHeader(), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
245 | bothTables.add(tagTable, GBC.eol().fill(GBC.BOTH));
|
---|
246 | bothTables.add(membershipTable.getTableHeader(), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
247 | bothTables.add(membershipTable, GBC.eol().fill(GBC.BOTH));
|
---|
248 | if(!top) {
|
---|
249 | bothTables.add(presets, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 2, 5, 2));
|
---|
250 | }
|
---|
251 |
|
---|
252 | setupKeyboardShortcuts();
|
---|
253 |
|
---|
254 | // Let the action know when selection in the tables change
|
---|
255 | tagTable.getSelectionModel().addListSelectionListener(editAction);
|
---|
256 | membershipTable.getSelectionModel().addListSelectionListener(editAction);
|
---|
257 | tagTable.getSelectionModel().addListSelectionListener(deleteAction);
|
---|
258 | membershipTable.getSelectionModel().addListSelectionListener(deleteAction);
|
---|
259 |
|
---|
260 |
|
---|
261 | JScrollPane scrollPane = (JScrollPane) createLayout(bothTables, true, Arrays.asList(new SideButton[] {
|
---|
262 | this.btnAdd, this.btnEdit, this.btnDel
|
---|
263 | }));
|
---|
264 |
|
---|
265 | MouseClickWatch mouseClickWatch = new MouseClickWatch();
|
---|
266 | tagTable.addMouseListener(mouseClickWatch);
|
---|
267 | membershipTable.addMouseListener(mouseClickWatch);
|
---|
268 | scrollPane.addMouseListener(mouseClickWatch);
|
---|
269 |
|
---|
270 | selectSth.setPreferredSize(scrollPane.getSize());
|
---|
271 | presets.setSize(scrollPane.getSize());
|
---|
272 |
|
---|
273 | editHelper.loadTagsIfNeeded();
|
---|
274 |
|
---|
275 | Main.pref.addPreferenceChangeListener(this);
|
---|
276 | }
|
---|
277 |
|
---|
278 | private void buildTagsTable() {
|
---|
279 | // setting up the tags table
|
---|
280 |
|
---|
281 | tagData.setColumnIdentifiers(new String[]{tr("Key"),tr("Value")});
|
---|
282 | tagTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
283 | tagTable.getTableHeader().setReorderingAllowed(false);
|
---|
284 |
|
---|
285 | PropertiesCellRenderer cellRenderer = new PropertiesCellRenderer();
|
---|
286 | tagTable.getColumnModel().getColumn(0).setCellRenderer(cellRenderer);
|
---|
287 | tagTable.getColumnModel().getColumn(1).setCellRenderer(cellRenderer);
|
---|
288 | }
|
---|
289 |
|
---|
290 | private void buildMembershipTable() {
|
---|
291 | membershipData.setColumnIdentifiers(new String[]{tr("Member Of"),tr("Role"),tr("Position")});
|
---|
292 | membershipTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
293 |
|
---|
294 | TableColumnModel mod = membershipTable.getColumnModel();
|
---|
295 | membershipTable.getTableHeader().setReorderingAllowed(false);
|
---|
296 | mod.getColumn(0).setCellRenderer(new DefaultTableCellRenderer() {
|
---|
297 | @Override public Component getTableCellRendererComponent(JTable table, Object value,
|
---|
298 | boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
299 | Component c = super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
|
---|
300 | if (value == null)
|
---|
301 | return this;
|
---|
302 | if (c instanceof JLabel) {
|
---|
303 | JLabel label = (JLabel)c;
|
---|
304 | Relation r = (Relation)value;
|
---|
305 | label.setText(r.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
306 | if (r.isDisabledAndHidden()) {
|
---|
307 | label.setFont(label.getFont().deriveFont(Font.ITALIC));
|
---|
308 | }
|
---|
309 | }
|
---|
310 | return c;
|
---|
311 | }
|
---|
312 | });
|
---|
313 |
|
---|
314 | mod.getColumn(1).setCellRenderer(new DefaultTableCellRenderer() {
|
---|
315 | @Override public Component getTableCellRendererComponent(JTable table, Object value,
|
---|
316 | boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
317 | if (value == null)
|
---|
318 | return this;
|
---|
319 | Component c = super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
|
---|
320 | boolean isDisabledAndHidden = (((Relation)table.getValueAt(row, 0))).isDisabledAndHidden();
|
---|
321 | if (c instanceof JLabel) {
|
---|
322 | JLabel label = (JLabel)c;
|
---|
323 | MemberInfo col = (MemberInfo) value;
|
---|
324 |
|
---|
325 | String text = null;
|
---|
326 | for (RelationMember r : col.role) {
|
---|
327 | if (text == null) {
|
---|
328 | text = r.getRole();
|
---|
329 | }
|
---|
330 | else if (!text.equals(r.getRole())) {
|
---|
331 | text = tr("<different>");
|
---|
332 | break;
|
---|
333 | }
|
---|
334 | }
|
---|
335 |
|
---|
336 | label.setText(text);
|
---|
337 | if (isDisabledAndHidden) {
|
---|
338 | label.setFont(label.getFont().deriveFont(Font.ITALIC));
|
---|
339 | }
|
---|
340 | }
|
---|
341 | return c;
|
---|
342 | }
|
---|
343 | });
|
---|
344 |
|
---|
345 | mod.getColumn(2).setCellRenderer(new DefaultTableCellRenderer() {
|
---|
346 | @Override public Component getTableCellRendererComponent(JTable table, Object value,
|
---|
347 | boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
348 | Component c = super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
|
---|
349 | boolean isDisabledAndHidden = (((Relation)table.getValueAt(row, 0))).isDisabledAndHidden();
|
---|
350 | if (c instanceof JLabel) {
|
---|
351 | JLabel label = (JLabel)c;
|
---|
352 | label.setText(((MemberInfo) table.getValueAt(row, 1)).getPositionString());
|
---|
353 | if (isDisabledAndHidden) {
|
---|
354 | label.setFont(label.getFont().deriveFont(Font.ITALIC));
|
---|
355 | }
|
---|
356 | }
|
---|
357 | return c;
|
---|
358 | }
|
---|
359 | });
|
---|
360 | mod.getColumn(2).setPreferredWidth(20);
|
---|
361 | mod.getColumn(1).setPreferredWidth(40);
|
---|
362 | mod.getColumn(0).setPreferredWidth(200);
|
---|
363 | }
|
---|
364 |
|
---|
365 | /**
|
---|
366 | * creates the popup menu @field membershipMenu and its launcher on membership table
|
---|
367 | */
|
---|
368 | private void setupMembershipMenu() {
|
---|
369 | // setting up the membership table
|
---|
370 | membershipMenuHandler.addAction(setRelationSelectionAction);
|
---|
371 | membershipMenuHandler.addAction(selectRelationAction);
|
---|
372 | membershipMenuHandler.addAction(addRelationToSelectionAction);
|
---|
373 | membershipMenuHandler.addAction(selectMembersAction);
|
---|
374 | membershipMenuHandler.addAction(addMembersToSelectionAction);
|
---|
375 | membershipMenu.addSeparator();
|
---|
376 | membershipMenuHandler.addAction(downloadMembersAction);
|
---|
377 | membershipMenuHandler.addAction(downloadSelectedIncompleteMembersAction);
|
---|
378 | membershipMenu.addSeparator();
|
---|
379 | membershipMenu.add(helpAction);
|
---|
380 |
|
---|
381 | membershipTable.addMouseListener(new PopupMenuLauncher(membershipMenu) {
|
---|
382 | @Override
|
---|
383 | protected int checkTableSelection(JTable table, Point p) {
|
---|
384 | int row = super.checkTableSelection(table, p);
|
---|
385 | List<Relation> rels = new ArrayList<Relation>();
|
---|
386 | for (int i: table.getSelectedRows()) {
|
---|
387 | rels.add((Relation) table.getValueAt(i, 0));
|
---|
388 | }
|
---|
389 | membershipMenuHandler.setPrimitives(rels);
|
---|
390 | return row;
|
---|
391 | }
|
---|
392 |
|
---|
393 | @Override
|
---|
394 | public void mouseClicked(MouseEvent e) {
|
---|
395 | //update highlights
|
---|
396 | if (Main.isDisplayingMapView()) {
|
---|
397 | int row = membershipTable.rowAtPoint(e.getPoint());
|
---|
398 | if (row>=0) {
|
---|
399 | if (highlightHelper.highlightOnly((Relation) membershipTable.getValueAt(row, 0))) {
|
---|
400 | Main.map.mapView.repaint();
|
---|
401 | }
|
---|
402 | }
|
---|
403 | }
|
---|
404 | super.mouseClicked(e);
|
---|
405 | }
|
---|
406 |
|
---|
407 | @Override
|
---|
408 | public void mouseExited(MouseEvent me) {
|
---|
409 | highlightHelper.clear();
|
---|
410 | }
|
---|
411 | });
|
---|
412 | }
|
---|
413 |
|
---|
414 | /**
|
---|
415 | * creates the popup menu @field tagMenu and its launcher on tag table
|
---|
416 | */
|
---|
417 | private void setupTagsMenu() {
|
---|
418 | tagMenu.add(pasteValueAction);
|
---|
419 | tagMenu.add(copyValueAction);
|
---|
420 | tagMenu.add(copyKeyValueAction);
|
---|
421 | tagMenu.add(copyAllKeyValueAction);
|
---|
422 | tagMenu.addSeparator();
|
---|
423 | tagMenu.add(searchActionAny);
|
---|
424 | tagMenu.add(searchActionSame);
|
---|
425 | tagMenu.addSeparator();
|
---|
426 | tagMenu.add(helpAction);
|
---|
427 | tagTable.addMouseListener(new PopupMenuLauncher(tagMenu));
|
---|
428 | }
|
---|
429 |
|
---|
430 | /**
|
---|
431 | * Assignas all needed keys like Enter and Spacebar to most important actions
|
---|
432 | */
|
---|
433 | private void setupKeyboardShortcuts() {
|
---|
434 |
|
---|
435 | // ENTER = editAction, open "edit" dialog
|
---|
436 | tagTable.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
|
---|
437 | .put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),"onTableEnter");
|
---|
438 | tagTable.getActionMap().put("onTableEnter",editAction);
|
---|
439 | membershipTable.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
|
---|
440 | .put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),"onTableEnter");
|
---|
441 | membershipTable.getActionMap().put("onTableEnter",editAction);
|
---|
442 |
|
---|
443 | // INSERT button = addAction, open "add tag" dialog
|
---|
444 | tagTable.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
|
---|
445 | .put(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, 0),"onTableInsert");
|
---|
446 | tagTable.getActionMap().put("onTableInsert",addAction);
|
---|
447 |
|
---|
448 | // unassign some standard shortcuts for JTable to allow upload / download / image browsing
|
---|
449 | InputMapUtils.unassignCtrlShiftUpDown(tagTable, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
|
---|
450 | InputMapUtils.unassignPageUpDown(tagTable, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
|
---|
451 |
|
---|
452 | // unassign some standard shortcuts for correct copy-pasting, fix #8508
|
---|
453 | tagTable.setTransferHandler(null);
|
---|
454 |
|
---|
455 | tagTable.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
|
---|
456 | .put(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK),"onCopy");
|
---|
457 | tagTable.getActionMap().put("onCopy",copyKeyValueAction);
|
---|
458 |
|
---|
459 | // allow using enter to add tags for all look&feel configurations
|
---|
460 | InputMapUtils.enableEnter(this.btnAdd);
|
---|
461 |
|
---|
462 | // DEL button = deleteAction
|
---|
463 | getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
|
---|
464 | KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0),"delete"
|
---|
465 | );
|
---|
466 | getActionMap().put("delete", deleteAction);
|
---|
467 |
|
---|
468 | // F1 button = custom help action
|
---|
469 | getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
|
---|
470 | KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0), "onHelp");
|
---|
471 | getActionMap().put("onHelp", helpAction);
|
---|
472 | }
|
---|
473 |
|
---|
474 | /**
|
---|
475 | * This simply fires up an {@link RelationEditor} for the relation shown; everything else
|
---|
476 | * is the editor's business.
|
---|
477 | *
|
---|
478 | * @param row
|
---|
479 | */
|
---|
480 | private void editMembership(int row) {
|
---|
481 | Relation relation = (Relation)membershipData.getValueAt(row, 0);
|
---|
482 | Main.map.relationListDialog.selectRelation(relation);
|
---|
483 | RelationEditor.getEditor(
|
---|
484 | Main.main.getEditLayer(),
|
---|
485 | relation,
|
---|
486 | ((MemberInfo) membershipData.getValueAt(row, 1)).role).setVisible(true);
|
---|
487 | }
|
---|
488 |
|
---|
489 | private int findRow(TableModel model, Object value) {
|
---|
490 | for (int i=0; i<model.getRowCount(); i++) {
|
---|
491 | if (model.getValueAt(i, 0).equals(value))
|
---|
492 | return i;
|
---|
493 | }
|
---|
494 | return -1;
|
---|
495 | }
|
---|
496 |
|
---|
497 | /**
|
---|
498 | * Update selection status, call @{link #selectionChanged} function.
|
---|
499 | */
|
---|
500 | private void updateSelection() {
|
---|
501 | // Parameter is ignored in this class
|
---|
502 | selectionChanged(null);
|
---|
503 | }
|
---|
504 |
|
---|
505 | // </editor-fold>
|
---|
506 |
|
---|
507 | // <editor-fold defaultstate="collapsed" desc="Event listeners methods">
|
---|
508 |
|
---|
509 | @Override
|
---|
510 | public void showNotify() {
|
---|
511 | DatasetEventManager.getInstance().addDatasetListener(dataChangedAdapter, FireMode.IN_EDT_CONSOLIDATED);
|
---|
512 | SelectionEventManager.getInstance().addSelectionListener(this, FireMode.IN_EDT_CONSOLIDATED);
|
---|
513 | MapView.addEditLayerChangeListener(this);
|
---|
514 | for (JosmAction action : josmActions) {
|
---|
515 | Main.registerActionShortcut(action);
|
---|
516 | }
|
---|
517 | updateSelection();
|
---|
518 | }
|
---|
519 |
|
---|
520 | @Override
|
---|
521 | public void hideNotify() {
|
---|
522 | DatasetEventManager.getInstance().removeDatasetListener(dataChangedAdapter);
|
---|
523 | SelectionEventManager.getInstance().removeSelectionListener(this);
|
---|
524 | MapView.removeEditLayerChangeListener(this);
|
---|
525 | for (JosmAction action : josmActions) {
|
---|
526 | Main.unregisterActionShortcut(action);
|
---|
527 | }
|
---|
528 | }
|
---|
529 |
|
---|
530 | @Override
|
---|
531 | public void setVisible(boolean b) {
|
---|
532 | super.setVisible(b);
|
---|
533 | if (b && Main.main.getCurrentDataSet() != null) {
|
---|
534 | updateSelection();
|
---|
535 | }
|
---|
536 | }
|
---|
537 |
|
---|
538 | @Override
|
---|
539 | public void destroy() {
|
---|
540 | super.destroy();
|
---|
541 | Main.pref.removePreferenceChangeListener(this);
|
---|
542 | for (JosmAction action : josmActions) {
|
---|
543 | action.destroy();
|
---|
544 | }
|
---|
545 | Container parent = pluginHook.getParent();
|
---|
546 | if (parent != null) {
|
---|
547 | parent.remove(pluginHook);
|
---|
548 | }
|
---|
549 | }
|
---|
550 |
|
---|
551 | @Override
|
---|
552 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
---|
553 | if (!isVisible())
|
---|
554 | return;
|
---|
555 | if (tagTable == null)
|
---|
556 | return; // selection changed may be received in base class constructor before init
|
---|
557 | if (tagTable.getCellEditor() != null) {
|
---|
558 | tagTable.getCellEditor().cancelCellEditing();
|
---|
559 | }
|
---|
560 |
|
---|
561 | // Discard parameter as we do not want to operate always on real selection here, especially in draw mode
|
---|
562 | newSelection = Main.main.getInProgressSelection();
|
---|
563 | if (newSelection == null) {
|
---|
564 | newSelection = Collections.<OsmPrimitive>emptyList();
|
---|
565 | }
|
---|
566 |
|
---|
567 | String selectedTag;
|
---|
568 | Relation selectedRelation = null;
|
---|
569 | selectedTag = editHelper.getChangedKey(); // select last added or last edited key by default
|
---|
570 | if (selectedTag == null && tagTable.getSelectedRowCount() == 1) {
|
---|
571 | selectedTag = (String)tagData.getValueAt(tagTable.getSelectedRow(), 0);
|
---|
572 | }
|
---|
573 | if (membershipTable.getSelectedRowCount() == 1) {
|
---|
574 | selectedRelation = (Relation)membershipData.getValueAt(membershipTable.getSelectedRow(), 0);
|
---|
575 | }
|
---|
576 |
|
---|
577 | // re-load tag data
|
---|
578 | tagData.setRowCount(0);
|
---|
579 |
|
---|
580 | final boolean displayDiscardableKeys = Main.pref.getBoolean("display.discardable-keys", false);
|
---|
581 | final Map<String, Integer> keyCount = new HashMap<String, Integer>();
|
---|
582 | final Map<String, String> tags = new HashMap<String, String>();
|
---|
583 | valueCount.clear();
|
---|
584 | EnumSet<TaggingPresetType> types = EnumSet.noneOf(TaggingPresetType.class);
|
---|
585 | for (OsmPrimitive osm : newSelection) {
|
---|
586 | types.add(TaggingPresetType.forPrimitive(osm));
|
---|
587 | for (String key : osm.keySet()) {
|
---|
588 | if (displayDiscardableKeys || !OsmPrimitive.getDiscardableKeys().contains(key)) {
|
---|
589 | String value = osm.get(key);
|
---|
590 | keyCount.put(key, keyCount.containsKey(key) ? keyCount.get(key) + 1 : 1);
|
---|
591 | if (valueCount.containsKey(key)) {
|
---|
592 | Map<String, Integer> v = valueCount.get(key);
|
---|
593 | v.put(value, v.containsKey(value) ? v.get(value) + 1 : 1);
|
---|
594 | } else {
|
---|
595 | TreeMap<String, Integer> v = new TreeMap<String, Integer>();
|
---|
596 | v.put(value, 1);
|
---|
597 | valueCount.put(key, v);
|
---|
598 | }
|
---|
599 | }
|
---|
600 | }
|
---|
601 | }
|
---|
602 | for (Entry<String, Map<String, Integer>> e : valueCount.entrySet()) {
|
---|
603 | int count = 0;
|
---|
604 | for (Entry<String, Integer> e1 : e.getValue().entrySet()) {
|
---|
605 | count += e1.getValue();
|
---|
606 | }
|
---|
607 | if (count < newSelection.size()) {
|
---|
608 | e.getValue().put("", newSelection.size() - count);
|
---|
609 | }
|
---|
610 | tagData.addRow(new Object[]{e.getKey(), e.getValue()});
|
---|
611 | tags.put(e.getKey(), e.getValue().size() == 1
|
---|
612 | ? e.getValue().keySet().iterator().next() : tr("<different>"));
|
---|
613 | }
|
---|
614 |
|
---|
615 | membershipData.setRowCount(0);
|
---|
616 |
|
---|
617 | Map<Relation, MemberInfo> roles = new HashMap<Relation, MemberInfo>();
|
---|
618 | for (OsmPrimitive primitive: newSelection) {
|
---|
619 | for (OsmPrimitive ref: primitive.getReferrers()) {
|
---|
620 | if (ref instanceof Relation && !ref.isIncomplete() && !ref.isDeleted()) {
|
---|
621 | Relation r = (Relation) ref;
|
---|
622 | MemberInfo mi = roles.get(r);
|
---|
623 | if(mi == null) {
|
---|
624 | mi = new MemberInfo();
|
---|
625 | }
|
---|
626 | roles.put(r, mi);
|
---|
627 | int i = 1;
|
---|
628 | for (RelationMember m : r.getMembers()) {
|
---|
629 | if (m.getMember() == primitive) {
|
---|
630 | mi.add(m, i);
|
---|
631 | }
|
---|
632 | ++i;
|
---|
633 | }
|
---|
634 | }
|
---|
635 | }
|
---|
636 | }
|
---|
637 |
|
---|
638 | List<Relation> sortedRelations = new ArrayList<Relation>(roles.keySet());
|
---|
639 | Collections.sort(sortedRelations, new Comparator<Relation>() {
|
---|
640 | @Override public int compare(Relation o1, Relation o2) {
|
---|
641 | int comp = Boolean.valueOf(o1.isDisabledAndHidden()).compareTo(o2.isDisabledAndHidden());
|
---|
642 | if (comp == 0) {
|
---|
643 | comp = o1.getDisplayName(DefaultNameFormatter.getInstance()).compareTo(o2.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
644 | }
|
---|
645 | return comp;
|
---|
646 | }}
|
---|
647 | );
|
---|
648 |
|
---|
649 | for (Relation r: sortedRelations) {
|
---|
650 | membershipData.addRow(new Object[]{r, roles.get(r)});
|
---|
651 | }
|
---|
652 |
|
---|
653 | presets.updatePresets(types, tags, presetHandler);
|
---|
654 |
|
---|
655 | membershipTable.getTableHeader().setVisible(membershipData.getRowCount() > 0);
|
---|
656 | membershipTable.setVisible(membershipData.getRowCount() > 0);
|
---|
657 |
|
---|
658 | boolean hasSelection = !newSelection.isEmpty();
|
---|
659 | boolean hasTags = hasSelection && tagData.getRowCount() > 0;
|
---|
660 | boolean hasMemberships = hasSelection && membershipData.getRowCount() > 0;
|
---|
661 | btnAdd.setEnabled(hasSelection);
|
---|
662 | btnEdit.setEnabled(hasTags || hasMemberships);
|
---|
663 | btnDel.setEnabled(hasTags || hasMemberships);
|
---|
664 | tagTable.setVisible(hasTags);
|
---|
665 | tagTable.getTableHeader().setVisible(hasTags);
|
---|
666 | selectSth.setVisible(!hasSelection);
|
---|
667 | pluginHook.setVisible(hasSelection);
|
---|
668 |
|
---|
669 | int selectedIndex;
|
---|
670 | if (selectedTag != null && (selectedIndex = findRow(tagData, selectedTag)) != -1) {
|
---|
671 | tagTable.changeSelection(selectedIndex, 0, false, false);
|
---|
672 | } else if (selectedRelation != null && (selectedIndex = findRow(membershipData, selectedRelation)) != -1) {
|
---|
673 | membershipTable.changeSelection(selectedIndex, 0, false, false);
|
---|
674 | } else if(hasTags) {
|
---|
675 | tagTable.changeSelection(0, 0, false, false);
|
---|
676 | } else if(hasMemberships) {
|
---|
677 | membershipTable.changeSelection(0, 0, false, false);
|
---|
678 | }
|
---|
679 |
|
---|
680 | if(tagData.getRowCount() != 0 || membershipData.getRowCount() != 0) {
|
---|
681 | setTitle(tr("Tags: {0} / Memberships: {1}",
|
---|
682 | tagData.getRowCount(), membershipData.getRowCount()));
|
---|
683 | } else {
|
---|
684 | setTitle(tr("Tags / Memberships"));
|
---|
685 | }
|
---|
686 | }
|
---|
687 |
|
---|
688 | /* ---------------------------------------------------------------------------------- */
|
---|
689 | /* EditLayerChangeListener */
|
---|
690 | /* ---------------------------------------------------------------------------------- */
|
---|
691 | @Override
|
---|
692 | public void editLayerChanged(OsmDataLayer oldLayer, OsmDataLayer newLayer) {
|
---|
693 | if (newLayer == null) editHelper.saveTagsIfNeeded();
|
---|
694 | // it is time to save history of tags
|
---|
695 | GuiHelper.runInEDT(new Runnable() {
|
---|
696 | @Override public void run() {
|
---|
697 | updateSelection();
|
---|
698 | }
|
---|
699 | });
|
---|
700 | }
|
---|
701 |
|
---|
702 | @Override
|
---|
703 | public void processDatasetEvent(AbstractDatasetChangedEvent event) {
|
---|
704 | updateSelection();
|
---|
705 | }
|
---|
706 |
|
---|
707 | // </editor-fold>
|
---|
708 |
|
---|
709 | // <editor-fold defaultstate="collapsed" desc="Methods that are called by plugins to extend fuctionality ">
|
---|
710 |
|
---|
711 | /**
|
---|
712 | * Replies the tag popup menu handler.
|
---|
713 | * @return The tag popup menu handler
|
---|
714 | */
|
---|
715 | public PopupMenuHandler getPropertyPopupMenuHandler() {
|
---|
716 | return tagMenuHandler;
|
---|
717 | }
|
---|
718 |
|
---|
719 | @SuppressWarnings("unchecked")
|
---|
720 | public Tag getSelectedProperty() {
|
---|
721 | int row = tagTable.getSelectedRow();
|
---|
722 | if (row == -1) return null;
|
---|
723 | TreeMap<String, Integer> map = (TreeMap<String, Integer>) tagData.getValueAt(row, 1);
|
---|
724 | return new Tag(
|
---|
725 | tagData.getValueAt(row, 0).toString(),
|
---|
726 | map.size() > 1 ? "" : map.keySet().iterator().next());
|
---|
727 | }
|
---|
728 |
|
---|
729 | /**
|
---|
730 | * Replies the membership popup menu handler.
|
---|
731 | * @return The membership popup menu handler
|
---|
732 | */
|
---|
733 | public PopupMenuHandler getMembershipPopupMenuHandler() {
|
---|
734 | return membershipMenuHandler;
|
---|
735 | }
|
---|
736 |
|
---|
737 | public IRelation getSelectedMembershipRelation() {
|
---|
738 | int row = membershipTable.getSelectedRow();
|
---|
739 | return row > -1 ? (IRelation) membershipData.getValueAt(row, 0) : null;
|
---|
740 | }
|
---|
741 |
|
---|
742 | // </editor-fold>
|
---|
743 |
|
---|
744 | /**
|
---|
745 | * Class that watches for mouse clicks
|
---|
746 | * @author imi
|
---|
747 | */
|
---|
748 | public class MouseClickWatch extends MouseAdapter {
|
---|
749 | @Override public void mouseClicked(MouseEvent e) {
|
---|
750 | if (e.getClickCount() < 2)
|
---|
751 | {
|
---|
752 | // single click, clear selection in other table not clicked in
|
---|
753 | if (e.getSource() == tagTable) {
|
---|
754 | membershipTable.clearSelection();
|
---|
755 | } else if (e.getSource() == membershipTable) {
|
---|
756 | tagTable.clearSelection();
|
---|
757 | }
|
---|
758 | }
|
---|
759 | // double click, edit or add tag
|
---|
760 | else if (e.getSource() == tagTable)
|
---|
761 | {
|
---|
762 | int row = tagTable.rowAtPoint(e.getPoint());
|
---|
763 | if (row > -1) {
|
---|
764 | boolean focusOnKey = (tagTable.columnAtPoint(e.getPoint()) == 0);
|
---|
765 | editHelper.editTag(row, focusOnKey);
|
---|
766 | } else {
|
---|
767 | editHelper.addTag();
|
---|
768 | btnAdd.requestFocusInWindow();
|
---|
769 | }
|
---|
770 | } else if (e.getSource() == membershipTable) {
|
---|
771 | int row = membershipTable.rowAtPoint(e.getPoint());
|
---|
772 | if (row > -1) {
|
---|
773 | editMembership(row);
|
---|
774 | }
|
---|
775 | }
|
---|
776 | else
|
---|
777 | {
|
---|
778 | editHelper.addTag();
|
---|
779 | btnAdd.requestFocusInWindow();
|
---|
780 | }
|
---|
781 | }
|
---|
782 | @Override public void mousePressed(MouseEvent e) {
|
---|
783 | if (e.getSource() == tagTable) {
|
---|
784 | membershipTable.clearSelection();
|
---|
785 | } else if (e.getSource() == membershipTable) {
|
---|
786 | tagTable.clearSelection();
|
---|
787 | }
|
---|
788 | }
|
---|
789 |
|
---|
790 | }
|
---|
791 |
|
---|
792 | static class MemberInfo {
|
---|
793 | List<RelationMember> role = new ArrayList<RelationMember>();
|
---|
794 | List<Integer> position = new ArrayList<Integer>();
|
---|
795 | private String positionString = null;
|
---|
796 | void add(RelationMember r, Integer p) {
|
---|
797 | role.add(r);
|
---|
798 | position.add(p);
|
---|
799 | }
|
---|
800 | String getPositionString() {
|
---|
801 | if (positionString == null) {
|
---|
802 | Collections.sort(position);
|
---|
803 | positionString = String.valueOf(position.get(0));
|
---|
804 | int cnt = 0;
|
---|
805 | int last = position.get(0);
|
---|
806 | for (int i = 1; i < position.size(); ++i) {
|
---|
807 | int cur = position.get(i);
|
---|
808 | if (cur == last + 1) {
|
---|
809 | ++cnt;
|
---|
810 | } else if (cnt == 0) {
|
---|
811 | positionString += "," + cur;
|
---|
812 | } else {
|
---|
813 | positionString += "-" + last;
|
---|
814 | positionString += "," + cur;
|
---|
815 | cnt = 0;
|
---|
816 | }
|
---|
817 | last = cur;
|
---|
818 | }
|
---|
819 | if (cnt >= 1) {
|
---|
820 | positionString += "-" + last;
|
---|
821 | }
|
---|
822 | }
|
---|
823 | if (positionString.length() > 20) {
|
---|
824 | positionString = positionString.substring(0, 17) + "...";
|
---|
825 | }
|
---|
826 | return positionString;
|
---|
827 | }
|
---|
828 | }
|
---|
829 |
|
---|
830 | /**
|
---|
831 | * Class that allows fast creation of read-only table model with String columns
|
---|
832 | */
|
---|
833 | public static class ReadOnlyTableModel extends DefaultTableModel {
|
---|
834 | @Override public boolean isCellEditable(int row, int column) {
|
---|
835 | return false;
|
---|
836 | }
|
---|
837 | @Override public Class<?> getColumnClass(int columnIndex) {
|
---|
838 | return String.class;
|
---|
839 | }
|
---|
840 | }
|
---|
841 |
|
---|
842 | /**
|
---|
843 | * Action handling delete button press in properties dialog.
|
---|
844 | */
|
---|
845 | class DeleteAction extends JosmAction implements ListSelectionListener {
|
---|
846 |
|
---|
847 | public DeleteAction() {
|
---|
848 | super(tr("Delete"), "dialogs/delete", tr("Delete the selected key in all objects"),
|
---|
849 | Shortcut.registerShortcut("properties:delete", tr("Delete Tags"), KeyEvent.VK_D,
|
---|
850 | Shortcut.ALT_CTRL_SHIFT), false);
|
---|
851 | updateEnabledState();
|
---|
852 | }
|
---|
853 |
|
---|
854 | protected void deleteTags(int[] rows){
|
---|
855 | // convert list of rows to HashMap (and find gap for nextKey)
|
---|
856 | HashMap<String, String> tags = new HashMap<String, String>(rows.length);
|
---|
857 | int nextKeyIndex = rows[0];
|
---|
858 | for (int row : rows) {
|
---|
859 | String key = tagData.getValueAt(row, 0).toString();
|
---|
860 | if (row == nextKeyIndex + 1) {
|
---|
861 | nextKeyIndex = row; // no gap yet
|
---|
862 | }
|
---|
863 | tags.put(key, null);
|
---|
864 | }
|
---|
865 |
|
---|
866 | // find key to select after deleting other tags
|
---|
867 | String nextKey = null;
|
---|
868 | int rowCount = tagData.getRowCount();
|
---|
869 | if (rowCount > rows.length) {
|
---|
870 | if (nextKeyIndex == rows[rows.length-1]) {
|
---|
871 | // no gap found, pick next or previous key in list
|
---|
872 | nextKeyIndex = (nextKeyIndex + 1 < rowCount ? nextKeyIndex + 1 : rows[0] - 1);
|
---|
873 | } else {
|
---|
874 | // gap found
|
---|
875 | nextKeyIndex++;
|
---|
876 | }
|
---|
877 | nextKey = (String)tagData.getValueAt(nextKeyIndex, 0);
|
---|
878 | }
|
---|
879 |
|
---|
880 | Collection<OsmPrimitive> sel = Main.main.getInProgressSelection();
|
---|
881 | Main.main.undoRedo.add(new ChangePropertyCommand(sel, tags));
|
---|
882 |
|
---|
883 | membershipTable.clearSelection();
|
---|
884 | if (nextKey != null) {
|
---|
885 | tagTable.changeSelection(findRow(tagData, nextKey), 0, false, false);
|
---|
886 | }
|
---|
887 | }
|
---|
888 |
|
---|
889 | protected void deleteFromRelation(int row) {
|
---|
890 | Relation cur = (Relation)membershipData.getValueAt(row, 0);
|
---|
891 |
|
---|
892 | Relation nextRelation = null;
|
---|
893 | int rowCount = membershipTable.getRowCount();
|
---|
894 | if (rowCount > 1) {
|
---|
895 | nextRelation = (Relation)membershipData.getValueAt((row + 1 < rowCount ? row + 1 : row - 1), 0);
|
---|
896 | }
|
---|
897 |
|
---|
898 | ExtendedDialog ed = new ExtendedDialog(Main.parent,
|
---|
899 | tr("Change relation"),
|
---|
900 | new String[] {tr("Delete from relation"), tr("Cancel")});
|
---|
901 | ed.setButtonIcons(new String[] {"dialogs/delete.png", "cancel.png"});
|
---|
902 | ed.setContent(tr("Really delete selection from relation {0}?", cur.getDisplayName(DefaultNameFormatter.getInstance())));
|
---|
903 | ed.toggleEnable("delete_from_relation");
|
---|
904 | ed.showDialog();
|
---|
905 |
|
---|
906 | if(ed.getValue() != 1)
|
---|
907 | return;
|
---|
908 |
|
---|
909 | Relation rel = new Relation(cur);
|
---|
910 | for (OsmPrimitive primitive: Main.main.getInProgressSelection()) {
|
---|
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.getInProgressSelection();
|
---|
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.getInProgressSelection();
|
---|
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.getInProgressSelection();
|
---|
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()) && Main.main.getCurrentDataSet() != null) {
|
---|
1246 | // Re-load data when display preference change
|
---|
1247 | updateSelection();
|
---|
1248 | }
|
---|
1249 | }
|
---|
1250 | }
|
---|