[3202] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[1599] | 2 | package org.openstreetmap.josm.gui.dialogs.relation;
|
---|
[343] | 3 |
|
---|
[2378] | 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
[792] | 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
[343] | 6 |
|
---|
[1790] | 7 | import java.awt.BorderLayout;
|
---|
[343] | 8 | import java.awt.Dimension;
|
---|
[1790] | 9 | import java.awt.FlowLayout;
|
---|
[9438] | 10 | import java.awt.GraphicsEnvironment;
|
---|
[1790] | 11 | import java.awt.GridBagConstraints;
|
---|
[343] | 12 | import java.awt.GridBagLayout;
|
---|
[10113] | 13 | import java.awt.Window;
|
---|
[10604] | 14 | import java.awt.datatransfer.Clipboard;
|
---|
| 15 | import java.awt.datatransfer.FlavorListener;
|
---|
[343] | 16 | import java.awt.event.ActionEvent;
|
---|
[1794] | 17 | import java.awt.event.FocusAdapter;
|
---|
| 18 | import java.awt.event.FocusEvent;
|
---|
[6024] | 19 | import java.awt.event.InputEvent;
|
---|
[343] | 20 | import java.awt.event.KeyEvent;
|
---|
[1816] | 21 | import java.awt.event.MouseAdapter;
|
---|
| 22 | import java.awt.event.MouseEvent;
|
---|
| 23 | import java.awt.event.WindowAdapter;
|
---|
| 24 | import java.awt.event.WindowEvent;
|
---|
[348] | 25 | import java.util.ArrayList;
|
---|
[810] | 26 | import java.util.Collection;
|
---|
[1839] | 27 | import java.util.Collections;
|
---|
[5972] | 28 | import java.util.EnumSet;
|
---|
[1886] | 29 | import java.util.HashSet;
|
---|
[1839] | 30 | import java.util.List;
|
---|
[3384] | 31 | import java.util.Set;
|
---|
[343] | 32 |
|
---|
[5724] | 33 | import javax.swing.AbstractAction;
|
---|
| 34 | import javax.swing.BorderFactory;
|
---|
| 35 | import javax.swing.InputMap;
|
---|
[8505] | 36 | import javax.swing.JButton;
|
---|
[5724] | 37 | import javax.swing.JComponent;
|
---|
| 38 | import javax.swing.JLabel;
|
---|
| 39 | import javax.swing.JMenuItem;
|
---|
| 40 | import javax.swing.JOptionPane;
|
---|
| 41 | import javax.swing.JPanel;
|
---|
[10113] | 42 | import javax.swing.JRootPane;
|
---|
[5724] | 43 | import javax.swing.JScrollPane;
|
---|
| 44 | import javax.swing.JSplitPane;
|
---|
| 45 | import javax.swing.JTabbedPane;
|
---|
[10113] | 46 | import javax.swing.JTable;
|
---|
[5724] | 47 | import javax.swing.JToolBar;
|
---|
| 48 | import javax.swing.KeyStroke;
|
---|
[343] | 49 |
|
---|
| 50 | import org.openstreetmap.josm.Main;
|
---|
[8505] | 51 | import org.openstreetmap.josm.actions.ExpertToggleAction;
|
---|
[4590] | 52 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
[343] | 53 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
[5082] | 54 | import org.openstreetmap.josm.command.Command;
|
---|
[12663] | 55 | import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
|
---|
[343] | 56 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 57 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
| 58 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
[3518] | 59 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
[1839] | 60 | import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
|
---|
[12643] | 61 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
[4590] | 62 | import org.openstreetmap.josm.gui.MainMenu;
|
---|
[12080] | 63 | import org.openstreetmap.josm.gui.ScrollViewport;
|
---|
[10604] | 64 | import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
|
---|
[9496] | 65 | import org.openstreetmap.josm.gui.dialogs.relation.actions.AddSelectedAfterSelection;
|
---|
| 66 | import org.openstreetmap.josm.gui.dialogs.relation.actions.AddSelectedAtEndAction;
|
---|
| 67 | import org.openstreetmap.josm.gui.dialogs.relation.actions.AddSelectedAtStartAction;
|
---|
| 68 | import org.openstreetmap.josm.gui.dialogs.relation.actions.AddSelectedBeforeSelection;
|
---|
| 69 | import org.openstreetmap.josm.gui.dialogs.relation.actions.ApplyAction;
|
---|
| 70 | import org.openstreetmap.josm.gui.dialogs.relation.actions.CancelAction;
|
---|
| 71 | import org.openstreetmap.josm.gui.dialogs.relation.actions.CopyMembersAction;
|
---|
| 72 | import org.openstreetmap.josm.gui.dialogs.relation.actions.DeleteCurrentRelationAction;
|
---|
| 73 | import org.openstreetmap.josm.gui.dialogs.relation.actions.DownloadIncompleteMembersAction;
|
---|
| 74 | import org.openstreetmap.josm.gui.dialogs.relation.actions.DownloadSelectedIncompleteMembersAction;
|
---|
| 75 | import org.openstreetmap.josm.gui.dialogs.relation.actions.DuplicateRelationAction;
|
---|
| 76 | import org.openstreetmap.josm.gui.dialogs.relation.actions.EditAction;
|
---|
| 77 | import org.openstreetmap.josm.gui.dialogs.relation.actions.MoveDownAction;
|
---|
| 78 | import org.openstreetmap.josm.gui.dialogs.relation.actions.MoveUpAction;
|
---|
| 79 | import org.openstreetmap.josm.gui.dialogs.relation.actions.OKAction;
|
---|
| 80 | import org.openstreetmap.josm.gui.dialogs.relation.actions.PasteMembersAction;
|
---|
[9657] | 81 | import org.openstreetmap.josm.gui.dialogs.relation.actions.RefreshAction;
|
---|
[9496] | 82 | import org.openstreetmap.josm.gui.dialogs.relation.actions.RemoveAction;
|
---|
| 83 | import org.openstreetmap.josm.gui.dialogs.relation.actions.RemoveSelectedAction;
|
---|
| 84 | import org.openstreetmap.josm.gui.dialogs.relation.actions.ReverseAction;
|
---|
| 85 | import org.openstreetmap.josm.gui.dialogs.relation.actions.SelectPrimitivesForSelectedMembersAction;
|
---|
| 86 | import org.openstreetmap.josm.gui.dialogs.relation.actions.SelectedMembersForSelectionAction;
|
---|
| 87 | import org.openstreetmap.josm.gui.dialogs.relation.actions.SetRoleAction;
|
---|
| 88 | import org.openstreetmap.josm.gui.dialogs.relation.actions.SortAction;
|
---|
| 89 | import org.openstreetmap.josm.gui.dialogs.relation.actions.SortBelowAction;
|
---|
[2378] | 90 | import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
|
---|
| 91 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
[1772] | 92 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
[2156] | 93 | import org.openstreetmap.josm.gui.tagging.TagEditorPanel;
|
---|
[3210] | 94 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
|
---|
[2048] | 95 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
|
---|
[12758] | 96 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
|
---|
[9059] | 97 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
|
---|
[8863] | 98 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetHandler;
|
---|
| 99 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetType;
|
---|
[9266] | 100 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;
|
---|
[12678] | 101 | import org.openstreetmap.josm.gui.util.WindowGeometry;
|
---|
[12846] | 102 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
[7050] | 103 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
[12620] | 104 | import org.openstreetmap.josm.tools.Logging;
|
---|
[1431] | 105 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[11848] | 106 | import org.openstreetmap.josm.tools.Utils;
|
---|
[343] | 107 |
|
---|
| 108 | /**
|
---|
| 109 | * This dialog is for editing relations.
|
---|
[7556] | 110 | * @since 343
|
---|
[343] | 111 | */
|
---|
[10378] | 112 | public class GenericRelationEditor extends RelationEditor {
|
---|
[1762] | 113 | /** the tag table and its model */
|
---|
[9078] | 114 | private final TagEditorPanel tagEditorPanel;
|
---|
| 115 | private final ReferringRelationsBrowser referrerBrowser;
|
---|
| 116 | private final ReferringRelationsBrowserModel referrerModel;
|
---|
[1432] | 117 |
|
---|
[10089] | 118 | /** the member table and its model */
|
---|
[10113] | 119 | private final MemberTable memberTable;
|
---|
[9078] | 120 | private final MemberTableModel memberTableModel;
|
---|
[1762] | 121 |
|
---|
[10089] | 122 | /** the selection table and its model */
|
---|
[10113] | 123 | private final SelectionTable selectionTable;
|
---|
[9078] | 124 | private final SelectionTableModel selectionTableModel;
|
---|
[1790] | 125 |
|
---|
[10113] | 126 | private final AutoCompletingTextField tfRole;
|
---|
[1794] | 127 |
|
---|
[10089] | 128 | /**
|
---|
| 129 | * the menu item in the windows menu. Required to properly hide on dialog close.
|
---|
[4590] | 130 | */
|
---|
| 131 | private JMenuItem windowMenuItem;
|
---|
[8505] | 132 | /**
|
---|
[10113] | 133 | * The toolbar with the buttons on the left
|
---|
[8505] | 134 | */
|
---|
[10113] | 135 | private final LeftButtonToolbar leftButtonToolbar;
|
---|
[9511] | 136 | /**
|
---|
[9657] | 137 | * Action for performing the {@link RefreshAction}
|
---|
| 138 | */
|
---|
[10113] | 139 | private final RefreshAction refreshAction;
|
---|
[9657] | 140 | /**
|
---|
| 141 | * Action for performing the {@link ApplyAction}
|
---|
| 142 | */
|
---|
[10113] | 143 | private final ApplyAction applyAction;
|
---|
[9657] | 144 | /**
|
---|
[10113] | 145 | * Action for performing the {@link DuplicateRelationAction}
|
---|
| 146 | */
|
---|
| 147 | private final DuplicateRelationAction duplicateAction;
|
---|
| 148 | /**
|
---|
| 149 | * Action for performing the {@link DeleteCurrentRelationAction}
|
---|
| 150 | */
|
---|
| 151 | private final DeleteCurrentRelationAction deleteAction;
|
---|
| 152 | /**
|
---|
| 153 | * Action for performing the {@link OKAction}
|
---|
| 154 | */
|
---|
| 155 | private final OKAction okAction;
|
---|
| 156 | /**
|
---|
[9511] | 157 | * Action for performing the {@link CancelAction}
|
---|
| 158 | */
|
---|
[10113] | 159 | private final CancelAction cancelAction;
|
---|
[10604] | 160 | /**
|
---|
| 161 | * A list of listeners that need to be notified on clipboard content changes.
|
---|
| 162 | */
|
---|
| 163 | private final ArrayList<FlavorListener> clipboardListeners = new ArrayList<>();
|
---|
[4590] | 164 |
|
---|
[1169] | 165 | /**
|
---|
[1822] | 166 | * Creates a new relation editor for the given relation. The relation will be saved if the user
|
---|
| 167 | * selects "ok" in the editor.
|
---|
[1925] | 168 | *
|
---|
[1169] | 169 | * If no relation is given, will create an editor for a new relation.
|
---|
[1925] | 170 | *
|
---|
[5266] | 171 | * @param layer the {@link OsmDataLayer} the new or edited relation belongs to
|
---|
[1169] | 172 | * @param relation relation to edit, or null to create a new one.
|
---|
[1790] | 173 | * @param selectedMembers a collection of members which shall be selected initially
|
---|
[1169] | 174 | */
|
---|
[1822] | 175 | public GenericRelationEditor(OsmDataLayer layer, Relation relation, Collection<RelationMember> selectedMembers) {
|
---|
[9496] | 176 | super(layer, relation);
|
---|
[1790] | 177 |
|
---|
[3045] | 178 | setRememberWindowGeometry(getClass().getName() + ".geometry",
|
---|
[3035] | 179 | WindowGeometry.centerInWindow(Main.parent, new Dimension(700, 650)));
|
---|
[3045] | 180 |
|
---|
[8863] | 181 | final TaggingPresetHandler presetHandler = new TaggingPresetHandler() {
|
---|
[1790] | 182 |
|
---|
[3518] | 183 | @Override
|
---|
| 184 | public void updateTags(List<Tag> tags) {
|
---|
[6092] | 185 | tagEditorPanel.getModel().updateTags(tags);
|
---|
[3518] | 186 | }
|
---|
| 187 |
|
---|
| 188 | @Override
|
---|
| 189 | public Collection<OsmPrimitive> getSelection() {
|
---|
| 190 | Relation relation = new Relation();
|
---|
| 191 | tagEditorPanel.getModel().applyToPrimitive(relation);
|
---|
| 192 | return Collections.<OsmPrimitive>singletonList(relation);
|
---|
| 193 | }
|
---|
[5614] | 194 | };
|
---|
[3518] | 195 |
|
---|
[5614] | 196 | // init the various models
|
---|
| 197 | //
|
---|
[9588] | 198 | memberTableModel = new MemberTableModel(relation, getLayer(), presetHandler);
|
---|
[5614] | 199 | memberTableModel.register();
|
---|
| 200 | selectionTableModel = new SelectionTableModel(getLayer());
|
---|
| 201 | selectionTableModel.register();
|
---|
| 202 | referrerModel = new ReferringRelationsBrowserModel(relation);
|
---|
| 203 |
|
---|
[9588] | 204 | tagEditorPanel = new TagEditorPanel(relation, presetHandler);
|
---|
[9657] | 205 | populateModels(relation);
|
---|
[2156] | 206 | tagEditorPanel.getModel().ensureOneTag();
|
---|
[1431] | 207 |
|
---|
[10113] | 208 | // setting up the member table
|
---|
| 209 | memberTable = new MemberTable(getLayer(), getRelation(), memberTableModel);
|
---|
| 210 | memberTable.addMouseListener(new MemberTableDblClickAdapter());
|
---|
| 211 | memberTableModel.addMemberModelListener(memberTable);
|
---|
| 212 |
|
---|
| 213 | MemberRoleCellEditor ce = (MemberRoleCellEditor) memberTable.getColumnModel().getColumn(0).getCellEditor();
|
---|
| 214 | selectionTable = new SelectionTable(selectionTableModel, memberTableModel);
|
---|
| 215 | selectionTable.setRowHeight(ce.getEditor().getPreferredSize().height);
|
---|
| 216 |
|
---|
| 217 | leftButtonToolbar = new LeftButtonToolbar(memberTable, memberTableModel, this);
|
---|
| 218 | tfRole = buildRoleTextField(this);
|
---|
| 219 |
|
---|
| 220 | JSplitPane pane = buildSplitPane(
|
---|
| 221 | buildTagEditorPanel(tagEditorPanel),
|
---|
| 222 | buildMemberEditorPanel(memberTable, memberTableModel, selectionTable, selectionTableModel, this, leftButtonToolbar, tfRole),
|
---|
| 223 | this);
|
---|
[1790] | 224 | pane.setPreferredSize(new Dimension(100, 100));
|
---|
[803] | 225 |
|
---|
[9543] | 226 | JPanel pnl = new JPanel(new BorderLayout());
|
---|
[1822] | 227 | pnl.add(pane, BorderLayout.CENTER);
|
---|
[1790] | 228 | pnl.setBorder(BorderFactory.createRaisedBevelBorder());
|
---|
| 229 |
|
---|
| 230 | getContentPane().setLayout(new BorderLayout());
|
---|
[1806] | 231 | JTabbedPane tabbedPane = new JTabbedPane();
|
---|
| 232 | tabbedPane.add(tr("Tags and Members"), pnl);
|
---|
[6361] | 233 | referrerBrowser = new ReferringRelationsBrowser(getLayer(), referrerModel);
|
---|
[1852] | 234 | tabbedPane.add(tr("Parent Relations"), referrerBrowser);
|
---|
[1828] | 235 | tabbedPane.add(tr("Child Relations"), new ChildRelationBrowser(getLayer(), relation));
|
---|
[10611] | 236 | tabbedPane.addChangeListener(e -> {
|
---|
| 237 | JTabbedPane sourceTabbedPane = (JTabbedPane) e.getSource();
|
---|
| 238 | int index = sourceTabbedPane.getSelectedIndex();
|
---|
| 239 | String title = sourceTabbedPane.getTitleAt(index);
|
---|
| 240 | if (title.equals(tr("Parent Relations"))) {
|
---|
| 241 | referrerBrowser.init();
|
---|
| 242 | }
|
---|
| 243 | });
|
---|
[1806] | 244 |
|
---|
[10113] | 245 | refreshAction = new RefreshAction(memberTable, memberTableModel, tagEditorPanel.getModel(), getLayer(), this);
|
---|
| 246 | applyAction = new ApplyAction(memberTable, memberTableModel, tagEditorPanel.getModel(), getLayer(), this);
|
---|
| 247 | duplicateAction = new DuplicateRelationAction(memberTableModel, tagEditorPanel.getModel(), getLayer());
|
---|
| 248 | deleteAction = new DeleteCurrentRelationAction(getLayer(), this);
|
---|
| 249 | addPropertyChangeListener(deleteAction);
|
---|
| 250 |
|
---|
| 251 | okAction = new OKAction(memberTable, memberTableModel, tagEditorPanel.getModel(), getLayer(), this, tfRole);
|
---|
| 252 | cancelAction = new CancelAction(memberTable, memberTableModel, tagEditorPanel.getModel(), getLayer(), this, tfRole);
|
---|
| 253 |
|
---|
| 254 | getContentPane().add(buildToolBar(refreshAction, applyAction, duplicateAction, deleteAction), BorderLayout.NORTH);
|
---|
[1822] | 255 | getContentPane().add(tabbedPane, BorderLayout.CENTER);
|
---|
[10113] | 256 | getContentPane().add(buildOkCancelButtonPanel(okAction, cancelAction), BorderLayout.SOUTH);
|
---|
[1790] | 257 |
|
---|
[1431] | 258 | setSize(findMaxDialogSize());
|
---|
[1845] | 259 |
|
---|
[9511] | 260 | setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
|
---|
[1845] | 261 | addWindowListener(
|
---|
| 262 | new WindowAdapter() {
|
---|
| 263 | @Override
|
---|
| 264 | public void windowOpened(WindowEvent e) {
|
---|
[10113] | 265 | cleanSelfReferences(memberTableModel, getRelation());
|
---|
[1845] | 266 | }
|
---|
[9511] | 267 |
|
---|
| 268 | @Override
|
---|
| 269 | public void windowClosing(WindowEvent e) {
|
---|
| 270 | cancel();
|
---|
| 271 | }
|
---|
[1845] | 272 | }
|
---|
| 273 | );
|
---|
[9991] | 274 | // CHECKSTYLE.OFF: LineLength
|
---|
| 275 | registerCopyPasteAction(tagEditorPanel.getPasteAction(), "PASTE_TAGS",
|
---|
[10113] | 276 | Shortcut.registerShortcut("system:pastestyle", tr("Edit: {0}", tr("Paste Tags")), KeyEvent.VK_V, Shortcut.CTRL_SHIFT).getKeyStroke(),
|
---|
| 277 | getRootPane(), memberTable, selectionTable);
|
---|
[9991] | 278 | // CHECKSTYLE.ON: LineLength
|
---|
[6070] | 279 |
|
---|
[11485] | 280 | KeyStroke key = Shortcut.getPasteKeyStroke();
|
---|
| 281 | if (key != null) {
|
---|
| 282 | // handle uncommon situation, that user has no keystroke assigned to paste
|
---|
| 283 | registerCopyPasteAction(new PasteMembersAction(memberTable, getLayer(), this) {
|
---|
| 284 | @Override
|
---|
| 285 | public void actionPerformed(ActionEvent e) {
|
---|
| 286 | super.actionPerformed(e);
|
---|
| 287 | tfRole.requestFocusInWindow();
|
---|
| 288 | }
|
---|
| 289 | }, "PASTE_MEMBERS", key, getRootPane(), memberTable, selectionTable);
|
---|
| 290 | }
|
---|
| 291 | key = Shortcut.getCopyKeyStroke();
|
---|
| 292 | if (key != null) {
|
---|
[11490] | 293 | // handle uncommon situation, that user has no keystroke assigned to copy
|
---|
[11485] | 294 | registerCopyPasteAction(new CopyMembersAction(memberTableModel, getLayer(), this),
|
---|
| 295 | "COPY_MEMBERS", key, getRootPane(), memberTable, selectionTable);
|
---|
| 296 | }
|
---|
[6064] | 297 | tagEditorPanel.setNextFocusComponent(memberTable);
|
---|
| 298 | selectionTable.setFocusable(false);
|
---|
[1886] | 299 | memberTableModel.setSelectedMembers(selectedMembers);
|
---|
[8510] | 300 | HelpUtil.setHelpContext(getRootPane(), ht("/Dialog/RelationEditor"));
|
---|
[1790] | 301 | }
|
---|
[343] | 302 |
|
---|
[9659] | 303 | @Override
|
---|
[9657] | 304 | public void reloadDataFromRelation() {
|
---|
| 305 | setRelation(getRelation());
|
---|
| 306 | populateModels(getRelation());
|
---|
| 307 | refreshAction.updateEnabledState();
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | private void populateModels(Relation relation) {
|
---|
| 311 | if (relation != null) {
|
---|
| 312 | tagEditorPanel.getModel().initFromPrimitive(relation);
|
---|
| 313 | memberTableModel.populate(relation);
|
---|
| 314 | if (!getLayer().data.getRelations().contains(relation)) {
|
---|
| 315 | // treat it as a new relation if it doesn't exist in the data set yet.
|
---|
| 316 | setRelation(null);
|
---|
| 317 | }
|
---|
| 318 | } else {
|
---|
| 319 | tagEditorPanel.getModel().clear();
|
---|
| 320 | memberTableModel.populate(null);
|
---|
| 321 | }
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 | /**
|
---|
| 325 | * Apply changes.
|
---|
| 326 | * @see ApplyAction
|
---|
| 327 | */
|
---|
| 328 | public void apply() {
|
---|
| 329 | applyAction.actionPerformed(null);
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 | /**
|
---|
| 333 | * Cancel changes.
|
---|
| 334 | * @see CancelAction
|
---|
| 335 | */
|
---|
| 336 | public void cancel() {
|
---|
[9511] | 337 | cancelAction.actionPerformed(null);
|
---|
| 338 | }
|
---|
| 339 |
|
---|
[1790] | 340 | /**
|
---|
[2563] | 341 | * Creates the toolbar
|
---|
[10113] | 342 | * @param refreshAction refresh action
|
---|
| 343 | * @param applyAction apply action
|
---|
| 344 | * @param duplicateAction duplicate action
|
---|
| 345 | * @param deleteAction delete action
|
---|
[2711] | 346 | *
|
---|
[2563] | 347 | * @return the toolbar
|
---|
| 348 | */
|
---|
[10113] | 349 | protected static JToolBar buildToolBar(RefreshAction refreshAction, ApplyAction applyAction,
|
---|
| 350 | DuplicateRelationAction duplicateAction, DeleteCurrentRelationAction deleteAction) {
|
---|
[9657] | 351 | JToolBar tb = new JToolBar();
|
---|
[2563] | 352 | tb.setFloatable(false);
|
---|
[9657] | 353 | tb.add(refreshAction);
|
---|
| 354 | tb.add(applyAction);
|
---|
[10113] | 355 | tb.add(duplicateAction);
|
---|
[2563] | 356 | tb.add(deleteAction);
|
---|
| 357 | return tb;
|
---|
| 358 | }
|
---|
| 359 |
|
---|
| 360 | /**
|
---|
[1822] | 361 | * builds the panel with the OK and the Cancel button
|
---|
[10113] | 362 | * @param okAction OK action
|
---|
| 363 | * @param cancelAction Cancel action
|
---|
[1925] | 364 | *
|
---|
[1822] | 365 | * @return the panel with the OK and the Cancel button
|
---|
[1790] | 366 | */
|
---|
[10113] | 367 | protected static JPanel buildOkCancelButtonPanel(OKAction okAction, CancelAction cancelAction) {
|
---|
[9496] | 368 | JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
|
---|
[10424] | 369 | pnl.add(new JButton(okAction));
|
---|
| 370 | pnl.add(new JButton(cancelAction));
|
---|
| 371 | pnl.add(new JButton(new ContextSensitiveHelpAction(ht("/Dialog/RelationEditor"))));
|
---|
[1790] | 372 | return pnl;
|
---|
[1432] | 373 | }
|
---|
[803] | 374 |
|
---|
[1790] | 375 | /**
|
---|
| 376 | * builds the panel with the tag editor
|
---|
[10113] | 377 | * @param tagEditorPanel tag editor panel
|
---|
[1925] | 378 | *
|
---|
[1790] | 379 | * @return the panel with the tag editor
|
---|
[1431] | 380 | */
|
---|
[10113] | 381 | protected static JPanel buildTagEditorPanel(TagEditorPanel tagEditorPanel) {
|
---|
[9496] | 382 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
[1790] | 383 |
|
---|
| 384 | GridBagConstraints gc = new GridBagConstraints();
|
---|
| 385 | gc.gridx = 0;
|
---|
| 386 | gc.gridy = 0;
|
---|
[1822] | 387 | gc.gridheight = 1;
|
---|
[2156] | 388 | gc.gridwidth = 1;
|
---|
[1790] | 389 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
| 390 | gc.anchor = GridBagConstraints.FIRST_LINE_START;
|
---|
| 391 | gc.weightx = 1.0;
|
---|
| 392 | gc.weighty = 0.0;
|
---|
| 393 | pnl.add(new JLabel(tr("Tags")), gc);
|
---|
| 394 |
|
---|
| 395 | gc.gridx = 0;
|
---|
| 396 | gc.gridy = 1;
|
---|
| 397 | gc.fill = GridBagConstraints.BOTH;
|
---|
| 398 | gc.anchor = GridBagConstraints.CENTER;
|
---|
[2156] | 399 | gc.weightx = 1.0;
|
---|
[1790] | 400 | gc.weighty = 1.0;
|
---|
[2156] | 401 | pnl.add(tagEditorPanel, gc);
|
---|
[1790] | 402 | return pnl;
|
---|
| 403 | }
|
---|
| 404 |
|
---|
| 405 | /**
|
---|
[10113] | 406 | * builds the role text field
|
---|
| 407 | * @param re relation editor
|
---|
| 408 | * @return the role text field
|
---|
| 409 | */
|
---|
| 410 | protected static AutoCompletingTextField buildRoleTextField(final IRelationEditor re) {
|
---|
| 411 | final AutoCompletingTextField tfRole = new AutoCompletingTextField(10);
|
---|
| 412 | tfRole.setToolTipText(tr("Enter a role and apply it to the selected relation members"));
|
---|
| 413 | tfRole.addFocusListener(new FocusAdapter() {
|
---|
| 414 | @Override
|
---|
| 415 | public void focusGained(FocusEvent e) {
|
---|
| 416 | tfRole.selectAll();
|
---|
| 417 | }
|
---|
| 418 | });
|
---|
| 419 | tfRole.setAutoCompletionList(new AutoCompletionList());
|
---|
| 420 | tfRole.addFocusListener(
|
---|
| 421 | new FocusAdapter() {
|
---|
| 422 | @Override
|
---|
| 423 | public void focusGained(FocusEvent e) {
|
---|
| 424 | AutoCompletionList list = tfRole.getAutoCompletionList();
|
---|
| 425 | if (list != null) {
|
---|
| 426 | list.clear();
|
---|
[12758] | 427 | AutoCompletionManager.of(re.getLayer().data).populateWithMemberRoles(list, re.getRelation());
|
---|
[10113] | 428 | }
|
---|
| 429 | }
|
---|
| 430 | }
|
---|
| 431 | );
|
---|
[12846] | 432 | tfRole.setText(Config.getPref().get("relation.editor.generic.lastrole", ""));
|
---|
[10113] | 433 | return tfRole;
|
---|
| 434 | }
|
---|
| 435 |
|
---|
| 436 | /**
|
---|
[1790] | 437 | * builds the panel for the relation member editor
|
---|
[10113] | 438 | * @param memberTable member table
|
---|
| 439 | * @param memberTableModel member table model
|
---|
| 440 | * @param selectionTable selection table
|
---|
| 441 | * @param selectionTableModel selection table model
|
---|
| 442 | * @param re relation editor
|
---|
| 443 | * @param leftButtonToolbar left button toolbar
|
---|
| 444 | * @param tfRole role text field
|
---|
[1925] | 445 | *
|
---|
[1790] | 446 | * @return the panel for the relation member editor
|
---|
| 447 | */
|
---|
[10113] | 448 | protected static JPanel buildMemberEditorPanel(final MemberTable memberTable, MemberTableModel memberTableModel,
|
---|
| 449 | SelectionTable selectionTable, SelectionTableModel selectionTableModel, IRelationEditor re,
|
---|
| 450 | LeftButtonToolbar leftButtonToolbar, final AutoCompletingTextField tfRole) {
|
---|
[7556] | 451 | final JPanel pnl = new JPanel(new GridBagLayout());
|
---|
[1790] | 452 | final JScrollPane scrollPane = new JScrollPane(memberTable);
|
---|
[1431] | 453 |
|
---|
[1790] | 454 | GridBagConstraints gc = new GridBagConstraints();
|
---|
| 455 | gc.gridx = 0;
|
---|
| 456 | gc.gridy = 0;
|
---|
[6732] | 457 | gc.gridwidth = 2;
|
---|
[1790] | 458 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
| 459 | gc.anchor = GridBagConstraints.FIRST_LINE_START;
|
---|
| 460 | gc.weightx = 1.0;
|
---|
| 461 | gc.weighty = 0.0;
|
---|
| 462 | pnl.add(new JLabel(tr("Members")), gc);
|
---|
[1431] | 463 |
|
---|
[1790] | 464 | gc.gridx = 0;
|
---|
| 465 | gc.gridy = 1;
|
---|
[6732] | 466 | gc.gridheight = 2;
|
---|
[1790] | 467 | gc.gridwidth = 1;
|
---|
| 468 | gc.fill = GridBagConstraints.VERTICAL;
|
---|
| 469 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
| 470 | gc.weightx = 0.0;
|
---|
| 471 | gc.weighty = 1.0;
|
---|
[12080] | 472 | pnl.add(new ScrollViewport(leftButtonToolbar, ScrollViewport.VERTICAL_DIRECTION), gc);
|
---|
[1790] | 473 |
|
---|
| 474 | gc.gridx = 1;
|
---|
| 475 | gc.gridy = 1;
|
---|
[6732] | 476 | gc.gridheight = 1;
|
---|
[1790] | 477 | gc.fill = GridBagConstraints.BOTH;
|
---|
| 478 | gc.anchor = GridBagConstraints.CENTER;
|
---|
[1801] | 479 | gc.weightx = 0.6;
|
---|
[1790] | 480 | gc.weighty = 1.0;
|
---|
| 481 | pnl.add(scrollPane, gc);
|
---|
| 482 |
|
---|
[2563] | 483 | // --- role editing
|
---|
| 484 | JPanel p3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
---|
| 485 | p3.add(new JLabel(tr("Apply Role:")));
|
---|
| 486 | p3.add(tfRole);
|
---|
[9496] | 487 | SetRoleAction setRoleAction = new SetRoleAction(memberTable, memberTableModel, tfRole);
|
---|
[2563] | 488 | memberTableModel.getSelectionModel().addListSelectionListener(setRoleAction);
|
---|
| 489 | tfRole.getDocument().addDocumentListener(setRoleAction);
|
---|
| 490 | tfRole.addActionListener(setRoleAction);
|
---|
| 491 | memberTableModel.getSelectionModel().addListSelectionListener(
|
---|
[10611] | 492 | e -> tfRole.setEnabled(memberTable.getSelectedRowCount() > 0)
|
---|
[2563] | 493 | );
|
---|
| 494 | tfRole.setEnabled(memberTable.getSelectedRowCount() > 0);
|
---|
[10424] | 495 | JButton btnApply = new JButton(setRoleAction);
|
---|
[8510] | 496 | btnApply.setPreferredSize(new Dimension(20, 20));
|
---|
[2665] | 497 | btnApply.setText("");
|
---|
| 498 | p3.add(btnApply);
|
---|
[2563] | 499 |
|
---|
| 500 | gc.gridx = 1;
|
---|
| 501 | gc.gridy = 2;
|
---|
[6732] | 502 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
| 503 | gc.anchor = GridBagConstraints.LAST_LINE_START;
|
---|
[2563] | 504 | gc.weightx = 1.0;
|
---|
| 505 | gc.weighty = 0.0;
|
---|
| 506 | pnl.add(p3, gc);
|
---|
| 507 |
|
---|
[9543] | 508 | JPanel pnl2 = new JPanel(new GridBagLayout());
|
---|
[1816] | 509 |
|
---|
| 510 | gc.gridx = 0;
|
---|
| 511 | gc.gridy = 0;
|
---|
[1822] | 512 | gc.gridheight = 1;
|
---|
[1816] | 513 | gc.gridwidth = 3;
|
---|
| 514 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
| 515 | gc.anchor = GridBagConstraints.FIRST_LINE_START;
|
---|
| 516 | gc.weightx = 1.0;
|
---|
| 517 | gc.weighty = 0.0;
|
---|
| 518 | pnl2.add(new JLabel(tr("Selection")), gc);
|
---|
| 519 |
|
---|
| 520 | gc.gridx = 0;
|
---|
[1790] | 521 | gc.gridy = 1;
|
---|
[1822] | 522 | gc.gridheight = 1;
|
---|
[1816] | 523 | gc.gridwidth = 1;
|
---|
| 524 | gc.fill = GridBagConstraints.VERTICAL;
|
---|
| 525 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
[1790] | 526 | gc.weightx = 0.0;
|
---|
| 527 | gc.weighty = 1.0;
|
---|
[12080] | 528 | pnl2.add(new ScrollViewport(buildSelectionControlButtonToolbar(memberTable, memberTableModel, selectionTableModel, re),
|
---|
| 529 | ScrollViewport.VERTICAL_DIRECTION), gc);
|
---|
[1790] | 530 |
|
---|
[1816] | 531 | gc.gridx = 1;
|
---|
[1790] | 532 | gc.gridy = 1;
|
---|
[1816] | 533 | gc.weightx = 1.0;
|
---|
[1790] | 534 | gc.weighty = 1.0;
|
---|
[1816] | 535 | gc.fill = GridBagConstraints.BOTH;
|
---|
[10113] | 536 | pnl2.add(buildSelectionTablePanel(selectionTable), gc);
|
---|
[1790] | 537 |
|
---|
[1822] | 538 | final JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
|
---|
[1816] | 539 | splitPane.setLeftComponent(pnl);
|
---|
| 540 | splitPane.setRightComponent(pnl2);
|
---|
| 541 | splitPane.setOneTouchExpandable(false);
|
---|
[10113] | 542 | if (re instanceof Window) {
|
---|
| 543 | ((Window) re).addWindowListener(new WindowAdapter() {
|
---|
| 544 | @Override
|
---|
| 545 | public void windowOpened(WindowEvent e) {
|
---|
| 546 | // has to be called when the window is visible, otherwise no effect
|
---|
| 547 | splitPane.setDividerLocation(0.6);
|
---|
| 548 | }
|
---|
| 549 | });
|
---|
| 550 | }
|
---|
[1790] | 551 |
|
---|
[9543] | 552 | JPanel pnl3 = new JPanel(new BorderLayout());
|
---|
[1816] | 553 | pnl3.add(splitPane, BorderLayout.CENTER);
|
---|
[3384] | 554 |
|
---|
[1816] | 555 | return pnl3;
|
---|
[1431] | 556 | }
|
---|
| 557 |
|
---|
| 558 | /**
|
---|
[1790] | 559 | * builds the panel with the table displaying the currently selected primitives
|
---|
[10113] | 560 | * @param selectionTable selection table
|
---|
[1925] | 561 | *
|
---|
[5903] | 562 | * @return panel with current selection
|
---|
[1431] | 563 | */
|
---|
[10113] | 564 | protected static JPanel buildSelectionTablePanel(SelectionTable selectionTable) {
|
---|
[7601] | 565 | JPanel pnl = new JPanel(new BorderLayout());
|
---|
| 566 | pnl.add(new JScrollPane(selectionTable), BorderLayout.CENTER);
|
---|
[1790] | 567 | return pnl;
|
---|
| 568 | }
|
---|
[674] | 569 |
|
---|
[1790] | 570 | /**
|
---|
[5266] | 571 | * builds the {@link JSplitPane} which divides the editor in an upper and a lower half
|
---|
[10113] | 572 | * @param top top panel
|
---|
| 573 | * @param bottom bottom panel
|
---|
| 574 | * @param re relation editor
|
---|
[1925] | 575 | *
|
---|
[1790] | 576 | * @return the split panel
|
---|
| 577 | */
|
---|
[10113] | 578 | protected static JSplitPane buildSplitPane(JPanel top, JPanel bottom, IRelationEditor re) {
|
---|
[1816] | 579 | final JSplitPane pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
|
---|
[10113] | 580 | pane.setTopComponent(top);
|
---|
| 581 | pane.setBottomComponent(bottom);
|
---|
[1790] | 582 | pane.setOneTouchExpandable(true);
|
---|
[10113] | 583 | if (re instanceof Window) {
|
---|
| 584 | ((Window) re).addWindowListener(new WindowAdapter() {
|
---|
| 585 | @Override
|
---|
| 586 | public void windowOpened(WindowEvent e) {
|
---|
| 587 | // has to be called when the window is visible, otherwise no effect
|
---|
| 588 | pane.setDividerLocation(0.3);
|
---|
| 589 | }
|
---|
| 590 | });
|
---|
| 591 | }
|
---|
[1790] | 592 | return pane;
|
---|
| 593 | }
|
---|
[343] | 594 |
|
---|
[1790] | 595 | /**
|
---|
[10113] | 596 | * The toolbar with the buttons on the left
|
---|
[1790] | 597 | */
|
---|
[10113] | 598 | static class LeftButtonToolbar extends JToolBar {
|
---|
[1748] | 599 |
|
---|
[10113] | 600 | /**
|
---|
| 601 | * Button for performing the {@link org.openstreetmap.josm.gui.dialogs.relation.actions.SortBelowAction}.
|
---|
| 602 | */
|
---|
| 603 | final JButton sortBelowButton;
|
---|
[1415] | 604 |
|
---|
[10113] | 605 | /**
|
---|
| 606 | * Constructs a new {@code LeftButtonToolbar}.
|
---|
| 607 | * @param memberTable member table
|
---|
| 608 | * @param memberTableModel member table model
|
---|
| 609 | * @param re relation editor
|
---|
| 610 | */
|
---|
| 611 | LeftButtonToolbar(MemberTable memberTable, MemberTableModel memberTableModel, IRelationEditor re) {
|
---|
| 612 | setOrientation(JToolBar.VERTICAL);
|
---|
| 613 | setFloatable(false);
|
---|
[6070] | 614 |
|
---|
[10113] | 615 | // -- move up action
|
---|
| 616 | MoveUpAction moveUpAction = new MoveUpAction(memberTable, memberTableModel, "moveUp");
|
---|
| 617 | memberTableModel.getSelectionModel().addListSelectionListener(moveUpAction);
|
---|
| 618 | add(moveUpAction);
|
---|
[2563] | 619 |
|
---|
[10113] | 620 | // -- move down action
|
---|
| 621 | MoveDownAction moveDownAction = new MoveDownAction(memberTable, memberTableModel, "moveDown");
|
---|
| 622 | memberTableModel.getSelectionModel().addListSelectionListener(moveDownAction);
|
---|
| 623 | add(moveDownAction);
|
---|
[6070] | 624 |
|
---|
[10113] | 625 | addSeparator();
|
---|
[6070] | 626 |
|
---|
[10113] | 627 | // -- edit action
|
---|
| 628 | EditAction editAction = new EditAction(memberTable, memberTableModel, re.getLayer());
|
---|
| 629 | memberTableModel.getSelectionModel().addListSelectionListener(editAction);
|
---|
| 630 | add(editAction);
|
---|
[1822] | 631 |
|
---|
[10113] | 632 | // -- delete action
|
---|
| 633 | RemoveAction removeSelectedAction = new RemoveAction(memberTable, memberTableModel, "removeSelected");
|
---|
| 634 | memberTable.getSelectionModel().addListSelectionListener(removeSelectedAction);
|
---|
| 635 | add(removeSelectedAction);
|
---|
[2700] | 636 |
|
---|
[10113] | 637 | addSeparator();
|
---|
| 638 | // -- sort action
|
---|
| 639 | SortAction sortAction = new SortAction(memberTable, memberTableModel);
|
---|
| 640 | memberTableModel.addTableModelListener(sortAction);
|
---|
| 641 | add(sortAction);
|
---|
| 642 | final SortBelowAction sortBelowAction = new SortBelowAction(memberTable, memberTableModel);
|
---|
| 643 | memberTableModel.addTableModelListener(sortBelowAction);
|
---|
| 644 | memberTableModel.getSelectionModel().addListSelectionListener(sortBelowAction);
|
---|
| 645 | sortBelowButton = add(sortBelowAction);
|
---|
[2563] | 646 |
|
---|
[10113] | 647 | // -- reverse action
|
---|
| 648 | ReverseAction reverseAction = new ReverseAction(memberTable, memberTableModel);
|
---|
| 649 | memberTableModel.addTableModelListener(reverseAction);
|
---|
| 650 | add(reverseAction);
|
---|
[2563] | 651 |
|
---|
[10113] | 652 | addSeparator();
|
---|
[2563] | 653 |
|
---|
[10113] | 654 | // -- download action
|
---|
| 655 | DownloadIncompleteMembersAction downloadIncompleteMembersAction = new DownloadIncompleteMembersAction(
|
---|
| 656 | memberTable, memberTableModel, "downloadIncomplete", re.getLayer(), re);
|
---|
| 657 | memberTable.getModel().addTableModelListener(downloadIncompleteMembersAction);
|
---|
| 658 | add(downloadIncompleteMembersAction);
|
---|
[6070] | 659 |
|
---|
[10113] | 660 | // -- download selected action
|
---|
| 661 | DownloadSelectedIncompleteMembersAction downloadSelectedIncompleteMembersAction = new DownloadSelectedIncompleteMembersAction(
|
---|
| 662 | memberTable, memberTableModel, null, re.getLayer(), re);
|
---|
| 663 | memberTable.getModel().addTableModelListener(downloadSelectedIncompleteMembersAction);
|
---|
| 664 | memberTable.getSelectionModel().addListSelectionListener(downloadSelectedIncompleteMembersAction);
|
---|
| 665 | add(downloadSelectedIncompleteMembersAction);
|
---|
| 666 |
|
---|
| 667 | InputMap inputMap = memberTable.getInputMap(MemberTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
|
---|
| 668 | inputMap.put((KeyStroke) removeSelectedAction.getValue(AbstractAction.ACCELERATOR_KEY), "removeSelected");
|
---|
| 669 | inputMap.put((KeyStroke) moveUpAction.getValue(AbstractAction.ACCELERATOR_KEY), "moveUp");
|
---|
| 670 | inputMap.put((KeyStroke) moveDownAction.getValue(AbstractAction.ACCELERATOR_KEY), "moveDown");
|
---|
| 671 | inputMap.put((KeyStroke) downloadIncompleteMembersAction.getValue(AbstractAction.ACCELERATOR_KEY), "downloadIncomplete");
|
---|
| 672 | }
|
---|
[1431] | 673 | }
|
---|
[733] | 674 |
|
---|
[1790] | 675 | /**
|
---|
[10113] | 676 | * build the toolbar with the buttons for adding or removing the current selection
|
---|
| 677 | * @param memberTable member table
|
---|
| 678 | * @param memberTableModel member table model
|
---|
| 679 | * @param selectionTableModel selection table model
|
---|
| 680 | * @param re relation editor
|
---|
[1925] | 681 | *
|
---|
[5903] | 682 | * @return control buttons panel for selection/members
|
---|
[1790] | 683 | */
|
---|
[10113] | 684 | protected static JToolBar buildSelectionControlButtonToolbar(MemberTable memberTable,
|
---|
| 685 | MemberTableModel memberTableModel, SelectionTableModel selectionTableModel, IRelationEditor re) {
|
---|
[2563] | 686 | JToolBar tb = new JToolBar(JToolBar.VERTICAL);
|
---|
| 687 | tb.setFloatable(false);
|
---|
[1748] | 688 |
|
---|
[3561] | 689 | // -- add at start action
|
---|
[9496] | 690 | AddSelectedAtStartAction addSelectionAction = new AddSelectedAtStartAction(
|
---|
[10113] | 691 | memberTableModel, selectionTableModel, re);
|
---|
[3561] | 692 | selectionTableModel.addTableModelListener(addSelectionAction);
|
---|
| 693 | tb.add(addSelectionAction);
|
---|
| 694 |
|
---|
| 695 | // -- add before selected action
|
---|
[9496] | 696 | AddSelectedBeforeSelection addSelectedBeforeSelectionAction = new AddSelectedBeforeSelection(
|
---|
[10113] | 697 | memberTableModel, selectionTableModel, re);
|
---|
[3561] | 698 | selectionTableModel.addTableModelListener(addSelectedBeforeSelectionAction);
|
---|
| 699 | memberTableModel.getSelectionModel().addListSelectionListener(addSelectedBeforeSelectionAction);
|
---|
| 700 | tb.add(addSelectedBeforeSelectionAction);
|
---|
| 701 |
|
---|
| 702 | // -- add after selected action
|
---|
[9496] | 703 | AddSelectedAfterSelection addSelectedAfterSelectionAction = new AddSelectedAfterSelection(
|
---|
[10113] | 704 | memberTableModel, selectionTableModel, re);
|
---|
[3561] | 705 | selectionTableModel.addTableModelListener(addSelectedAfterSelectionAction);
|
---|
| 706 | memberTableModel.getSelectionModel().addListSelectionListener(addSelectedAfterSelectionAction);
|
---|
| 707 | tb.add(addSelectedAfterSelectionAction);
|
---|
| 708 |
|
---|
[2563] | 709 | // -- add at end action
|
---|
[9496] | 710 | AddSelectedAtEndAction addSelectedAtEndAction = new AddSelectedAtEndAction(
|
---|
[10113] | 711 | memberTableModel, selectionTableModel, re);
|
---|
[1832] | 712 | selectionTableModel.addTableModelListener(addSelectedAtEndAction);
|
---|
[2563] | 713 | tb.add(addSelectedAtEndAction);
|
---|
[1832] | 714 |
|
---|
[3561] | 715 | tb.addSeparator();
|
---|
| 716 |
|
---|
[2563] | 717 | // -- select members action
|
---|
[9496] | 718 | SelectedMembersForSelectionAction selectMembersForSelectionAction = new SelectedMembersForSelectionAction(
|
---|
[10113] | 719 | memberTableModel, selectionTableModel, re.getLayer());
|
---|
[1927] | 720 | selectionTableModel.addTableModelListener(selectMembersForSelectionAction);
|
---|
| 721 | memberTableModel.addTableModelListener(selectMembersForSelectionAction);
|
---|
[2563] | 722 | tb.add(selectMembersForSelectionAction);
|
---|
[1927] | 723 |
|
---|
[2563] | 724 | // -- select action
|
---|
[9496] | 725 | SelectPrimitivesForSelectedMembersAction selectAction = new SelectPrimitivesForSelectedMembersAction(
|
---|
[10113] | 726 | memberTable, memberTableModel, re.getLayer());
|
---|
[2563] | 727 | memberTable.getSelectionModel().addListSelectionListener(selectAction);
|
---|
| 728 | tb.add(selectAction);
|
---|
[1832] | 729 |
|
---|
[2563] | 730 | tb.addSeparator();
|
---|
| 731 |
|
---|
[3561] | 732 | // -- remove selected action
|
---|
[10113] | 733 | RemoveSelectedAction removeSelectedAction = new RemoveSelectedAction(memberTableModel, selectionTableModel, re.getLayer());
|
---|
[3561] | 734 | selectionTableModel.addTableModelListener(removeSelectedAction);
|
---|
| 735 | tb.add(removeSelectedAction);
|
---|
[1748] | 736 |
|
---|
[2563] | 737 | return tb;
|
---|
[1790] | 738 | }
|
---|
[1785] | 739 |
|
---|
[1431] | 740 | @Override
|
---|
| 741 | protected Dimension findMaxDialogSize() {
|
---|
[2563] | 742 | return new Dimension(700, 650);
|
---|
[1169] | 743 | }
|
---|
[803] | 744 |
|
---|
[1790] | 745 | @Override
|
---|
[2156] | 746 | public void setVisible(boolean visible) {
|
---|
[10646] | 747 | if (isVisible() == visible) {
|
---|
| 748 | return;
|
---|
| 749 | }
|
---|
[2156] | 750 | if (visible) {
|
---|
[2563] | 751 | tagEditorPanel.initAutoCompletion(getLayer());
|
---|
[2156] | 752 | }
|
---|
| 753 | super.setVisible(visible);
|
---|
[10604] | 754 | Clipboard clipboard = ClipboardUtils.getClipboard();
|
---|
[3182] | 755 | if (visible) {
|
---|
[10113] | 756 | leftButtonToolbar.sortBelowButton.setVisible(ExpertToggleAction.isExpert());
|
---|
[3182] | 757 | RelationDialogManager.getRelationDialogManager().positionOnScreen(this);
|
---|
[8510] | 758 | if (windowMenuItem == null) {
|
---|
[10113] | 759 | windowMenuItem = addToWindowMenu(this, getLayer().getName());
|
---|
[4590] | 760 | }
|
---|
[6064] | 761 | tagEditorPanel.requestFocusInWindow();
|
---|
[10604] | 762 | for (FlavorListener listener : clipboardListeners) {
|
---|
| 763 | clipboard.addFlavorListener(listener);
|
---|
| 764 | }
|
---|
[3182] | 765 | } else {
|
---|
[2563] | 766 | // make sure all registered listeners are unregistered
|
---|
| 767 | //
|
---|
[6038] | 768 | memberTable.stopHighlighting();
|
---|
[2563] | 769 | selectionTableModel.unregister();
|
---|
[3095] | 770 | memberTableModel.unregister();
|
---|
[10454] | 771 | memberTable.unregisterListeners();
|
---|
[8510] | 772 | if (windowMenuItem != null) {
|
---|
[12643] | 773 | MainApplication.getMenu().windowMenu.remove(windowMenuItem);
|
---|
[4590] | 774 | windowMenuItem = null;
|
---|
| 775 | }
|
---|
[10604] | 776 | for (FlavorListener listener : clipboardListeners) {
|
---|
| 777 | clipboard.removeFlavorListener(listener);
|
---|
| 778 | }
|
---|
[1790] | 779 | dispose();
|
---|
[1772] | 780 | }
|
---|
| 781 | }
|
---|
| 782 |
|
---|
[10113] | 783 | /**
|
---|
| 784 | * Adds current relation editor to the windows menu (in the "volatile" group)
|
---|
| 785 | * @param re relation editor
|
---|
| 786 | * @param layerName layer name
|
---|
| 787 | * @return created menu item
|
---|
| 788 | */
|
---|
| 789 | protected static JMenuItem addToWindowMenu(IRelationEditor re, String layerName) {
|
---|
| 790 | Relation r = re.getRelation();
|
---|
| 791 | String name = r == null ? tr("New Relation") : r.getLocalName();
|
---|
| 792 | JosmAction focusAction = new JosmAction(
|
---|
| 793 | tr("Relation Editor: {0}", name == null && r != null ? r.getId() : name),
|
---|
| 794 | "dialogs/relationlist",
|
---|
| 795 | tr("Focus Relation Editor with relation ''{0}'' in layer ''{1}''", name, layerName),
|
---|
| 796 | null, false, false) {
|
---|
[4590] | 797 | @Override
|
---|
| 798 | public void actionPerformed(ActionEvent e) {
|
---|
[10113] | 799 | ((RelationEditor) getValue("relationEditor")).setVisible(true);
|
---|
[4590] | 800 | }
|
---|
| 801 | };
|
---|
[10113] | 802 | focusAction.putValue("relationEditor", re);
|
---|
[12643] | 803 | return MainMenu.add(MainApplication.getMenu().windowMenu, focusAction, MainMenu.WINDOW_MENU_GROUP.VOLATILE);
|
---|
[4590] | 804 | }
|
---|
| 805 |
|
---|
[1845] | 806 | /**
|
---|
| 807 | * checks whether the current relation has members referring to itself. If so,
|
---|
| 808 | * warns the users and provides an option for removing these members.
|
---|
[10113] | 809 | * @param memberTableModel member table model
|
---|
| 810 | * @param relation relation
|
---|
[1845] | 811 | */
|
---|
[10113] | 812 | protected static void cleanSelfReferences(MemberTableModel memberTableModel, Relation relation) {
|
---|
[7005] | 813 | List<OsmPrimitive> toCheck = new ArrayList<>();
|
---|
[10113] | 814 | toCheck.add(relation);
|
---|
[1845] | 815 | if (memberTableModel.hasMembersReferringTo(toCheck)) {
|
---|
| 816 | int ret = ConditionalOptionPaneUtil.showOptionDialog(
|
---|
| 817 | "clean_relation_self_references",
|
---|
| 818 | Main.parent,
|
---|
| 819 | tr("<html>There is at least one member in this relation referring<br>"
|
---|
| 820 | + "to the relation itself.<br>"
|
---|
[2163] | 821 | + "This creates circular dependencies and is discouraged.<br>"
|
---|
[1845] | 822 | + "How do you want to proceed with circular dependencies?</html>"),
|
---|
| 823 | tr("Warning"),
|
---|
| 824 | JOptionPane.YES_NO_OPTION,
|
---|
| 825 | JOptionPane.WARNING_MESSAGE,
|
---|
| 826 | new String[]{tr("Remove them, clean up relation"), tr("Ignore them, leave relation as is")},
|
---|
| 827 | tr("Remove them, clean up relation")
|
---|
| 828 | );
|
---|
| 829 | switch(ret) {
|
---|
[7801] | 830 | case ConditionalOptionPaneUtil.DIALOG_DISABLED_OPTION:
|
---|
| 831 | case JOptionPane.CLOSED_OPTION:
|
---|
| 832 | case JOptionPane.NO_OPTION:
|
---|
| 833 | return;
|
---|
[2381] | 834 | case JOptionPane.YES_OPTION:
|
---|
| 835 | memberTableModel.removeMembersReferringTo(toCheck);
|
---|
| 836 | break;
|
---|
[10217] | 837 | default: // Do nothing
|
---|
[1845] | 838 | }
|
---|
| 839 | }
|
---|
| 840 | }
|
---|
| 841 |
|
---|
[10604] | 842 | private void registerCopyPasteAction(AbstractAction action, Object actionName, KeyStroke shortcut,
|
---|
[10113] | 843 | JRootPane rootPane, JTable... tables) {
|
---|
[6024] | 844 | int mods = shortcut.getModifiers();
|
---|
| 845 | int code = shortcut.getKeyCode();
|
---|
[8510] | 846 | if (code != KeyEvent.VK_INSERT && (mods == 0 || mods == InputEvent.SHIFT_DOWN_MASK)) {
|
---|
[12620] | 847 | Logging.info(tr("Sorry, shortcut \"{0}\" can not be enabled in Relation editor dialog"), shortcut);
|
---|
[6024] | 848 | return;
|
---|
| 849 | }
|
---|
[10113] | 850 | rootPane.getActionMap().put(actionName, action);
|
---|
| 851 | rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(shortcut, actionName);
|
---|
[8540] | 852 | // Assign also to JTables because they have their own Copy&Paste implementation
|
---|
| 853 | // (which is disabled in this case but eats key shortcuts anyway)
|
---|
[10113] | 854 | for (JTable table : tables) {
|
---|
| 855 | table.getInputMap(JComponent.WHEN_FOCUSED).put(shortcut, actionName);
|
---|
| 856 | table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(shortcut, actionName);
|
---|
| 857 | table.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(shortcut, actionName);
|
---|
| 858 | }
|
---|
[10604] | 859 | if (action instanceof FlavorListener) {
|
---|
| 860 | clipboardListeners.add((FlavorListener) action);
|
---|
| 861 | }
|
---|
[3384] | 862 | }
|
---|
| 863 |
|
---|
[9496] | 864 | /**
|
---|
| 865 | * Exception thrown when user aborts add operation.
|
---|
| 866 | */
|
---|
| 867 | public static class AddAbortException extends Exception {
|
---|
[1839] | 868 | }
|
---|
| 869 |
|
---|
[9496] | 870 | /**
|
---|
| 871 | * Asks confirmationbefore adding a primitive.
|
---|
| 872 | * @param primitive primitive to add
|
---|
| 873 | * @return {@code true} is user confirms the operation, {@code false} otherwise
|
---|
| 874 | * @throws AddAbortException if user aborts operation
|
---|
| 875 | */
|
---|
| 876 | public static boolean confirmAddingPrimitive(OsmPrimitive primitive) throws AddAbortException {
|
---|
[5082] | 877 | String msg = tr("<html>This relation already has one or more members referring to<br>"
|
---|
| 878 | + "the object ''{0}''<br>"
|
---|
| 879 | + "<br>"
|
---|
| 880 | + "Do you really want to add another relation member?</html>",
|
---|
[11848] | 881 | Utils.escapeReservedCharactersHTML(primitive.getDisplayName(DefaultNameFormatter.getInstance()))
|
---|
[5082] | 882 | );
|
---|
| 883 | int ret = ConditionalOptionPaneUtil.showOptionDialog(
|
---|
| 884 | "add_primitive_to_relation",
|
---|
| 885 | Main.parent,
|
---|
| 886 | msg,
|
---|
| 887 | tr("Multiple members referring to same object."),
|
---|
| 888 | JOptionPane.YES_NO_CANCEL_OPTION,
|
---|
| 889 | JOptionPane.WARNING_MESSAGE,
|
---|
| 890 | null,
|
---|
| 891 | null
|
---|
| 892 | );
|
---|
| 893 | switch(ret) {
|
---|
[7801] | 894 | case ConditionalOptionPaneUtil.DIALOG_DISABLED_OPTION:
|
---|
| 895 | case JOptionPane.YES_OPTION:
|
---|
| 896 | return true;
|
---|
| 897 | case JOptionPane.NO_OPTION:
|
---|
| 898 | case JOptionPane.CLOSED_OPTION:
|
---|
| 899 | return false;
|
---|
| 900 | case JOptionPane.CANCEL_OPTION:
|
---|
[9496] | 901 | default:
|
---|
[7801] | 902 | throw new AddAbortException();
|
---|
[1839] | 903 | }
|
---|
[5082] | 904 | }
|
---|
[1839] | 905 |
|
---|
[9496] | 906 | /**
|
---|
| 907 | * Warn about circular references.
|
---|
| 908 | * @param primitive the concerned primitive
|
---|
| 909 | */
|
---|
| 910 | public static void warnOfCircularReferences(OsmPrimitive primitive) {
|
---|
[5082] | 911 | String msg = tr("<html>You are trying to add a relation to itself.<br>"
|
---|
| 912 | + "<br>"
|
---|
| 913 | + "This creates circular references and is therefore discouraged.<br>"
|
---|
| 914 | + "Skipping relation ''{0}''.</html>",
|
---|
[11848] | 915 | Utils.escapeReservedCharactersHTML(primitive.getDisplayName(DefaultNameFormatter.getInstance())));
|
---|
[5082] | 916 | JOptionPane.showMessageDialog(
|
---|
| 917 | Main.parent,
|
---|
| 918 | msg,
|
---|
| 919 | tr("Warning"),
|
---|
| 920 | JOptionPane.WARNING_MESSAGE);
|
---|
| 921 | }
|
---|
| 922 |
|
---|
[7742] | 923 | /**
|
---|
| 924 | * Adds primitives to a given relation.
|
---|
| 925 | * @param orig The relation to modify
|
---|
| 926 | * @param primitivesToAdd The primitives to add as relation members
|
---|
| 927 | * @return The resulting command
|
---|
| 928 | * @throws IllegalArgumentException if orig is null
|
---|
| 929 | */
|
---|
[8291] | 930 | public static Command addPrimitivesToRelation(final Relation orig, Collection<? extends OsmPrimitive> primitivesToAdd) {
|
---|
[7050] | 931 | CheckParameterUtil.ensureParameterNotNull(orig, "orig");
|
---|
[5082] | 932 | try {
|
---|
[9266] | 933 | final Collection<TaggingPreset> presets = TaggingPresets.getMatchingPresets(
|
---|
[9588] | 934 | EnumSet.of(TaggingPresetType.forPrimitive(orig)), orig.getKeys(), false);
|
---|
[5082] | 935 | Relation relation = new Relation(orig);
|
---|
| 936 | boolean modified = false;
|
---|
| 937 | for (OsmPrimitive p : primitivesToAdd) {
|
---|
[7050] | 938 | if (p instanceof Relation && orig.equals(p)) {
|
---|
[9438] | 939 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
| 940 | warnOfCircularReferences(p);
|
---|
| 941 | }
|
---|
[5082] | 942 | continue;
|
---|
| 943 | } else if (MemberTableModel.hasMembersReferringTo(relation.getMembers(), Collections.singleton(p))
|
---|
[5724] | 944 | && !confirmAddingPrimitive(p)) {
|
---|
[5082] | 945 | continue;
|
---|
| 946 | }
|
---|
[7742] | 947 | final Set<String> roles = findSuggestedRoles(presets, p);
|
---|
| 948 | relation.addMember(new RelationMember(roles.size() == 1 ? roles.iterator().next() : "", p));
|
---|
[5082] | 949 | modified = true;
|
---|
[1839] | 950 | }
|
---|
[5082] | 951 | return modified ? new ChangeCommand(orig, relation) : null;
|
---|
| 952 | } catch (AddAbortException ign) {
|
---|
[12620] | 953 | Logging.trace(ign);
|
---|
[5082] | 954 | return null;
|
---|
[1839] | 955 | }
|
---|
[5082] | 956 | }
|
---|
[1839] | 957 |
|
---|
[7742] | 958 | protected static Set<String> findSuggestedRoles(final Collection<TaggingPreset> presets, OsmPrimitive p) {
|
---|
| 959 | final Set<String> roles = new HashSet<>();
|
---|
| 960 | for (TaggingPreset preset : presets) {
|
---|
| 961 | String role = preset.suggestRoleForOsmPrimitive(p);
|
---|
| 962 | if (role != null && !role.isEmpty()) {
|
---|
| 963 | roles.add(role);
|
---|
| 964 | }
|
---|
| 965 | }
|
---|
| 966 | return roles;
|
---|
| 967 | }
|
---|
| 968 |
|
---|
[1816] | 969 | class MemberTableDblClickAdapter extends MouseAdapter {
|
---|
| 970 | @Override
|
---|
| 971 | public void mouseClicked(MouseEvent e) {
|
---|
| 972 | if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2) {
|
---|
[9496] | 973 | new EditAction(memberTable, memberTableModel, getLayer()).actionPerformed(null);
|
---|
[1816] | 974 | }
|
---|
| 975 | }
|
---|
| 976 | }
|
---|
[343] | 977 | }
|
---|