1 19 package org.netbeans.modules.ruby.rubyproject.execution; 20 21 import java.awt.event.ActionEvent ; 22 import java.io.IOException ; 23 24 import javax.swing.AbstractAction ; 25 import javax.swing.Action ; 26 import javax.swing.ImageIcon ; 27 28 import org.openide.ErrorManager; 29 import org.openide.cookies.SaveCookie; 30 import org.openide.filesystems.FileAttributeEvent; 31 import org.openide.filesystems.FileChangeListener; 32 import org.openide.filesystems.FileEvent; 33 import org.openide.filesystems.FileObject; 34 import org.openide.filesystems.FileRenameEvent; 35 import org.openide.filesystems.FileUtil; 36 import org.openide.loaders.DataObject; 37 import org.openide.loaders.DataObjectNotFoundException; 38 import org.openide.util.NbBundle; 39 40 41 49 final class RerunAction extends AbstractAction implements FileChangeListener { 50 private ExecutionService prototype; 51 private FileObject fileObject; 52 53 public RerunAction(ExecutionService prototype, FileObject fileObject) { 54 this.prototype = prototype; 55 setEnabled(false); this.fileObject = fileObject; 57 58 if (fileObject != null) { 59 fileObject.addFileChangeListener(FileUtil.weakFileChangeListener(this, fileObject)); 60 } 61 } 62 63 @Override 64 public Object getValue(String key) { 65 if (key.equals(Action.SMALL_ICON)) { 66 return new ImageIcon (ExecutionService.class.getResource( 67 "/org/netbeans/modules/ruby/rubyproject/execution/rerun.png")); } else if (key.equals(Action.SHORT_DESCRIPTION)) { 69 return NbBundle.getMessage(ExecutionService.class, "Rerun"); 70 } else { 71 return super.getValue(key); 72 } 73 } 74 75 public void actionPerformed(ActionEvent e) { 76 setEnabled(false); 77 78 if (fileObject != null) { 80 try { 82 DataObject dobj = DataObject.find(fileObject); 83 84 if (dobj != null) { 85 SaveCookie saveCookie = dobj.getCookie(SaveCookie.class); 86 87 if (saveCookie != null) { 88 saveCookie.save(); 89 } 90 } 91 } catch (DataObjectNotFoundException donfe) { 92 ErrorManager.getDefault().notify(donfe); 93 } catch (IOException ioe) { 94 ErrorManager.getDefault().notify(ioe); 95 } 96 } 97 98 prototype.rerun(); 99 } 100 101 public void fileDeleted(FileEvent fe) { 102 firePropertyChange("enabled", null, false); } 104 105 public void fileFolderCreated(FileEvent fe) { 106 } 107 108 public void fileDataCreated(FileEvent fe) { 109 } 110 111 public void fileChanged(FileEvent fe) { 112 } 113 114 public void fileRenamed(FileRenameEvent fe) { 115 } 116 117 public void fileAttributeChanged(FileAttributeEvent fe) { 118 } 119 120 public boolean isEnabled() { 121 return super.isEnabled() && ((fileObject == null) || fileObject.isValid()); 123 } 124 } 125 | Popular Tags |