1 19 20 package org.netbeans.modules.versioning.system.cvss.ui.syncview; 21 22 import org.openide.util.NbBundle; 23 import org.openide.nodes.Node; 24 import org.openide.cookies.EditorCookie; 25 import org.openide.cookies.EditCookie; 26 import org.openide.cookies.OpenCookie; 27 import org.openide.cookies.ViewCookie; 28 import org.openide.filesystems.FileObject; 29 import org.openide.filesystems.FileUtil; 30 import org.openide.loaders.DataObject; 31 import org.openide.loaders.DataObjectNotFoundException; 32 import org.netbeans.modules.versioning.system.cvss.util.Utils; 33 34 import javax.swing.*; 35 import java.awt.event.ActionEvent ; 36 import java.io.File ; 37 38 43 public class OpenInEditorAction extends AbstractAction { 44 45 public OpenInEditorAction() { 46 super(NbBundle.getBundle(OpenInEditorAction.class).getString("CTL_Synchronize_Popup_OpenInEditor")); 47 setEnabled(isActionEnabled()); 48 } 49 50 private boolean isActionEnabled() { 51 File [] files = Utils.getCurrentContext(null).getFiles(); 52 for (File file : files) { 53 if (file.canRead()) return true; 54 } 55 return false; 56 } 57 58 public void actionPerformed(ActionEvent e) { 59 File [] files = Utils.getCurrentContext(null).getFiles(); 60 for (File file : files) { 61 FileObject fo = FileUtil.toFileObject(file); 62 if (fo != null) { 63 try { 64 openDataObjectByCookie(DataObject.find(fo)); 65 } catch (DataObjectNotFoundException ex) { 66 } 68 } 69 } 70 } 71 72 private final boolean openDataObjectByCookie(DataObject dataObject) { 73 Node.Cookie cookie; 74 Class cookieClass; 75 if ((((cookie = dataObject.getCookie(cookieClass = EditorCookie.Observable.class)) != null 76 || (cookie = dataObject.getCookie(cookieClass = EditorCookie.class)) != null)) 77 || (cookie = dataObject.getCookie(cookieClass = OpenCookie.class)) != null 78 || (cookie = dataObject.getCookie(cookieClass = EditCookie.class)) != null 79 || (cookie = dataObject.getCookie(cookieClass = ViewCookie.class)) != null) { 80 return openByCookie(cookie, cookieClass); 81 } 82 return false; 83 } 84 85 private boolean openByCookie(Node.Cookie cookie, Class cookieClass) { 86 if ((cookieClass == EditorCookie.class) 87 || (cookieClass == EditorCookie.Observable.class)) { 88 ((EditorCookie) cookie).open(); 89 } else if (cookieClass == OpenCookie.class) { 90 ((OpenCookie) cookie).open(); 91 } else if (cookieClass == EditCookie.class) { 92 ((EditCookie) cookie).edit(); 93 } else if (cookieClass == ViewCookie.class) { 94 ((ViewCookie) cookie).view(); 95 } else { 96 throw new IllegalArgumentException ("Reopen #58766: " + cookieClass); } 98 return true; 99 } 100 } 101 | Popular Tags |