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