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-2007 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.compapp.projects.jbi.jeese.actions;
043:
044: import java.awt.Dialog;
045: import java.awt.Dimension;
046: import java.io.File;
047: import javax.swing.JDialog;
048: import org.netbeans.api.project.Project;
049: import org.netbeans.api.project.ProjectManager;
050: import org.netbeans.modules.compapp.javaee.sunresources.SunResourcesUtil;
051: import org.netbeans.modules.compapp.javaee.sunresources.tool.JavaEETool;
052: import org.netbeans.modules.compapp.javaee.sunresources.tool.archive.ArchiveConstants;
053: import org.netbeans.modules.compapp.javaee.sunresources.ui.ResourcesPanel;
054: import org.netbeans.modules.compapp.projects.jbi.JbiProject;
055: import org.netbeans.modules.compapp.projects.jbi.ui.customizer.VisualClassPathItem;
056: import org.netbeans.spi.project.support.ant.AntProjectHelper;
057: import org.netbeans.spi.project.support.ant.EditableProperties;
058: import org.netbeans.spi.project.support.ant.PropertyEvaluator;
059: import org.openide.DialogDisplayer;
060: import org.openide.NotifyDescriptor;
061: import org.openide.filesystems.FileObject;
062: import org.openide.filesystems.FileUtil;
063: import org.openide.nodes.Node;
064: import org.openide.util.HelpCtx;
065: import org.openide.util.Lookup;
066: import org.openide.util.NbBundle;
067: import org.openide.util.actions.NodeAction;
068: import org.openide.windows.WindowManager;
069:
070: /**
071: *
072: * @author echou
073: */
074: public class ServerResourcesAction extends NodeAction {
075:
076: private String name;
077:
078: /** Creates a new instance of ServerResourcesAction */
079: public ServerResourcesAction() {
080: name = NbBundle.getBundle(this .getClass()).getString(
081: "SERVER_RES_ACTION_NAME");
082: }
083:
084: protected void performAction(Node[] activatedNodes) {
085: Node node = activatedNodes[0];
086: Lookup lookup = node.getLookup();
087: JbiProject jbiProject = (JbiProject) lookup
088: .lookup(JbiProject.class);
089: VisualClassPathItem vcpi = (VisualClassPathItem) lookup
090: .lookup(VisualClassPathItem.class);
091: String refProjectName = "project." + vcpi.getProjectName(); // NOI18N
092: AntProjectHelper aph = (AntProjectHelper) jbiProject
093: .getLookup().lookup(AntProjectHelper.class);
094: EditableProperties ep = aph
095: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
096: PropertyEvaluator pe = aph.getStandardPropertyEvaluator();
097: String refProjectLocation = pe.evaluate(ep
098: .getProperty(refProjectName));
099: FileObject parentDir = jbiProject.getProjectDirectory();
100: try {
101: File refProjectDirFile = new File(FileUtil
102: .toFile(parentDir).getCanonicalPath()
103: + File.separator + refProjectLocation);
104: FileObject refProjectDir = FileUtil.toFileObject(FileUtil
105: .normalizeFile(refProjectDirFile));
106: if (refProjectDir == null) {
107: DialogDisplayer.getDefault().notify(
108: new NotifyDescriptor.Message(NbBundle
109: .getMessage(
110: ServerResourcesAction.class,
111: "EXC_proj_notfound", vcpi
112: .getProjectName(),
113: refProjectLocation),
114: NotifyDescriptor.ERROR_MESSAGE));
115: return;
116: }
117:
118: Project refProject = ProjectManager.getDefault()
119: .findProject(refProjectDir);
120: if (refProject == null) {
121: DialogDisplayer.getDefault().notify(
122: new NotifyDescriptor.Message(NbBundle
123: .getMessage(
124: ServerResourcesAction.class,
125: "EXC_cannot_open_proj", vcpi
126: .getProjectName()),
127: NotifyDescriptor.ERROR_MESSAGE));
128: return;
129: }
130:
131: // check type of project
132: ArchiveConstants.ArchiveType projType = SunResourcesUtil
133: .getJavaEEProjectType(refProject);
134: if (projType == ArchiveConstants.ArchiveType.EAR
135: || projType == ArchiveConstants.ArchiveType.EJB
136: || projType == ArchiveConstants.ArchiveType.WAR) {
137: JavaEETool javaEETool = null;
138: try {
139: javaEETool = new JavaEETool(projType, refProject);
140: } catch (ProjectNotBuiltException pnbe) {
141: DialogDisplayer
142: .getDefault()
143: .notify(
144: new NotifyDescriptor.Message(
145: NbBundle
146: .getMessage(
147: ServerResourcesAction.class,
148: "EXC_proj_notbuild",
149: pnbe
150: .getMessage()),
151: NotifyDescriptor.ERROR_MESSAGE));
152: return;
153: }
154:
155: // display dialog box ask for user input on which resource to generate
156: Dialog resourceDialog = createResourcesDialog(javaEETool);
157: resourceDialog.setVisible(true);
158:
159: javaEETool.close();
160: } else {
161: DialogDisplayer.getDefault().notify(
162: new NotifyDescriptor.Message(NbBundle
163: .getMessage(
164: ServerResourcesAction.class,
165: "EXC_proj_notsupported", vcpi
166: .getProjectName(),
167: projType),
168: NotifyDescriptor.ERROR_MESSAGE));
169: return;
170: }
171:
172: } catch (Exception ex) {
173: ex.printStackTrace();
174: return;
175: }
176: }
177:
178: protected boolean enable(Node[] activatedNodes) {
179: return true;
180: }
181:
182: public String getName() {
183: return name;
184: }
185:
186: public HelpCtx getHelpCtx() {
187: return null;
188: }
189:
190: protected boolean asynchronous() {
191: return false;
192: }
193:
194: /* helper method to create a resources dialog window
195: */
196: private Dialog createResourcesDialog(JavaEETool javaEETool) {
197: JDialog dlg = new JDialog(WindowManager.getDefault()
198: .getMainWindow(), NbBundle.getMessage(
199: ServerResourcesAction.class, "SERVER_RES_ACTION_NAME"));
200: ResourcesPanel innerPane = new ResourcesPanel(dlg, javaEETool);
201: dlg.getContentPane().add(innerPane);
202: dlg.setModal(true);
203: dlg.setPreferredSize(innerPane.getPreferredSize());
204: int width = dlg.getPreferredSize().width;
205: int height = dlg.getPreferredSize().height;
206: Dimension screenSize = java.awt.Toolkit.getDefaultToolkit()
207: .getScreenSize();
208: dlg.setBounds((screenSize.width - width) / 2,
209: (screenSize.height - height) / 2, width, height);
210: return dlg;
211: }
212:
213: public static class ProjectNotBuiltException extends Exception {
214: public ProjectNotBuiltException(String msg) {
215: super(msg);
216: }
217: }
218: }
|