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