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