You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProjectProxy.java 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 1999, 2000 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant.gui;
  55. import org.apache.tools.ant.*;
  56. import org.apache.tools.ant.gui.event.*;
  57. import org.apache.tools.ant.gui.acs.*;
  58. import java.io.File;
  59. import java.io.Writer;
  60. import java.io.IOException;
  61. import javax.swing.tree.TreeModel;
  62. import javax.swing.text.Document;
  63. import javax.swing.tree.TreeSelectionModel;
  64. import javax.swing.event.TreeSelectionEvent;
  65. import javax.swing.event.TreeSelectionListener;
  66. import java.util.*;
  67. import org.xml.sax.SAXException;
  68. /**
  69. * This class provides the gateway interface to the data model for
  70. * the application. The translation between the Ant datamodel,
  71. * (or other external datamodel) occurs. This class also provides various
  72. * views into the data model, such as TreeModel, Documenet, etc.
  73. *
  74. * @version $Revision$
  75. * @author Simeon Fitch
  76. */
  77. public class ProjectProxy {
  78. /** Application context */
  79. private AppContext _context = null;
  80. /** The file where the project was last saved. */
  81. private File _file = null;
  82. /** The real Ant Project instance. */
  83. private ACSProjectElement _project = null;
  84. /** The current thread executing a build. */
  85. private Thread _buildThread = null;
  86. /** The selection model for selected targets. */
  87. private ElementSelectionModel _selections = null;
  88. /**
  89. * File loading ctor.
  90. *
  91. * @param file File containing build file to load.
  92. */
  93. public ProjectProxy(AppContext context, File file)
  94. throws IOException, SAXException {
  95. _file = file;
  96. _context = context;
  97. loadProject();
  98. }
  99. /**
  100. * Load the project from the build file.
  101. *
  102. */
  103. private void loadProject() throws IOException, SAXException {
  104. _project = ACSFactory.getInstance().load(_file);
  105. _selections = new ElementSelectionModel();
  106. _selections.addTreeSelectionListener(new SelectionForwarder());
  107. }
  108. /**
  109. * Write the project in XML format to the given output.
  110. *
  111. * @param out Output to write to.
  112. */
  113. public void write(Writer out) throws IOException {
  114. if(_project == null) return;
  115. _project.write(out);
  116. }
  117. /**
  118. * Build the project with the current target (or the default target
  119. * if none is selected. Build occurs on a separate thread, so method
  120. * returns immediately.
  121. *
  122. */
  123. public void build() throws BuildException {
  124. Project project = new Project();
  125. project.init();
  126. // XXX there is a bunch of stuff in the class
  127. // org.apache.tools.ant.Main that needs to be
  128. // refactored out so that it doesn't have to be
  129. // replicated here.
  130. // XXX need to provide a way to pass in externally
  131. // defined properties. Perhaps define an external
  132. // Antidote properties file. JAVA_HOME may have to be set,
  133. // as well as checking the .ant.properties
  134. project.setUserProperty("ant.file" , _file.getAbsolutePath());
  135. ProjectHelper.configureProject(project, _file);
  136. _buildThread = new Thread(new BuildRunner(project));
  137. _buildThread.start();
  138. }
  139. /**
  140. * Get the file where the project is saved to. If the project
  141. * is a new one that has never been saved the this will return null.
  142. *
  143. * @return Project file, or null if not save yet.
  144. */
  145. public File getFile() {
  146. return _file;
  147. }
  148. /**
  149. * Set the file that this is to be saved to.
  150. *
  151. * @param file File to save to.
  152. */
  153. public void setFile(File file) {
  154. _file = file;
  155. }
  156. /**
  157. * Get the TreeModel perspective on the data.
  158. *
  159. * @return TreeModel view on project.
  160. */
  161. public TreeModel getTreeModel() {
  162. if(_project != null) {
  163. return new ProjectTreeModel(_project);
  164. }
  165. return null;
  166. }
  167. /**
  168. * Get the tree selection model for selected targets.
  169. *
  170. * @return Selection model.
  171. */
  172. public ElementSelectionModel getTreeSelectionModel() {
  173. return _selections;
  174. }
  175. /**
  176. * Get the Document perspective on the data.
  177. *
  178. * @return Document view on project.
  179. */
  180. public Document getDocument() {
  181. // This is what the call should look like
  182. //return new ProjectDocument(_project);
  183. if(_file != null) {
  184. return new ProjectDocument(_file);
  185. }
  186. return null;
  187. }
  188. /** Class for executing the build in a separate thread. */
  189. private class BuildRunner implements Runnable {
  190. /** The project to execute build on. */
  191. private Project _project = null;
  192. /**
  193. * Standard ctor.
  194. *
  195. * @param project Project to execute build on.
  196. */
  197. public BuildRunner(Project project) {
  198. _project = project;
  199. }
  200. /**
  201. * Convenience method for causeing the project to fire a build event.
  202. * Implemented because the corresponding method in the Project class
  203. * is not publically accessible.
  204. *
  205. * @param event Event to fire.
  206. */
  207. private void fireBuildEvent(BuildEvent event, BuildEventType type) {
  208. Enumeration enum = _project.getBuildListeners().elements();
  209. while(enum.hasMoreElements()) {
  210. BuildListener l = (BuildListener) enum.nextElement();
  211. type.fireEvent(event, l);
  212. }
  213. }
  214. /**
  215. * Run the build.
  216. *
  217. */
  218. public void run() {
  219. // Add the build listeners
  220. BuildListener[] listeners = _context.getBuildListeners();
  221. for(int i = 0; i < listeners.length; i++) {
  222. _project.addBuildListener(listeners[i]);
  223. }
  224. try {
  225. fireBuildEvent(new BuildEvent(
  226. _project), BuildEventType.BUILD_STARTED);
  227. // Generate list of targets to execute.
  228. ACSTargetElement[] targets = _selections.getSelectedTargets();
  229. Vector targetNames = new Vector();
  230. if(targets.length == 0) {
  231. targetNames.add(_project.getDefaultTarget());
  232. }
  233. else {
  234. for(int i = 0; i < targets.length; i++) {
  235. targetNames.add(targets[i].getName());
  236. }
  237. }
  238. // Execute build on selected targets. XXX It would be
  239. // nice if the Project API supported passing in target
  240. // objects rather than String names.
  241. _project.executeTargets(targetNames);
  242. }
  243. catch(BuildException ex) {
  244. BuildEvent errorEvent = new BuildEvent(_project);
  245. errorEvent.setException(ex);
  246. errorEvent.setMessage(ex.getMessage(), Project.MSG_ERR);
  247. fireBuildEvent(errorEvent, BuildEventType.MESSAGE_LOGGED);
  248. }
  249. finally {
  250. fireBuildEvent(new BuildEvent(
  251. _project), BuildEventType.BUILD_FINISHED);
  252. // Remove the build listeners.
  253. for(int i = 0; i < listeners.length; i++) {
  254. _project.removeBuildListener(listeners[i]);
  255. }
  256. }
  257. }
  258. }
  259. /** Forwards selection events to the event bus. */
  260. private class SelectionForwarder implements TreeSelectionListener {
  261. public void valueChanged(TreeSelectionEvent e) {
  262. _context.getEventBus().postEvent(ElementSelectionEvent.createEvent(
  263. _context, _selections.getSelectedElements()));
  264. }
  265. }
  266. }