Lines 35-114
public class Handler extends CLIHandler
Link Here
|
35 |
return (Callback)Lookup.getDefault().lookup(Callback.class); |
35 |
return (Callback)Lookup.getDefault().lookup(Callback.class); |
36 |
} |
36 |
} |
37 |
|
37 |
|
|
|
38 |
private File findFile (File curDir, String name) { |
39 |
File f = new File(name); |
40 |
if (!f.isAbsolute()) { |
41 |
f = new File(curDir, name); |
42 |
} |
43 |
return f; |
44 |
} |
45 |
|
46 |
private int openFile (File curDir, CLIHandler.Args args, String[] argv, int i) { |
47 |
String s = argv[i]; |
48 |
if (s == null) { |
49 |
log("Missing argument to --open", args); |
50 |
return 2; |
51 |
} |
52 |
argv[i] = null; |
53 |
Callback c = getCallback(); |
54 |
if (c == null) { |
55 |
// XXX I18N required for cmdline? |
56 |
log("The User Utilities module must be installed for open-file functionality to work.", args); |
57 |
return 2; |
58 |
} |
59 |
int line = -1; |
60 |
File f = findFile (curDir, s); |
61 |
if (!f.exists()) { |
62 |
// Check if it is file:line syntax. |
63 |
int idx = s.lastIndexOf(':'); // NOI18N |
64 |
if (idx != -1) { |
65 |
try { |
66 |
line = Integer.parseInt(s.substring(idx + 1)) - 1; |
67 |
f = findFile (curDir, s.substring(0, idx)); |
68 |
} catch (NumberFormatException e) { |
69 |
// OK, leave as a filename |
70 |
} |
71 |
} |
72 |
} |
73 |
// Just make sure it was opened, then exit. |
74 |
boolean success = c.open(f, line, null); |
75 |
return success ? 0 : 1; |
76 |
} |
77 |
|
38 |
protected int cli(CLIHandler.Args args) { |
78 |
protected int cli(CLIHandler.Args args) { |
39 |
String[] argv = args.getArguments(); |
79 |
String[] argv = args.getArguments(); |
40 |
File curDir = args.getCurrentDirectory (); |
80 |
File curDir = args.getCurrentDirectory (); |
41 |
boolean wait = false; |
|
|
42 |
for (int i = 0; i < argv.length; i++) { |
81 |
for (int i = 0; i < argv.length; i++) { |
43 |
if (argv[i] == null) { |
82 |
if (argv[i] == null) { |
44 |
continue; |
83 |
continue; |
45 |
} |
84 |
} |
46 |
if (argv[i].equals("-open")) { // NOI18N |
85 |
if (argv[i].equals("--open") || argv[i].equals("-open")) { // NOI18N |
47 |
argv[i] = null; |
86 |
argv[i] = null; |
48 |
if (i == argv.length - 1) { |
87 |
if (i == argv.length - 1) { |
49 |
log("Missing argument to -open", args); |
88 |
log("Missing argument to --open", args); |
50 |
return 2; |
89 |
return 2; |
51 |
} |
90 |
} |
52 |
String s = argv[++i]; |
91 |
i++; |
53 |
if (s == null) { |
92 |
while (i < argv.length && !argv[i].startsWith ("-")) { |
54 |
log("Missing argument to -open", args); |
93 |
int res = openFile (curDir, args, argv, i++); |
55 |
return 2; |
94 |
if (res != 0) { |
56 |
} |
95 |
return res; |
57 |
argv[i] = null; |
|
|
58 |
Callback c = getCallback(); |
59 |
if (c == null) { |
60 |
// XXX I18N required for cmdline? |
61 |
log("The User Utilities module must be installed for open-file functionality to work.", args); |
62 |
return 2; |
63 |
} |
64 |
int line = -1; |
65 |
File f = new File(s); |
66 |
if (!f.isAbsolute()) { |
67 |
f = new File(curDir, s); |
68 |
} |
69 |
if (!f.exists()) { |
70 |
// Check if it is file:line syntax. |
71 |
int idx = s.lastIndexOf(':'); // NOI18N |
72 |
if (idx != -1) { |
73 |
try { |
74 |
line = Integer.parseInt(s.substring(idx + 1)) - 1; |
75 |
f = new File(s.substring(0, idx)); |
76 |
} catch (NumberFormatException e) { |
77 |
// OK, leave as a filename |
78 |
} |
79 |
} |
96 |
} |
80 |
} |
97 |
} |
81 |
boolean success; |
98 |
} |
82 |
if (wait) { |
|
|
83 |
// Open it, and wait for it to be closed. |
84 |
Callback.Waiter w = new Callback.Waiter() { |
85 |
public synchronized void done() { |
86 |
notify(); |
87 |
} |
88 |
}; |
89 |
success = c.open(f, line, w); |
90 |
/* XXX uncomment when implemented in module |
91 |
if (success) { |
92 |
try { |
93 |
synchronized (w) { |
94 |
w.wait(); |
95 |
} |
96 |
} catch (InterruptedException e) { |
97 |
e.printStackTrace(); |
98 |
} |
99 |
} |
100 |
*/ |
101 |
} else { |
102 |
// Just make sure it was opened, then exit. |
103 |
success = c.open(f, line, null); |
104 |
} |
105 |
if (!success) { |
106 |
return 1; |
107 |
} |
108 |
} else if (argv[i].equals("-wait")) { // NOI18N |
109 |
argv[i] = null; |
110 |
wait = true; |
111 |
} |
112 |
} |
99 |
} |
113 |
// No problems. |
100 |
// No problems. |
114 |
return 0; |
101 |
return 0; |
Lines 122-136
public class Handler extends CLIHandler
Link Here
|
122 |
} |
109 |
} |
123 |
|
110 |
|
124 |
protected void usage(PrintWriter w) { |
111 |
protected void usage(PrintWriter w) { |
125 |
w.println("-open FILE"); |
112 |
w.println("Open File Module options:"); |
126 |
w.println(" Open FILE."); |
113 |
w.println(" --open FILE open FILE."); |
127 |
w.println("-open FILE:LINE"); |
114 |
w.println(" --open FILE:LINE open FILE at line LINE (starting from 1)."); |
128 |
w.println(" Open FILE at line LINE (starting from 1)."); |
|
|
129 |
/* XXX uncomment when implemented in module |
130 |
w.println("-wait -open FILE[:LINE]"); |
131 |
w.println(" Open FILE (maybe at line LINE), and wait until it is closed before exiting."); |
132 |
w.println(" (Currently unimplemented.)"); |
133 |
*/ |
134 |
} |
115 |
} |
135 |
|
116 |
|
136 |
} |
117 |
} |