001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.openfile;
043:
044: import java.io.File;
045: import java.io.PrintWriter;
046: import java.util.HashSet;
047: import java.util.Map;
048: import java.util.Set;
049: import org.netbeans.api.sendopts.CommandException;
050: import org.netbeans.spi.sendopts.Env;
051: import org.netbeans.spi.sendopts.Option;
052: import org.netbeans.spi.sendopts.OptionProcessor;
053: import org.openide.DialogDisplayer;
054: import org.openide.NotifyDescriptor;
055: import org.openide.util.NbBundle;
056:
057: /**
058: * Processor for command line options.
059: * @author Jesse Glick, Jaroslav Tulach
060: */
061: public class Handler extends OptionProcessor {
062: private Option open;
063: private Option defaultOpen;
064:
065: public Handler() {
066: }
067:
068: protected Set<Option> getOptions() {
069: if (open == null) {
070: defaultOpen = Option.defaultArguments();
071: Option o = Option.additionalArguments(Option.NO_SHORT_NAME,
072: "open"); // NOI18N
073: String bundle = "org.netbeans.modules.openfile.Bundle"; // NOI18N
074: o = Option.shortDescription(o, bundle,
075: "MSG_OpenOptionDescription"); // NOI18N
076: o = Option.displayName(o, bundle,
077: "MSG_OpenOptionDisplayName"); // NOI18N
078: open = o;
079:
080: assert open != null;
081: assert defaultOpen != null;
082: }
083:
084: HashSet<Option> set = new HashSet<Option>();
085: set.add(open);
086: set.add(defaultOpen);
087:
088: return set;
089: }
090:
091: protected void process(Env env, Map<Option, String[]> optionValues)
092: throws CommandException {
093: String[] argv = optionValues.get(open);
094: if (argv == null) {
095: argv = optionValues.get(defaultOpen);
096: }
097: if (argv == null || argv.length == 0) {
098: throw new CommandException(2, NbBundle.getMessage(
099: Handler.class, "EXC_MissingArgOpen"));
100: }
101:
102: File curDir = env.getCurrentDirectory();
103:
104: StringBuffer failures = new StringBuffer();
105: String sep = "";
106: for (int i = 0; i < argv.length; i++) {
107: String error = openFile(curDir, env, argv[i]);
108: if (error != null) {
109: failures.append(sep);
110: failures.append(error);
111: sep = "\n";
112: }
113: }
114: if (failures.length() > 0) {
115: DialogDisplayer.getDefault().notifyLater(
116: new NotifyDescriptor.Message(failures.toString()));
117: throw new CommandException(1, failures.toString());
118: }
119: }
120:
121: private File findFile(File curDir, String name) {
122: File f = new File(name);
123: if (!f.isAbsolute()) {
124: f = new File(curDir, name);
125: }
126: return f;
127: }
128:
129: private String openFile(File curDir, Env args, String s) {
130: int line = -1;
131: File f = findFile(curDir, s);
132: if (!f.exists()) {
133: // Check if it is file:line syntax.
134: int idx = s.lastIndexOf(':'); // NOI18N
135: if (idx != -1) {
136: try {
137: line = Integer.parseInt(s.substring(idx + 1)) - 1;
138: f = findFile(curDir, s.substring(0, idx));
139: } catch (NumberFormatException e) {
140: // OK, leave as a filename
141: }
142: }
143: }
144: // Just make sure it was opened, then exit.
145: return OpenFile.openFile(f, line);
146: }
147: }
|