Added
Link Here
|
1 |
/* |
2 |
* Sun Public License Notice |
3 |
* |
4 |
* The contents of this file are subject to the Sun Public License |
5 |
* Version 1.0 (the "License"). You may not use this file except in |
6 |
* compliance with the License. A copy of the License is available at |
7 |
* http://www.sun.com/ |
8 |
* |
9 |
* The Original Code is NetBeans. The Initial Developer of the Original |
10 |
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun |
11 |
* Microsystems, Inc. All Rights Reserved. |
12 |
*/ |
13 |
|
14 |
package org.netbeans.modules.openfile; |
15 |
|
16 |
import java.io.File; |
17 |
import java.io.PrintWriter; |
18 |
import org.netbeans.spi.sendopts.AdditionalArgumentsProcessor; |
19 |
import org.netbeans.spi.sendopts.Env; |
20 |
import org.netbeans.spi.sendopts.Option; |
21 |
import org.netbeans.spi.sendopts.OptionProvider; |
22 |
import org.openide.util.Lookup; |
23 |
|
24 |
/** |
25 |
* A sendopts provider for Open File. |
26 |
* @author Jaroslav Tulach |
27 |
*/ |
28 |
public class OptProvider implements OptionProvider, AdditionalArgumentsProcessor { |
29 |
/** option to open the file */ |
30 |
private Option open; |
31 |
|
32 |
/** |
33 |
* Create the provider. |
34 |
*/ |
35 |
public OptProvider() { |
36 |
open = Option.additionalArguments((char)-1, "open", this); // NOI18N |
37 |
} |
38 |
|
39 |
public Option[] getOptions() { |
40 |
return new Option[] { open }; |
41 |
} |
42 |
|
43 |
private File findFile (File curDir, String name) { |
44 |
File f = new File(name); |
45 |
if (!f.isAbsolute()) { |
46 |
f = new File(curDir, name); |
47 |
} |
48 |
return f; |
49 |
} |
50 |
|
51 |
private int openFile (File curDir, Env args, String[] argv, int i) { |
52 |
String s = argv[i]; |
53 |
if (s == null) { |
54 |
log("Missing argument to --open", args); |
55 |
return 2; |
56 |
} |
57 |
argv[i] = null; |
58 |
int line = -1; |
59 |
File f = findFile (curDir, s); |
60 |
if (!f.exists()) { |
61 |
// Check if it is 1file:line syntax. |
62 |
int idx = s.lastIndexOf(':'); // NOI18N |
63 |
if (idx != -1) { |
64 |
try { |
65 |
line = Integer.parseInt(s.substring(idx + 1)) - 1; |
66 |
f = findFile (curDir, s.substring(0, idx)); |
67 |
} catch (NumberFormatException e) { |
68 |
// OK, leave as a filename |
69 |
} |
70 |
} |
71 |
} |
72 |
// Just make sure it was opened, then exit. |
73 |
boolean success = OpenFile.openFile(f, line, null); |
74 |
return success ? 0 : 1; |
75 |
} |
76 |
|
77 |
|
78 |
public void process(Option option, Env env, String[] argv) throws org.netbeans.api.sendopts.CommandException { |
79 |
File curDir = env.getCurrentDirectory (); |
80 |
for (int i = 0; i < argv.length; i++) { |
81 |
int res = openFile (curDir, env, argv, i++); |
82 |
if (res != 0) { |
83 |
throw org.netbeans.api.sendopts.CommandException.exitCode(res); |
84 |
} |
85 |
} |
86 |
} |
87 |
|
88 |
private static void log(String msg, Env args) { |
89 |
PrintWriter w = new PrintWriter(args.getOutputStream()); |
90 |
w.println(msg); |
91 |
w.flush(); |
92 |
// don't close however - might be another user |
93 |
} |
94 |
|
95 |
protected void usage(PrintWriter w) { |
96 |
w.println("OpenFile module options:"); |
97 |
w.println(" --open FILE open FILE."); |
98 |
w.println(" --open FILE:LINE open FILE at line LINE (starting from 1)."); |
99 |
w.println(""); |
100 |
} |
101 |
|
102 |
} |