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: package org.netbeans.modules.collab.channel.filesharing.ui.actions;
042:
043: import org.openide.DialogDisplayer;
044: import org.openide.NotifyDescriptor;
045: import org.openide.filesystems.FileObject;
046: import org.openide.filesystems.FileSystem;
047: import org.openide.loaders.DataFolder;
048: import org.openide.loaders.DataObject;
049: import org.openide.nodes.Node;
050: import org.openide.util.HelpCtx;
051: import org.openide.util.NbBundle;
052: import org.openide.util.actions.CookieAction;
053:
054: import java.io.IOException;
055:
056: import org.netbeans.modules.collab.channel.filesharing.FilesharingContext;
057: import org.netbeans.modules.collab.channel.filesharing.ui.ProjectsRootNode;
058: import org.netbeans.modules.collab.core.Debug;
059:
060: /**
061: *
062: */
063: public class NewFileAction extends CookieAction {
064: ////////////////////////////////////////////////////////////////////////////
065: // Class variables
066: ////////////////////////////////////////////////////////////////////////////
067: public static final String NEW_FILE_ACTION = NbBundle.getMessage(
068: NewFileAction.class, "LBL_NewFileAction_ActionName"); //"Empty File"
069:
070: ////////////////////////////////////////////////////////////////////////////
071: // Instant variables
072: ////////////////////////////////////////////////////////////////////////////
073: FilesharingContext context = null;
074: Node node = null;
075:
076: public NewFileAction() {
077: }
078:
079: public NewFileAction(FilesharingContext context, Node node) {
080: this .context = context;
081: this .node = node;
082: }
083:
084: /* public members */
085: public String getName() {
086: return NEW_FILE_ACTION;
087: }
088:
089: public HelpCtx getHelpCtx() {
090: return HelpCtx.DEFAULT_HELP;
091: }
092:
093: /* protected members */
094: protected Class[] cookieClasses() {
095: return new Class[] { DataFolder.class };
096: }
097:
098: protected int mode() {
099: return MODE_ANY; // allow creation of tests for multiple selected nodes (classes, packages)
100: }
101:
102: public boolean asynchronous() {
103: return true; // yes, this action should run asynchronously
104:
105: // would be better to rewrite it to synchronous (running in AWT thread),
106: // just replanning test generation to RequestProcessor
107: }
108:
109: /** Perform special enablement check in addition to the normal one.
110: * protected boolean enable (Node[] nodes) {
111: * if (! super.enable (nodes)) return false;
112: * if (...) ...;
113: * }
114: */
115: protected boolean enable(Node[] nodes) {
116: return true;
117: }
118:
119: /**
120: * Returns true iff the node represents a source file or package.
121: * @param node the Node to query
122: * @return true or false
123: */
124: private static boolean isEnabledOnNode(Node node) {
125: DataObject dob = (DataObject) node.getCookie(DataObject.class);
126: FileObject fo = null;
127:
128: if (dob != null) {
129: fo = dob.getPrimaryFile();
130: }
131:
132: if (fo == null) {
133: return false;
134: }
135:
136: return true;
137: }
138:
139: protected String iconResource() {
140: return "org/netbeans/modules/collab/channel/filesharing/resources/" //NOI18N
141: + "newFile.gif"; //NOI18N
142: }
143:
144: protected void performAction(Node[] nodes) {
145: Debug.out.println(NEW_FILE_ACTION + ", actionPerformed"); //NoI18n
146:
147: NotifyDescriptor descriptor = new NotifyDescriptor.InputLine(
148: NbBundle.getMessage(NewFileAction.class,
149: "MSG_NewFileAction_NewFileName"), NbBundle
150: .getMessage(NewFileAction.class,
151: "MSG_NewFileAction_NewFileTitle"),
152: NotifyDescriptor.OK_CANCEL_OPTION,
153: NotifyDescriptor.PLAIN_MESSAGE);
154:
155: if (DialogDisplayer.getDefault().notify(descriptor) != NotifyDescriptor.OK_OPTION) {
156: return;
157: }
158:
159: final String fileName = ((NotifyDescriptor.InputLine) descriptor)
160: .getInputText();
161:
162: try {
163: FileSystem fs = context.getCollabFilesystem();
164: fs.runAtomicAction(new FileSystem.AtomicAction() {
165: public void run() throws IOException {
166: try {
167: Node projectNode = context.createProjectNode(
168: context.getLoginUser(),
169: ProjectsRootNode.SHARED_COMMON_DIR);
170:
171: if (projectNode != null) {
172: //expand node
173: context.getFilesystemExplorer()
174: .expandTreeNode(projectNode, "");
175:
176: DataObject d = (DataObject) node
177: .getCookie(DataObject.class);
178: FileObject fObj = null;
179:
180: if (node instanceof ProjectsRootNode
181: || node instanceof ProjectsRootNode.ProjectNode) {
182: fObj = ((ProjectsRootNode.ProjectNode) projectNode)
183: .getProjectDir();
184: } else if ((d != null)
185: && (d.getPrimaryFile() != null)) {
186: fObj = d.getPrimaryFile();
187: }
188:
189: //expand node
190: context.getFilesystemExplorer()
191: .expandTreeNode(node, "");
192:
193: //create file
194: if (fObj != null) {
195: fObj.createData(fileName);
196: }
197: }
198: } catch (IOException iox) {
199: Debug.log("ProjectsRootNode",
200: "ProjectsRootNode, ex: " + iox);
201: iox.printStackTrace(Debug.out);
202: }
203: }
204: });
205: } catch (IOException iox) {
206: iox.printStackTrace(Debug.out);
207: }
208: }
209: }
|