KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > archive > project > ProvidesAction


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
21 package org.netbeans.modules.j2ee.archive.project;
22
23 import java.io.IOException JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import org.netbeans.api.project.Project;
27 import org.netbeans.spi.project.ActionProvider;
28 import org.netbeans.spi.project.support.ant.GeneratedFilesHelper;
29 import org.openide.DialogDisplayer;
30 import org.openide.ErrorManager;
31 import org.openide.NotifyDescriptor;
32 import org.openide.filesystems.FileObject;
33 import org.openide.util.Lookup;
34 import org.netbeans.spi.project.ui.support.DefaultProjectOperations;
35 import org.openide.util.NbBundle;
36 import org.apache.tools.ant.module.api.support.ActionUtils;
37
38 public class ProvidesAction implements ActionProvider {
39     private static final String JavaDoc COMMAND_VERIFY = "verify"; //NOI18N
40

41     private Project project;
42     static Map JavaDoc commands;
43     static {
44         commands = new HashMap JavaDoc();
45         commands.put(COMMAND_BUILD, new String JavaDoc[] {"dist"}); // NOI18N
46
commands.put(COMMAND_RUN, new String JavaDoc[] {"run-deploy"}); // NOI18N
47
commands.put(COMMAND_VERIFY, new String JavaDoc[] {"verify"}); // NOI18N
48
}
49     
50     ProvidesAction(Project project) {
51         this.project=project;
52     }
53
54     public String JavaDoc[] getSupportedActions() {
55         // TODO -- Determine actions to implement
56
return new String JavaDoc[] {ActionProvider.COMMAND_BUILD,
57             ActionProvider.COMMAND_RUN, COMMAND_VERIFY,
58             ActionProvider.COMMAND_DELETE,
59             ActionProvider.COMMAND_COPY,
60             ActionProvider.COMMAND_MOVE,
61             ActionProvider.COMMAND_RENAME,
62         };
63     }
64
65     public void invokeAction(final String JavaDoc command, final Lookup context) {
66         if (COMMAND_DELETE.equals(command)) {
67             DefaultProjectOperations.performDefaultDeleteOperation(project);
68             return ;
69         }
70                 
71         if (COMMAND_COPY.equals(command)) {
72             DefaultProjectOperations.performDefaultCopyOperation(project);
73             return ;
74         }
75         
76         if (COMMAND_MOVE.equals(command)) {
77             DefaultProjectOperations.performDefaultMoveOperation(project);
78             return ;
79         }
80         
81         if (COMMAND_RENAME.equals(command)) {
82             DefaultProjectOperations.performDefaultRenameOperation(project, null);
83             return ;
84         }
85         
86         
87         // TODO -- implement action invokation
88
Runnable JavaDoc action = new Runnable JavaDoc () {
89             public void run () {
90                 String JavaDoc[] targetNames;
91         
92                 targetNames = getTargetNames(command, context);
93                 if (targetNames == null) {
94                     return;
95                 }
96                 if (targetNames.length == 0) {
97                     targetNames = null;
98                 }
99                 try {
100                     FileObject buildFo = findBuildXml();
101                     if (buildFo == null || !buildFo.isValid()) {
102                         //The build.xml was deleted after the isActionEnabled was called
103
NotifyDescriptor nd = new NotifyDescriptor.Message(NbBundle.getMessage(ProvidesAction.class,
104                                 java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/archive/project/Bundle").getString("LBL_No_Build_XML_Found")), NotifyDescriptor.WARNING_MESSAGE);
105                     DialogDisplayer.getDefault().notify(nd);
106                     }
107                     else {
108                         ActionUtils.runTarget(buildFo, targetNames, null); // p;
109
}
110                 }
111                 catch (IOException JavaDoc e) {
112                     ErrorManager.getDefault().notify(e);
113                 }
114             }
115         };
116         
117         action.run();
118     }
119
120     public boolean isActionEnabled(String JavaDoc command, Lookup context) throws IllegalArgumentException JavaDoc {
121         // TODO -- figure out the states for actions
122
return ! (findBuildXml() == null);
123     }
124     
125     /**
126      * @return array of targets or null to stop execution; can return empty array
127      */

128     String JavaDoc[] getTargetNames(String JavaDoc command, Lookup context) /*, Properties p) */ throws IllegalArgumentException JavaDoc {
129         String JavaDoc[] targetNames = (String JavaDoc[])commands.get(command);
130         return targetNames;
131     }
132     
133     private FileObject findBuildXml() {
134         return project.getProjectDirectory().getFileObject(GeneratedFilesHelper.BUILD_XML_PATH);
135     }
136 }
137
Popular Tags