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