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 |
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.data.UndoRedoHandler.CommandQueueListener;
|
---|
14 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
15 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
16 | import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
|
---|
17 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
18 | import org.openstreetmap.josm.gui.dialogs.relation.IRelationEditor;
|
---|
19 | import org.openstreetmap.josm.gui.dialogs.relation.MemberTable;
|
---|
20 | import org.openstreetmap.josm.gui.dialogs.relation.MemberTableModel;
|
---|
21 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
22 | import org.openstreetmap.josm.gui.tagging.TagEditorModel;
|
---|
23 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
24 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Refresh relation.
|
---|
28 | * @since 9657
|
---|
29 | */
|
---|
30 | public class RefreshAction extends SavingAction implements CommandQueueListener {
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Constructs a new {@code RefreshAction}.
|
---|
34 | * @param memberTable member table
|
---|
35 | * @param memberTableModel member table model
|
---|
36 | * @param layer OSM data layer
|
---|
37 | * @param editor relation editor
|
---|
38 | * @param tagModel tag editor model
|
---|
39 | */
|
---|
40 | public RefreshAction(MemberTable memberTable, MemberTableModel memberTableModel, TagEditorModel tagModel, OsmDataLayer layer,
|
---|
41 | IRelationEditor editor) {
|
---|
42 | super(memberTable, memberTableModel, tagModel, layer, editor, null);
|
---|
43 | // CHECKSTYLE.OFF: LineLength
|
---|
44 | Shortcut sc = Shortcut.registerShortcut("relationeditor:refresh", tr("Relation Editor: Refresh"), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE);
|
---|
45 | // CHECKSTYLE.ON: LineLength
|
---|
46 | putValue(SHORT_DESCRIPTION, Main.platform.makeTooltip(tr("Refresh relation from data layer"), sc));
|
---|
47 | putValue(SMALL_ICON, ImageProvider.get("dialogs/refresh"));
|
---|
48 | putValue(NAME, tr("Refresh"));
|
---|
49 | if (editor instanceof JComponent) {
|
---|
50 | ((JComponent) editor).getRootPane().getActionMap().put("refresh", this);
|
---|
51 | ((JComponent) editor).getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(sc.getKeyStroke(), "refresh");
|
---|
52 | }
|
---|
53 | MainApplication.undoRedo.addCommandQueueListener(this);
|
---|
54 | updateEnabledState();
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | public void actionPerformed(ActionEvent e) {
|
---|
59 | Relation relation = editor.getRelation();
|
---|
60 | if (relation == null)
|
---|
61 | return;
|
---|
62 | if (relation.isDeleted()) {
|
---|
63 | if (confirmCloseDeletedRelation() == 0) {
|
---|
64 | hideEditor();
|
---|
65 | }
|
---|
66 | return;
|
---|
67 | }
|
---|
68 | if (isEditorDirty() && confirmDiscardDirtyData() != 0)
|
---|
69 | return;
|
---|
70 | editor.reloadDataFromRelation();
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | public void updateEnabledState() {
|
---|
75 | Relation relation = editor.getRelation();
|
---|
76 | Relation snapshot = editor.getRelationSnapshot();
|
---|
77 | setEnabled(snapshot != null && (
|
---|
78 | !relation.hasEqualTechnicalAttributes(snapshot) ||
|
---|
79 | !relation.hasEqualSemanticAttributes(snapshot)
|
---|
80 | ));
|
---|
81 | }
|
---|
82 |
|
---|
83 | protected int confirmDiscardDirtyData() {
|
---|
84 | ButtonSpec[] options = new ButtonSpec[] {
|
---|
85 | new ButtonSpec(
|
---|
86 | tr("Yes, discard changes and reload"),
|
---|
87 | ImageProvider.get("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 | ImageProvider.get("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 | 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 | }
|
---|