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