source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/relation/actions/RefreshAction.java@ 14690

Last change on this file since 14690 was 14690, checked in by simon04, 5 years ago

see #17202 - Use Shortcut#setTooltip to fix deprecations

File size: 5.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.dialogs.relation.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.awt.event.KeyEvent;
8
9import javax.swing.JComponent;
10import javax.swing.JOptionPane;
11import javax.swing.JRootPane;
12
13import org.openstreetmap.josm.data.UndoRedoHandler;
14import org.openstreetmap.josm.data.UndoRedoHandler.CommandQueueListener;
15import org.openstreetmap.josm.data.osm.Relation;
16import org.openstreetmap.josm.gui.HelpAwareOptionPane;
17import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
18import org.openstreetmap.josm.gui.MainApplication;
19import org.openstreetmap.josm.gui.dialogs.relation.IRelationEditor;
20import org.openstreetmap.josm.tools.ImageProvider;
21import org.openstreetmap.josm.tools.Shortcut;
22
23/**
24 * Refresh relation.
25 * @since 9657
26 */
27public 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 relation = editorAccess.getEditor().getRelation();
71 Relation snapshot = editorAccess.getEditor().getRelationSnapshot();
72 setEnabled(snapshot != null && (
73 !relation.hasEqualTechnicalAttributes(snapshot) ||
74 !relation.hasEqualSemanticAttributes(snapshot)
75 ));
76 }
77
78 protected int confirmDiscardDirtyData() {
79 ButtonSpec[] options = new ButtonSpec[] {
80 new ButtonSpec(
81 tr("Yes, discard changes and reload"),
82 new ImageProvider("ok"),
83 tr("Click to discard the changes and reload data from layer"),
84 null /* no specific help topic */
85 ),
86 new ButtonSpec(
87 tr("Cancel, continue editing"),
88 new ImageProvider("cancel"),
89 tr("Click to return to the relation editor and to resume relation editing"),
90 null /* no specific help topic */
91 )
92 };
93
94 return HelpAwareOptionPane.showOptionDialog(
95 MainApplication.getMainFrame(),
96 tr("<html>You have unsaved changes in this editor window.<br>"+
97 "<br>Do you want to discard these changes and reload data from layer?</html>"),
98 tr("Unsaved changes"),
99 JOptionPane.WARNING_MESSAGE,
100 null,
101 options,
102 options[1], // Cancel is default
103 "/Dialog/RelationEditor#Reload"
104 );
105 }
106
107 protected int confirmCloseDeletedRelation() {
108 ButtonSpec[] options = new ButtonSpec[] {
109 new ButtonSpec(
110 tr("Yes"),
111 new ImageProvider("ok"),
112 tr("Click to close window"),
113 null /* no specific help topic */
114 ),
115 new ButtonSpec(
116 tr("No, continue editing"),
117 new ImageProvider("cancel"),
118 tr("Click to return to the relation editor and to resume relation editing"),
119 null /* no specific help topic */
120 )
121 };
122
123 return HelpAwareOptionPane.showOptionDialog(
124 MainApplication.getMainFrame(),
125 tr("<html>Relation has been deleted outside editor.<br><br>Do you want to close this window?</html>"),
126 tr("Deleted relation"),
127 JOptionPane.WARNING_MESSAGE,
128 null,
129 options,
130 options[0], // Yes is default
131 "/Dialog/RelationEditor#Reload"
132 );
133 }
134
135 @Override
136 public void commandChanged(int queueSize, int redoSize) {
137 updateEnabledState();
138 }
139
140 /**
141 * Allow GC to do its work
142 */
143 public void destroy() {
144 UndoRedoHandler.getInstance().removeCommandQueueListener(this);
145 }
146}
Note: See TracBrowser for help on using the repository browser.