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.PlatformManager;
|
---|
22 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Refresh relation.
|
---|
26 | * @since 9657
|
---|
27 | */
|
---|
28 | public class RefreshAction extends SavingAction implements CommandQueueListener {
|
---|
29 | private static final long serialVersionUID = 1L;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Constructs a new {@code RefreshAction}.
|
---|
33 | * @param editorAccess An interface to access the relation editor contents.
|
---|
34 | */
|
---|
35 | public RefreshAction(IRelationEditorActionAccess editorAccess) {
|
---|
36 | super(editorAccess);
|
---|
37 | // CHECKSTYLE.OFF: LineLength
|
---|
38 | Shortcut sc = Shortcut.registerShortcut("relationeditor:refresh", tr("Relation Editor: Refresh"), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE);
|
---|
39 | // CHECKSTYLE.ON: LineLength
|
---|
40 | putValue(SHORT_DESCRIPTION, PlatformManager.getPlatform().makeTooltip(tr("Refresh relation from data layer"), sc));
|
---|
41 | new ImageProvider("dialogs/refresh").getResource().attachImageIcon(this, true);
|
---|
42 | putValue(NAME, tr("Refresh"));
|
---|
43 | IRelationEditor editor = editorAccess.getEditor();
|
---|
44 | if (editor instanceof JComponent) {
|
---|
45 | JRootPane rootPane = ((JComponent) editor).getRootPane();
|
---|
46 | rootPane.getActionMap().put("refresh", this);
|
---|
47 | rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(sc.getKeyStroke(), "refresh");
|
---|
48 | }
|
---|
49 | UndoRedoHandler.getInstance().addCommandQueueListener(this);
|
---|
50 | updateEnabledState();
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | public void actionPerformed(ActionEvent e) {
|
---|
55 | Relation relation = editorAccess.getEditor().getRelation();
|
---|
56 | if (relation == null)
|
---|
57 | return;
|
---|
58 | if (relation.isDeleted()) {
|
---|
59 | if (confirmCloseDeletedRelation() == 0) {
|
---|
60 | hideEditor();
|
---|
61 | }
|
---|
62 | return;
|
---|
63 | }
|
---|
64 | if (isEditorDirty() && confirmDiscardDirtyData() != 0)
|
---|
65 | return;
|
---|
66 | editorAccess.getEditor().reloadDataFromRelation();
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | public void updateEnabledState() {
|
---|
71 | Relation relation = editorAccess.getEditor().getRelation();
|
---|
72 | Relation snapshot = editorAccess.getEditor().getRelationSnapshot();
|
---|
73 | setEnabled(snapshot != null && (
|
---|
74 | !relation.hasEqualTechnicalAttributes(snapshot) ||
|
---|
75 | !relation.hasEqualSemanticAttributes(snapshot)
|
---|
76 | ));
|
---|
77 | }
|
---|
78 |
|
---|
79 | protected int confirmDiscardDirtyData() {
|
---|
80 | ButtonSpec[] options = new ButtonSpec[] {
|
---|
81 | new ButtonSpec(
|
---|
82 | tr("Yes, discard changes and reload"),
|
---|
83 | new ImageProvider("ok"),
|
---|
84 | tr("Click to discard the changes and reload data from layer"),
|
---|
85 | null /* no specific help topic */
|
---|
86 | ),
|
---|
87 | new ButtonSpec(
|
---|
88 | tr("Cancel, continue editing"),
|
---|
89 | new ImageProvider("cancel"),
|
---|
90 | tr("Click to return to the relation editor and to resume relation editing"),
|
---|
91 | null /* no specific help topic */
|
---|
92 | )
|
---|
93 | };
|
---|
94 |
|
---|
95 | return HelpAwareOptionPane.showOptionDialog(
|
---|
96 | MainApplication.getMainFrame(),
|
---|
97 | tr("<html>You have unsaved changes in this editor window.<br>"+
|
---|
98 | "<br>Do you want to discard these changes and reload data from layer?</html>"),
|
---|
99 | tr("Unsaved changes"),
|
---|
100 | JOptionPane.WARNING_MESSAGE,
|
---|
101 | null,
|
---|
102 | options,
|
---|
103 | options[1], // Cancel is default
|
---|
104 | "/Dialog/RelationEditor#Reload"
|
---|
105 | );
|
---|
106 | }
|
---|
107 |
|
---|
108 | protected int confirmCloseDeletedRelation() {
|
---|
109 | ButtonSpec[] options = new ButtonSpec[] {
|
---|
110 | new ButtonSpec(
|
---|
111 | tr("Yes"),
|
---|
112 | new ImageProvider("ok"),
|
---|
113 | tr("Click to close window"),
|
---|
114 | null /* no specific help topic */
|
---|
115 | ),
|
---|
116 | new ButtonSpec(
|
---|
117 | tr("No, continue editing"),
|
---|
118 | new ImageProvider("cancel"),
|
---|
119 | tr("Click to return to the relation editor and to resume relation editing"),
|
---|
120 | null /* no specific help topic */
|
---|
121 | )
|
---|
122 | };
|
---|
123 |
|
---|
124 | return HelpAwareOptionPane.showOptionDialog(
|
---|
125 | MainApplication.getMainFrame(),
|
---|
126 | tr("<html>Relation has been deleted outside editor.<br><br>Do you want to close this window?</html>"),
|
---|
127 | tr("Deleted relation"),
|
---|
128 | JOptionPane.WARNING_MESSAGE,
|
---|
129 | null,
|
---|
130 | options,
|
---|
131 | options[0], // Yes is default
|
---|
132 | "/Dialog/RelationEditor#Reload"
|
---|
133 | );
|
---|
134 | }
|
---|
135 |
|
---|
136 | @Override
|
---|
137 | public void commandChanged(int queueSize, int redoSize) {
|
---|
138 | updateEnabledState();
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Allow GC to do its work
|
---|
143 | */
|
---|
144 | public void destroy() {
|
---|
145 | UndoRedoHandler.getInstance().removeCommandQueueListener(this);
|
---|
146 | }
|
---|
147 | }
|
---|