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