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 | value = java.text.Normalizer.normalize(value, java.text.Normalizer.Form.NFC);
|
---|
212 | if (value.equals(""))
|
---|
213 | value = null; // delete the key
|
---|
214 | String newkey = keys.getEditor().getItem().toString();
|
---|
215 | newkey = java.text.Normalizer.normalize(newkey, java.text.Normalizer.Form.NFC);
|
---|
216 | if (newkey.equals("")) {
|
---|
217 | newkey = key;
|
---|
218 | value = null; // delete the key instead
|
---|
219 | }
|
---|
220 | if (newkey.equals("created_by"))
|
---|
221 | {
|
---|
222 | // we don't allow created_by to be changed.
|
---|
223 | return;
|
---|
224 | }
|
---|
225 | if (key.equals(newkey) || value == null)
|
---|
226 | Main.main.undoRedo.add(new ChangePropertyCommand(sel, newkey, value));
|
---|
227 | else {
|
---|
228 | Collection<Command> commands=new Vector<Command>();
|
---|
229 | commands.add(new ChangePropertyCommand(sel, key, null));
|
---|
230 | if (value.equals(tr("<different>"))) {
|
---|
231 | HashMap<String, Vector<OsmPrimitive>> map=new HashMap<String, Vector<OsmPrimitive>>();
|
---|
232 | for (OsmPrimitive osm: sel) {
|
---|
233 | if(osm.keys != null)
|
---|
234 | {
|
---|
235 | String val=osm.keys.get(key);
|
---|
236 | if(val != null)
|
---|
237 | {
|
---|
238 | if (map.containsKey(val)) {
|
---|
239 | map.get(val).add(osm);
|
---|
240 | } else {
|
---|
241 | Vector<OsmPrimitive> v = new Vector<OsmPrimitive>();
|
---|
242 | v.add(osm);
|
---|
243 | map.put(val, v);
|
---|
244 | }
|
---|
245 | }
|
---|
246 | }
|
---|
247 | }
|
---|
248 | for (Entry<String, Vector<OsmPrimitive>> e: map.entrySet()) {
|
---|
249 | commands.add(new ChangePropertyCommand(e.getValue(), newkey, e.getKey()));
|
---|
250 | }
|
---|
251 | } else {
|
---|
252 | commands.add(new ChangePropertyCommand(sel, newkey, value));
|
---|
253 | }
|
---|
254 | 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));
|
---|
255 | }
|
---|
256 |
|
---|
257 | Main.ds.fireSelectionChanged(sel);
|
---|
258 | selectionChanged(sel); // update whole table
|
---|
259 | Main.parent.repaint(); // repaint all - drawing could have been changed
|
---|
260 | }
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * @param key
|
---|
264 | * @param allData
|
---|
265 | * @param values
|
---|
266 | */
|
---|
267 | private void updateListData(String key, final TreeMap<String, TreeSet<String>> allData, final AutoCompleteComboBox values) {
|
---|
268 | Collection<String> newItems;
|
---|
269 | if (allData.containsKey(key)) {
|
---|
270 | newItems = allData.get(key);
|
---|
271 | } else {
|
---|
272 | newItems = Collections.emptyList();
|
---|
273 | }
|
---|
274 | values.setPossibleItems(newItems);
|
---|
275 | }
|
---|
276 |
|
---|
277 | /**
|
---|
278 | * This simply fires up an relation editor for the relation shown; everything else
|
---|
279 | * is the editor's business.
|
---|
280 | *
|
---|
281 | * @param row
|
---|
282 | */
|
---|
283 | void membershipEdit(int row) {
|
---|
284 | final RelationEditor editor = new RelationEditor((Relation)membershipData.getValueAt(row, 0),
|
---|
285 | (Collection<RelationMember>) membershipData.getValueAt(row, 1) );
|
---|
286 | editor.setVisible(true);
|
---|
287 | }
|
---|
288 |
|
---|
289 | /**
|
---|
290 | * Open the add selection dialog and add a new key/value to the table (and
|
---|
291 | * to the dataset, of course).
|
---|
292 | */
|
---|
293 | void add() {
|
---|
294 | Collection<OsmPrimitive> sel = Main.ds.getSelected();
|
---|
295 | if (sel.isEmpty()) {
|
---|
296 | JOptionPane.showMessageDialog(Main.parent, tr("Please select objects for which you want to change properties."));
|
---|
297 | return;
|
---|
298 | }
|
---|
299 |
|
---|
300 | JPanel p = new JPanel(new BorderLayout());
|
---|
301 | 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")),
|
---|
302 | BorderLayout.NORTH);
|
---|
303 | final TreeMap<String, TreeSet<String>> allData = createAutoCompletionInfo(false);
|
---|
304 | final AutoCompleteComboBox keys = new AutoCompleteComboBox();
|
---|
305 | keys.setPossibleItems(allData.keySet());
|
---|
306 | keys.setEditable(true);
|
---|
307 |
|
---|
308 | p.add(keys, BorderLayout.CENTER);
|
---|
309 |
|
---|
310 | JPanel p2 = new JPanel(new BorderLayout());
|
---|
311 | p.add(p2, BorderLayout.SOUTH);
|
---|
312 | p2.add(new JLabel(tr("Please select a value")), BorderLayout.NORTH);
|
---|
313 | final AutoCompleteComboBox values = new AutoCompleteComboBox();
|
---|
314 | values.setEditable(true);
|
---|
315 | p2.add(values, BorderLayout.CENTER);
|
---|
316 |
|
---|
317 | addFocusAdapter(-1, allData, keys, values);
|
---|
318 | JOptionPane pane = new JOptionPane(p, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION){
|
---|
319 | @Override public void selectInitialValue() {
|
---|
320 | keys.requestFocusInWindow();
|
---|
321 | keys.getEditor().selectAll();
|
---|
322 | }
|
---|
323 | };
|
---|
324 | pane.createDialog(Main.parent, tr("Change values?")).setVisible(true);
|
---|
325 | if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
|
---|
326 | return;
|
---|
327 | String key = keys.getEditor().getItem().toString();
|
---|
328 | String value = values.getEditor().getItem().toString();
|
---|
329 | if (value.equals(""))
|
---|
330 | return;
|
---|
331 | if (key.equals("created_by"))
|
---|
332 | return;
|
---|
333 | Main.main.undoRedo.add(new ChangePropertyCommand(sel, key, value));
|
---|
334 | Main.ds.fireSelectionChanged(sel);
|
---|
335 | selectionChanged(sel); // update table
|
---|
336 | Main.parent.repaint(); // repaint all - drawing could have been changed
|
---|
337 | }
|
---|
338 |
|
---|
339 | /**
|
---|
340 | * @param allData
|
---|
341 | * @param keys
|
---|
342 | * @param values
|
---|
343 | */
|
---|
344 | private void addFocusAdapter(final int row, final TreeMap<String, TreeSet<String>> allData,final AutoCompleteComboBox keys, final AutoCompleteComboBox values) {
|
---|
345 | // get the combo box' editor component
|
---|
346 | JTextComponent editor = (JTextComponent)values.getEditor()
|
---|
347 | .getEditorComponent();
|
---|
348 | // Refresh the values model when focus is gained
|
---|
349 | editor.addFocusListener(new FocusAdapter() {
|
---|
350 | @Override public void focusGained(FocusEvent e) {
|
---|
351 | String key = keys.getEditor().getItem().toString();
|
---|
352 | updateListData(key, allData, values);
|
---|
353 | objKey=key;
|
---|
354 | }
|
---|
355 | });
|
---|
356 | }
|
---|
357 | private String objKey;
|
---|
358 | /**
|
---|
359 | * @return
|
---|
360 | */
|
---|
361 | private TreeMap<String, TreeSet<String>> createAutoCompletionInfo(
|
---|
362 | boolean edit) {
|
---|
363 | final TreeMap<String, TreeSet<String>> allData = new TreeMap<String, TreeSet<String>>();
|
---|
364 | for (OsmPrimitive osm : Main.ds.allNonDeletedPrimitives()) {
|
---|
365 | for (String key : osm.keySet()) {
|
---|
366 | TreeSet<String> values = null;
|
---|
367 | if (allData.containsKey(key))
|
---|
368 | values = allData.get(key);
|
---|
369 | else {
|
---|
370 | values = new TreeSet<String>();
|
---|
371 | allData.put(key, values);
|
---|
372 | }
|
---|
373 | values.add(osm.get(key));
|
---|
374 | }
|
---|
375 | }
|
---|
376 | if (!edit) {
|
---|
377 | for (int i = 0; i < propertyData.getRowCount(); ++i)
|
---|
378 | allData.remove(propertyData.getValueAt(i, 0));
|
---|
379 | }
|
---|
380 | return allData;
|
---|
381 | }
|
---|
382 |
|
---|
383 | /**
|
---|
384 | * Delete the keys from the given row.
|
---|
385 | * @param row The row, which key gets deleted from the dataset.
|
---|
386 | */
|
---|
387 | private void delete(int row) {
|
---|
388 | String key = propertyData.getValueAt(row, 0).toString();
|
---|
389 | Collection<OsmPrimitive> sel = Main.ds.getSelected();
|
---|
390 | Main.main.undoRedo.add(new ChangePropertyCommand(sel, key, null));
|
---|
391 | Main.ds.fireSelectionChanged(sel);
|
---|
392 | selectionChanged(sel); // update table
|
---|
393 | }
|
---|
394 |
|
---|
395 | /**
|
---|
396 | * The property data.
|
---|
397 | */
|
---|
398 | private final DefaultTableModel propertyData = new DefaultTableModel() {
|
---|
399 | @Override public boolean isCellEditable(int row, int column) {
|
---|
400 | return false;
|
---|
401 | }
|
---|
402 | @Override public Class<?> getColumnClass(int columnIndex) {
|
---|
403 | return String.class;
|
---|
404 | }
|
---|
405 | };
|
---|
406 |
|
---|
407 | /**
|
---|
408 | * The membership data.
|
---|
409 | */
|
---|
410 | private final DefaultTableModel membershipData = new DefaultTableModel() {
|
---|
411 | @Override public boolean isCellEditable(int row, int column) {
|
---|
412 | return false;
|
---|
413 | }
|
---|
414 | @Override public Class<?> getColumnClass(int columnIndex) {
|
---|
415 | return String.class;
|
---|
416 | }
|
---|
417 | };
|
---|
418 |
|
---|
419 | /**
|
---|
420 | * The properties list.
|
---|
421 | */
|
---|
422 | private final JTable propertyTable = new JTable(propertyData);
|
---|
423 | private final JTable membershipTable = new JTable(membershipData);
|
---|
424 |
|
---|
425 | public JComboBox taggingPresets = new JComboBox();
|
---|
426 |
|
---|
427 |
|
---|
428 | /**
|
---|
429 | * Create a new PropertiesDialog
|
---|
430 | */
|
---|
431 | public PropertiesDialog(MapFrame mapFrame) {
|
---|
432 | super(tr("Properties/Memberships"), "propertiesdialog", tr("Properties for selected objects."),
|
---|
433 | Shortcut.registerShortcut("subwindow:properties", tr("Toggle: {0}", tr("Properties/Memberships")), KeyEvent.VK_P,
|
---|
434 | Shortcut.GROUP_LAYER, Shortcut.SHIFT_DEFAULT), 150);
|
---|
435 |
|
---|
436 | // ---------------------------------------
|
---|
437 | // This drop-down is really deprecated but we offer people a chance to
|
---|
438 | // activate it if they really want. Presets should be used from the
|
---|
439 | // menu.
|
---|
440 | if (TaggingPresetPreference.taggingPresets.size() > 0 &&
|
---|
441 | Main.pref.getBoolean("taggingpreset.in-properties-dialog", false)) {
|
---|
442 | Vector<ActionListener> allPresets = new Vector<ActionListener>();
|
---|
443 | for (final TaggingPreset p : TaggingPresetPreference.taggingPresets)
|
---|
444 | allPresets.add(new ForwardActionListener(this, p));
|
---|
445 |
|
---|
446 | TaggingPreset empty = new TaggingPreset();
|
---|
447 | // empty.setName("this drop-down will be removed soon");
|
---|
448 | allPresets.add(0, new ForwardActionListener(this, empty));
|
---|
449 | taggingPresets.setModel(new DefaultComboBoxModel(allPresets));
|
---|
450 | JPanel north = new JPanel(new GridBagLayout());
|
---|
451 | north.add(getComponent(0),GBC.eol().fill(GBC.HORIZONTAL));
|
---|
452 | north.add(taggingPresets,GBC.eol().fill(GBC.HORIZONTAL));
|
---|
453 | add(north, BorderLayout.NORTH);
|
---|
454 | }
|
---|
455 | taggingPresets.addActionListener(new ActionListener(){
|
---|
456 | public void actionPerformed(ActionEvent e) {
|
---|
457 | TaggingPreset preset = ((ForwardActionListener)taggingPresets.getSelectedItem()).preset;
|
---|
458 | preset.actionPerformed(e);
|
---|
459 | taggingPresets.setSelectedItem(null);
|
---|
460 | }
|
---|
461 | });
|
---|
462 | taggingPresets.setRenderer(new TaggingCellRenderer());
|
---|
463 |
|
---|
464 | // setting up the properties table
|
---|
465 |
|
---|
466 | propertyData.setColumnIdentifiers(new String[]{tr("Key"),tr("Value")});
|
---|
467 | propertyTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
468 |
|
---|
469 | propertyTable.getColumnModel().getColumn(1).setCellRenderer(new DefaultTableCellRenderer(){
|
---|
470 | @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
471 | Component c = super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
|
---|
472 | if (c instanceof JLabel) {
|
---|
473 | String str = null;
|
---|
474 | switch (column) {
|
---|
475 | case 0:
|
---|
476 | str = (String) value;
|
---|
477 | break;
|
---|
478 | case 1:
|
---|
479 | Map<String, Integer> v = (Map<String,Integer>) value;
|
---|
480 | if (v.size()!=1) {
|
---|
481 | str=tr("<different>");
|
---|
482 | c.setFont(c.getFont().deriveFont(Font.ITALIC));
|
---|
483 | } else {
|
---|
484 | str=v.entrySet().iterator().next().getKey();
|
---|
485 | }
|
---|
486 | break;
|
---|
487 | }
|
---|
488 | ((JLabel)c).setText(str);
|
---|
489 | }
|
---|
490 | return c;
|
---|
491 | }
|
---|
492 | });
|
---|
493 |
|
---|
494 | // setting up the membership table
|
---|
495 |
|
---|
496 | membershipData.setColumnIdentifiers(new String[]{tr("Member Of"),tr("Role")});
|
---|
497 | membershipTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
498 |
|
---|
499 | membershipTable.getColumnModel().getColumn(0).setCellRenderer(new DefaultTableCellRenderer() {
|
---|
500 | @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
501 | Component c = super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
|
---|
502 | if (c instanceof JLabel) {
|
---|
503 | nameVisitor.visit((Relation)value);
|
---|
504 | ((JLabel)c).setText(nameVisitor.name);
|
---|
505 | }
|
---|
506 | return c;
|
---|
507 | }
|
---|
508 | });
|
---|
509 |
|
---|
510 | membershipTable.getColumnModel().getColumn(1).setCellRenderer(new DefaultTableCellRenderer() {
|
---|
511 | @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
512 | Component c = super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
|
---|
513 | if (c instanceof JLabel) {
|
---|
514 | Collection<RelationMember> col = (Collection<RelationMember>) value;
|
---|
515 |
|
---|
516 | String text = null;
|
---|
517 | for (RelationMember r : col) {
|
---|
518 | if (text == null) {
|
---|
519 | text = r.role;
|
---|
520 | }
|
---|
521 | else if (!text.equals(r.role)) {
|
---|
522 | text = tr("<different>");
|
---|
523 | break;
|
---|
524 | }
|
---|
525 | }
|
---|
526 |
|
---|
527 | ((JLabel)c).setText(text);
|
---|
528 | }
|
---|
529 | return c;
|
---|
530 | }
|
---|
531 | });
|
---|
532 |
|
---|
533 | // combine both tables and wrap them in a scrollPane
|
---|
534 | JPanel bothTables = new JPanel();
|
---|
535 | bothTables.setLayout(new GridBagLayout());
|
---|
536 | bothTables.add(propertyTable.getTableHeader(), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
537 | bothTables.add(propertyTable, GBC.eol().fill(GBC.BOTH));
|
---|
538 | bothTables.add(membershipTable.getTableHeader(), GBC.eol().fill(GBC.HORIZONTAL));
|
---|
539 | bothTables.add(membershipTable, GBC.eol().fill(GBC.BOTH));
|
---|
540 |
|
---|
541 | DblClickWatch dblClickWatch = new DblClickWatch();
|
---|
542 | propertyTable.addMouseListener(dblClickWatch);
|
---|
543 | membershipTable.addMouseListener(dblClickWatch);
|
---|
544 | JScrollPane scrollPane = new JScrollPane(bothTables);
|
---|
545 | scrollPane.addMouseListener(dblClickWatch);
|
---|
546 | add(scrollPane, BorderLayout.CENTER);
|
---|
547 |
|
---|
548 | JPanel buttonPanel = new JPanel(new GridLayout(1,3));
|
---|
549 | ActionListener buttonAction = new ActionListener(){
|
---|
550 | public void actionPerformed(ActionEvent e) {
|
---|
551 | int row = membershipTable.getSelectedRow();
|
---|
552 | if (e.getActionCommand().equals("Add"))
|
---|
553 | add();
|
---|
554 | else if(row >= 0)
|
---|
555 | {
|
---|
556 | if (e.getActionCommand().equals("Edit"))
|
---|
557 | membershipEdit(row);
|
---|
558 | else if (e.getActionCommand().equals("Delete")) {
|
---|
559 | Relation cur = (Relation)membershipData.getValueAt(row, 0);
|
---|
560 | NameVisitor n = new NameVisitor();
|
---|
561 | cur.visit(n);
|
---|
562 | if(JOptionPane.showConfirmDialog(Main.parent, tr("Really delete selection from relation {0}?", n.name),
|
---|
563 | tr("Change relation"), JOptionPane.YES_NO_OPTION) == JOptionPane.YES_NO_OPTION)
|
---|
564 | {
|
---|
565 | Relation rel = new Relation(cur);
|
---|
566 | Collection<OsmPrimitive> sel = Main.ds.getSelected();
|
---|
567 | for (RelationMember rm : cur.members) {
|
---|
568 | for (OsmPrimitive osm : sel) {
|
---|
569 | if (rm.member == osm)
|
---|
570 | {
|
---|
571 | RelationMember mem = new RelationMember();
|
---|
572 | mem.role = rm.role;
|
---|
573 | mem.member = rm.member;
|
---|
574 | rel.members.remove(mem);
|
---|
575 | break;
|
---|
576 | }
|
---|
577 | }
|
---|
578 | }
|
---|
579 | Main.main.undoRedo.add(new ChangeCommand(cur, rel));
|
---|
580 | Main.ds.fireSelectionChanged(sel);
|
---|
581 | selectionChanged(sel); // update whole table
|
---|
582 | }
|
---|
583 |
|
---|
584 | }
|
---|
585 | }
|
---|
586 | else
|
---|
587 | {
|
---|
588 | int sel = propertyTable.getSelectedRow();
|
---|
589 | if (e.getActionCommand().equals("Edit")) {
|
---|
590 | if(propertyTable.getRowCount() == 1)
|
---|
591 | sel = 0;
|
---|
592 | if (sel == -1)
|
---|
593 | JOptionPane.showMessageDialog(Main.parent, tr("Please select the row to edit."));
|
---|
594 | else
|
---|
595 | propertyEdit(sel);
|
---|
596 | } else if (e.getActionCommand().equals("Delete")) {
|
---|
597 | if (sel == -1)
|
---|
598 | JOptionPane.showMessageDialog(Main.parent, tr("Please select the row to delete."));
|
---|
599 | else
|
---|
600 | delete(sel);
|
---|
601 | }
|
---|
602 | }
|
---|
603 | }
|
---|
604 | };
|
---|
605 |
|
---|
606 | Shortcut s = Shortcut.registerShortcut("properties:add", tr("Add Properties"), KeyEvent.VK_B,
|
---|
607 | Shortcut.GROUP_MNEMONIC);
|
---|
608 | buttonPanel.add(new SideButton(marktr("Add"),"add","Properties",
|
---|
609 | tr("Add a new key/value pair to all objects"), s, buttonAction));
|
---|
610 |
|
---|
611 | s = Shortcut.registerShortcut("properties:edit", tr("Edit Properties"), KeyEvent.VK_I,
|
---|
612 | Shortcut.GROUP_MNEMONIC);
|
---|
613 | buttonPanel.add(new SideButton(marktr("Edit"),"edit","Properties",
|
---|
614 | tr("Edit the value of the selected key for all objects"), s, buttonAction));
|
---|
615 |
|
---|
616 | s = Shortcut.registerShortcut("properties:delete", tr("Delete Properties"), KeyEvent.VK_Q,
|
---|
617 | Shortcut.GROUP_MNEMONIC);
|
---|
618 | buttonPanel.add(new SideButton(marktr("Delete"),"delete","Properties",
|
---|
619 | tr("Delete the selected key in all objects"), s, buttonAction));
|
---|
620 | add(buttonPanel, BorderLayout.SOUTH);
|
---|
621 |
|
---|
622 | DataSet.selListeners.add(this);
|
---|
623 | }
|
---|
624 |
|
---|
625 | @Override public void setVisible(boolean b) {
|
---|
626 | super.setVisible(b);
|
---|
627 | if (b)
|
---|
628 | selectionChanged(Main.ds.getSelected());
|
---|
629 | }
|
---|
630 |
|
---|
631 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
---|
632 | if (!isVisible())
|
---|
633 | return;
|
---|
634 | if (propertyTable == null)
|
---|
635 | return; // selection changed may be received in base class constructor before init
|
---|
636 | if (propertyTable.getCellEditor() != null)
|
---|
637 | propertyTable.getCellEditor().cancelCellEditing();
|
---|
638 |
|
---|
639 | // re-load property data
|
---|
640 |
|
---|
641 | propertyData.setRowCount(0);
|
---|
642 |
|
---|
643 | Map<String, Integer> keyCount = new HashMap<String, Integer>();
|
---|
644 | valueCount.clear();
|
---|
645 | for (OsmPrimitive osm : newSelection) {
|
---|
646 | for (Entry<String, String> e : osm.entrySet()) {
|
---|
647 | keyCount.put(e.getKey(), keyCount.containsKey(e.getKey()) ? keyCount.get(e.getKey())+1 : 1);
|
---|
648 | if (valueCount.containsKey(e.getKey())) {
|
---|
649 | Map<String, Integer> v = valueCount.get(e.getKey());
|
---|
650 | v.put(e.getValue(), v.containsKey(e.getValue())? v.get(e.getValue())+1 : 1 );
|
---|
651 | } else {
|
---|
652 | TreeMap<String,Integer> v = new TreeMap<String, Integer>();
|
---|
653 | v.put(e.getValue(), 1);
|
---|
654 | valueCount.put(e.getKey(), v);
|
---|
655 | }
|
---|
656 | }
|
---|
657 | }
|
---|
658 | for (Entry<String, Map<String, Integer>> e : valueCount.entrySet()) {
|
---|
659 | int count=0;
|
---|
660 | for (Entry<String, Integer> e1: e.getValue().entrySet()) {
|
---|
661 | count+=e1.getValue();
|
---|
662 | }
|
---|
663 | if (count < newSelection.size()) {
|
---|
664 | e.getValue().put("", newSelection.size()-count);
|
---|
665 | }
|
---|
666 | propertyData.addRow(new Object[]{e.getKey(), e.getValue()});
|
---|
667 | }
|
---|
668 |
|
---|
669 | // re-load membership data
|
---|
670 | // this is rather expensive since we have to walk through all members of all existing relationships.
|
---|
671 | // could use back references here for speed if necessary.
|
---|
672 |
|
---|
673 | membershipData.setRowCount(0);
|
---|
674 |
|
---|
675 | Map<Relation, Collection<RelationMember>> roles = new HashMap<Relation, Collection<RelationMember>>();
|
---|
676 | for (Relation r : Main.ds.relations) {
|
---|
677 | if (!r.deleted && !r.incomplete) {
|
---|
678 | for (RelationMember m : r.members) {
|
---|
679 | if (newSelection.contains(m.member)) {
|
---|
680 | Collection<RelationMember> value = roles.get(r);
|
---|
681 | if (value == null) {
|
---|
682 | value = new HashSet<RelationMember>();
|
---|
683 | roles.put(r, value);
|
---|
684 | }
|
---|
685 | value.add(m);
|
---|
686 | }
|
---|
687 | }
|
---|
688 | }
|
---|
689 | }
|
---|
690 |
|
---|
691 | for (Entry<Relation, Collection<RelationMember>> e : roles.entrySet()) {
|
---|
692 | membershipData.addRow(new Object[]{e.getKey(), e.getValue()});
|
---|
693 | }
|
---|
694 |
|
---|
695 | membershipTable.getTableHeader().setVisible(membershipData.getRowCount() > 0);
|
---|
696 | }
|
---|
697 | }
|
---|