001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.dialogs.relation.actions;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.event.ActionEvent;
007import java.awt.event.KeyEvent;
008
009import javax.swing.JComponent;
010import javax.swing.JOptionPane;
011
012import org.openstreetmap.josm.Main;
013import org.openstreetmap.josm.data.UndoRedoHandler.CommandQueueListener;
014import org.openstreetmap.josm.data.osm.Relation;
015import org.openstreetmap.josm.gui.HelpAwareOptionPane;
016import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
017import org.openstreetmap.josm.gui.MainApplication;
018import org.openstreetmap.josm.gui.dialogs.relation.IRelationEditor;
019import org.openstreetmap.josm.gui.dialogs.relation.MemberTable;
020import org.openstreetmap.josm.gui.dialogs.relation.MemberTableModel;
021import org.openstreetmap.josm.gui.layer.OsmDataLayer;
022import org.openstreetmap.josm.gui.tagging.TagEditorModel;
023import org.openstreetmap.josm.tools.ImageProvider;
024import org.openstreetmap.josm.tools.Shortcut;
025
026/**
027 * Refresh relation.
028 * @since 9657
029 */
030public class RefreshAction extends SavingAction implements CommandQueueListener {
031
032    /**
033     * Constructs a new {@code RefreshAction}.
034     * @param memberTable member table
035     * @param memberTableModel member table model
036     * @param layer OSM data layer
037     * @param editor relation editor
038     * @param tagModel tag editor model
039     */
040    public RefreshAction(MemberTable memberTable, MemberTableModel memberTableModel, TagEditorModel tagModel, OsmDataLayer layer,
041            IRelationEditor editor) {
042        super(memberTable, memberTableModel, tagModel, layer, editor, null);
043        // CHECKSTYLE.OFF: LineLength
044        Shortcut sc = Shortcut.registerShortcut("relationeditor:refresh", tr("Relation Editor: Refresh"), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE);
045        // CHECKSTYLE.ON: LineLength
046        putValue(SHORT_DESCRIPTION, Main.platform.makeTooltip(tr("Refresh relation from data layer"), sc));
047        new ImageProvider("dialogs/refresh").getResource().attachImageIcon(this, true);
048        putValue(NAME, tr("Refresh"));
049        if (editor instanceof JComponent) {
050            ((JComponent) editor).getRootPane().getActionMap().put("refresh", this);
051            ((JComponent) editor).getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(sc.getKeyStroke(), "refresh");
052        }
053        MainApplication.undoRedo.addCommandQueueListener(this);
054        updateEnabledState();
055    }
056
057    @Override
058    public void actionPerformed(ActionEvent e) {
059        Relation relation = editor.getRelation();
060        if (relation == null)
061            return;
062        if (relation.isDeleted()) {
063            if (confirmCloseDeletedRelation() == 0) {
064                hideEditor();
065            }
066            return;
067        }
068        if (isEditorDirty() && confirmDiscardDirtyData() != 0)
069            return;
070        editor.reloadDataFromRelation();
071    }
072
073    @Override
074    public void updateEnabledState() {
075        Relation relation = editor.getRelation();
076        Relation snapshot = editor.getRelationSnapshot();
077        setEnabled(snapshot != null && (
078            !relation.hasEqualTechnicalAttributes(snapshot) ||
079            !relation.hasEqualSemanticAttributes(snapshot)
080        ));
081    }
082
083    protected int confirmDiscardDirtyData() {
084        ButtonSpec[] options = new ButtonSpec[] {
085                new ButtonSpec(
086                        tr("Yes, discard changes and reload"),
087                        ImageProvider.get("ok"),
088                        tr("Click to discard the changes and reload data from layer"),
089                        null /* no specific help topic */
090                ),
091                new ButtonSpec(
092                        tr("Cancel, continue editing"),
093                        ImageProvider.get("cancel"),
094                        tr("Click to return to the relation editor and to resume relation editing"),
095                        null /* no specific help topic */
096                )
097        };
098
099        return HelpAwareOptionPane.showOptionDialog(
100                Main.parent,
101                tr("<html>You have unsaved changes in this editor window.<br>"+
102                   "<br>Do you want to discard these changes and reload data from layer?</html>"),
103                        tr("Unsaved changes"),
104                        JOptionPane.WARNING_MESSAGE,
105                        null,
106                        options,
107                        options[1], // Cancel is default
108                        "/Dialog/RelationEditor#Reload"
109        );
110    }
111
112    protected int confirmCloseDeletedRelation() {
113        ButtonSpec[] options = new ButtonSpec[] {
114                new ButtonSpec(
115                        tr("Yes"),
116                        ImageProvider.get("ok"),
117                        tr("Click to close window"),
118                        null /* no specific help topic */
119                ),
120                new ButtonSpec(
121                        tr("No, continue editing"),
122                        ImageProvider.get("cancel"),
123                        tr("Click to return to the relation editor and to resume relation editing"),
124                        null /* no specific help topic */
125                )
126        };
127
128        return HelpAwareOptionPane.showOptionDialog(
129                Main.parent,
130                tr("<html>Relation has been deleted outside editor.<br><br>Do you want to close this window?</html>"),
131                        tr("Deleted relation"),
132                        JOptionPane.WARNING_MESSAGE,
133                        null,
134                        options,
135                        options[0], // Yes is default
136                        "/Dialog/RelationEditor#Reload"
137        );
138    }
139
140    @Override
141    public void commandChanged(int queueSize, int redoSize) {
142        updateEnabledState();
143    }
144}