1 19 20 package org.netbeans.modules.openfile; 21 22 import java.io.File ; 23 import javax.swing.JFileChooser ; 24 import org.netbeans.modules.utilities.Manager; 25 import org.openide.filesystems.FileUtil; 26 import org.openide.loaders.DataObject; 27 import org.openide.nodes.Node; 28 import org.openide.util.HelpCtx; 29 import org.openide.util.NbBundle; 30 import org.openide.util.UserCancelException; 31 import org.openide.util.actions.CallableSystemAction; 32 import org.openide.windows.TopComponent; 33 import org.openide.windows.WindowManager; 34 35 42 public class OpenFileAction extends CallableSystemAction { 43 44 public OpenFileAction() { 45 putValue("noIconInMenu", Boolean.TRUE); 46 } 47 48 public String getName() { 49 return NbBundle.getMessage(OpenFileAction.class, "LBL_openFile"); 50 } 51 52 public HelpCtx getHelpCtx() { 53 return new HelpCtx(OpenFileAction.class); 54 } 55 56 protected String iconResource() { 57 return "org/netbeans/modules/openfile/openFile.png"; } 59 60 65 protected JFileChooser prepareFileChooser() { 66 JFileChooser chooser = new FileChooser(); 67 File currDir = findStartingDirectory(); 68 FileUtil.preventFileChooserSymlinkTraversal(chooser, currDir); 69 HelpCtx.setHelpIDString(chooser, getHelpCtx().getHelpID()); 70 71 return chooser; 72 } 73 74 82 public static File [] chooseFilesToOpen(JFileChooser chooser) 83 throws UserCancelException { 84 File [] files; 85 do { 86 int selectedOption = chooser.showOpenDialog( 87 WindowManager.getDefault().getMainWindow()); 88 89 if (selectedOption != JFileChooser.APPROVE_OPTION) { 90 throw new UserCancelException(); 91 } 92 files = chooser.getSelectedFiles(); 93 } while (files.length == 0); 94 return files; 95 } 96 97 101 public void performAction() { 102 if (!Manager.actionActivated(this)) { 103 return; 104 } 105 try { 106 JFileChooser chooser = prepareFileChooser(); 107 File [] files; 108 try { 109 files = chooseFilesToOpen(chooser); 110 } catch (UserCancelException ex) { 111 return; 112 } 113 for (int i = 0; i < files.length; i++) { 114 OpenFile.openFile(files[i], -1); 115 } 116 } finally { 117 Manager.actionFinished(this); 118 } 119 } 120 121 protected boolean asynchronous() { 122 return false; 123 } 124 125 130 private static File findStartingDirectory() { 131 Node[] nodes = TopComponent.getRegistry().getActivatedNodes(); 132 for (int i = 0; i < nodes.length; i++) { 133 DataObject d = (DataObject) nodes[i].getCookie(DataObject.class); 134 if (d != null) { 135 File f = FileUtil.toFile(d.getPrimaryFile()); 136 if (f != null) { 137 if (f.isFile()) { 138 f = f.getParentFile(); 139 } 140 return f; 141 } 142 } 143 } 144 return new File (System.getProperty("user.home")); 146 } 147 148 } 149 | Popular Tags |