1 | // License: GPL. See LICENSE file for details.
|
---|
2 |
|
---|
3 | package org.openstreetmap.josm.gui.dialogs;
|
---|
4 |
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
6 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
7 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
8 |
|
---|
9 | import java.awt.BorderLayout;
|
---|
10 | import java.awt.Component;
|
---|
11 | import java.awt.Font;
|
---|
12 | import java.awt.GridBagLayout;
|
---|
13 | import java.awt.GridLayout;
|
---|
14 | import java.awt.event.ActionEvent;
|
---|
15 | import java.awt.event.ActionListener;
|
---|
16 | import java.awt.event.FocusAdapter;
|
---|
17 | import java.awt.event.FocusEvent;
|
---|
18 | import java.awt.event.KeyEvent;
|
---|
19 | import java.awt.event.MouseAdapter;
|
---|
20 | import java.awt.event.MouseEvent;
|
---|
21 | import java.util.Collection;
|
---|
22 | import java.util.Collections;
|
---|
23 | import java.util.HashMap;
|
---|
24 | import java.util.HashSet;
|
---|
25 | import java.util.Map;
|
---|
26 | import java.util.TreeMap;
|
---|
27 | import java.util.TreeSet;
|
---|
28 | import java.util.Vector;
|
---|
29 | import java.util.Map.Entry;
|
---|
30 |
|
---|
31 | import javax.swing.Box;
|
---|
32 | import javax.swing.DefaultComboBoxModel;
|
---|
33 | import javax.swing.DefaultListCellRenderer;
|
---|
34 | import javax.swing.JComboBox;
|
---|
35 | import javax.swing.JDialog;
|
---|
36 | import javax.swing.JLabel;
|
---|
37 | import javax.swing.JList;
|
---|
38 | import javax.swing.JOptionPane;
|
---|
39 | import javax.swing.JPanel;
|
---|
40 | import javax.swing.JScrollPane;
|
---|
41 | import javax.swing.JTable;
|
---|
42 | import javax.swing.ListSelectionModel;
|
---|
43 | import javax.swing.table.DefaultTableCellRenderer;
|
---|
44 | import javax.swing.table.DefaultTableModel;
|
---|
45 | import javax.swing.text.JTextComponent;
|
---|
46 |
|
---|
47 | import org.openstreetmap.josm.Main;
|
---|
48 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
49 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
50 | import org.openstreetmap.josm.command.Command;
|
---|
51 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
52 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
53 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
54 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
55 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
56 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
57 | import org.openstreetmap.josm.data.osm.visitor.NameVisitor;
|
---|
58 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
59 | import org.openstreetmap.josm.gui.SideButton;
|
---|
60 | import org.openstreetmap.josm.gui.preferences.TaggingPresetPreference;
|
---|
61 | import org.openstreetmap.josm.gui.tagging.ForwardActionListener;
|
---|
62 | import org.openstreetmap.josm.gui.tagging.TaggingCellRenderer;
|
---|
63 | import org.openstreetmap.josm.gui.tagging.TaggingPreset;
|
---|
64 | import org.openstreetmap.josm.tools.AutoCompleteComboBox;
|
---|
65 | import org.openstreetmap.josm.tools.GBC;
|
---|
66 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * This dialog displays the properties of the current selected primitives.
|
---|
70 | *
|
---|
71 | * If no object is selected, the dialog list is empty.
|
---|
72 | * If only one is selected, all properties of this object are selected.
|
---|
73 | * If more than one object are selected, the sum of all properties are displayed. If the
|
---|
74 | * different objects share the same property, the shared value is displayed. If they have
|
---|
75 | * different values, all of them are put in a combo box and the string "<different>"
|
---|
76 | * is displayed in italic.
|
---|
77 | *
|
---|
78 | * Below the list, the user can click on an add, modify and delete property button to
|
---|
79 | * edit the table selection value.
|
---|
80 | *
|
---|
81 | * The command is applied to all selected entries.
|
---|
82 | *
|
---|
83 | * @author imi
|
---|
84 | */
|
---|
85 | public class PropertiesDialog extends ToggleDialog implements SelectionChangedListener {
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Used to display relation names in the membership table
|
---|
89 | */
|
---|
90 | private NameVisitor nameVisitor = new NameVisitor();
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Watches for double clicks and from editing or new property, depending on the
|
---|
94 | * location, the click was.
|
---|
95 | * @author imi
|
---|
96 | */
|
---|
97 | public class DblClickWatch extends MouseAdapter {
|
---|
98 | @Override public void mouseClicked(MouseEvent e) {
|
---|
99 | if (e.getClickCount() < 2)
|
---|
100 | {
|
---|
101 | if (e.getSource() == propertyTable)
|
---|
102 | membershipTable.clearSelection();
|
---|
103 | else if (e.getSource() == membershipTable)
|
---|
104 | propertyTable.clearSelection();
|
---|
105 | }
|
---|
106 | else if (e.getSource() == propertyTable)
|
---|
107 | {
|
---|
108 | int row = propertyTable.rowAtPoint(e.getPoint());
|
---|
109 | if (row > -1)
|
---|
110 | propertyEdit(row);
|
---|
111 | } else if (e.getSource() == membershipTable) {
|
---|
112 | int row = membershipTable.rowAtPoint(e.getPoint());
|
---|
113 | if (row > -1)
|
---|
114 | membershipEdit(row);
|
---|
115 | }
|
---|
116 | else
|
---|
117 | {
|
---|
118 | add();
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | private final Map<String, Map<String, Integer>> valueCount = new TreeMap<String, Map<String, Integer>>();
|
---|
124 | /**
|
---|
125 | * Edit the value in the properties table row
|
---|
126 | * @param row The row of the table from which the value is edited.
|
---|
127 | */
|
---|
128 | void propertyEdit(int row) {
|
---|
129 | String key = propertyData.getValueAt(row, 0).toString();
|
---|
130 | objKey=key;
|
---|
131 | Collection<OsmPrimitive> sel = Main.ds.getSelected();
|
---|
132 | if (sel.isEmpty()) {
|
---|
133 | JOptionPane.showMessageDialog(Main.parent, tr("Please select the objects you want to change properties for."));
|
---|
134 | return;
|
---|
135 | }
|
---|
136 | String msg = "<html>"+trn("This will change up to {0} object.", "This will change up to {0} objects.", sel.size(), sel.size())+"<br><br>("+tr("An empty value deletes the key.", key)+")</html>";
|
---|
137 |
|
---|
138 | JPanel panel = new JPanel(new BorderLayout());
|
---|
139 | panel.add(new JLabel(msg), BorderLayout.NORTH);
|
---|
140 |
|
---|
141 | final TreeMap<String, TreeSet<String>> allData = createAutoCompletionInfo(true);
|
---|
142 |
|
---|
143 | JPanel p = new JPanel(new GridBagLayout());
|
---|
144 | panel.add(p, BorderLayout.CENTER);
|
---|
145 |
|
---|
146 | final AutoCompleteComboBox keys = new AutoCompleteComboBox();
|
---|
147 | keys.setPossibleItems(allData.keySet());
|
---|
148 | keys.setEditable(true);
|
---|
149 | keys.setSelectedItem(key);
|
---|
150 |
|
---|
151 | p.add(new JLabel(tr("Key")), GBC.std());
|
---|
152 | p.add(Box.createHorizontalStrut(10), GBC.std());
|
---|
153 | p.add(keys, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
154 |
|
---|
155 | final AutoCompleteComboBox values = new AutoCompleteComboBox();
|
---|
156 | values.setRenderer(new DefaultListCellRenderer() {
|
---|
157 | @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
|
---|
158 | Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
|
---|
159 | if (c instanceof JLabel) {
|
---|
160 | String str = null;
|
---|
161 | str=(String) value;
|
---|
162 | if (valueCount.containsKey(objKey)){
|
---|
163 | Map<String, Integer> m=valueCount.get(objKey);
|
---|
164 | if (m.containsKey(str)) {
|
---|
165 | str+="("+m.get(str)+")";
|
---|
166 | c.setFont(c.getFont().deriveFont(Font.ITALIC+Font.BOLD));
|
---|
167 | }
|
---|
168 | }
|
---|
169 | ((JLabel)c).setText(str);
|
---|
170 | }
|
---|
171 | return c;
|
---|
172 | }
|
---|
173 | });
|
---|
174 | values.setEditable(true);
|
---|
175 | updateListData(key, allData, values);
|
---|
176 | Map<String, Integer> m=(Map<String, Integer>)propertyData.getValueAt(row, 1);
|
---|
177 | final String selection= m.size()!=1?tr("<different>"):m.entrySet().iterator().next().getKey();
|
---|
178 | values.setSelectedItem(selection);
|
---|
179 | values.getEditor().setItem(selection);
|
---|
180 | p.add(new JLabel(tr("Value")), GBC.std());
|
---|
181 | p.add(Box.createHorizontalStrut(10), GBC.std());
|
---|
182 | p.add(values, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
183 | addFocusAdapter(row, allData, keys, values);
|
---|
184 |
|
---|
185 | final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION) {
|
---|
186 | @Override public void selectInitialValue() {
|
---|
187 | values.requestFocusInWindow();
|
---|
188 | values.getEditor().selectAll();
|
---|
189 | }
|
---|
190 | };
|
---|
191 | final JDialog dlg = optionPane.createDialog(Main.parent, tr("Change values?"));
|
---|
192 |
|
---|
193 | values.getEditor().addActionListener(new ActionListener() {
|
---|
194 | public void actionPerformed(ActionEvent e) {
|
---|
195 | dlg.setVisible(false);
|
---|
196 | optionPane.setValue(JOptionPane.OK_OPTION);
|
---|
197 | }
|
---|
198 | });
|
---|
199 |
|
---|
200 | String oldValue = values.getEditor().getItem().toString();
|
---|
201 | dlg.setVisible(true);
|
---|
202 |
|
---|
203 | Object answer = optionPane.getValue();
|
---|
204 | if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE ||
|
---|
205 | (answer instanceof Integer && (Integer)answer != JOptionPane.OK_OPTION)) {
|
---|
206 | values.getEditor().setItem(oldValue);
|
---|
207 | return;
|
---|
208 | }
|
---|
209 |
|
---|
210 | String value = values.getEditor().getItem().toString();
|
---|
211 | if (value.equals(""))
|
---|
212 | value = null; // delete the key
|
---|
213 | String newkey = keys.getEditor().getItem().toString();
|
---|
214 | if (newkey.equals("")) {
|
---|
215 | newkey = key;
|
---|
216 | value = null; // delete the key instead
|
---|
217 | }
|
---|
218 | if (newkey.equals("created_by"))
|
---|
219 | {
|
---|
220 | // we don't allow created_by to be changed.
|
---|
221 | return;
|
---|
222 | }
|
---|
223 | if (key.equals(newkey) || value == null)
|
---|
224 | Main.main.undoRedo.add(new ChangePropertyCommand(sel, newkey, value));
|
---|
225 | else {
|
---|
226 | Collection<Command> commands=new Vector<Command>();
|
---|
227 | commands.add(new ChangePropertyCommand(sel, key, null));
|
---|
228 | if (value.equals(tr("<different>"))) {
|
---|
229 | HashMap<String, Vector<OsmPrimitive>> map=new HashMap<String, Vector<OsmPrimitive>>();
|
---|
230 | for (OsmPrimitive osm: sel) {
|
---|
231 | if(osm.keys != null)
|
---|
232 | {
|
---|
233 | String val=osm.keys.get(key);
|
---|
234 | if(val != null)
|
---|
235 | {
|
---|
236 | if (map.containsKey(val)) {
|
---|
237 | map.get(val).add(osm);
|
---|
238 | } else {
|
---|
239 | Vector<OsmPrimitive> v = new Vector<OsmPrimitive>();
|
---|
240 | v.add(osm);
|
---|
241 | map.put(val, v);
|
---|
242 | }
|
---|
243 | }
|
---|
244 | }
|
---|
245 | }
|
---|
246 | for (Entry<String, Vector<OsmPrimitive>> e: map.entrySet()) {
|
---|
247 | commands.add(new ChangePropertyCommand(e.getValue(), newkey, e.getKey()));
|
---|
248 | }
|
---|
249 | } else {
|
---|
250 | commands.add(new ChangePropertyCommand(sel, newkey, value));
|
---|
251 | }
|
---|
252 | Main.main.undoRedo.add(new SequenceCommand(trn("Change properties of up to {0} object", "Change properties of up to {0} objects", sel.size(), sel.size()), commands));
|
---|
253 | }
|
---|
254 |
|
---|
255 | Main.ds.fireSelectionChanged(sel);
|
---|
256 | selectionChanged(sel); // update whole table
|
---|
257 | Main.parent.repaint(); // repaint all - drawing could have been changed
|
---|
258 | }
|
---|
259 |
|
---|
260 | /**
|
---|
261 | * @param key
|
---|
262 | * @param allData
|
---|
263 | * @param values
|
---|
264 | */
|
---|
265 | private void updateListData(String key, final TreeMap<String, TreeSet<String>> allData, final AutoCompleteComboBox values) {
|
---|
266 | Collection<String> newItems;
|
---|
267 | if (allData.containsKey(key)) {
|
---|
268 | newItems = allData.get(key);
|
---|
269 | } else {
|
---|
270 | newItems = Collections.emptyList();
|
---|
271 | }
|
---|
272 | values.setPossibleItems(newItems);
|
---|
273 | }
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * This simply fires up an relation editor for the relation shown; everything else
|
---|
277 | * is the editor's business.
|
---|
278 | *
|
---|
279 | * @param row
|
---|
280 | */
|
---|
281 | void membershipEdit(int row) {
|
---|
282 | final RelationEditor editor = new RelationEditor((Relation)membershipData.getValueAt(row, 0),
|
---|
283 | (Collection<RelationMember>) membershipData.getValueAt(row, 1) );
|
---|
284 | editor.setVisible(true);
|
---|
285 | }
|
---|
286 |
|
---|
287 | /**
|
---|
288 | * Open the add selection dialog and add a new key/value to the table (and
|
---|
289 | * to the dataset, of course).
|
---|
290 | */
|
---|
291 | void add() {
|
---|
292 | Collection<OsmPrimitive> sel = Main.ds.getSelected();
|
---|
293 | if (sel.isEmpty()) {
|
---|
294 | JOptionPane.showMessageDialog(Main.parent, tr("Please select objects for which you want to change properties."));
|
---|
295 | return;
|
---|
296 | }
|
---|
297 |
|
---|
298 | JPanel p = new JPanel(new BorderLayout());
|
---|
299 | p.add(new JLabel("<html>"+trn("This will change up to {0} object.","This will change up to {0} objects.", sel.size(),sel.size())+"<br><br>"+tr("Please select a key")),
|
---|
300 | BorderLayout.NORTH);
|
---|
301 | final TreeMap<String, TreeSet<String>> allData = createAutoCompletionInfo(false);
|
---|
302 | final AutoCompleteComboBox keys = new AutoCompleteComboBox();
|
---|
303 | keys.setPossibleItems(allData.keySet());
|
---|
304 | keys.setEditable(true);
|
---|
305 |
|
---|
306 | p.add(keys, BorderLayout.CENTER);
|
---|
307 |
|
---|
308 | JPanel p2 = new JPanel(new BorderLayout());
|
---|
309 | p.add(p2, BorderLayout.SOUTH);
|
---|
310 | p2.add(new JLabel(tr("Please select a value")), BorderLayout.NORTH);
|
---|
311 | final AutoCompleteComboBox values = new AutoCompleteComboBox();
|
---|
312 | values.setEditable(true);
|
---|
313 | p2.add(values, BorderLayout.CENTER);
|
---|
314 |
|
---|
315 | addFocusAdapter(-1, allData, keys, values);
|
---|
316 | JOptionPane pane = new JOptionPane(p, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION){
|
---|
317 | @Override public void selectInitialValue() {
|
---|
318 | keys.requestFocusInWindow();
|
---|
319 | keys.getEditor().selectAll();
|
---|
320 | }
|
---|
321 | };
|
---|
322 | pane.createDialog(Main.parent, tr("Change values?")).setVisible(true);
|
---|
323 | if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
|
---|
324 | return;
|
---|
325 | String key = keys.getEditor().getItem().toString();
|
---|
326 | String value = values.getEditor().getItem().toString();
|
---|
327 | if (value.equals(""))
|
---|
328 | return;
|
---|
329 | if (key.equals("created_by"))
|
---|
330 | return;
|
---|
331 | Main.main.undoRedo.add(new ChangePropertyCommand(sel, key, value));
|
---|
332 | Main.ds.fireSelectionChanged(sel);
|
---|
333 | selectionChanged(sel); // update table
|
---|
334 | Main.parent.repaint(); // repaint all - drawing could have been changed
|
---|
335 | }
|
---|
336 |
|
---|
337 | /**
|
---|
338 | * @param allData
|
---|
339 | * @param keys
|
---|
340 | * @param values
|
---|
341 | */
|
---|
342 | private void addFocusAdapter(final int row, final TreeMap<String, TreeSet<String>> allData,final AutoCompleteComboBox keys, final AutoCompleteComboBox values) {
|
---|
343 | // get the combo box' editor component
|
---|
344 | JTextComponent editor = (JTextComponent)values.getEditor()
|
---|
345 | .getEditorComponent();
|
---|
346 | // Refresh the values model when focus is gained
|
---|
347 | editor.addFocusListener(new FocusAdapter() {
|
---|
348 | @Override public void focusGained(FocusEvent e) {
|
---|
349 | String key = keys.getEditor().getItem().toString();
|
---|
350 | updateListData(key, allData, values);
|
---|
351 | objKey=key;
|
---|
352 | }
|
---|
353 | });
|
---|
354 | }
|
---|
355 | private String objKey;
|
---|
356 | /**
|
---|
357 | * @return
|
---|
358 | */
|
---|
359 | private TreeMap<String, TreeSet<String>> createAutoCompletionInfo(
|
---|
360 | boolean edit) {
|
---|
361 | final TreeMap<String, TreeSet<String>> allData = new TreeMap<String, TreeSet<String>>();
|
---|
362 | for (OsmPrimitive osm : Main.ds.allNonDeletedPrimitives()) {
|
---|
363 | for (String key : osm.keySet()) {
|
---|
364 | TreeSet<String> values = null;
|
---|
365 | if (allData.containsKey(key))
|
---|
366 | values = allData.get(key);
|
---|
367 | else {
|
---|
368 | values = new TreeSet<String>();
|
---|
369 | allData.put(key, values);
|
---|
370 | }
|
---|
371 | values.add(osm.get(key));
|
---|
372 | }
|
---|
373 | }
|
---|
374 | if (!edit) {
|
---|
375 | for (int i = 0; i < propertyData.getRowCount(); ++i)
|
---|
376 | allData.remove(propertyData.getValueAt(i, 0));
|
---|
377 | }
|
---|
378 | return allData;
|
---|
379 | }
|
---|
380 |
|
---|
381 | /**
|
---|
382 | * Delete the keys from the given row.
|
---|
383 | * @param row The row, which key gets deleted from the dataset.
|
---|
384 | */
|
---|
385 | private void delete(int row) {
|
---|
386 | String key = propertyData.getValueAt(row, 0).toString();
|
---|
387 | Collection<OsmPrimitive> sel = Main.ds.getSelected();
|
---|
388 | Main.main.undoRedo.add(new ChangePropertyCommand(sel, key, null));
|
---|
389 | Main.ds.fireSelectionChanged(sel);
|
---|
390 | selectionChanged(sel); // update table
|
---|
391 | }
|
---|
392 |
|
---|
393 | /**
|
---|
394 | * The property data.
|
---|
395 | */
|
---|
396 | private final DefaultTableModel propertyData = new DefaultTableModel() {
|
---|
397 | @Override public boolean isCellEditable(int row, int column) {
|
---|
398 | return false;
|
---|
399 | }
|
---|
400 | @Override public Class<?> getColumnClass(int columnIndex) {
|
---|
401 | return String.class;
|
---|
402 | }
|
---|
403 | };
|
---|
404 |
|
---|
405 | /**
|
---|
406 | * The membership data.
|
---|
407 | */
|
---|
408 | private final DefaultTableModel membershipData = new DefaultTableModel() {
|
---|
409 | @Override public boolean isCellEditable(int row, int column) {
|
---|
410 | return false;
|
---|
411 | }
|
---|
412 | @Override public Class<?> getColumnClass(int columnIndex) {
|
---|
413 | return String.class;
|
---|
414 | }
|
---|
415 | };
|
---|
416 |
|
---|
417 | /**
|
---|
418 | * The properties list.
|
---|
419 | */
|
---|
420 | private final JTable propertyTable = new JTable(propertyData);
|
---|
421 | private final JTable membershipTable = new JTable(membershipData);
|
---|
422 |
|
---|
423 | public JComboBox taggingPresets = new JComboBox();
|
---|
424 |
|
---|
425 |
|
---|
426 | /**
|
---|
427 | * Create a new PropertiesDialog
|
---|
428 | */
|
---|
429 | public PropertiesDialog(MapFrame mapFrame) {
|
---|
430 | super(tr("Properties/Memberships"), "propertiesdialog", tr("Properties for selected objects."),
|
---|
431 | Shortcut.registerShortcut("subwindow:properties", tr("Toggle: {0}", tr("Properties/Memberships")), KeyEvent.VK_P,
|
---|
432 | Shortcut.GROUP_LAYER, Shortcut.SHIFT_DEFAULT), 150);
|
---|
433 |
|
---|
434 | // ---------------------------------------
|
---|
435 | // This drop-down is really deprecated but we offer people a chance to
|
---|
436 | // activate it if they really want. Presets should be used from the
|
---|
437 | // menu.
|
---|
438 | if (TaggingPresetPreference.taggingPresets.size() > 0 &&
|
---|
439 | Main.pref.getBoolean("taggingpreset.in-properties-dialog", false)) {
|
---|
440 | Vector<ActionListener> allPresets = new Vector<ActionListener>();
|
---|
441 | for (final TaggingPreset p : TaggingPresetPreference.taggingPresets)
|
---|
442 | allPresets.add(new ForwardActionListener(this, p));
|
---|
443 |
|
---|
444 | TaggingPreset empty = new TaggingPreset();
|
---|
445 | // empty.setName("this drop-down will be removed soon");
|
---|
446 | allPresets.add(0, new ForwardActionListener(this, empty));
|
---|
447 | taggingPresets.setModel(new DefaultComboBoxModel(allPresets));
|
---|
448 | JPanel north = new JPanel(new GridBagLayout());
|
---|
449 | north.add(getComponent(0),GBC.eol().fill(GBC.HORIZONTAL));
|
---|
450 | north.add(taggingPresets,GBC.eol().fill(GBC.HORIZONTAL));
|
---|
451 | add(north, BorderLayout.NORTH);
|
---|
452 | }
|
---|
453 | taggingPresets.addActionListener(new ActionListener(){
|
---|
454 | public void actionPerformed(ActionEvent e) {
|
---|
455 | TaggingPreset preset = ((ForwardActionListener)taggingPresets.getSelectedItem()).preset;
|
---|
456 | preset.actionPerformed(e);
|
---|
457 | taggingPresets.setSelectedItem(null);
|
---|
458 | }
|
---|
459 | });
|
---|
460 | taggingPresets.setRenderer(new TaggingCellRenderer());
|
---|
461 |
|
---|
462 | // setting up the properties table
|
---|
463 |
|
---|
464 | propertyData.setColumnIdentifiers(new String[]{tr("Key"),tr("Value")});
|
---|
465 | propertyTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
466 |
|
---|
467 | propertyTable.getColumnModel().getColumn(1).setCellRenderer(new DefaultTableCellRenderer(){
|
---|
468 | @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
469 | Component c = super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
|
---|
470 | if (c instanceof JLabel) {
|
---|
471 | String str = null;
|
---|
472 | switch (column) {
|
---|
473 | case 0:
|
---|
474 | str = (String) value;
|
---|
475 | break;
|
---|
476 | case 1:
|
---|
477 | Map<String, Integer> v = (Map<String,Integer>) value;
|
---|
478 | if (v.size()!=1) {
|
---|
479 | str=tr("<different>");
|
---|
480 | c.setFont(c.getFont().deriveFont(Font.ITALIC));
|
---|
481 | } else {
|
---|
482 | str=v.entrySet().iterator().next().getKey();
|
---|
483 | }
|
---|
484 | break;
|
---|
485 | }
|
---|
486 | ((JLabel)c).setText(str);
|
---|
487 | }
|
---|
488 | return c;
|
---|
489 | }
|
---|
490 | });
|
---|
491 |
|
---|
492 | // setting up the membership table
|
---|
493 |
|
---|
494 | membershipData.setColumnIdentifiers(new String[]{tr("Member Of"),tr("Role")});
|
---|
495 | membershipTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
496 |
|
---|
497 | membershipTable.getColumnModel().getColumn(0).setCellRenderer(new DefaultTableCellRenderer() {
|
---|
498 | @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
499 | Component c = super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
|
---|
500 | if (c instanceof JLabel) {
|
---|
501 | nameVisitor.visit((Relation)value);
|
---|
502 | ((JLabel)c).setText(nameVisitor.name);
|
---|
503 | }
|
---|
504 | return c;
|
---|
505 | }
|
---|
506 | });
|
---|
507 |
|
---|
508 | membershipTable.getColumnModel().getColumn(1).setCellRenderer(new DefaultTableCellRenderer() {
|
---|
509 | @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
510 | Component c = super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
|
---|
511 | if (c instanceof JLabel) {
|
---|
512 | Collection<RelationMember> col = (Collection<RelationMember>) value;
|
---|
513 |
|
---|
514 | String text = null;
|
---|
515 | for (RelationMember r : col) {
|
---|
516 | if (text == null) {
|
---|
517 | text = r.role;
|
---|
518 | }
|
---|
519 | else if (!text.equals(r.role)) {
|
---|
520 | text = tr("<different>");
|
---|
521 | break;
|
---|
522 | }
|
---|
523 | }
|
---|
524 |
|
---|
525 | ((JLabel)c).setText(text);
|
---|
526 | }
|
---|
527 | return c;
|
---|
528 | }
|
---|
529 | });
|
---|
530 |
|
---|
531 | // combine both tables and wrap them in a scrollPane
|
---|
532 | JPanel bothTables = new JPanel();
|
---|
533 | bothTables.setLayout(new GridBagLayout());
|
---|
534 | bothTables.add(propertyTable.getTableHeader(), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
535 | bothTables.add(propertyTable, GBC.eol().fill(GBC.BOTH));
|
---|
536 | bothTables.add(membershipTable.getTableHeader(), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
537 | bothTables.add(membershipTable, GBC.eol().fill(GBC.BOTH));
|
---|
538 |
|
---|
539 | DblClickWatch dblClickWatch = new DblClickWatch();
|
---|
540 | propertyTable.addMouseListener(dblClickWatch);
|
---|
541 | membershipTable.addMouseListener(dblClickWatch);
|
---|
542 | JScrollPane scrollPane = new JScrollPane(bothTables);
|
---|
543 | scrollPane.addMouseListener(dblClickWatch);
|
---|
544 | add(scrollPane, BorderLayout.CENTER);
|
---|
545 |
|
---|
546 | JPanel buttonPanel = new JPanel(new GridLayout(1,3));
|
---|
547 | ActionListener buttonAction = new ActionListener(){
|
---|
548 | public void actionPerformed(ActionEvent e) {
|
---|
549 | int row = membershipTable.getSelectedRow();
|
---|
550 | if (e.getActionCommand().equals("Add"))
|
---|
551 | add();
|
---|
552 | else if(row >= 0)
|
---|
553 | {
|
---|
554 | if (e.getActionCommand().equals("Edit"))
|
---|
555 | membershipEdit(row);
|
---|
556 | else if (e.getActionCommand().equals("Delete")) {
|
---|
557 | Relation cur = (Relation)membershipData.getValueAt(row, 0);
|
---|
558 | NameVisitor n = new NameVisitor();
|
---|
559 | cur.visit(n);
|
---|
560 | if(JOptionPane.showConfirmDialog(Main.parent, tr("Really delete selection from relation {0}?", n.name),
|
---|
561 | tr("Change relation"), JOptionPane.YES_NO_OPTION) == JOptionPane.YES_NO_OPTION)
|
---|
562 | {
|
---|
563 | Relation rel = new Relation(cur);
|
---|
564 | Collection<OsmPrimitive> sel = Main.ds.getSelected();
|
---|
565 | for (RelationMember rm : cur.members) {
|
---|
566 | for (OsmPrimitive osm : sel) {
|
---|
567 | if (rm.member == osm)
|
---|
568 | {
|
---|
569 | RelationMember mem = new RelationMember();
|
---|
570 | mem.role = rm.role;
|
---|
571 | mem.member = rm.member;
|
---|
572 | rel.members.remove(mem);
|
---|
573 | break;
|
---|
574 | }
|
---|
575 | }
|
---|
576 | }
|
---|
577 | Main.main.undoRedo.add(new ChangeCommand(cur, rel));
|
---|
578 | Main.ds.fireSelectionChanged(sel);
|
---|
579 | selectionChanged(sel); // update whole table
|
---|
580 | }
|
---|
581 |
|
---|
582 | }
|
---|
583 | }
|
---|
584 | else
|
---|
585 | {
|
---|
586 | int sel = propertyTable.getSelectedRow();
|
---|
587 | if (e.getActionCommand().equals("Edit")) {
|
---|
588 | if(propertyTable.getRowCount() == 1)
|
---|
589 | sel = 0;
|
---|
590 | if (sel == -1)
|
---|
591 | JOptionPane.showMessageDialog(Main.parent, tr("Please select the row to edit."));
|
---|
592 | else
|
---|
593 | propertyEdit(sel);
|
---|
594 | } else if (e.getActionCommand().equals("Delete")) {
|
---|
595 | if (sel == -1)
|
---|
596 | JOptionPane.showMessageDialog(Main.parent, tr("Please select the row to delete."));
|
---|
597 | else
|
---|
598 | delete(sel);
|
---|
599 | }
|
---|
600 | }
|
---|
601 | }
|
---|
602 | };
|
---|
603 |
|
---|
604 | Shortcut s = Shortcut.registerShortcut("properties:add", tr("Add Properties"), KeyEvent.VK_B,
|
---|
605 | Shortcut.GROUP_MNEMONIC);
|
---|
606 | buttonPanel.add(new SideButton(marktr("Add"),"add","Properties",
|
---|
607 | tr("Add a new key/value pair to all objects"), s, buttonAction));
|
---|
608 |
|
---|
609 | s = Shortcut.registerShortcut("properties:edit", tr("Edit Properties"), KeyEvent.VK_I,
|
---|
610 | Shortcut.GROUP_MNEMONIC);
|
---|
611 | buttonPanel.add(new SideButton(marktr("Edit"),"edit","Properties",
|
---|
612 | tr("Edit the value of the selected key for all objects"), s, buttonAction));
|
---|
613 |
|
---|
614 | s = Shortcut.registerShortcut("properties:delete", tr("Delete Properties"), KeyEvent.VK_Q,
|
---|
615 | Shortcut.GROUP_MNEMONIC);
|
---|
616 | buttonPanel.add(new SideButton(marktr("Delete"),"delete","Properties",
|
---|
617 | tr("Delete the selected key in all objects"), s, buttonAction));
|
---|
618 | add(buttonPanel, BorderLayout.SOUTH);
|
---|
619 |
|
---|
620 | DataSet.selListeners.add(this);
|
---|
621 | }
|
---|
622 |
|
---|
623 | @Override public void setVisible(boolean b) {
|
---|
624 | super.setVisible(b);
|
---|
625 | if (b)
|
---|
626 | selectionChanged(Main.ds.getSelected());
|
---|
627 | }
|
---|
628 |
|
---|
629 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
---|
630 | if (!isVisible())
|
---|
631 | return;
|
---|
632 | if (propertyTable == null)
|
---|
633 | return; // selection changed may be received in base class constructor before init
|
---|
634 | if (propertyTable.getCellEditor() != null)
|
---|
635 | propertyTable.getCellEditor().cancelCellEditing();
|
---|
636 |
|
---|
637 | // re-load property data
|
---|
638 |
|
---|
639 | propertyData.setRowCount(0);
|
---|
640 |
|
---|
641 | Map<String, Integer> keyCount = new HashMap<String, Integer>();
|
---|
642 | valueCount.clear();
|
---|
643 | for (OsmPrimitive osm : newSelection) {
|
---|
644 | for (Entry<String, String> e : osm.entrySet()) {
|
---|
645 | keyCount.put(e.getKey(), keyCount.containsKey(e.getKey()) ? keyCount.get(e.getKey())+1 : 1);
|
---|
646 | if (valueCount.containsKey(e.getKey())) {
|
---|
647 | Map<String, Integer> v = valueCount.get(e.getKey());
|
---|
648 | v.put(e.getValue(), v.containsKey(e.getValue())? v.get(e.getValue())+1 : 1 );
|
---|
649 | } else {
|
---|
650 | TreeMap<String,Integer> v = new TreeMap<String, Integer>();
|
---|
651 | v.put(e.getValue(), 1);
|
---|
652 | valueCount.put(e.getKey(), v);
|
---|
653 | }
|
---|
654 | }
|
---|
655 | }
|
---|
656 | for (Entry<String, Map<String, Integer>> e : valueCount.entrySet()) {
|
---|
657 | int count=0;
|
---|
658 | for (Entry<String, Integer> e1: e.getValue().entrySet()) {
|
---|
659 | count+=e1.getValue();
|
---|
660 | }
|
---|
661 | if (count < newSelection.size()) {
|
---|
662 | e.getValue().put("", newSelection.size()-count);
|
---|
663 | }
|
---|
664 | propertyData.addRow(new Object[]{e.getKey(), e.getValue()});
|
---|
665 | }
|
---|
666 |
|
---|
667 | // re-load membership data
|
---|
668 | // this is rather expensive since we have to walk through all members of all existing relationships.
|
---|
669 | // could use back references here for speed if necessary.
|
---|
670 |
|
---|
671 | membershipData.setRowCount(0);
|
---|
672 |
|
---|
673 | Map<Relation, Collection<RelationMember>> roles = new HashMap<Relation, Collection<RelationMember>>();
|
---|
674 | for (Relation r : Main.ds.relations) {
|
---|
675 | if (!r.deleted && !r.incomplete) {
|
---|
676 | for (RelationMember m : r.members) {
|
---|
677 | if (newSelection.contains(m.member)) {
|
---|
678 | Collection<RelationMember> value = roles.get(r);
|
---|
679 | if (value == null) {
|
---|
680 | value = new HashSet<RelationMember>();
|
---|
681 | roles.put(r, value);
|
---|
682 | }
|
---|
683 | value.add(m);
|
---|
684 | }
|
---|
685 | }
|
---|
686 | }
|
---|
687 | }
|
---|
688 |
|
---|
689 | for (Entry<Relation, Collection<RelationMember>> e : roles.entrySet()) {
|
---|
690 | membershipData.addRow(new Object[]{e.getKey(), e.getValue()});
|
---|
691 | }
|
---|
692 |
|
---|
693 | membershipTable.getTableHeader().setVisible(membershipData.getRowCount() > 0);
|
---|
694 | }
|
---|
695 | }
|
---|