001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.dialogs.relation.actions; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.event.ActionEvent; 007import java.awt.event.KeyEvent; 008 009import javax.swing.JComponent; 010import javax.swing.JOptionPane; 011import javax.swing.JRootPane; 012 013import org.openstreetmap.josm.data.UndoRedoHandler; 014import org.openstreetmap.josm.data.UndoRedoHandler.CommandQueueListener; 015import org.openstreetmap.josm.data.osm.Relation; 016import org.openstreetmap.josm.gui.HelpAwareOptionPane; 017import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec; 018import org.openstreetmap.josm.gui.MainApplication; 019import org.openstreetmap.josm.gui.dialogs.relation.IRelationEditor; 020import org.openstreetmap.josm.tools.ImageProvider; 021import org.openstreetmap.josm.tools.Shortcut; 022 023/** 024 * Refresh relation. 025 * @since 9657 026 */ 027public class RefreshAction extends SavingAction implements CommandQueueListener { 028 private static final long serialVersionUID = 1L; 029 030 /** 031 * Constructs a new {@code RefreshAction}. 032 * @param editorAccess An interface to access the relation editor contents. 033 */ 034 public RefreshAction(IRelationEditorActionAccess editorAccess) { 035 super(editorAccess); 036 // CHECKSTYLE.OFF: LineLength 037 Shortcut sc = Shortcut.registerShortcut("relationeditor:refresh", tr("Relation Editor: Refresh"), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE); 038 // CHECKSTYLE.ON: LineLength 039 sc.setTooltip(this, tr("Refresh relation from data layer")); 040 new ImageProvider("dialogs/refresh").getResource().attachImageIcon(this, true); 041 putValue(NAME, tr("Refresh")); 042 IRelationEditor editor = editorAccess.getEditor(); 043 if (editor instanceof JComponent) { 044 JRootPane rootPane = ((JComponent) editor).getRootPane(); 045 rootPane.getActionMap().put("refresh", this); 046 rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(sc.getKeyStroke(), "refresh"); 047 } 048 UndoRedoHandler.getInstance().addCommandQueueListener(this); 049 updateEnabledState(); 050 } 051 052 @Override 053 public void actionPerformed(ActionEvent e) { 054 Relation relation = editorAccess.getEditor().getRelation(); 055 if (relation == null) 056 return; 057 if (relation.isDeleted()) { 058 if (confirmCloseDeletedRelation() == 0) { 059 hideEditor(); 060 } 061 return; 062 } 063 if (isEditorDirty() && confirmDiscardDirtyData() != 0) 064 return; 065 editorAccess.getEditor().reloadDataFromRelation(); 066 } 067 068 @Override 069 public void updateEnabledState() { 070 setEnabled(getEditor().isDirtyRelation()); 071 } 072 073 protected int confirmDiscardDirtyData() { 074 ButtonSpec[] options = { 075 new ButtonSpec( 076 tr("Yes, discard changes and reload"), 077 new ImageProvider("ok"), 078 tr("Click to discard the changes and reload data from layer"), 079 null /* no specific help topic */ 080 ), 081 new ButtonSpec( 082 tr("Cancel, continue editing"), 083 new ImageProvider("cancel"), 084 tr("Click to return to the relation editor and to resume relation editing"), 085 null /* no specific help topic */ 086 ) 087 }; 088 089 return HelpAwareOptionPane.showOptionDialog( 090 MainApplication.getMainFrame(), 091 tr("<html>You have unsaved changes in this editor window.<br>"+ 092 "<br>Do you want to discard these changes and reload data from layer?</html>"), 093 tr("Unsaved changes"), 094 JOptionPane.WARNING_MESSAGE, 095 null, 096 options, 097 options[1], // Cancel is default 098 "/Dialog/RelationEditor#Reload" 099 ); 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}