1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.relation.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.awt.event.KeyEvent;
|
---|
8 |
|
---|
9 | import javax.swing.JComponent;
|
---|
10 | import javax.swing.JOptionPane;
|
---|
11 | import javax.swing.JRootPane;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
---|
14 | import org.openstreetmap.josm.data.UndoRedoHandler.CommandQueueListener;
|
---|
15 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
16 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
17 | import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
|
---|
18 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
19 | import org.openstreetmap.josm.gui.dialogs.relation.IRelationEditor;
|
---|
20 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
21 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Refresh relation.
|
---|
25 | * @since 9657
|
---|
26 | */
|
---|
27 | public class RefreshAction extends SavingAction implements CommandQueueListener {
|
---|
28 | private static final long serialVersionUID = 1L;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Constructs a new {@code RefreshAction}.
|
---|
32 | * @param editorAccess An interface to access the relation editor contents.
|
---|
33 | */
|
---|
34 | public RefreshAction(IRelationEditorActionAccess editorAccess) {
|
---|
35 | super(editorAccess);
|
---|
36 | // CHECKSTYLE.OFF: LineLength
|
---|
37 | Shortcut sc = Shortcut.registerShortcut("relationeditor:refresh", tr("Relation Editor: Refresh"), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE);
|
---|
38 | // CHECKSTYLE.ON: LineLength
|
---|
39 | sc.setTooltip(this, tr("Refresh relation from data layer"));
|
---|
40 | new ImageProvider("dialogs/refresh").getResource().attachImageIcon(this, true);
|
---|
41 | putValue(NAME, tr("Refresh"));
|
---|
42 | IRelationEditor editor = editorAccess.getEditor();
|
---|
43 | if (editor instanceof JComponent) {
|
---|
44 | JRootPane rootPane = ((JComponent) editor).getRootPane();
|
---|
45 | rootPane.getActionMap().put("refresh", this);
|
---|
46 | rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(sc.getKeyStroke(), "refresh");
|
---|
47 | }
|
---|
48 | UndoRedoHandler.getInstance().addCommandQueueListener(this);
|
---|
49 | updateEnabledState();
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Override
|
---|
53 | public void actionPerformed(ActionEvent e) {
|
---|
54 | Relation relation = editorAccess.getEditor().getRelation();
|
---|
55 | if (relation == null)
|
---|
56 | return;
|
---|
57 | if (relation.isDeleted()) {
|
---|
58 | if (confirmCloseDeletedRelation() == 0) {
|
---|
59 | hideEditor();
|
---|
60 | }
|
---|
61 | return;
|
---|
62 | }
|
---|
63 | if (isEditorDirty() && confirmDiscardDirtyData() != 0)
|
---|
64 | return;
|
---|
65 | editorAccess.getEditor().reloadDataFromRelation();
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | public void updateEnabledState() {
|
---|
70 | Relation snapshot = getEditor().getRelationSnapshot();
|
---|
71 | Relation relation = getEditor().getRelation();
|
---|
72 | if (relation != null && relation.getDataSet() == null)
|
---|
73 | relation = null; // see #19915
|
---|
74 | if (relation != null && snapshot != null && snapshot.getDataSet() == null) {
|
---|
75 | // relation was changed outside of the editor
|
---|
76 | // either it was modified or deleted or changed by an undo
|
---|
77 | setEnabled(!snapshot.hasEqualSemanticAttributes(relation, false /* don't ignore uninteresting keys */));
|
---|
78 | return;
|
---|
79 | }
|
---|
80 | setEnabled(false);
|
---|
81 | }
|
---|
82 |
|
---|
83 | protected int confirmDiscardDirtyData() {
|
---|
84 | ButtonSpec[] options = {
|
---|
85 | new ButtonSpec(
|
---|
86 | tr("Yes, discard changes and reload"),
|
---|
87 | new ImageProvider("ok"),
|
---|
88 | tr("Click to discard the changes and reload data from layer"),
|
---|
89 | null /* no specific help topic */
|
---|
90 | ),
|
---|
91 | new ButtonSpec(
|
---|
92 | tr("Cancel, continue editing"),
|
---|
93 | new ImageProvider("cancel"),
|
---|
94 | tr("Click to return to the relation editor and to resume relation editing"),
|
---|
95 | null /* no specific help topic */
|
---|
96 | )
|
---|
97 | };
|
---|
98 |
|
---|
99 | return HelpAwareOptionPane.showOptionDialog(
|
---|
100 | MainApplication.getMainFrame(),
|
---|
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 = {
|
---|
114 | new ButtonSpec(
|
---|
115 | tr("Yes"),
|
---|
116 | new ImageProvider("ok"),
|
---|
117 | tr("Click to close window"),
|
---|
118 | null /* no specific help topic */
|
---|
119 | ),
|
---|
120 | new ButtonSpec(
|
---|
121 | tr("No, continue editing"),
|
---|
122 | new ImageProvider("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 | MainApplication.getMainFrame(),
|
---|
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 |
|
---|
145 | /**
|
---|
146 | * Allow GC to do its work
|
---|
147 | */
|
---|
148 | public void destroy() {
|
---|
149 | UndoRedoHandler.getInstance().removeCommandQueueListener(this);
|
---|
150 | }
|
---|
151 | }
|
---|