1 19 20 package org.netbeans.modules.openfile; 21 22 import java.io.File ; 23 import java.io.PrintWriter ; 24 import java.util.HashSet ; 25 import java.util.Map ; 26 import java.util.Set ; 27 import org.netbeans.api.sendopts.CommandException; 28 import org.netbeans.spi.sendopts.Env; 29 import org.netbeans.spi.sendopts.Option; 30 import org.netbeans.spi.sendopts.OptionProcessor; 31 import org.openide.util.NbBundle; 32 33 37 public class Handler extends OptionProcessor { 38 private Option open; 39 private Option defaultOpen; 40 41 public Handler() { 42 } 43 44 protected Set <Option> getOptions() { 45 if (open == null) { 46 defaultOpen = Option.defaultArguments(); 47 Option o = Option.additionalArguments(Option.NO_SHORT_NAME, "open"); String bundle = "org.netbeans.modules.openfile.Bundle"; o = Option.shortDescription(o, bundle, "MSG_OpenOptionDescription"); o = Option.displayName(o, bundle, "MSG_OpenOptionDisplayName"); open = o; 52 53 assert open != null; 54 assert defaultOpen != null; 55 } 56 57 HashSet <Option> set = new HashSet <Option>(); 58 set.add(open); 59 set.add(defaultOpen); 60 61 return set; 62 } 63 64 protected void process(Env env, Map <Option, String []> optionValues) throws CommandException { 65 String [] argv = optionValues.get(open); 66 if (argv == null) { 67 argv = optionValues.get(defaultOpen); 68 } 69 if (argv == null || argv.length == 0) { 70 throw new CommandException(2, NbBundle.getMessage(Handler.class, "EXC_MissingArgOpen")); 71 } 72 73 File curDir = env.getCurrentDirectory (); 74 75 for (int i = 0; i < argv.length; i++) { 76 int res = openFile (curDir, env, argv[i]); 77 if (res != 0) { 78 throw new CommandException(res); 79 } 80 } 81 } 82 83 private File findFile (File curDir, String name) { 84 File f = new File (name); 85 if (!f.isAbsolute()) { 86 f = new File (curDir, name); 87 } 88 return f; 89 } 90 91 private int openFile (File curDir, Env args, String s) { 92 int line = -1; 93 File f = findFile (curDir, s); 94 if (!f.exists()) { 95 int idx = s.lastIndexOf(':'); if (idx != -1) { 98 try { 99 line = Integer.parseInt(s.substring(idx + 1)) - 1; 100 f = findFile (curDir, s.substring(0, idx)); 101 } catch (NumberFormatException e) { 102 } 104 } 105 } 106 boolean success = OpenFile.openFile(f, line); 108 return success ? 0 : 1; 109 } 110 } 111 | Popular Tags |