| 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 | setEnabled(getEditor().isDirtyRelation());
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | protected int confirmDiscardDirtyData() {
|
|---|
| 74 | ButtonSpec[] options = {
|
|---|
| 75 | new ButtonSpec(
|
|---|
| 76 | tr("Yes, discard changes and reload"),
|
|---|
| 77 | new ImageProvider("ok"),
|
|---|
| 78 | tr("Click to discard the changes and reload data from layer"),
|
|---|
| 79 | null /* no specific help topic */
|
|---|
| 80 | ),
|
|---|
| 81 | new ButtonSpec(
|
|---|
| 82 | tr("Cancel, continue editing"),
|
|---|
| 83 | new ImageProvider("cancel"),
|
|---|
| 84 | tr("Click to return to the relation editor and to resume relation editing"),
|
|---|
| 85 | null /* no specific help topic */
|
|---|
| 86 | )
|
|---|
| 87 | };
|
|---|
| 88 |
|
|---|
| 89 | return HelpAwareOptionPane.showOptionDialog(
|
|---|
| 90 | MainApplication.getMainFrame(),
|
|---|
| 91 | tr("<html>You have unsaved changes in this editor window.<br>"+
|
|---|
| 92 | "<br>Do you want to discard these changes and reload data from layer?</html>"),
|
|---|
| 93 | tr("Unsaved changes"),
|
|---|
| 94 | JOptionPane.WARNING_MESSAGE,
|
|---|
| 95 | null,
|
|---|
| 96 | options,
|
|---|
| 97 | options[1], // Cancel is default
|
|---|
| 98 | "/Dialog/RelationEditor#Reload"
|
|---|
| 99 | );
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | protected int confirmCloseDeletedRelation() {
|
|---|
| 103 | ButtonSpec[] options = {
|
|---|
| 104 | new ButtonSpec(
|
|---|
| 105 | tr("Yes"),
|
|---|
| 106 | new ImageProvider("ok"),
|
|---|
| 107 | tr("Click to close window"),
|
|---|
| 108 | null /* no specific help topic */
|
|---|
| 109 | ),
|
|---|
| 110 | new ButtonSpec(
|
|---|
| 111 | tr("No, continue editing"),
|
|---|
| 112 | new ImageProvider("cancel"),
|
|---|
| 113 | tr("Click to return to the relation editor and to resume relation editing"),
|
|---|
| 114 | null /* no specific help topic */
|
|---|
| 115 | )
|
|---|
| 116 | };
|
|---|
| 117 |
|
|---|
| 118 | return HelpAwareOptionPane.showOptionDialog(
|
|---|
| 119 | MainApplication.getMainFrame(),
|
|---|
| 120 | tr("<html>Relation has been deleted outside editor.<br><br>Do you want to close this window?</html>"),
|
|---|
| 121 | tr("Deleted relation"),
|
|---|
| 122 | JOptionPane.WARNING_MESSAGE,
|
|---|
| 123 | null,
|
|---|
| 124 | options,
|
|---|
| 125 | options[0], // Yes is default
|
|---|
| 126 | "/Dialog/RelationEditor#Reload"
|
|---|
| 127 | );
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | @Override
|
|---|
| 131 | public void commandChanged(int queueSize, int redoSize) {
|
|---|
| 132 | updateEnabledState();
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | /**
|
|---|
| 136 | * Allow GC to do its work
|
|---|
| 137 | */
|
|---|
| 138 | public void destroy() {
|
|---|
| 139 | UndoRedoHandler.getInstance().removeCommandQueueListener(this);
|
|---|
| 140 | }
|
|---|
| 141 | }
|
|---|