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 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.awt.BorderLayout;
|
---|
8 | import java.awt.Component;
|
---|
9 | import java.awt.Container;
|
---|
10 | import java.awt.Cursor;
|
---|
11 | import java.awt.Dialog.ModalityType;
|
---|
12 | import java.awt.Dimension;
|
---|
13 | import java.awt.FlowLayout;
|
---|
14 | import java.awt.Font;
|
---|
15 | import java.awt.GridBagConstraints;
|
---|
16 | import java.awt.GridBagLayout;
|
---|
17 | import java.awt.Point;
|
---|
18 | import java.awt.Toolkit;
|
---|
19 | import java.awt.datatransfer.Clipboard;
|
---|
20 | import java.awt.datatransfer.Transferable;
|
---|
21 | import java.awt.event.ActionEvent;
|
---|
22 | import java.awt.event.ActionListener;
|
---|
23 | import java.awt.event.FocusAdapter;
|
---|
24 | import java.awt.event.FocusEvent;
|
---|
25 | import java.awt.event.KeyEvent;
|
---|
26 | import java.awt.event.MouseAdapter;
|
---|
27 | import java.awt.event.MouseEvent;
|
---|
28 | import java.awt.image.BufferedImage;
|
---|
29 | import java.net.HttpURLConnection;
|
---|
30 | import java.net.URI;
|
---|
31 | import java.net.URLEncoder;
|
---|
32 | import java.util.ArrayList;
|
---|
33 | import java.util.Arrays;
|
---|
34 | import java.util.Collection;
|
---|
35 | import java.util.Collections;
|
---|
36 | import java.util.Comparator;
|
---|
37 | import java.util.EnumSet;
|
---|
38 | import java.util.HashMap;
|
---|
39 | import java.util.HashSet;
|
---|
40 | import java.util.Iterator;
|
---|
41 | import java.util.LinkedHashMap;
|
---|
42 | import java.util.LinkedList;
|
---|
43 | import java.util.List;
|
---|
44 | import java.util.Map;
|
---|
45 | import java.util.Map.Entry;
|
---|
46 | import java.util.Set;
|
---|
47 | import java.util.TreeMap;
|
---|
48 | import java.util.TreeSet;
|
---|
49 | import java.util.Vector;
|
---|
50 |
|
---|
51 | import javax.swing.AbstractAction;
|
---|
52 | import javax.swing.Action;
|
---|
53 | import javax.swing.Box;
|
---|
54 | import javax.swing.DefaultListCellRenderer;
|
---|
55 | import javax.swing.ImageIcon;
|
---|
56 | import javax.swing.JComponent;
|
---|
57 | import javax.swing.JDialog;
|
---|
58 | import javax.swing.JLabel;
|
---|
59 | import javax.swing.JList;
|
---|
60 | import javax.swing.JMenuItem;
|
---|
61 | import javax.swing.JOptionPane;
|
---|
62 | import javax.swing.JPanel;
|
---|
63 | import javax.swing.JPopupMenu;
|
---|
64 | import javax.swing.JScrollPane;
|
---|
65 | import javax.swing.JTable;
|
---|
66 | import javax.swing.KeyStroke;
|
---|
67 | import javax.swing.ListSelectionModel;
|
---|
68 | import javax.swing.event.ListSelectionEvent;
|
---|
69 | import javax.swing.event.ListSelectionListener;
|
---|
70 | import javax.swing.event.PopupMenuListener;
|
---|
71 | import javax.swing.table.DefaultTableCellRenderer;
|
---|
72 | import javax.swing.table.DefaultTableModel;
|
---|
73 | import javax.swing.table.TableColumnModel;
|
---|
74 | import javax.swing.table.TableModel;
|
---|
75 | import javax.swing.text.JTextComponent;
|
---|
76 |
|
---|
77 | import org.openstreetmap.josm.Main;
|
---|
78 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
79 | import org.openstreetmap.josm.actions.mapmode.DrawAction;
|
---|
80 | import org.openstreetmap.josm.actions.search.SearchAction.SearchMode;
|
---|
81 | import org.openstreetmap.josm.actions.search.SearchAction.SearchSetting;
|
---|
82 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
83 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
84 | import org.openstreetmap.josm.command.Command;
|
---|
85 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
86 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
87 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
88 | import org.openstreetmap.josm.data.osm.IRelation;
|
---|
89 | import org.openstreetmap.josm.data.osm.Node;
|
---|
90 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
91 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
92 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
93 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
94 | import org.openstreetmap.josm.data.osm.Way;
|
---|
95 | import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
|
---|
96 | import org.openstreetmap.josm.data.osm.event.DataSetListenerAdapter;
|
---|
97 | import org.openstreetmap.josm.data.osm.event.DatasetEventManager;
|
---|
98 | import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode;
|
---|
99 | import org.openstreetmap.josm.data.osm.event.SelectionEventManager;
|
---|
100 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
101 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
102 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
103 | import org.openstreetmap.josm.gui.MapView;
|
---|
104 | import org.openstreetmap.josm.gui.SideButton;
|
---|
105 | import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
|
---|
106 | import org.openstreetmap.josm.gui.dialogs.properties.PresetListPanel.PresetHandler;
|
---|
107 | import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationMemberTask;
|
---|
108 | import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor;
|
---|
109 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
110 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
|
---|
111 | import org.openstreetmap.josm.gui.tagging.TaggingPreset;
|
---|
112 | import org.openstreetmap.josm.gui.tagging.TaggingPreset.PresetType;
|
---|
113 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingComboBox;
|
---|
114 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionListItem;
|
---|
115 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
|
---|
116 | import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
|
---|
117 | import org.openstreetmap.josm.tools.GBC;
|
---|
118 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
119 | import org.openstreetmap.josm.tools.InputMapUtils;
|
---|
120 | import org.openstreetmap.josm.tools.LanguageInfo;
|
---|
121 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
122 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
123 | import org.openstreetmap.josm.tools.Utils;
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * This dialog displays the properties of the current selected primitives.
|
---|
127 | *
|
---|
128 | * If no object is selected, the dialog list is empty.
|
---|
129 | * If only one is selected, all properties of this object are selected.
|
---|
130 | * If more than one object are selected, the sum of all properties are displayed. If the
|
---|
131 | * different objects share the same property, the shared value is displayed. If they have
|
---|
132 | * different values, all of them are put in a combo box and the string "<different>"
|
---|
133 | * is displayed in italic.
|
---|
134 | *
|
---|
135 | * Below the list, the user can click on an add, modify and delete property button to
|
---|
136 | * edit the table selection value.
|
---|
137 | *
|
---|
138 | * The command is applied to all selected entries.
|
---|
139 | *
|
---|
140 | * @author imi
|
---|
141 | */
|
---|
142 | public class PropertiesDialog extends ToggleDialog implements SelectionChangedListener, MapView.EditLayerChangeListener, DataSetListenerAdapter.Listener {
|
---|
143 | /**
|
---|
144 | * Watches for mouse clicks
|
---|
145 | * @author imi
|
---|
146 | */
|
---|
147 | public class MouseClickWatch extends MouseAdapter {
|
---|
148 | @Override public void mouseClicked(MouseEvent e) {
|
---|
149 | if (e.getClickCount() < 2)
|
---|
150 | {
|
---|
151 | // single click, clear selection in other table not clicked in
|
---|
152 | if (e.getSource() == propertyTable) {
|
---|
153 | membershipTable.clearSelection();
|
---|
154 | } else if (e.getSource() == membershipTable) {
|
---|
155 | propertyTable.clearSelection();
|
---|
156 | }
|
---|
157 | }
|
---|
158 | // double click, edit or add property
|
---|
159 | else if (e.getSource() == propertyTable)
|
---|
160 | {
|
---|
161 | int row = propertyTable.rowAtPoint(e.getPoint());
|
---|
162 | if (row > -1) {
|
---|
163 | editProperty(row);
|
---|
164 | } else {
|
---|
165 | addProperty();
|
---|
166 | }
|
---|
167 | } else if (e.getSource() == membershipTable) {
|
---|
168 | int row = membershipTable.rowAtPoint(e.getPoint());
|
---|
169 | if (row > -1) {
|
---|
170 | editMembership(row);
|
---|
171 | }
|
---|
172 | }
|
---|
173 | else
|
---|
174 | {
|
---|
175 | addProperty();
|
---|
176 | }
|
---|
177 | }
|
---|
178 | @Override public void mousePressed(MouseEvent e) {
|
---|
179 | if (e.getSource() == propertyTable) {
|
---|
180 | membershipTable.clearSelection();
|
---|
181 | } else if (e.getSource() == membershipTable) {
|
---|
182 | propertyTable.clearSelection();
|
---|
183 | }
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | // hook for roadsigns plugin to display a small
|
---|
188 | // button in the upper right corner of this dialog
|
---|
189 | public static final JPanel pluginHook = new JPanel();
|
---|
190 |
|
---|
191 | private JPopupMenu propertyMenu;
|
---|
192 | private JPopupMenu membershipMenu;
|
---|
193 |
|
---|
194 | private final Map<String, Map<String, Integer>> valueCount = new TreeMap<String, Map<String, Integer>>();
|
---|
195 |
|
---|
196 | Comparator<AutoCompletionListItem> defaultACItemComparator = new Comparator<AutoCompletionListItem>() {
|
---|
197 | public int compare(AutoCompletionListItem o1, AutoCompletionListItem o2) {
|
---|
198 | return String.CASE_INSENSITIVE_ORDER.compare(o1.getValue(), o2.getValue());
|
---|
199 | }
|
---|
200 | };
|
---|
201 |
|
---|
202 | private final DataSetListenerAdapter dataChangedAdapter = new DataSetListenerAdapter(this);
|
---|
203 | private final HelpAction helpAction = new HelpAction();
|
---|
204 | private final CopyValueAction copyValueAction = new CopyValueAction();
|
---|
205 | private final CopyKeyValueAction copyKeyValueAction = new CopyKeyValueAction();
|
---|
206 | private final CopyAllKeyValueAction copyAllKeyValueAction = new CopyAllKeyValueAction();
|
---|
207 | private final SearchAction searchActionSame = new SearchAction(true);
|
---|
208 | private final SearchAction searchActionAny = new SearchAction(false);
|
---|
209 | private final AddAction addAction = new AddAction();
|
---|
210 | private final EditAction editAction = new EditAction();
|
---|
211 | private final DeleteAction deleteAction = new DeleteAction();
|
---|
212 | private final JosmAction[] josmActions = new JosmAction[]{addAction, editAction, deleteAction};
|
---|
213 |
|
---|
214 | @Override
|
---|
215 | public void showNotify() {
|
---|
216 | DatasetEventManager.getInstance().addDatasetListener(dataChangedAdapter, FireMode.IN_EDT_CONSOLIDATED);
|
---|
217 | SelectionEventManager.getInstance().addSelectionListener(this, FireMode.IN_EDT_CONSOLIDATED);
|
---|
218 | MapView.addEditLayerChangeListener(this);
|
---|
219 | for (JosmAction action : josmActions) {
|
---|
220 | Main.registerActionShortcut(action);
|
---|
221 | }
|
---|
222 | updateSelection();
|
---|
223 | }
|
---|
224 |
|
---|
225 | @Override
|
---|
226 | public void hideNotify() {
|
---|
227 | DatasetEventManager.getInstance().removeDatasetListener(dataChangedAdapter);
|
---|
228 | SelectionEventManager.getInstance().removeSelectionListener(this);
|
---|
229 | MapView.removeEditLayerChangeListener(this);
|
---|
230 | for (JosmAction action : josmActions) {
|
---|
231 | Main.unregisterActionShortcut(action);
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * Edit the value in the properties table row
|
---|
237 | * @param row The row of the table from which the value is edited.
|
---|
238 | */
|
---|
239 | @SuppressWarnings("unchecked")
|
---|
240 | private void editProperty(int row) {
|
---|
241 | Collection<OsmPrimitive> sel = Main.main.getCurrentDataSet().getSelected();
|
---|
242 | if (sel.isEmpty()) return;
|
---|
243 |
|
---|
244 | String key = propertyData.getValueAt(row, 0).toString();
|
---|
245 | objKey=key;
|
---|
246 |
|
---|
247 | String msg = "<html>"+trn("This will change {0} object.",
|
---|
248 | "This will change up to {0} objects.", sel.size(), sel.size())
|
---|
249 | +"<br><br>("+tr("An empty value deletes the tag.", key)+")</html>";
|
---|
250 |
|
---|
251 | JPanel panel = new JPanel(new BorderLayout());
|
---|
252 | panel.add(new JLabel(msg), BorderLayout.NORTH);
|
---|
253 |
|
---|
254 | JPanel p = new JPanel(new GridBagLayout());
|
---|
255 | panel.add(p, BorderLayout.CENTER);
|
---|
256 |
|
---|
257 | AutoCompletionManager autocomplete = Main.main.getEditLayer().data.getAutoCompletionManager();
|
---|
258 | List<AutoCompletionListItem> keyList = autocomplete.getKeys();
|
---|
259 | Collections.sort(keyList, defaultACItemComparator);
|
---|
260 |
|
---|
261 | final AutoCompletingComboBox keys = new AutoCompletingComboBox();
|
---|
262 | keys.setPossibleACItems(keyList);
|
---|
263 | keys.setEditable(true);
|
---|
264 | keys.setSelectedItem(key);
|
---|
265 |
|
---|
266 | p.add(new JLabel(tr("Key")), GBC.std());
|
---|
267 | p.add(Box.createHorizontalStrut(10), GBC.std());
|
---|
268 | p.add(keys, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
269 |
|
---|
270 | final AutoCompletingComboBox values = new AutoCompletingComboBox();
|
---|
271 | values.setRenderer(new DefaultListCellRenderer() {
|
---|
272 | @Override public Component getListCellRendererComponent(JList list,
|
---|
273 | Object value, int index, boolean isSelected, boolean cellHasFocus){
|
---|
274 | Component c = super.getListCellRendererComponent(list, value,
|
---|
275 | index, isSelected, cellHasFocus);
|
---|
276 | if (c instanceof JLabel) {
|
---|
277 | String str = ((AutoCompletionListItem) value).getValue();
|
---|
278 | if (valueCount.containsKey(objKey)) {
|
---|
279 | Map<String, Integer> m = valueCount.get(objKey);
|
---|
280 | if (m.containsKey(str)) {
|
---|
281 | str = tr("{0} ({1})", str, m.get(str));
|
---|
282 | c.setFont(c.getFont().deriveFont(Font.ITALIC + Font.BOLD));
|
---|
283 | }
|
---|
284 | }
|
---|
285 | ((JLabel) c).setText(str);
|
---|
286 | }
|
---|
287 | return c;
|
---|
288 | }
|
---|
289 | });
|
---|
290 | values.setEditable(true);
|
---|
291 |
|
---|
292 | final Map<String, Integer> m = (Map<String, Integer>) propertyData.getValueAt(row, 1);
|
---|
293 |
|
---|
294 | Comparator<AutoCompletionListItem> usedValuesAwareComparator = new Comparator<AutoCompletionListItem>() {
|
---|
295 |
|
---|
296 | @Override
|
---|
297 | public int compare(AutoCompletionListItem o1, AutoCompletionListItem o2) {
|
---|
298 | boolean c1 = m.containsKey(o1.getValue());
|
---|
299 | boolean c2 = m.containsKey(o2.getValue());
|
---|
300 | if (c1 == c2)
|
---|
301 | return String.CASE_INSENSITIVE_ORDER.compare(o1.getValue(), o2.getValue());
|
---|
302 | else if (c1)
|
---|
303 | return -1;
|
---|
304 | else
|
---|
305 | return +1;
|
---|
306 | }
|
---|
307 | };
|
---|
308 |
|
---|
309 | List<AutoCompletionListItem> valueList = autocomplete.getValues(getAutocompletionKeys(key));
|
---|
310 | Collections.sort(valueList, usedValuesAwareComparator);
|
---|
311 |
|
---|
312 | values.setPossibleACItems(valueList);
|
---|
313 | final String selection= m.size()!=1?tr("<different>"):m.entrySet().iterator().next().getKey();
|
---|
314 | values.setSelectedItem(selection);
|
---|
315 | values.getEditor().setItem(selection);
|
---|
316 | p.add(new JLabel(tr("Value")), GBC.std());
|
---|
317 | p.add(Box.createHorizontalStrut(10), GBC.std());
|
---|
318 | p.add(values, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
319 | addFocusAdapter(row, keys, values, autocomplete, usedValuesAwareComparator);
|
---|
320 |
|
---|
321 | final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION) {
|
---|
322 | @Override public void selectInitialValue() {
|
---|
323 | // save unix system selection (middle mouse paste)
|
---|
324 | Clipboard sysSel = Toolkit.getDefaultToolkit().getSystemSelection();
|
---|
325 | if(sysSel != null) {
|
---|
326 | Transferable old = sysSel.getContents(null);
|
---|
327 | values.requestFocusInWindow();
|
---|
328 | values.getEditor().selectAll();
|
---|
329 | sysSel.setContents(old, null);
|
---|
330 | } else {
|
---|
331 | values.requestFocusInWindow();
|
---|
332 | values.getEditor().selectAll();
|
---|
333 | }
|
---|
334 | }
|
---|
335 | };
|
---|
336 | final JDialog dlg = optionPane.createDialog(Main.parent, trn("Change value?", "Change values?", m.size()));
|
---|
337 | dlg.setModalityType(ModalityType.DOCUMENT_MODAL);
|
---|
338 | Dimension dlgSize = dlg.getSize();
|
---|
339 | if(dlgSize.width > Main.parent.getSize().width) {
|
---|
340 | dlgSize.width = Math.max(250, Main.parent.getSize().width);
|
---|
341 | dlg.setSize(dlgSize);
|
---|
342 | }
|
---|
343 | dlg.setLocationRelativeTo(Main.parent);
|
---|
344 | values.getEditor().addActionListener(new ActionListener() {
|
---|
345 | public void actionPerformed(ActionEvent e) {
|
---|
346 | dlg.setVisible(false);
|
---|
347 | optionPane.setValue(JOptionPane.OK_OPTION);
|
---|
348 | }
|
---|
349 | });
|
---|
350 |
|
---|
351 | String oldValue = values.getEditor().getItem().toString();
|
---|
352 | dlg.setVisible(true);
|
---|
353 |
|
---|
354 | Object answer = optionPane.getValue();
|
---|
355 | if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE ||
|
---|
356 | (answer instanceof Integer && (Integer)answer != JOptionPane.OK_OPTION)) {
|
---|
357 | values.getEditor().setItem(oldValue);
|
---|
358 | return;
|
---|
359 | }
|
---|
360 |
|
---|
361 | String value = values.getEditor().getItem().toString().trim();
|
---|
362 | // is not Java 1.5
|
---|
363 | //value = java.text.Normalizer.normalize(value, java.text.Normalizer.Form.NFC);
|
---|
364 | if (value.equals("")) {
|
---|
365 | value = null; // delete the key
|
---|
366 | }
|
---|
367 | String newkey = keys.getEditor().getItem().toString().trim();
|
---|
368 | //newkey = java.text.Normalizer.normalize(newkey, java.text.Normalizer.Form.NFC);
|
---|
369 | if (newkey.equals("")) {
|
---|
370 | newkey = key;
|
---|
371 | value = null; // delete the key instead
|
---|
372 | }
|
---|
373 | if (key.equals(newkey) && tr("<different>").equals(value))
|
---|
374 | return;
|
---|
375 | if (key.equals(newkey) || value == null) {
|
---|
376 | Main.main.undoRedo.add(new ChangePropertyCommand(sel, newkey, value));
|
---|
377 | } else {
|
---|
378 | for (OsmPrimitive osm: sel) {
|
---|
379 | if(osm.get(newkey) != null) {
|
---|
380 | ExtendedDialog ed = new ExtendedDialog(
|
---|
381 | Main.parent,
|
---|
382 | tr("Overwrite key"),
|
---|
383 | new String[]{tr("Replace"), tr("Cancel")});
|
---|
384 | ed.setButtonIcons(new String[]{"purge", "cancel"});
|
---|
385 | ed.setContent(tr("You changed the key from ''{0}'' to ''{1}''.\n"
|
---|
386 | + "The new key is already used, overwrite values?", key, newkey));
|
---|
387 | ed.setCancelButton(2);
|
---|
388 | ed.toggleEnable("overwriteEditKey");
|
---|
389 | ed.showDialog();
|
---|
390 |
|
---|
391 | if (ed.getValue() != 1)
|
---|
392 | return;
|
---|
393 | break;
|
---|
394 | }
|
---|
395 | }
|
---|
396 | Collection<Command> commands=new Vector<Command>();
|
---|
397 | commands.add(new ChangePropertyCommand(sel, key, null));
|
---|
398 | if (value.equals(tr("<different>"))) {
|
---|
399 | HashMap<String, Vector<OsmPrimitive>> map=new HashMap<String, Vector<OsmPrimitive>>();
|
---|
400 | for (OsmPrimitive osm: sel) {
|
---|
401 | String val=osm.get(key);
|
---|
402 | if(val != null)
|
---|
403 | {
|
---|
404 | if (map.containsKey(val)) {
|
---|
405 | map.get(val).add(osm);
|
---|
406 | } else {
|
---|
407 | Vector<OsmPrimitive> v = new Vector<OsmPrimitive>();
|
---|
408 | v.add(osm);
|
---|
409 | map.put(val, v);
|
---|
410 | }
|
---|
411 | }
|
---|
412 | }
|
---|
413 | for (Entry<String, Vector<OsmPrimitive>> e: map.entrySet()) {
|
---|
414 | commands.add(new ChangePropertyCommand(e.getValue(), newkey, e.getKey()));
|
---|
415 | }
|
---|
416 | } else {
|
---|
417 | commands.add(new ChangePropertyCommand(sel, newkey, value));
|
---|
418 | }
|
---|
419 | Main.main.undoRedo.add(new SequenceCommand(
|
---|
420 | trn("Change properties of up to {0} object",
|
---|
421 | "Change properties of up to {0} objects", sel.size(), sel.size()),
|
---|
422 | commands));
|
---|
423 | }
|
---|
424 |
|
---|
425 | if(!key.equals(newkey)) {
|
---|
426 | for(int i=0; i < propertyTable.getRowCount(); i++)
|
---|
427 | if(propertyData.getValueAt(i, 0).toString().equals(newkey)) {
|
---|
428 | row=i;
|
---|
429 | break;
|
---|
430 | }
|
---|
431 | }
|
---|
432 | propertyTable.changeSelection(row, 0, false, false);
|
---|
433 | }
|
---|
434 |
|
---|
435 | /**
|
---|
436 | * For a given key k, return a list of keys which are used as keys for
|
---|
437 | * auto-completing values to increase the search space.
|
---|
438 | * @param key the key k
|
---|
439 | * @return a list of keys
|
---|
440 | */
|
---|
441 | private static List<String> getAutocompletionKeys(String key) {
|
---|
442 | if ("name".equals(key) || "addr:street".equals(key))
|
---|
443 | return Arrays.asList("addr:street", "name");
|
---|
444 | else
|
---|
445 | return Arrays.asList(key);
|
---|
446 | }
|
---|
447 |
|
---|
448 | /**
|
---|
449 | * This simply fires up an relation editor for the relation shown; everything else
|
---|
450 | * is the editor's business.
|
---|
451 | *
|
---|
452 | * @param row
|
---|
453 | */
|
---|
454 | private void editMembership(int row) {
|
---|
455 | Relation relation = (Relation)membershipData.getValueAt(row, 0);
|
---|
456 | Main.map.relationListDialog.selectRelation(relation);
|
---|
457 | RelationEditor.getEditor(
|
---|
458 | Main.map.mapView.getEditLayer(),
|
---|
459 | relation,
|
---|
460 | ((MemberInfo) membershipData.getValueAt(row, 1)).role).setVisible(true);
|
---|
461 | }
|
---|
462 |
|
---|
463 | private static String lastAddKey = null;
|
---|
464 | private static String lastAddValue = null;
|
---|
465 |
|
---|
466 | public static final int DEFAULT_LRU_TAGS_NUMBER = 5;
|
---|
467 | public static final int MAX_LRU_TAGS_NUMBER = 9;
|
---|
468 |
|
---|
469 | // LRU cache for recently added tags (http://java-planet.blogspot.com/2005/08/how-to-set-up-simple-lru-cache-using.html)
|
---|
470 | private static final Map<Tag, Void> recentTags = new LinkedHashMap<Tag, Void>(MAX_LRU_TAGS_NUMBER+1, 1.1f, true) {
|
---|
471 | @Override
|
---|
472 | protected boolean removeEldestEntry(Entry<Tag, Void> eldest) {
|
---|
473 | return size() > MAX_LRU_TAGS_NUMBER;
|
---|
474 | }
|
---|
475 | };
|
---|
476 |
|
---|
477 | /**
|
---|
478 | * Open the add selection dialog and add a new key/value to the table (and
|
---|
479 | * to the dataset, of course).
|
---|
480 | */
|
---|
481 | private void addProperty() {
|
---|
482 | Collection<OsmPrimitive> sel;
|
---|
483 | if (Main.map.mapMode instanceof DrawAction) {
|
---|
484 | sel = ((DrawAction) Main.map.mapMode).getInProgressSelection();
|
---|
485 | } else {
|
---|
486 | DataSet ds = Main.main.getCurrentDataSet();
|
---|
487 | if (ds == null) return;
|
---|
488 | sel = ds.getSelected();
|
---|
489 | }
|
---|
490 | if (sel.isEmpty()) return;
|
---|
491 |
|
---|
492 | JPanel p = new JPanel(new GridBagLayout());
|
---|
493 | p.add(new JLabel("<html>"+trn("This will change up to {0} object.",
|
---|
494 | "This will change up to {0} objects.", sel.size(),sel.size())
|
---|
495 | +"<br><br>"+tr("Please select a key")), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
496 | final AutoCompletingComboBox keys = new AutoCompletingComboBox();
|
---|
497 | AutoCompletionManager autocomplete = Main.main.getEditLayer().data.getAutoCompletionManager();
|
---|
498 | List<AutoCompletionListItem> keyList = autocomplete.getKeys();
|
---|
499 |
|
---|
500 | AutoCompletionListItem itemToSelect = null;
|
---|
501 | // remove the object's tag keys from the list
|
---|
502 | Iterator<AutoCompletionListItem> iter = keyList.iterator();
|
---|
503 | while (iter.hasNext()) {
|
---|
504 | AutoCompletionListItem item = iter.next();
|
---|
505 | if (item.getValue().equals(lastAddKey)) {
|
---|
506 | itemToSelect = item;
|
---|
507 | }
|
---|
508 | for (int i = 0; i < propertyData.getRowCount(); ++i) {
|
---|
509 | if (item.getValue().equals(propertyData.getValueAt(i, 0))) {
|
---|
510 | if (itemToSelect == item) {
|
---|
511 | itemToSelect = null;
|
---|
512 | }
|
---|
513 | iter.remove();
|
---|
514 | break;
|
---|
515 | }
|
---|
516 | }
|
---|
517 | }
|
---|
518 |
|
---|
519 | Collections.sort(keyList, defaultACItemComparator);
|
---|
520 | keys.setPossibleACItems(keyList);
|
---|
521 | keys.setEditable(true);
|
---|
522 |
|
---|
523 | p.add(keys, GBC.eop().fill());
|
---|
524 |
|
---|
525 | p.add(new JLabel(tr("Please select a value")), GBC.eol());
|
---|
526 | final AutoCompletingComboBox values = new AutoCompletingComboBox();
|
---|
527 | values.setEditable(true);
|
---|
528 | p.add(values, GBC.eop().fill());
|
---|
529 | if (itemToSelect != null) {
|
---|
530 | keys.setSelectedItem(itemToSelect);
|
---|
531 | /* don't add single chars, as they are no properly selected */
|
---|
532 | if(lastAddValue != null && lastAddValue.length() > 1) {
|
---|
533 | values.setSelectedItem(lastAddValue);
|
---|
534 | }
|
---|
535 | }
|
---|
536 |
|
---|
537 | FocusAdapter focus = addFocusAdapter(-1, keys, values, autocomplete, defaultACItemComparator);
|
---|
538 | // fire focus event in advance or otherwise the popup list will be too small at first
|
---|
539 | focus.focusGained(null);
|
---|
540 |
|
---|
541 | int recentTagsToShow = Main.pref.getInteger("properties.recently-added-tags", DEFAULT_LRU_TAGS_NUMBER);
|
---|
542 | if (recentTagsToShow > MAX_LRU_TAGS_NUMBER) {
|
---|
543 | recentTagsToShow = MAX_LRU_TAGS_NUMBER;
|
---|
544 | }
|
---|
545 | List<JosmAction> recentTagsActions = new ArrayList<JosmAction>();
|
---|
546 | suggestRecentlyAddedTags(p, keys, values, recentTagsActions, recentTagsToShow, focus);
|
---|
547 |
|
---|
548 | JOptionPane pane = new JOptionPane(p, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION){
|
---|
549 | @Override public void selectInitialValue() {
|
---|
550 | // save unix system selection (middle mouse paste)
|
---|
551 | Clipboard sysSel = Toolkit.getDefaultToolkit().getSystemSelection();
|
---|
552 | if(sysSel != null) {
|
---|
553 | Transferable old = sysSel.getContents(null);
|
---|
554 | keys.requestFocusInWindow();
|
---|
555 | keys.getEditor().selectAll();
|
---|
556 | sysSel.setContents(old, null);
|
---|
557 | } else {
|
---|
558 | keys.requestFocusInWindow();
|
---|
559 | keys.getEditor().selectAll();
|
---|
560 | }
|
---|
561 | }
|
---|
562 | };
|
---|
563 | JDialog dialog = pane.createDialog(Main.parent, tr("Add value?"));
|
---|
564 | dialog.setModalityType(ModalityType.DOCUMENT_MODAL);
|
---|
565 | dialog.setVisible(true);
|
---|
566 |
|
---|
567 | for (JosmAction action : recentTagsActions) {
|
---|
568 | action.destroy();
|
---|
569 | }
|
---|
570 |
|
---|
571 | if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
|
---|
572 | return;
|
---|
573 | String key = keys.getEditor().getItem().toString().trim();
|
---|
574 | String value = values.getEditor().getItem().toString().trim();
|
---|
575 | if (key.isEmpty() || value.isEmpty())
|
---|
576 | return;
|
---|
577 | lastAddKey = key;
|
---|
578 | lastAddValue = value;
|
---|
579 | recentTags.put(new Tag(key, value), null);
|
---|
580 | Main.main.undoRedo.add(new ChangePropertyCommand(sel, key, value));
|
---|
581 | btnAdd.requestFocusInWindow();
|
---|
582 | }
|
---|
583 |
|
---|
584 | private void suggestRecentlyAddedTags(JPanel p, final AutoCompletingComboBox keys, final AutoCompletingComboBox values, List<JosmAction> tagsActions, int tagsToShow, final FocusAdapter focus) {
|
---|
585 | if (tagsToShow > 0 && !recentTags.isEmpty()) {
|
---|
586 | p.add(new JLabel(tr("Recently added tags")), GBC.eol());
|
---|
587 |
|
---|
588 | int count = 1;
|
---|
589 | // We store the maximum number (9) of recent tags to allow dynamic change of number of tags shown in the preferences.
|
---|
590 | // This implies to iterate in descending order, as the oldest elements will only be removed after we reach the maximum numbern and not the number of tags to show.
|
---|
591 | // However, as Set does not allow to iterate in descending order, we need to copy its elements into a List we can access in reverse order.
|
---|
592 | List<Tag> tags = new LinkedList<Tag>(recentTags.keySet());
|
---|
593 | for (int i = tags.size()-1; i >= 0 && count <= tagsToShow; i--, count++) {
|
---|
594 | final Tag t = tags.get(i);
|
---|
595 | // Find and display icon
|
---|
596 | ImageIcon icon = MapPaintStyles.getNodeIcon(t, false); // Filters deprecated icon
|
---|
597 | if (icon == null) {
|
---|
598 | icon = new ImageIcon(new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB));
|
---|
599 | }
|
---|
600 | GridBagConstraints gbc = new GridBagConstraints();
|
---|
601 | gbc.ipadx = 5;
|
---|
602 | p.add(new JLabel(icon), gbc);
|
---|
603 | // Create action for reusing the tag, with keyboard shortcut Ctrl+(1-5)
|
---|
604 | String actionShortcutKey = "properties:recent:"+count;
|
---|
605 | Shortcut sc = Shortcut.registerShortcut(actionShortcutKey, null, KeyEvent.VK_0+count, Shortcut.CTRL);
|
---|
606 | final JosmAction action = new JosmAction(actionShortcutKey, null, tr("Use this tag again"), sc, false) {
|
---|
607 | @Override
|
---|
608 | public void actionPerformed(ActionEvent e) {
|
---|
609 | keys.setSelectedItem(t.getKey());
|
---|
610 | values.setSelectedItem(t.getValue());
|
---|
611 | // Update list of values (fix #7951)
|
---|
612 | focus.focusGained(null);
|
---|
613 | }
|
---|
614 | };
|
---|
615 | tagsActions.add(action);
|
---|
616 | // Disable action if its key is already set on the object (the key being absent from the keys list for this reason
|
---|
617 | // performing this action leads to autocomplete to the next key (see #7671 comments)
|
---|
618 | for (int j = 0; j < propertyData.getRowCount(); ++j) {
|
---|
619 | System.out.println(propertyData.getValueAt(j, 0));
|
---|
620 | if (t.getKey().equals(propertyData.getValueAt(j, 0))) {
|
---|
621 | action.setEnabled(false);
|
---|
622 | break;
|
---|
623 | }
|
---|
624 | }
|
---|
625 | // Create tag label
|
---|
626 | final JLabel tagLabel = new JLabel("<html>"
|
---|
627 | + "<style>td{border:1px solid gray; font-weight:normal;}</style>"
|
---|
628 | + "<table><tr><td>" + t.toString() + "</td></tr></table></html>");
|
---|
629 | if (action.isEnabled()) {
|
---|
630 | // Register action
|
---|
631 | p.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(sc.getKeyStroke(), actionShortcutKey);
|
---|
632 | p.getActionMap().put(actionShortcutKey, action);
|
---|
633 | // Make the tag label clickable and set tooltip to the action description (this displays also the keyboard shortcut)
|
---|
634 | tagLabel.setToolTipText((String) action.getValue(Action.SHORT_DESCRIPTION));
|
---|
635 | tagLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
|
---|
636 | tagLabel.addMouseListener(new MouseAdapter() {
|
---|
637 | @Override
|
---|
638 | public void mouseClicked(MouseEvent e) {
|
---|
639 | action.actionPerformed(null);
|
---|
640 | }
|
---|
641 | });
|
---|
642 | } else {
|
---|
643 | // Disable tag label
|
---|
644 | tagLabel.setEnabled(false);
|
---|
645 | // Explain in the tooltip why
|
---|
646 | tagLabel.setToolTipText(tr("The key ''{0}'' is already used", t.getKey()));
|
---|
647 | }
|
---|
648 | // Finally add label to the resulting panel
|
---|
649 | JPanel tagPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
|
---|
650 | tagPanel.add(tagLabel);
|
---|
651 | p.add(tagPanel, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
652 | }
|
---|
653 | }
|
---|
654 | }
|
---|
655 |
|
---|
656 | /**
|
---|
657 | * @param allData
|
---|
658 | * @param keys
|
---|
659 | * @param values
|
---|
660 | */
|
---|
661 | private FocusAdapter addFocusAdapter(final int row,
|
---|
662 | final AutoCompletingComboBox keys, final AutoCompletingComboBox values,
|
---|
663 | final AutoCompletionManager autocomplete, final Comparator<AutoCompletionListItem> comparator) {
|
---|
664 | // get the combo box' editor component
|
---|
665 | JTextComponent editor = (JTextComponent)values.getEditor()
|
---|
666 | .getEditorComponent();
|
---|
667 | // Refresh the values model when focus is gained
|
---|
668 | FocusAdapter focus = new FocusAdapter() {
|
---|
669 | @Override public void focusGained(FocusEvent e) {
|
---|
670 | String key = keys.getEditor().getItem().toString();
|
---|
671 |
|
---|
672 | List<AutoCompletionListItem> valueList = autocomplete.getValues(getAutocompletionKeys(key));
|
---|
673 | Collections.sort(valueList, comparator);
|
---|
674 |
|
---|
675 | values.setPossibleACItems(valueList);
|
---|
676 | objKey=key;
|
---|
677 | }
|
---|
678 | };
|
---|
679 | editor.addFocusListener(focus);
|
---|
680 | return focus;
|
---|
681 | }
|
---|
682 | private String objKey;
|
---|
683 |
|
---|
684 | /**
|
---|
685 | * The property data.
|
---|
686 | */
|
---|
687 | private final DefaultTableModel propertyData = new DefaultTableModel() {
|
---|
688 | @Override public boolean isCellEditable(int row, int column) {
|
---|
689 | return false;
|
---|
690 | }
|
---|
691 | @Override public Class<?> getColumnClass(int columnIndex) {
|
---|
692 | return String.class;
|
---|
693 | }
|
---|
694 | };
|
---|
695 |
|
---|
696 | /**
|
---|
697 | * The membership data.
|
---|
698 | */
|
---|
699 | private final DefaultTableModel membershipData = new DefaultTableModel() {
|
---|
700 | @Override public boolean isCellEditable(int row, int column) {
|
---|
701 | return false;
|
---|
702 | }
|
---|
703 | @Override public Class<?> getColumnClass(int columnIndex) {
|
---|
704 | return String.class;
|
---|
705 | }
|
---|
706 | };
|
---|
707 |
|
---|
708 | /**
|
---|
709 | * The properties list.
|
---|
710 | */
|
---|
711 | private final JTable propertyTable = new JTable(propertyData);
|
---|
712 | private final JTable membershipTable = new JTable(membershipData);
|
---|
713 |
|
---|
714 | /**
|
---|
715 | * The Add/Edit/Delete buttons (needed to be able to disable them)
|
---|
716 | */
|
---|
717 | private final SideButton btnAdd;
|
---|
718 | private final SideButton btnEdit;
|
---|
719 | private final SideButton btnDel;
|
---|
720 | private final PresetListPanel presets = new PresetListPanel();
|
---|
721 |
|
---|
722 | private final JLabel selectSth = new JLabel("<html><p>"
|
---|
723 | + tr("Select objects for which to change properties.") + "</p></html>");
|
---|
724 |
|
---|
725 | static class MemberInfo {
|
---|
726 | List<RelationMember> role = new ArrayList<RelationMember>();
|
---|
727 | List<Integer> position = new ArrayList<Integer>();
|
---|
728 | private String positionString = null;
|
---|
729 | void add(RelationMember r, Integer p) {
|
---|
730 | role.add(r);
|
---|
731 | position.add(p);
|
---|
732 | }
|
---|
733 | String getPositionString() {
|
---|
734 | if (positionString == null) {
|
---|
735 | Collections.sort(position);
|
---|
736 | positionString = String.valueOf(position.get(0));
|
---|
737 | int cnt = 0;
|
---|
738 | int last = position.get(0);
|
---|
739 | for (int i = 1; i < position.size(); ++i) {
|
---|
740 | int cur = position.get(i);
|
---|
741 | if (cur == last + 1) {
|
---|
742 | ++cnt;
|
---|
743 | } else if (cnt == 0) {
|
---|
744 | positionString += "," + String.valueOf(cur);
|
---|
745 | } else {
|
---|
746 | positionString += "-" + String.valueOf(last);
|
---|
747 | positionString += "," + String.valueOf(cur);
|
---|
748 | cnt = 0;
|
---|
749 | }
|
---|
750 | last = cur;
|
---|
751 | }
|
---|
752 | if (cnt >= 1) {
|
---|
753 | positionString += "-" + String.valueOf(last);
|
---|
754 | }
|
---|
755 | }
|
---|
756 | if (positionString.length() > 20) {
|
---|
757 | positionString = positionString.substring(0, 17) + "...";
|
---|
758 | }
|
---|
759 | return positionString;
|
---|
760 | }
|
---|
761 | }
|
---|
762 |
|
---|
763 | /**
|
---|
764 | * Create a new PropertiesDialog
|
---|
765 | */
|
---|
766 | public PropertiesDialog(MapFrame mapFrame) {
|
---|
767 | super(tr("Properties/Memberships"), "propertiesdialog", tr("Properties for selected objects."),
|
---|
768 | Shortcut.registerShortcut("subwindow:properties", tr("Toggle: {0}", tr("Properties/Memberships")), KeyEvent.VK_P,
|
---|
769 | Shortcut.ALT_SHIFT), 150, true);
|
---|
770 |
|
---|
771 | // setting up the properties table
|
---|
772 | propertyMenu = new JPopupMenu();
|
---|
773 | propertyMenu.add(copyValueAction);
|
---|
774 | propertyMenu.add(copyKeyValueAction);
|
---|
775 | propertyMenu.add(copyAllKeyValueAction);
|
---|
776 | propertyMenu.addSeparator();
|
---|
777 | propertyMenu.add(searchActionAny);
|
---|
778 | propertyMenu.add(searchActionSame);
|
---|
779 | propertyMenu.addSeparator();
|
---|
780 | propertyMenu.add(helpAction);
|
---|
781 |
|
---|
782 | propertyData.setColumnIdentifiers(new String[]{tr("Key"),tr("Value")});
|
---|
783 | propertyTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
784 | propertyTable.getTableHeader().setReorderingAllowed(false);
|
---|
785 | propertyTable.addMouseListener(new PopupMenuLauncher() {
|
---|
786 | @Override
|
---|
787 | public void launch(MouseEvent evt) {
|
---|
788 | Point p = evt.getPoint();
|
---|
789 | int row = propertyTable.rowAtPoint(p);
|
---|
790 | if (row > -1) {
|
---|
791 | propertyTable.changeSelection(row, 0, false, false);
|
---|
792 | propertyMenu.show(propertyTable, p.x, p.y-3);
|
---|
793 | }
|
---|
794 | }
|
---|
795 | });
|
---|
796 |
|
---|
797 | propertyTable.getColumnModel().getColumn(1).setCellRenderer(new DefaultTableCellRenderer(){
|
---|
798 | @Override public Component getTableCellRendererComponent(JTable table, Object value,
|
---|
799 | boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
800 | Component c = super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
|
---|
801 | if (value == null)
|
---|
802 | return this;
|
---|
803 | if (c instanceof JLabel) {
|
---|
804 | String str = null;
|
---|
805 | if (value instanceof String) {
|
---|
806 | str = (String) value;
|
---|
807 | } else if (value instanceof Map<?, ?>) {
|
---|
808 | Map<?, ?> v = (Map<?, ?>) value;
|
---|
809 | if (v.size() != 1) {
|
---|
810 | str=tr("<different>");
|
---|
811 | c.setFont(c.getFont().deriveFont(Font.ITALIC));
|
---|
812 | } else {
|
---|
813 | final Map.Entry<?, ?> entry = v.entrySet().iterator().next();
|
---|
814 | str = (String) entry.getKey();
|
---|
815 | }
|
---|
816 | }
|
---|
817 | ((JLabel)c).setText(str);
|
---|
818 | }
|
---|
819 | return c;
|
---|
820 | }
|
---|
821 | });
|
---|
822 |
|
---|
823 | // setting up the membership table
|
---|
824 | membershipMenu = new JPopupMenu();
|
---|
825 | membershipMenu.add(new SelectRelationAction(true));
|
---|
826 | membershipMenu.add(new SelectRelationAction(false));
|
---|
827 | membershipMenu.add(new SelectRelationMembersAction());
|
---|
828 | membershipMenu.add(new DownloadIncompleteMembersAction());
|
---|
829 | membershipMenu.addSeparator();
|
---|
830 | membershipMenu.add(helpAction);
|
---|
831 |
|
---|
832 | membershipData.setColumnIdentifiers(new String[]{tr("Member Of"),tr("Role"),tr("Position")});
|
---|
833 | membershipTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
834 | membershipTable.addMouseListener(new PopupMenuLauncher() {
|
---|
835 | @Override
|
---|
836 | public void launch(MouseEvent evt) {
|
---|
837 | Point p = evt.getPoint();
|
---|
838 | int row = membershipTable.rowAtPoint(p);
|
---|
839 | if (row > -1) {
|
---|
840 | membershipTable.changeSelection(row, 0, false, false);
|
---|
841 | Relation relation = (Relation)membershipData.getValueAt(row, 0);
|
---|
842 | for (Component c : membershipMenu.getComponents()) {
|
---|
843 | if (c instanceof JMenuItem) {
|
---|
844 | Action action = ((JMenuItem) c).getAction();
|
---|
845 | if (action instanceof RelationRelated) {
|
---|
846 | ((RelationRelated)action).setRelation(relation);
|
---|
847 | }
|
---|
848 | }
|
---|
849 | }
|
---|
850 | membershipMenu.show(membershipTable, p.x, p.y-3);
|
---|
851 | }
|
---|
852 | }
|
---|
853 | });
|
---|
854 |
|
---|
855 | TableColumnModel mod = membershipTable.getColumnModel();
|
---|
856 | membershipTable.getTableHeader().setReorderingAllowed(false);
|
---|
857 | mod.getColumn(0).setCellRenderer(new DefaultTableCellRenderer() {
|
---|
858 | @Override public Component getTableCellRendererComponent(JTable table, Object value,
|
---|
859 | boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
860 | Component c = super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
|
---|
861 | if (value == null)
|
---|
862 | return this;
|
---|
863 | if (c instanceof JLabel) {
|
---|
864 | JLabel label = (JLabel)c;
|
---|
865 | Relation r = (Relation)value;
|
---|
866 | label.setText(r.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
867 | if (r.isDisabledAndHidden()) {
|
---|
868 | label.setFont(label.getFont().deriveFont(Font.ITALIC));
|
---|
869 | }
|
---|
870 | }
|
---|
871 | return c;
|
---|
872 | }
|
---|
873 | });
|
---|
874 |
|
---|
875 | mod.getColumn(1).setCellRenderer(new DefaultTableCellRenderer() {
|
---|
876 | @Override public Component getTableCellRendererComponent(JTable table, Object value,
|
---|
877 | boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
878 | if (value == null)
|
---|
879 | return this;
|
---|
880 | Component c = super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
|
---|
881 | boolean isDisabledAndHidden = (((Relation)table.getValueAt(row, 0))).isDisabledAndHidden();
|
---|
882 | if (c instanceof JLabel) {
|
---|
883 | JLabel label = (JLabel)c;
|
---|
884 | MemberInfo col = (MemberInfo) value;
|
---|
885 |
|
---|
886 | String text = null;
|
---|
887 | for (RelationMember r : col.role) {
|
---|
888 | if (text == null) {
|
---|
889 | text = r.getRole();
|
---|
890 | }
|
---|
891 | else if (!text.equals(r.getRole())) {
|
---|
892 | text = tr("<different>");
|
---|
893 | break;
|
---|
894 | }
|
---|
895 | }
|
---|
896 |
|
---|
897 | label.setText(text);
|
---|
898 | if (isDisabledAndHidden) {
|
---|
899 | label.setFont(label.getFont().deriveFont(Font.ITALIC));
|
---|
900 | }
|
---|
901 | }
|
---|
902 | return c;
|
---|
903 | }
|
---|
904 | });
|
---|
905 |
|
---|
906 | mod.getColumn(2).setCellRenderer(new DefaultTableCellRenderer() {
|
---|
907 | @Override public Component getTableCellRendererComponent(JTable table, Object value,
|
---|
908 | boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
909 | Component c = super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
|
---|
910 | boolean isDisabledAndHidden = (((Relation)table.getValueAt(row, 0))).isDisabledAndHidden();
|
---|
911 | if (c instanceof JLabel) {
|
---|
912 | JLabel label = (JLabel)c;
|
---|
913 | label.setText(((MemberInfo) table.getValueAt(row, 1)).getPositionString());
|
---|
914 | if (isDisabledAndHidden) {
|
---|
915 | label.setFont(label.getFont().deriveFont(Font.ITALIC));
|
---|
916 | }
|
---|
917 | }
|
---|
918 | return c;
|
---|
919 | }
|
---|
920 | });
|
---|
921 | mod.getColumn(2).setPreferredWidth(20);
|
---|
922 | mod.getColumn(1).setPreferredWidth(40);
|
---|
923 | mod.getColumn(0).setPreferredWidth(200);
|
---|
924 |
|
---|
925 | // combine both tables and wrap them in a scrollPane
|
---|
926 | JPanel bothTables = new JPanel();
|
---|
927 | boolean top = Main.pref.getBoolean("properties.presets.top", true);
|
---|
928 | bothTables.setLayout(new GridBagLayout());
|
---|
929 | if(top) {
|
---|
930 | bothTables.add(presets, GBC.std().fill(GBC.HORIZONTAL).insets(5, 2, 5, 2).anchor(GBC.NORTHWEST));
|
---|
931 | double epsilon = Double.MIN_VALUE; // need to set a weight or else anchor value is ignored
|
---|
932 | bothTables.add(pluginHook, GBC.eol().insets(0,1,1,1).anchor(GBC.NORTHEAST).weight(epsilon, epsilon));
|
---|
933 | }
|
---|
934 | bothTables.add(selectSth, GBC.eol().fill().insets(10, 10, 10, 10));
|
---|
935 | bothTables.add(propertyTable.getTableHeader(), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
936 | bothTables.add(propertyTable, GBC.eol().fill(GBC.BOTH));
|
---|
937 | bothTables.add(membershipTable.getTableHeader(), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
938 | bothTables.add(membershipTable, GBC.eol().fill(GBC.BOTH));
|
---|
939 | if(!top) {
|
---|
940 | bothTables.add(presets, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 2, 5, 2));
|
---|
941 | }
|
---|
942 |
|
---|
943 | // Open edit dialog whe enter pressed in tables
|
---|
944 | propertyTable.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
|
---|
945 | .put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),"onTableEnter");
|
---|
946 | propertyTable.getActionMap().put("onTableEnter",editAction);
|
---|
947 | membershipTable.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
|
---|
948 | .put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),"onTableEnter");
|
---|
949 | membershipTable.getActionMap().put("onTableEnter",editAction);
|
---|
950 |
|
---|
951 | // Open add property dialog when INS is pressed in tables
|
---|
952 | propertyTable.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
|
---|
953 | .put(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, 0),"onTableInsert");
|
---|
954 | propertyTable.getActionMap().put("onTableInsert",addAction);
|
---|
955 |
|
---|
956 | // unassign some standard shortcuts for JTable to allow upload / download
|
---|
957 | InputMapUtils.unassignCtrlShiftUpDown(propertyTable, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
|
---|
958 |
|
---|
959 | // -- add action and shortcut
|
---|
960 | this.btnAdd = new SideButton(addAction);
|
---|
961 | InputMapUtils.enableEnter(this.btnAdd);
|
---|
962 |
|
---|
963 | // -- edit action
|
---|
964 | //
|
---|
965 | propertyTable.getSelectionModel().addListSelectionListener(editAction);
|
---|
966 | membershipTable.getSelectionModel().addListSelectionListener(editAction);
|
---|
967 | this.btnEdit = new SideButton(editAction);
|
---|
968 |
|
---|
969 | // -- delete action
|
---|
970 | //
|
---|
971 | this.btnDel = new SideButton(deleteAction);
|
---|
972 | membershipTable.getSelectionModel().addListSelectionListener(deleteAction);
|
---|
973 | propertyTable.getSelectionModel().addListSelectionListener(deleteAction);
|
---|
974 | getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
|
---|
975 | KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0),"delete"
|
---|
976 | );
|
---|
977 | getActionMap().put("delete", deleteAction);
|
---|
978 |
|
---|
979 | JScrollPane scrollPane = (JScrollPane) createLayout(bothTables, true, Arrays.asList(new SideButton[] {
|
---|
980 | this.btnAdd, this.btnEdit, this.btnDel
|
---|
981 | }));
|
---|
982 |
|
---|
983 | MouseClickWatch mouseClickWatch = new MouseClickWatch();
|
---|
984 | propertyTable.addMouseListener(mouseClickWatch);
|
---|
985 | membershipTable.addMouseListener(mouseClickWatch);
|
---|
986 | scrollPane.addMouseListener(mouseClickWatch);
|
---|
987 |
|
---|
988 | selectSth.setPreferredSize(scrollPane.getSize());
|
---|
989 | presets.setSize(scrollPane.getSize());
|
---|
990 |
|
---|
991 | // -- help action
|
---|
992 | //
|
---|
993 | getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
|
---|
994 | KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0), "onHelp");
|
---|
995 | getActionMap().put("onHelp", helpAction);
|
---|
996 | }
|
---|
997 |
|
---|
998 | @Override public void setVisible(boolean b) {
|
---|
999 | super.setVisible(b);
|
---|
1000 | if (b && Main.main.getCurrentDataSet() != null) {
|
---|
1001 | selectionChanged(Main.main.getCurrentDataSet().getSelected());
|
---|
1002 | }
|
---|
1003 | }
|
---|
1004 |
|
---|
1005 | private int findRow(TableModel model, Object value) {
|
---|
1006 | for (int i=0; i<model.getRowCount(); i++) {
|
---|
1007 | if (model.getValueAt(i, 0).equals(value))
|
---|
1008 | return i;
|
---|
1009 | }
|
---|
1010 | return -1;
|
---|
1011 | }
|
---|
1012 |
|
---|
1013 | private PresetHandler presetHandler = new PresetHandler() {
|
---|
1014 |
|
---|
1015 | @Override
|
---|
1016 | public void updateTags(List<Tag> tags) {
|
---|
1017 | Command command = TaggingPreset.createCommand(getSelection(), tags);
|
---|
1018 | if (command != null) {
|
---|
1019 | Main.main.undoRedo.add(command);
|
---|
1020 | }
|
---|
1021 | }
|
---|
1022 |
|
---|
1023 | @Override
|
---|
1024 | public Collection<OsmPrimitive> getSelection() {
|
---|
1025 | if (Main.main == null) return null;
|
---|
1026 | if (Main.main.getCurrentDataSet() == null) return null;
|
---|
1027 |
|
---|
1028 | return Main.main.getCurrentDataSet().getSelected();
|
---|
1029 | }
|
---|
1030 | };
|
---|
1031 |
|
---|
1032 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
---|
1033 | if (!isVisible())
|
---|
1034 | return;
|
---|
1035 | if (propertyTable == null)
|
---|
1036 | return; // selection changed may be received in base class constructor before init
|
---|
1037 | if (propertyTable.getCellEditor() != null) {
|
---|
1038 | propertyTable.getCellEditor().cancelCellEditing();
|
---|
1039 | }
|
---|
1040 |
|
---|
1041 | String selectedTag = null;
|
---|
1042 | Relation selectedRelation = null;
|
---|
1043 | if (propertyTable.getSelectedRowCount() == 1) {
|
---|
1044 | selectedTag = (String)propertyData.getValueAt(propertyTable.getSelectedRow(), 0);
|
---|
1045 | }
|
---|
1046 | if (membershipTable.getSelectedRowCount() == 1) {
|
---|
1047 | selectedRelation = (Relation)membershipData.getValueAt(membershipTable.getSelectedRow(), 0);
|
---|
1048 | }
|
---|
1049 |
|
---|
1050 | // re-load property data
|
---|
1051 | propertyData.setRowCount(0);
|
---|
1052 |
|
---|
1053 | final Map<String, Integer> keyCount = new HashMap<String, Integer>();
|
---|
1054 | final Map<String, String> tags = new HashMap<String, String>();
|
---|
1055 | valueCount.clear();
|
---|
1056 | EnumSet<PresetType> types = EnumSet.noneOf(TaggingPreset.PresetType.class);
|
---|
1057 | for (OsmPrimitive osm : newSelection) {
|
---|
1058 | types.add(PresetType.forPrimitive(osm));
|
---|
1059 | for (String key : osm.keySet()) {
|
---|
1060 | String value = osm.get(key);
|
---|
1061 | keyCount.put(key, keyCount.containsKey(key) ? keyCount.get(key) + 1 : 1);
|
---|
1062 | if (valueCount.containsKey(key)) {
|
---|
1063 | Map<String, Integer> v = valueCount.get(key);
|
---|
1064 | v.put(value, v.containsKey(value) ? v.get(value) + 1 : 1);
|
---|
1065 | } else {
|
---|
1066 | TreeMap<String, Integer> v = new TreeMap<String, Integer>();
|
---|
1067 | v.put(value, 1);
|
---|
1068 | valueCount.put(key, v);
|
---|
1069 | }
|
---|
1070 | }
|
---|
1071 | }
|
---|
1072 | for (Entry<String, Map<String, Integer>> e : valueCount.entrySet()) {
|
---|
1073 | int count = 0;
|
---|
1074 | for (Entry<String, Integer> e1 : e.getValue().entrySet()) {
|
---|
1075 | count += e1.getValue();
|
---|
1076 | }
|
---|
1077 | if (count < newSelection.size()) {
|
---|
1078 | e.getValue().put("", newSelection.size() - count);
|
---|
1079 | }
|
---|
1080 | propertyData.addRow(new Object[]{e.getKey(), e.getValue()});
|
---|
1081 | tags.put(e.getKey(), e.getValue().size() == 1
|
---|
1082 | ? e.getValue().keySet().iterator().next() : tr("<different>"));
|
---|
1083 | }
|
---|
1084 |
|
---|
1085 | membershipData.setRowCount(0);
|
---|
1086 |
|
---|
1087 | Map<Relation, MemberInfo> roles = new HashMap<Relation, MemberInfo>();
|
---|
1088 | for (OsmPrimitive primitive: newSelection) {
|
---|
1089 | for (OsmPrimitive ref: primitive.getReferrers()) {
|
---|
1090 | if (ref instanceof Relation && !ref.isIncomplete() && !ref.isDeleted()) {
|
---|
1091 | Relation r = (Relation) ref;
|
---|
1092 | MemberInfo mi = roles.get(r);
|
---|
1093 | if(mi == null) {
|
---|
1094 | mi = new MemberInfo();
|
---|
1095 | }
|
---|
1096 | roles.put(r, mi);
|
---|
1097 | int i = 1;
|
---|
1098 | for (RelationMember m : r.getMembers()) {
|
---|
1099 | if (m.getMember() == primitive) {
|
---|
1100 | mi.add(m, i);
|
---|
1101 | }
|
---|
1102 | ++i;
|
---|
1103 | }
|
---|
1104 | }
|
---|
1105 | }
|
---|
1106 | }
|
---|
1107 |
|
---|
1108 | List<Relation> sortedRelations = new ArrayList<Relation>(roles.keySet());
|
---|
1109 | Collections.sort(sortedRelations, new Comparator<Relation>() {
|
---|
1110 | public int compare(Relation o1, Relation o2) {
|
---|
1111 | int comp = Boolean.valueOf(o1.isDisabledAndHidden()).compareTo(o2.isDisabledAndHidden());
|
---|
1112 | if (comp == 0) {
|
---|
1113 | comp = o1.getDisplayName(DefaultNameFormatter.getInstance()).compareTo(o2.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
1114 | }
|
---|
1115 | return comp;
|
---|
1116 | }}
|
---|
1117 | );
|
---|
1118 |
|
---|
1119 | for (Relation r: sortedRelations) {
|
---|
1120 | membershipData.addRow(new Object[]{r, roles.get(r)});
|
---|
1121 | }
|
---|
1122 |
|
---|
1123 | presets.updatePresets(types, tags, presetHandler);
|
---|
1124 |
|
---|
1125 | membershipTable.getTableHeader().setVisible(membershipData.getRowCount() > 0);
|
---|
1126 | membershipTable.setVisible(membershipData.getRowCount() > 0);
|
---|
1127 |
|
---|
1128 | boolean hasSelection = !newSelection.isEmpty();
|
---|
1129 | boolean hasTags = hasSelection && propertyData.getRowCount() > 0;
|
---|
1130 | boolean hasMemberships = hasSelection && membershipData.getRowCount() > 0;
|
---|
1131 | btnAdd.setEnabled(hasSelection);
|
---|
1132 | btnEdit.setEnabled(hasTags || hasMemberships);
|
---|
1133 | btnDel.setEnabled(hasTags || hasMemberships);
|
---|
1134 | propertyTable.setVisible(hasTags);
|
---|
1135 | propertyTable.getTableHeader().setVisible(hasTags);
|
---|
1136 | selectSth.setVisible(!hasSelection);
|
---|
1137 | pluginHook.setVisible(hasSelection);
|
---|
1138 |
|
---|
1139 | int selectedIndex;
|
---|
1140 | if (selectedTag != null && (selectedIndex = findRow(propertyData, selectedTag)) != -1) {
|
---|
1141 | propertyTable.changeSelection(selectedIndex, 0, false, false);
|
---|
1142 | } else if (selectedRelation != null && (selectedIndex = findRow(membershipData, selectedRelation)) != -1) {
|
---|
1143 | membershipTable.changeSelection(selectedIndex, 0, false, false);
|
---|
1144 | } else if(hasTags) {
|
---|
1145 | propertyTable.changeSelection(0, 0, false, false);
|
---|
1146 | } else if(hasMemberships) {
|
---|
1147 | membershipTable.changeSelection(0, 0, false, false);
|
---|
1148 | }
|
---|
1149 |
|
---|
1150 | if(propertyData.getRowCount() != 0 || membershipData.getRowCount() != 0) {
|
---|
1151 | setTitle(tr("Properties: {0} / Memberships: {1}",
|
---|
1152 | propertyData.getRowCount(), membershipData.getRowCount()));
|
---|
1153 | } else {
|
---|
1154 | setTitle(tr("Properties / Memberships"));
|
---|
1155 | }
|
---|
1156 | }
|
---|
1157 |
|
---|
1158 | private void updateSelection() {
|
---|
1159 | if (Main.main.getCurrentDataSet() == null) {
|
---|
1160 | selectionChanged(Collections.<OsmPrimitive>emptyList());
|
---|
1161 | } else {
|
---|
1162 | selectionChanged(Main.main.getCurrentDataSet().getSelected());
|
---|
1163 | }
|
---|
1164 | }
|
---|
1165 |
|
---|
1166 | /* ---------------------------------------------------------------------------------- */
|
---|
1167 | /* EditLayerChangeListener */
|
---|
1168 | /* ---------------------------------------------------------------------------------- */
|
---|
1169 | public void editLayerChanged(OsmDataLayer oldLayer, OsmDataLayer newLayer) {
|
---|
1170 | updateSelection();
|
---|
1171 | }
|
---|
1172 |
|
---|
1173 | public void processDatasetEvent(AbstractDatasetChangedEvent event) {
|
---|
1174 | updateSelection();
|
---|
1175 | }
|
---|
1176 |
|
---|
1177 | class DeleteAction extends JosmAction implements ListSelectionListener {
|
---|
1178 |
|
---|
1179 | public DeleteAction() {
|
---|
1180 | super(tr("Delete"), "dialogs/delete", tr("Delete the selected key in all objects"),
|
---|
1181 | Shortcut.registerShortcut("properties:delete", tr("Delete Properties"), KeyEvent.VK_D,
|
---|
1182 | Shortcut.ALT_CTRL_SHIFT), false);
|
---|
1183 | updateEnabledState();
|
---|
1184 | }
|
---|
1185 |
|
---|
1186 | protected void deleteProperties(int[] rows){
|
---|
1187 | // convert list of rows to HashMap (and find gap for nextKey)
|
---|
1188 | HashMap<String, String> tags = new HashMap<String, String>(rows.length);
|
---|
1189 | int nextKeyIndex = rows[0];
|
---|
1190 | for (int row : rows) {
|
---|
1191 | String key = propertyData.getValueAt(row, 0).toString();
|
---|
1192 | if (row == nextKeyIndex + 1) {
|
---|
1193 | nextKeyIndex = row; // no gap yet
|
---|
1194 | }
|
---|
1195 | tags.put(key, null);
|
---|
1196 | }
|
---|
1197 |
|
---|
1198 | // find key to select after deleting other properties
|
---|
1199 | String nextKey = null;
|
---|
1200 | int rowCount = propertyData.getRowCount();
|
---|
1201 | if (rowCount > rows.length) {
|
---|
1202 | if (nextKeyIndex == rows[rows.length-1]) {
|
---|
1203 | // no gap found, pick next or previous key in list
|
---|
1204 | nextKeyIndex = (nextKeyIndex + 1 < rowCount ? nextKeyIndex + 1 : rows[0] - 1);
|
---|
1205 | } else {
|
---|
1206 | // gap found
|
---|
1207 | nextKeyIndex++;
|
---|
1208 | }
|
---|
1209 | nextKey = (String)propertyData.getValueAt(nextKeyIndex, 0);
|
---|
1210 | }
|
---|
1211 |
|
---|
1212 | Collection<OsmPrimitive> sel = Main.main.getCurrentDataSet().getSelected();
|
---|
1213 | Main.main.undoRedo.add(new ChangePropertyCommand(sel, tags));
|
---|
1214 |
|
---|
1215 | membershipTable.clearSelection();
|
---|
1216 | if (nextKey != null) {
|
---|
1217 | propertyTable.changeSelection(findRow(propertyData, nextKey), 0, false, false);
|
---|
1218 | }
|
---|
1219 | }
|
---|
1220 |
|
---|
1221 | protected void deleteFromRelation(int row) {
|
---|
1222 | Relation cur = (Relation)membershipData.getValueAt(row, 0);
|
---|
1223 |
|
---|
1224 | Relation nextRelation = null;
|
---|
1225 | int rowCount = membershipTable.getRowCount();
|
---|
1226 | if (rowCount > 1) {
|
---|
1227 | nextRelation = (Relation)membershipData.getValueAt((row + 1 < rowCount ? row + 1 : row - 1), 0);
|
---|
1228 | }
|
---|
1229 |
|
---|
1230 | ExtendedDialog ed = new ExtendedDialog(Main.parent,
|
---|
1231 | tr("Change relation"),
|
---|
1232 | new String[] {tr("Delete from relation"), tr("Cancel")});
|
---|
1233 | ed.setButtonIcons(new String[] {"dialogs/delete.png", "cancel.png"});
|
---|
1234 | ed.setContent(tr("Really delete selection from relation {0}?", cur.getDisplayName(DefaultNameFormatter.getInstance())));
|
---|
1235 | ed.toggleEnable("delete_from_relation");
|
---|
1236 | ed.showDialog();
|
---|
1237 |
|
---|
1238 | if(ed.getValue() != 1)
|
---|
1239 | return;
|
---|
1240 |
|
---|
1241 | Relation rel = new Relation(cur);
|
---|
1242 | Collection<OsmPrimitive> sel = Main.main.getCurrentDataSet().getSelected();
|
---|
1243 | for (OsmPrimitive primitive: sel) {
|
---|
1244 | rel.removeMembersFor(primitive);
|
---|
1245 | }
|
---|
1246 | Main.main.undoRedo.add(new ChangeCommand(cur, rel));
|
---|
1247 |
|
---|
1248 | propertyTable.clearSelection();
|
---|
1249 | if (nextRelation != null) {
|
---|
1250 | membershipTable.changeSelection(findRow(membershipData, nextRelation), 0, false, false);
|
---|
1251 | }
|
---|
1252 | }
|
---|
1253 |
|
---|
1254 | @Override
|
---|
1255 | public void actionPerformed(ActionEvent e) {
|
---|
1256 | if (propertyTable.getSelectedRowCount() > 0) {
|
---|
1257 | int[] rows = propertyTable.getSelectedRows();
|
---|
1258 | deleteProperties(rows);
|
---|
1259 | } else if (membershipTable.getSelectedRowCount() > 0) {
|
---|
1260 | int row = membershipTable.getSelectedRow();
|
---|
1261 | deleteFromRelation(row);
|
---|
1262 | }
|
---|
1263 | }
|
---|
1264 |
|
---|
1265 | @Override
|
---|
1266 | protected void updateEnabledState() {
|
---|
1267 | setEnabled(
|
---|
1268 | (propertyTable != null && propertyTable.getSelectedRowCount() >= 1)
|
---|
1269 | || (membershipTable != null && membershipTable.getSelectedRowCount() == 1)
|
---|
1270 | );
|
---|
1271 | }
|
---|
1272 |
|
---|
1273 | @Override
|
---|
1274 | public void valueChanged(ListSelectionEvent e) {
|
---|
1275 | updateEnabledState();
|
---|
1276 | }
|
---|
1277 | }
|
---|
1278 |
|
---|
1279 | class AddAction extends JosmAction {
|
---|
1280 | public AddAction() {
|
---|
1281 | super(tr("Add"), "dialogs/add", tr("Add a new key/value pair to all objects"),
|
---|
1282 | Shortcut.registerShortcut("properties:add", tr("Add Property"), KeyEvent.VK_A,
|
---|
1283 | Shortcut.ALT), false);
|
---|
1284 | }
|
---|
1285 |
|
---|
1286 | @Override
|
---|
1287 | public void actionPerformed(ActionEvent e) {
|
---|
1288 | addProperty();
|
---|
1289 | }
|
---|
1290 | }
|
---|
1291 |
|
---|
1292 | class EditAction extends JosmAction implements ListSelectionListener {
|
---|
1293 | public EditAction() {
|
---|
1294 | super(tr("Edit"), "dialogs/edit", tr("Edit the value of the selected key for all objects"),
|
---|
1295 | Shortcut.registerShortcut("properties:edit", tr("Edit Properties"), KeyEvent.VK_S,
|
---|
1296 | Shortcut.ALT), false);
|
---|
1297 | updateEnabledState();
|
---|
1298 | }
|
---|
1299 |
|
---|
1300 | @Override
|
---|
1301 | public void actionPerformed(ActionEvent e) {
|
---|
1302 | if (!isEnabled())
|
---|
1303 | return;
|
---|
1304 | if (propertyTable.getSelectedRowCount() == 1) {
|
---|
1305 | int row = propertyTable.getSelectedRow();
|
---|
1306 | editProperty(row);
|
---|
1307 | } else if (membershipTable.getSelectedRowCount() == 1) {
|
---|
1308 | int row = membershipTable.getSelectedRow();
|
---|
1309 | editMembership(row);
|
---|
1310 | }
|
---|
1311 | }
|
---|
1312 |
|
---|
1313 | @Override
|
---|
1314 | protected void updateEnabledState() {
|
---|
1315 | setEnabled(
|
---|
1316 | (propertyTable != null && propertyTable.getSelectedRowCount() == 1)
|
---|
1317 | ^ (membershipTable != null && membershipTable.getSelectedRowCount() == 1)
|
---|
1318 | );
|
---|
1319 | }
|
---|
1320 |
|
---|
1321 | @Override
|
---|
1322 | public void valueChanged(ListSelectionEvent e) {
|
---|
1323 | updateEnabledState();
|
---|
1324 | }
|
---|
1325 | }
|
---|
1326 |
|
---|
1327 | class HelpAction extends AbstractAction {
|
---|
1328 | public HelpAction() {
|
---|
1329 | putValue(NAME, tr("Go to OSM wiki for tag help (F1)"));
|
---|
1330 | putValue(SHORT_DESCRIPTION, tr("Launch browser with wiki help for selected object"));
|
---|
1331 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "search"));
|
---|
1332 | }
|
---|
1333 |
|
---|
1334 | public void actionPerformed(ActionEvent e) {
|
---|
1335 | try {
|
---|
1336 | String base = Main.pref.get("url.openstreetmap-wiki", "http://wiki.openstreetmap.org/wiki/");
|
---|
1337 | String lang = LanguageInfo.getWikiLanguagePrefix();
|
---|
1338 | final List<URI> uris = new ArrayList<URI>();
|
---|
1339 | int row;
|
---|
1340 | if (propertyTable.getSelectedRowCount() == 1) {
|
---|
1341 | row = propertyTable.getSelectedRow();
|
---|
1342 | String key = URLEncoder.encode(propertyData.getValueAt(row, 0).toString(), "UTF-8");
|
---|
1343 | String val = URLEncoder.encode(
|
---|
1344 | ((Map<String,Integer>)propertyData.getValueAt(row, 1))
|
---|
1345 | .entrySet().iterator().next().getKey(), "UTF-8"
|
---|
1346 | );
|
---|
1347 |
|
---|
1348 | uris.add(new URI(String.format("%s%sTag:%s=%s", base, lang, key, val)));
|
---|
1349 | uris.add(new URI(String.format("%sTag:%s=%s", base, key, val)));
|
---|
1350 | uris.add(new URI(String.format("%s%sKey:%s", base, lang, key)));
|
---|
1351 | uris.add(new URI(String.format("%sKey:%s", base, key)));
|
---|
1352 | uris.add(new URI(String.format("%s%sMap_Features", base, lang)));
|
---|
1353 | uris.add(new URI(String.format("%sMap_Features", base)));
|
---|
1354 | } else if (membershipTable.getSelectedRowCount() == 1) {
|
---|
1355 | row = membershipTable.getSelectedRow();
|
---|
1356 | String type = URLEncoder.encode(
|
---|
1357 | ((Relation)membershipData.getValueAt(row, 0)).get("type"), "UTF-8"
|
---|
1358 | );
|
---|
1359 |
|
---|
1360 | if (type != null && !type.equals("")) {
|
---|
1361 | uris.add(new URI(String.format("%s%sRelation:%s", base, lang, type)));
|
---|
1362 | uris.add(new URI(String.format("%sRelation:%s", base, type)));
|
---|
1363 | }
|
---|
1364 |
|
---|
1365 | uris.add(new URI(String.format("%s%sRelations", base, lang)));
|
---|
1366 | uris.add(new URI(String.format("%sRelations", base)));
|
---|
1367 | } else {
|
---|
1368 | // give the generic help page, if more than one element is selected
|
---|
1369 | uris.add(new URI(String.format("%s%sMap_Features", base, lang)));
|
---|
1370 | uris.add(new URI(String.format("%sMap_Features", base)));
|
---|
1371 | }
|
---|
1372 |
|
---|
1373 | Main.worker.execute(new Runnable(){
|
---|
1374 | public void run() {
|
---|
1375 | try {
|
---|
1376 | // find a page that actually exists in the wiki
|
---|
1377 | HttpURLConnection conn;
|
---|
1378 | for (URI u : uris) {
|
---|
1379 | conn = (HttpURLConnection) u.toURL().openConnection();
|
---|
1380 | conn.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000);
|
---|
1381 |
|
---|
1382 | if (conn.getResponseCode() != 200) {
|
---|
1383 | System.out.println("INFO: " + u + " does not exist");
|
---|
1384 | conn.disconnect();
|
---|
1385 | } else {
|
---|
1386 | int osize = conn.getContentLength();
|
---|
1387 | conn.disconnect();
|
---|
1388 |
|
---|
1389 | conn = (HttpURLConnection) new URI(u.toString()
|
---|
1390 | .replace("=", "%3D") /* do not URLencode whole string! */
|
---|
1391 | .replaceFirst("/wiki/", "/w/index.php?redirect=no&title=")
|
---|
1392 | ).toURL().openConnection();
|
---|
1393 | conn.setConnectTimeout(Main.pref.getInteger("socket.timeout.connect",15)*1000);
|
---|
1394 |
|
---|
1395 | /* redirect pages have different content length, but retrieving a "nonredirect"
|
---|
1396 | * page using index.php and the direct-link method gives slightly different
|
---|
1397 | * content lengths, so we have to be fuzzy.. (this is UGLY, recode if u know better)
|
---|
1398 | */
|
---|
1399 | if (Math.abs(conn.getContentLength() - osize) > 200) {
|
---|
1400 | System.out.println("INFO: " + u + " is a mediawiki redirect");
|
---|
1401 | conn.disconnect();
|
---|
1402 | } else {
|
---|
1403 | System.out.println("INFO: browsing to " + u);
|
---|
1404 | conn.disconnect();
|
---|
1405 |
|
---|
1406 | OpenBrowser.displayUrl(u.toString());
|
---|
1407 | break;
|
---|
1408 | }
|
---|
1409 | }
|
---|
1410 | }
|
---|
1411 | } catch (Exception e) {
|
---|
1412 | e.printStackTrace();
|
---|
1413 | }
|
---|
1414 | }
|
---|
1415 | });
|
---|
1416 | } catch (Exception e1) {
|
---|
1417 | e1.printStackTrace();
|
---|
1418 | }
|
---|
1419 | }
|
---|
1420 | }
|
---|
1421 |
|
---|
1422 | public void addPropertyPopupMenuSeparator() {
|
---|
1423 | propertyMenu.addSeparator();
|
---|
1424 | }
|
---|
1425 |
|
---|
1426 | public JMenuItem addPropertyPopupMenuAction(Action a) {
|
---|
1427 | return propertyMenu.add(a);
|
---|
1428 | }
|
---|
1429 |
|
---|
1430 | public void addPropertyPopupMenuListener(PopupMenuListener l) {
|
---|
1431 | propertyMenu.addPopupMenuListener(l);
|
---|
1432 | }
|
---|
1433 |
|
---|
1434 | public void removePropertyPopupMenuListener(PopupMenuListener l) {
|
---|
1435 | propertyMenu.addPopupMenuListener(l);
|
---|
1436 | }
|
---|
1437 |
|
---|
1438 | @SuppressWarnings("unchecked")
|
---|
1439 | public Tag getSelectedProperty() {
|
---|
1440 | int row = propertyTable.getSelectedRow();
|
---|
1441 | if (row == -1) return null;
|
---|
1442 | TreeMap<String, Integer> map = (TreeMap<String, Integer>) propertyData.getValueAt(row, 1);
|
---|
1443 | return new Tag(
|
---|
1444 | propertyData.getValueAt(row, 0).toString(),
|
---|
1445 | map.size() > 1 ? "" : map.keySet().iterator().next());
|
---|
1446 | }
|
---|
1447 |
|
---|
1448 | public void addMembershipPopupMenuSeparator() {
|
---|
1449 | membershipMenu.addSeparator();
|
---|
1450 | }
|
---|
1451 |
|
---|
1452 | public JMenuItem addMembershipPopupMenuAction(Action a) {
|
---|
1453 | return membershipMenu.add(a);
|
---|
1454 | }
|
---|
1455 |
|
---|
1456 | public void addMembershipPopupMenuListener(PopupMenuListener l) {
|
---|
1457 | membershipMenu.addPopupMenuListener(l);
|
---|
1458 | }
|
---|
1459 |
|
---|
1460 | public void removeMembershipPopupMenuListener(PopupMenuListener l) {
|
---|
1461 | membershipMenu.addPopupMenuListener(l);
|
---|
1462 | }
|
---|
1463 |
|
---|
1464 | public IRelation getSelectedMembershipRelation() {
|
---|
1465 | int row = membershipTable.getSelectedRow();
|
---|
1466 | return row > -1 ? (IRelation) membershipData.getValueAt(row, 0) : null;
|
---|
1467 | }
|
---|
1468 |
|
---|
1469 | public static interface RelationRelated {
|
---|
1470 | public Relation getRelation();
|
---|
1471 | public void setRelation(Relation relation);
|
---|
1472 | }
|
---|
1473 |
|
---|
1474 | static abstract class AbstractRelationAction extends AbstractAction implements RelationRelated {
|
---|
1475 | protected Relation relation;
|
---|
1476 | public Relation getRelation() {
|
---|
1477 | return this.relation;
|
---|
1478 | }
|
---|
1479 | public void setRelation(Relation relation) {
|
---|
1480 | this.relation = relation;
|
---|
1481 | }
|
---|
1482 | }
|
---|
1483 |
|
---|
1484 | static class SelectRelationAction extends AbstractRelationAction {
|
---|
1485 | boolean selectionmode;
|
---|
1486 | public SelectRelationAction(boolean select) {
|
---|
1487 | selectionmode = select;
|
---|
1488 | if(select) {
|
---|
1489 | putValue(NAME, tr("Select relation"));
|
---|
1490 | putValue(SHORT_DESCRIPTION, tr("Select relation in main selection."));
|
---|
1491 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "select"));
|
---|
1492 | } else {
|
---|
1493 | putValue(NAME, tr("Select in relation list"));
|
---|
1494 | putValue(SHORT_DESCRIPTION, tr("Select relation in relation list."));
|
---|
1495 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "relationlist"));
|
---|
1496 | }
|
---|
1497 | }
|
---|
1498 |
|
---|
1499 | public void actionPerformed(ActionEvent e) {
|
---|
1500 | if(selectionmode) {
|
---|
1501 | Main.map.mapView.getEditLayer().data.setSelected(relation);
|
---|
1502 | } else {
|
---|
1503 | Main.map.relationListDialog.selectRelation(relation);
|
---|
1504 | Main.map.relationListDialog.unfurlDialog();
|
---|
1505 | }
|
---|
1506 | }
|
---|
1507 | }
|
---|
1508 |
|
---|
1509 |
|
---|
1510 | /**
|
---|
1511 | * Sets the current selection to the members of selected relation
|
---|
1512 | *
|
---|
1513 | */
|
---|
1514 | class SelectRelationMembersAction extends AbstractRelationAction {
|
---|
1515 | public SelectRelationMembersAction() {
|
---|
1516 | putValue(SHORT_DESCRIPTION,tr("Select the members of selected relation"));
|
---|
1517 | putValue(SMALL_ICON, ImageProvider.get("selectall"));
|
---|
1518 | putValue(NAME, tr("Select members"));
|
---|
1519 | }
|
---|
1520 |
|
---|
1521 | public void actionPerformed(ActionEvent e) {
|
---|
1522 | HashSet<OsmPrimitive> members = new HashSet<OsmPrimitive>();
|
---|
1523 | members.addAll(relation.getMemberPrimitives());
|
---|
1524 | Main.map.mapView.getEditLayer().data.setSelected(members);
|
---|
1525 | }
|
---|
1526 |
|
---|
1527 | }
|
---|
1528 |
|
---|
1529 | /**
|
---|
1530 | * Action for downloading incomplete members of selected relation
|
---|
1531 | *
|
---|
1532 | */
|
---|
1533 | class DownloadIncompleteMembersAction extends AbstractRelationAction {
|
---|
1534 | public DownloadIncompleteMembersAction() {
|
---|
1535 | putValue(SHORT_DESCRIPTION, tr("Download incomplete members of selected relations"));
|
---|
1536 | putValue(SMALL_ICON, ImageProvider.get("dialogs/relation", "downloadincompleteselected"));
|
---|
1537 | putValue(NAME, tr("Download incomplete members"));
|
---|
1538 | }
|
---|
1539 |
|
---|
1540 | public Set<OsmPrimitive> buildSetOfIncompleteMembers(Relation r) {
|
---|
1541 | Set<OsmPrimitive> ret = new HashSet<OsmPrimitive>();
|
---|
1542 | ret.addAll(r.getIncompleteMembers());
|
---|
1543 | return ret;
|
---|
1544 | }
|
---|
1545 |
|
---|
1546 | public void actionPerformed(ActionEvent e) {
|
---|
1547 | if (!relation.hasIncompleteMembers()) return;
|
---|
1548 | ArrayList<Relation> rels = new ArrayList<Relation>();
|
---|
1549 | rels.add(relation);
|
---|
1550 | Main.worker.submit(new DownloadRelationMemberTask(
|
---|
1551 | rels,
|
---|
1552 | buildSetOfIncompleteMembers(relation),
|
---|
1553 | Main.map.mapView.getEditLayer()
|
---|
1554 | ));
|
---|
1555 | }
|
---|
1556 | }
|
---|
1557 |
|
---|
1558 | abstract class AbstractCopyAction extends AbstractAction {
|
---|
1559 |
|
---|
1560 | protected abstract Collection<String> getString(OsmPrimitive p, String key);
|
---|
1561 |
|
---|
1562 | @Override
|
---|
1563 | public void actionPerformed(ActionEvent ae) {
|
---|
1564 | if (propertyTable.getSelectedRowCount() != 1)
|
---|
1565 | return;
|
---|
1566 | String key = propertyData.getValueAt(propertyTable.getSelectedRow(), 0).toString();
|
---|
1567 | Collection<OsmPrimitive> sel = Main.main.getCurrentDataSet().getSelected();
|
---|
1568 | if (sel.isEmpty())
|
---|
1569 | return;
|
---|
1570 | Set<String> values = new TreeSet<String>();
|
---|
1571 | for (OsmPrimitive p : sel) {
|
---|
1572 | Collection<String> s = getString(p,key);
|
---|
1573 | if (s != null) {
|
---|
1574 | values.addAll(s);
|
---|
1575 | }
|
---|
1576 | }
|
---|
1577 | Utils.copyToClipboard(Utils.join("\n", values));
|
---|
1578 | }
|
---|
1579 | }
|
---|
1580 |
|
---|
1581 | class CopyValueAction extends AbstractCopyAction {
|
---|
1582 |
|
---|
1583 | public CopyValueAction() {
|
---|
1584 | putValue(NAME, tr("Copy Value"));
|
---|
1585 | putValue(SHORT_DESCRIPTION, tr("Copy the value of the selected tag to clipboard"));
|
---|
1586 | }
|
---|
1587 |
|
---|
1588 | @Override
|
---|
1589 | protected Collection<String> getString(OsmPrimitive p, String key) {
|
---|
1590 | String v = p.get(key);
|
---|
1591 | return v == null ? null : Collections.singleton(v);
|
---|
1592 | }
|
---|
1593 | }
|
---|
1594 |
|
---|
1595 | class CopyKeyValueAction extends AbstractCopyAction {
|
---|
1596 |
|
---|
1597 | public CopyKeyValueAction() {
|
---|
1598 | putValue(NAME, tr("Copy Key/Value"));
|
---|
1599 | putValue(SHORT_DESCRIPTION, tr("Copy the key and value of the selected tag to clipboard"));
|
---|
1600 | }
|
---|
1601 |
|
---|
1602 | @Override
|
---|
1603 | protected Collection<String> getString(OsmPrimitive p, String key) {
|
---|
1604 | String v = p.get(key);
|
---|
1605 | return v == null ? null : Collections.singleton(new Tag(key, v).toString());
|
---|
1606 | }
|
---|
1607 | }
|
---|
1608 |
|
---|
1609 | class CopyAllKeyValueAction extends AbstractCopyAction {
|
---|
1610 |
|
---|
1611 | public CopyAllKeyValueAction() {
|
---|
1612 | putValue(NAME, tr("Copy all Keys/Values"));
|
---|
1613 | putValue(SHORT_DESCRIPTION, tr("Copy the key and value of the all tags to clipboard"));
|
---|
1614 | }
|
---|
1615 |
|
---|
1616 | @Override
|
---|
1617 | protected Collection<String> getString(OsmPrimitive p, String key) {
|
---|
1618 | List<String> r = new LinkedList<String>();
|
---|
1619 | for (Entry<String, String> kv : p.getKeys().entrySet()) {
|
---|
1620 | r.add(new Tag(kv.getKey(), kv.getValue()).toString());
|
---|
1621 | }
|
---|
1622 | return r;
|
---|
1623 | }
|
---|
1624 | }
|
---|
1625 |
|
---|
1626 | class SearchAction extends AbstractAction {
|
---|
1627 | final boolean sameType;
|
---|
1628 |
|
---|
1629 | public SearchAction(boolean sameType) {
|
---|
1630 | this.sameType = sameType;
|
---|
1631 | if (sameType) {
|
---|
1632 | putValue(NAME, tr("Search Key/Value/Type"));
|
---|
1633 | putValue(SHORT_DESCRIPTION, tr("Search with the key and value of the selected tag, restrict to type (i.e., node/way/relation)"));
|
---|
1634 | } else {
|
---|
1635 | putValue(NAME, tr("Search Key/Value"));
|
---|
1636 | putValue(SHORT_DESCRIPTION, tr("Search with the key and value of the selected tag"));
|
---|
1637 | }
|
---|
1638 | }
|
---|
1639 |
|
---|
1640 | public void actionPerformed(ActionEvent e) {
|
---|
1641 | if (propertyTable.getSelectedRowCount() != 1)
|
---|
1642 | return;
|
---|
1643 | String key = propertyData.getValueAt(propertyTable.getSelectedRow(), 0).toString();
|
---|
1644 | Collection<OsmPrimitive> sel = Main.main.getCurrentDataSet().getSelected();
|
---|
1645 | if (sel.isEmpty())
|
---|
1646 | return;
|
---|
1647 | String sep = "";
|
---|
1648 | String s = "";
|
---|
1649 | for (OsmPrimitive p : sel) {
|
---|
1650 | String val = p.get(key);
|
---|
1651 | if (val == null) {
|
---|
1652 | continue;
|
---|
1653 | }
|
---|
1654 | String t = "";
|
---|
1655 | if (!sameType) {
|
---|
1656 | t = "";
|
---|
1657 | } else if (p instanceof Node) {
|
---|
1658 | t = "type:node ";
|
---|
1659 | } else if (p instanceof Way) {
|
---|
1660 | t = "type:way ";
|
---|
1661 | } else if (p instanceof Relation) {
|
---|
1662 | t = "type:relation ";
|
---|
1663 | }
|
---|
1664 | s += sep + "(" + t + "\"" +
|
---|
1665 | org.openstreetmap.josm.actions.search.SearchAction.escapeStringForSearch(key) + "\"=\"" +
|
---|
1666 | org.openstreetmap.josm.actions.search.SearchAction.escapeStringForSearch(val) + "\")";
|
---|
1667 | sep = " OR ";
|
---|
1668 | }
|
---|
1669 |
|
---|
1670 | SearchSetting ss = new SearchSetting(s, SearchMode.replace, true, false, false);
|
---|
1671 | org.openstreetmap.josm.actions.search.SearchAction.searchWithoutHistory(ss);
|
---|
1672 | }
|
---|
1673 | }
|
---|
1674 |
|
---|
1675 | @Override
|
---|
1676 | public void destroy() {
|
---|
1677 | super.destroy();
|
---|
1678 | for (JosmAction action : josmActions) {
|
---|
1679 | action.destroy();
|
---|
1680 | }
|
---|
1681 | Container parent = pluginHook.getParent();
|
---|
1682 | if (parent != null) {
|
---|
1683 | parent.remove(pluginHook);
|
---|
1684 | }
|
---|
1685 | }
|
---|
1686 | }
|
---|