Ignore:
Timestamp:
2018-07-12T23:02:02+02:00 (5 months ago)
Author:
michael2402
Message:

See #16388: New mechanism for plugins to register relation editor actions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/GenericRelationEditor.java

    r13761 r14027  
    2424import java.awt.event.WindowEvent;
    2525import java.util.ArrayList;
     26import java.util.Arrays;
    2627import java.util.Collection;
    2728import java.util.Collections;
     
    4950
    5051import org.openstreetmap.josm.Main;
    51 import org.openstreetmap.josm.actions.ExpertToggleAction;
    5252import org.openstreetmap.josm.actions.JosmAction;
    5353import org.openstreetmap.josm.command.ChangeCommand;
     
    7676import org.openstreetmap.josm.gui.dialogs.relation.actions.DuplicateRelationAction;
    7777import org.openstreetmap.josm.gui.dialogs.relation.actions.EditAction;
     78import org.openstreetmap.josm.gui.dialogs.relation.actions.IRelationEditorActionAccess;
     79import org.openstreetmap.josm.gui.dialogs.relation.actions.IRelationEditorActionGroup;
    7880import org.openstreetmap.josm.gui.dialogs.relation.actions.MoveDownAction;
    7981import org.openstreetmap.josm.gui.dialogs.relation.actions.MoveUpAction;
     
    9395import org.openstreetmap.josm.gui.help.HelpUtil;
    9496import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     97import org.openstreetmap.josm.gui.tagging.TagEditorModel;
    9598import org.openstreetmap.josm.gui.tagging.TagEditorPanel;
    9699import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
     
    221224        selectionTable.setRowHeight(ce.getEditor().getPreferredSize().height);
    222225
    223         leftButtonToolbar = new LeftButtonToolbar(memberTable, memberTableModel, this);
     226        leftButtonToolbar = new LeftButtonToolbar(new RelationEditorActionAccess());
    224227        tfRole = buildRoleTextField(this);
    225228
    226229        JSplitPane pane = buildSplitPane(
    227230                buildTagEditorPanel(tagEditorPanel),
    228                 buildMemberEditorPanel(memberTable, memberTableModel, selectionTable, selectionTableModel, this, leftButtonToolbar, tfRole),
     231                buildMemberEditorPanel(leftButtonToolbar, new RelationEditorActionAccess()),
    229232                this);
    230233        pane.setPreferredSize(new Dimension(100, 100));
     
    248251            }
    249252        });
    250 
    251         refreshAction = new RefreshAction(memberTable, memberTableModel, tagEditorPanel.getModel(), getLayer(), this);
    252         applyAction = new ApplyAction(memberTable, memberTableModel, tagEditorPanel.getModel(), getLayer(), this);
    253         selectAction = new SelectAction(getLayer(), this);
    254         duplicateAction = new DuplicateRelationAction(memberTableModel, tagEditorPanel.getModel(), getLayer());
    255         deleteAction = new DeleteCurrentRelationAction(getLayer(), this);
     253       
     254        IRelationEditorActionAccess actionAccess = new RelationEditorActionAccess();
     255
     256        refreshAction = new RefreshAction(actionAccess);
     257        applyAction = new ApplyAction(actionAccess);
     258        selectAction = new SelectAction(actionAccess);
     259        duplicateAction = new DuplicateRelationAction(actionAccess);
     260        deleteAction = new DeleteCurrentRelationAction(actionAccess);
    256261        addPropertyChangeListener(deleteAction);
    257262
    258         okAction = new OKAction(memberTable, memberTableModel, tagEditorPanel.getModel(), getLayer(), this, tfRole);
    259         cancelAction = new CancelAction(memberTable, memberTableModel, tagEditorPanel.getModel(), getLayer(), this, tfRole);
     263        okAction = new OKAction(actionAccess);
     264        cancelAction = new CancelAction(actionAccess);
    260265
    261266        getContentPane().add(buildToolBar(refreshAction, applyAction, selectAction, duplicateAction, deleteAction), BorderLayout.NORTH);
     
    288293        if (key != null) {
    289294            // handle uncommon situation, that user has no keystroke assigned to paste
    290             registerCopyPasteAction(new PasteMembersAction(memberTable, getLayer(), this) {
    291                 @Override
     295            registerCopyPasteAction(new PasteMembersAction(actionAccess) {
     296                                private static final long serialVersionUID = 1L;
     297
     298                                @Override
    292299                public void actionPerformed(ActionEvent e) {
    293300                    super.actionPerformed(e);
     
    299306        if (key != null) {
    300307            // handle uncommon situation, that user has no keystroke assigned to copy
    301             registerCopyPasteAction(new CopyMembersAction(memberTableModel, getLayer(), this),
     308            registerCopyPasteAction(new CopyMembersAction(actionAccess),
    302309                    "COPY_MEMBERS", key, getRootPane(), memberTable, selectionTable);
    303310        }
     
    447454    /**
    448455     * builds the panel for the relation member editor
    449      * @param memberTable member table
    450      * @param memberTableModel member table model
    451      * @param selectionTable selection table
    452      * @param selectionTableModel selection table model
    453      * @param re relation editor
    454456     * @param leftButtonToolbar left button toolbar
    455      * @param tfRole role text field
     457     * @param editorAccess The relation editor
    456458     *
    457459     * @return the panel for the relation member editor
    458460     */
    459     protected static JPanel buildMemberEditorPanel(final MemberTable memberTable, MemberTableModel memberTableModel,
    460             SelectionTable selectionTable, SelectionTableModel selectionTableModel, IRelationEditor re,
    461             LeftButtonToolbar leftButtonToolbar, final AutoCompletingTextField tfRole) {
     461    protected static JPanel buildMemberEditorPanel(
     462            LeftButtonToolbar leftButtonToolbar, IRelationEditorActionAccess editorAccess) {
    462463        final JPanel pnl = new JPanel(new GridBagLayout());
    463         final JScrollPane scrollPane = new JScrollPane(memberTable);
     464        final JScrollPane scrollPane = new JScrollPane(editorAccess.getMemberTable());
    464465
    465466        GridBagConstraints gc = new GridBagConstraints();
     
    495496        JPanel p3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
    496497        p3.add(new JLabel(tr("Apply Role:")));
    497         p3.add(tfRole);
    498         SetRoleAction setRoleAction = new SetRoleAction(memberTable, memberTableModel, tfRole);
    499         memberTableModel.getSelectionModel().addListSelectionListener(setRoleAction);
    500         tfRole.getDocument().addDocumentListener(setRoleAction);
    501         tfRole.addActionListener(setRoleAction);
    502         memberTableModel.getSelectionModel().addListSelectionListener(
    503                 e -> tfRole.setEnabled(memberTable.getSelectedRowCount() > 0)
     498        p3.add(editorAccess.getTextFieldRole());
     499        SetRoleAction setRoleAction = new SetRoleAction(editorAccess);
     500        editorAccess.getMemberTableModel().getSelectionModel().addListSelectionListener(setRoleAction);
     501        editorAccess.getTextFieldRole().getDocument().addDocumentListener(setRoleAction);
     502        editorAccess.getTextFieldRole().addActionListener(setRoleAction);
     503        editorAccess.getMemberTableModel().getSelectionModel().addListSelectionListener(
     504                e -> editorAccess.getTextFieldRole().setEnabled(editorAccess.getMemberTable().getSelectedRowCount() > 0)
    504505        );
    505         tfRole.setEnabled(memberTable.getSelectedRowCount() > 0);
     506        editorAccess.getTextFieldRole().setEnabled(editorAccess.getMemberTable().getSelectedRowCount() > 0);
    506507        JButton btnApply = new JButton(setRoleAction);
    507508        btnApply.setPreferredSize(new Dimension(20, 20));
     
    537538        gc.weightx = 0.0;
    538539        gc.weighty = 1.0;
    539         pnl2.add(new ScrollViewport(buildSelectionControlButtonToolbar(memberTable, memberTableModel, selectionTableModel, re),
     540        pnl2.add(new ScrollViewport(buildSelectionControlButtonToolbar(editorAccess),
    540541                ScrollViewport.VERTICAL_DIRECTION), gc);
    541542
     
    545546        gc.weighty = 1.0;
    546547        gc.fill = GridBagConstraints.BOTH;
    547         pnl2.add(buildSelectionTablePanel(selectionTable), gc);
     548        pnl2.add(buildSelectionTablePanel(editorAccess.getSelectionTable()), gc);
    548549
    549550        final JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
     
    551552        splitPane.setRightComponent(pnl2);
    552553        splitPane.setOneTouchExpandable(false);
    553         if (re instanceof Window) {
    554             ((Window) re).addWindowListener(new WindowAdapter() {
     554        if (editorAccess.getEditor() instanceof Window) {
     555            ((Window) editorAccess.getEditor()).addWindowListener(new WindowAdapter() {
    555556                @Override
    556557                public void windowOpened(WindowEvent e) {
     
    608609     */
    609610    static class LeftButtonToolbar extends JToolBar {
    610 
    611         /**
    612          * Button for performing the {@link org.openstreetmap.josm.gui.dialogs.relation.actions.SortBelowAction}.
    613          */
    614         final JButton sortBelowButton;
    615 
    616         /**
     611                private static final long serialVersionUID = 1L;
     612
     613                /**
    617614         * Constructs a new {@code LeftButtonToolbar}.
    618          * @param memberTable member table
    619          * @param memberTableModel member table model
    620615         * @param re relation editor
    621616         */
    622         LeftButtonToolbar(MemberTable memberTable, MemberTableModel memberTableModel, IRelationEditor re) {
     617        LeftButtonToolbar(IRelationEditorActionAccess editorAccess) {
    623618            setOrientation(JToolBar.VERTICAL);
    624619            setFloatable(false);
    625620
    626             // -- move up action
    627             MoveUpAction moveUpAction = new MoveUpAction(memberTable, memberTableModel, "moveUp");
    628             memberTableModel.getSelectionModel().addListSelectionListener(moveUpAction);
    629             add(moveUpAction);
    630 
    631             // -- move down action
    632             MoveDownAction moveDownAction = new MoveDownAction(memberTable, memberTableModel, "moveDown");
    633             memberTableModel.getSelectionModel().addListSelectionListener(moveDownAction);
    634             add(moveDownAction);
    635 
    636             addSeparator();
    637 
    638             // -- edit action
    639             EditAction editAction = new EditAction(memberTable, memberTableModel, re.getLayer());
    640             memberTableModel.getSelectionModel().addListSelectionListener(editAction);
    641             add(editAction);
    642 
    643             // -- delete action
    644             RemoveAction removeSelectedAction = new RemoveAction(memberTable, memberTableModel, "removeSelected");
    645             memberTable.getSelectionModel().addListSelectionListener(removeSelectedAction);
    646             add(removeSelectedAction);
    647 
    648             addSeparator();
    649             // -- sort action
    650             SortAction sortAction = new SortAction(memberTable, memberTableModel);
    651             memberTableModel.addTableModelListener(sortAction);
    652             add(sortAction);
    653             final SortBelowAction sortBelowAction = new SortBelowAction(memberTable, memberTableModel);
    654             memberTableModel.addTableModelListener(sortBelowAction);
    655             memberTableModel.getSelectionModel().addListSelectionListener(sortBelowAction);
    656             sortBelowButton = add(sortBelowAction);
    657 
    658             // -- reverse action
    659             ReverseAction reverseAction = new ReverseAction(memberTable, memberTableModel);
    660             memberTableModel.addTableModelListener(reverseAction);
    661             add(reverseAction);
    662 
    663             addSeparator();
    664 
    665             // -- download action
    666             DownloadIncompleteMembersAction downloadIncompleteMembersAction = new DownloadIncompleteMembersAction(
    667                     memberTable, memberTableModel, "downloadIncomplete", re.getLayer(), re);
    668             memberTable.getModel().addTableModelListener(downloadIncompleteMembersAction);
    669             add(downloadIncompleteMembersAction);
    670 
    671             // -- download selected action
    672             DownloadSelectedIncompleteMembersAction downloadSelectedIncompleteMembersAction = new DownloadSelectedIncompleteMembersAction(
    673                     memberTable, memberTableModel, null, re.getLayer(), re);
    674             memberTable.getModel().addTableModelListener(downloadSelectedIncompleteMembersAction);
    675             memberTable.getSelectionModel().addListSelectionListener(downloadSelectedIncompleteMembersAction);
    676             add(downloadSelectedIncompleteMembersAction);
    677 
    678             InputMap inputMap = memberTable.getInputMap(MemberTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    679             inputMap.put((KeyStroke) removeSelectedAction.getValue(AbstractAction.ACCELERATOR_KEY), "removeSelected");
    680             inputMap.put((KeyStroke) moveUpAction.getValue(AbstractAction.ACCELERATOR_KEY), "moveUp");
    681             inputMap.put((KeyStroke) moveDownAction.getValue(AbstractAction.ACCELERATOR_KEY), "moveDown");
    682             inputMap.put((KeyStroke) downloadIncompleteMembersAction.getValue(AbstractAction.ACCELERATOR_KEY), "downloadIncomplete");
     621            List<IRelationEditorActionGroup> groups = new ArrayList<>();
     622            // Move
     623            groups.add(buildNativeGroup(10,
     624                        new MoveUpAction(editorAccess, "moveUp"),
     625                        new MoveDownAction(editorAccess, "moveDown")
     626                        ));
     627            // Edit
     628            groups.add(buildNativeGroup(20,
     629                        new EditAction(editorAccess),
     630                        new RemoveAction(editorAccess, "removeSelected")
     631                        ));
     632            // Sort
     633            groups.add(buildNativeGroup(30,
     634                        new SortAction(editorAccess),
     635                        new SortBelowAction(editorAccess)
     636                        ));
     637            // Reverse
     638            groups.add(buildNativeGroup(40,
     639                        new ReverseAction(editorAccess)
     640                        ));
     641            // Download
     642            groups.add(buildNativeGroup(50,
     643                        new DownloadIncompleteMembersAction(editorAccess, "downloadIncomplete"),
     644                        new DownloadSelectedIncompleteMembersAction(editorAccess)
     645                        ));
     646            groups.addAll(RelationEditorHooks.getMemberActions());
     647           
     648            IRelationEditorActionGroup.fillToolbar(this, groups, editorAccess);
     649           
     650
     651            InputMap inputMap = editorAccess.getMemberTable().getInputMap(MemberTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
     652            inputMap.put((KeyStroke) new RemoveAction(editorAccess, "removeSelected").getValue(AbstractAction.ACCELERATOR_KEY), "removeSelected");
     653            inputMap.put((KeyStroke) new MoveUpAction(editorAccess, "moveUp").getValue(AbstractAction.ACCELERATOR_KEY), "moveUp");
     654            inputMap.put((KeyStroke) new MoveDownAction(editorAccess, "moveDown").getValue(AbstractAction.ACCELERATOR_KEY), "moveDown");
     655            inputMap.put((KeyStroke) new DownloadIncompleteMembersAction(
     656                        editorAccess, "downloadIncomplete").getValue(AbstractAction.ACCELERATOR_KEY), "downloadIncomplete");
    683657        }
    684658    }
     
    693667     * @return control buttons panel for selection/members
    694668     */
    695     protected static JToolBar buildSelectionControlButtonToolbar(MemberTable memberTable,
    696             MemberTableModel memberTableModel, SelectionTableModel selectionTableModel, IRelationEditor re) {
     669    protected static JToolBar buildSelectionControlButtonToolbar(IRelationEditorActionAccess editorAccess) {
    697670        JToolBar tb = new JToolBar(JToolBar.VERTICAL);
    698671        tb.setFloatable(false);
    699672
    700         // -- add at start action
    701         AddSelectedAtStartAction addSelectionAction = new AddSelectedAtStartAction(
    702                 memberTableModel, selectionTableModel, re);
    703         selectionTableModel.addTableModelListener(addSelectionAction);
    704         tb.add(addSelectionAction);
    705 
    706         // -- add before selected action
    707         AddSelectedBeforeSelection addSelectedBeforeSelectionAction = new AddSelectedBeforeSelection(
    708                 memberTableModel, selectionTableModel, re);
    709         selectionTableModel.addTableModelListener(addSelectedBeforeSelectionAction);
    710         memberTableModel.getSelectionModel().addListSelectionListener(addSelectedBeforeSelectionAction);
    711         tb.add(addSelectedBeforeSelectionAction);
    712 
    713         // -- add after selected action
    714         AddSelectedAfterSelection addSelectedAfterSelectionAction = new AddSelectedAfterSelection(
    715                 memberTableModel, selectionTableModel, re);
    716         selectionTableModel.addTableModelListener(addSelectedAfterSelectionAction);
    717         memberTableModel.getSelectionModel().addListSelectionListener(addSelectedAfterSelectionAction);
    718         tb.add(addSelectedAfterSelectionAction);
    719 
    720         // -- add at end action
    721         AddSelectedAtEndAction addSelectedAtEndAction = new AddSelectedAtEndAction(
    722                 memberTableModel, selectionTableModel, re);
    723         selectionTableModel.addTableModelListener(addSelectedAtEndAction);
    724         tb.add(addSelectedAtEndAction);
    725 
    726         tb.addSeparator();
    727 
    728         // -- select members action
    729         SelectedMembersForSelectionAction selectMembersForSelectionAction = new SelectedMembersForSelectionAction(
    730                 memberTableModel, selectionTableModel, re.getLayer());
    731         selectionTableModel.addTableModelListener(selectMembersForSelectionAction);
    732         memberTableModel.addTableModelListener(selectMembersForSelectionAction);
    733         tb.add(selectMembersForSelectionAction);
    734 
    735         // -- select action
    736         SelectPrimitivesForSelectedMembersAction selectAction = new SelectPrimitivesForSelectedMembersAction(
    737                 memberTable, memberTableModel, re.getLayer());
    738         memberTable.getSelectionModel().addListSelectionListener(selectAction);
    739         tb.add(selectAction);
    740 
    741         tb.addSeparator();
    742 
    743         // -- remove selected action
    744         RemoveSelectedAction removeSelectedAction = new RemoveSelectedAction(memberTableModel, selectionTableModel, re.getLayer());
    745         selectionTableModel.addTableModelListener(removeSelectedAction);
    746         tb.add(removeSelectedAction);
    747 
     673                List<IRelationEditorActionGroup> groups = new ArrayList<>();
     674                groups.add(buildNativeGroup(10,
     675                                new AddSelectedAtStartAction(editorAccess),
     676                                new AddSelectedBeforeSelection(editorAccess),
     677                                new AddSelectedAfterSelection(editorAccess),
     678                                new AddSelectedAtEndAction(editorAccess)
     679                                ));
     680                groups.add(buildNativeGroup(20,
     681                                new SelectedMembersForSelectionAction(editorAccess),
     682                                new SelectPrimitivesForSelectedMembersAction(editorAccess)
     683                                ));
     684                groups.add(buildNativeGroup(30,
     685                                new RemoveSelectedAction(editorAccess)
     686                                ));
     687                groups.addAll(RelationEditorHooks.getSelectActions());
     688       
     689        IRelationEditorActionGroup.fillToolbar(tb, groups, editorAccess);
    748690        return tb;
     691    }
     692   
     693    private static IRelationEditorActionGroup buildNativeGroup(int order, AbstractRelationEditorAction... actions) {
     694        return new IRelationEditorActionGroup() {
     695                @Override
     696                public int order() {
     697                        return order;
     698                }
     699               
     700                        @Override
     701                        public List<AbstractRelationEditorAction> getActions(IRelationEditorActionAccess editorAccess) {
     702                                return Arrays.asList(actions);
     703                        }
     704                };
    749705    }
    750706
     
    765721        Clipboard clipboard = ClipboardUtils.getClipboard();
    766722        if (visible) {
    767             leftButtonToolbar.sortBelowButton.setVisible(ExpertToggleAction.isExpert());
    768723            RelationDialogManager.getRelationDialogManager().positionOnScreen(this);
    769724            if (windowMenuItem == null) {
     
    806761                tr("Focus Relation Editor with relation ''{0}'' in layer ''{1}''", name, layerName),
    807762                null, false, false) {
    808             @Override
     763                        private static final long serialVersionUID = 1L;
     764
     765                        @Override
    809766            public void actionPerformed(ActionEvent e) {
    810767                ((RelationEditor) getValue("relationEditor")).setVisible(true);
     
    988945        public void mouseClicked(MouseEvent e) {
    989946            if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2) {
    990                 new EditAction(memberTable, memberTableModel, getLayer()).actionPerformed(null);
    991             }
    992         }
     947                new EditAction(new RelationEditorActionAccess()).actionPerformed(null);
     948            }
     949        }
     950    }
     951   
     952    private class RelationEditorActionAccess implements IRelationEditorActionAccess {
     953
     954                @Override
     955                public MemberTable getMemberTable() {
     956                        return memberTable;
     957                }
     958
     959                @Override
     960                public MemberTableModel getMemberTableModel() {
     961                        return memberTableModel;
     962                }
     963
     964                @Override
     965                public SelectionTable getSelectionTable() {
     966                        return selectionTable;
     967                }
     968
     969                @Override
     970                public SelectionTableModel getSelectionTableModel() {
     971                        return selectionTableModel;
     972                }
     973
     974                @Override
     975                public IRelationEditor getEditor() {
     976                        return GenericRelationEditor.this;
     977                }
     978
     979                @Override
     980                public TagEditorModel getTagModel() {
     981                        return tagEditorPanel.getModel();
     982                }
     983
     984                @Override
     985                public AutoCompletingTextField getTextFieldRole() {
     986                        return tfRole;
     987                }
     988       
    993989    }
    994990}
Note: See TracChangeset for help on using the changeset viewer.