KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > scripting > php > webproject > PhpProjectActionProvider


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.scripting.php.webproject;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Properties JavaDoc;
27 import org.apache.tools.ant.module.api.support.ActionUtils;
28 import org.netbeans.modules.scripting.php.dbginterface.api.DbgDebugger;
29 import org.netbeans.modules.scripting.php.dbginterface.api.DbgSourceMap;
30 import org.netbeans.modules.scripting.php.dbginterface.api.DebuggerStartException;
31 import org.netbeans.spi.project.ActionProvider;
32 import org.netbeans.spi.project.ui.support.DefaultProjectOperations;
33 import org.openide.ErrorManager;
34 import org.openide.awt.HtmlBrowser;
35 import org.openide.execution.ExecutorTask;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.FileUtil;
38 import org.openide.loaders.DataFolder;
39 import org.openide.loaders.DataObject;
40 import org.openide.loaders.DataObjectNotFoundException;
41 import org.openide.util.Lookup;
42
43 /**
44  *
45  * @author marcow
46  */

47 public class PhpProjectActionProvider implements ActionProvider {
48     private final PhpProject project;
49
50     private static final String JavaDoc[] supportedActions = {
51         COMMAND_RUN,
52         COMMAND_DEBUG,
53         COMMAND_DELETE,
54         COMMAND_COPY,
55         COMMAND_MOVE,
56         COMMAND_RENAME
57     };
58
59     static int debugSessionNo = 1;
60     
61     /** Creates a new instance of ActionProvider */
62     public PhpProjectActionProvider(PhpProject project) {
63         this.project = project;
64     }
65     
66     public String JavaDoc[] getSupportedActions() {
67         return supportedActions;
68     }
69
70     public void invokeAction(String JavaDoc command, Lookup context) throws IllegalArgumentException JavaDoc {
71         String JavaDoc urlSuffix = "";
72         String JavaDoc useSourceDir = project.getProperty(PhpProjectProperties.USE_SOURCE_DIR);
73         boolean openBrowser = true;
74                     
75         System.err.println("mw PhpProjectActionProvider useSourceDir= " + useSourceDir);
76                     
77         
78         if (COMMAND_DELETE.equals(command)) {
79             DefaultProjectOperations.performDefaultDeleteOperation(project);
80             return;
81         }
82
83         if (COMMAND_COPY.equals(command)) {
84             DefaultProjectOperations.performDefaultCopyOperation(project);
85             return;
86         }
87
88         if (COMMAND_MOVE.equals(command)) {
89             DefaultProjectOperations.performDefaultMoveOperation(project);
90             return;
91         }
92
93         if (COMMAND_RENAME.equals(command)) {
94             DefaultProjectOperations.performDefaultRenameOperation(project, null
95 );
96             return;
97         }
98         
99         if ((COMMAND_DEBUG.equals(command))) {
100                URL JavaDoc serverBaseUrl = null;
101               String JavaDoc serverTargetDir = project.getProperty(PhpProjectProperties.TARGET_DIR);
102                FileObject baseDirFO = project.getProjectDirectory();
103
104                 try {
105                     serverBaseUrl = new URL JavaDoc(project.getProperty(PhpProjectProperties.URL));
106                 }
107                 catch (MalformedURLException JavaDoc mue) {
108                     ErrorManager.getDefault().notify(ErrorManager.WARNING, mue);
109                 }
110                
111                 DbgSourceMap sourceMap = new DbgSourceMap(serverBaseUrl, serverTargetDir,
112                         baseDirFO);
113
114             try {
115                 urlSuffix = "?DBGSESSID=" + debugSessionNo++ + "@" +
116                             DbgDebugger.startListening(sourceMap);
117                 
118                 System.err.println("mw Debug urlSuffix= " + urlSuffix);
119             }
120             catch (DebuggerStartException ex) {
121                 ErrorManager.getDefault().notify(ErrorManager.WARNING, ex);
122             }
123
124         }
125         
126         if (COMMAND_RUN.equals(command) || COMMAND_DEBUG.equals(command)) {
127             if (!Boolean.parseBoolean(useSourceDir)) {
128                 openBrowser = false;
129                         
130                 try {
131                     Properties JavaDoc p = new Properties JavaDoc();
132                     String JavaDoc[] targetNames = new String JavaDoc[] { "copy-hack" };
133                     FileObject buildFo = project.getProjectDirectory().getFileObject("nbproject/copyHack.xml");
134                     
135                     p.put("srcDir", FileUtil.toFile(project.getProjectDirectory()).getAbsolutePath());
136                     p.put("targetDir", project.getProperty(PhpProjectProperties.TARGET_DIR));
137                     
138                     p.list(System.err);
139                     
140                     if (buildFo != null) {
141                         ExecutorTask task = ActionUtils.runTarget(buildFo, targetNames, p);
142                         
143                         if (task.result() == 0) {
144                             openBrowser = true;
145                         }
146                     }
147                 } catch (DataObjectNotFoundException ex) {
148                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
149                 } catch (IOException JavaDoc ioe) {
150                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
151                 }
152             }
153                     
154             if (openBrowser) {
155                 try {
156                     String JavaDoc url = project.getProperty(PhpProjectProperties.URL);
157                     
158                     HtmlBrowser.URLDisplayer.getDefault().showURL(new URL JavaDoc(url + urlSuffix));
159                 } catch (MalformedURLException JavaDoc ex) {
160                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
161                 }
162             }
163
164             return;
165         }
166
167         throw new UnsupportedOperationException JavaDoc("Not supported yet.");
168     }
169
170     public boolean isActionEnabled(String JavaDoc command, Lookup context) throws IllegalArgumentException JavaDoc {
171         if (COMMAND_DEBUG.equals(command)) {
172             return true;
173         }
174         else if (COMMAND_RUN.equals(command)) {
175             return true;
176         }
177         else {
178             // other actions are global
179
return true;
180         }
181     }
182 }
183
Popular Tags