KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > openfile > Handler


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.openfile;
21
22 import java.io.File JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
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 /**
34  * Processor for command line options.
35  * @author Jesse Glick, Jaroslav Tulach
36  */

37 public class Handler extends OptionProcessor {
38     private Option open;
39     private Option defaultOpen;
40
41     public Handler() {
42     }
43
44     protected Set JavaDoc<Option> getOptions() {
45         if (open == null) {
46             defaultOpen = Option.defaultArguments();
47             Option o = Option.additionalArguments(Option.NO_SHORT_NAME, "open"); // NOI18N
48
String JavaDoc bundle = "org.netbeans.modules.openfile.Bundle"; // NOI18N
49
o = Option.shortDescription(o, bundle, "MSG_OpenOptionDescription"); // NOI18N
50
o = Option.displayName(o, bundle, "MSG_OpenOptionDisplayName"); // NOI18N
51
open = o;
52             
53             assert open != null;
54             assert defaultOpen != null;
55         }
56         
57         HashSet JavaDoc<Option> set = new HashSet JavaDoc<Option>();
58         set.add(open);
59         set.add(defaultOpen);
60         
61         return set;
62     }
63
64     protected void process(Env env, Map JavaDoc<Option, String JavaDoc[]> optionValues) throws CommandException {
65         String JavaDoc[] 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 JavaDoc 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 JavaDoc findFile (File JavaDoc curDir, String JavaDoc name) {
84         File JavaDoc f = new File JavaDoc(name);
85         if (!f.isAbsolute()) {
86             f = new File JavaDoc(curDir, name);
87         }
88         return f;
89     }
90     
91     private int openFile (File JavaDoc curDir, Env args, String JavaDoc s) {
92         int line = -1;
93         File JavaDoc f = findFile (curDir, s);
94         if (!f.exists()) {
95             // Check if it is file:line syntax.
96
int idx = s.lastIndexOf(':'); // NOI18N
97
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 JavaDoc e) {
102                     // OK, leave as a filename
103
}
104             }
105         }
106         // Just make sure it was opened, then exit.
107
boolean success = OpenFile.openFile(f, line);
108         return success ? 0 : 1;
109     }
110 }
111
Popular Tags