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 8.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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.Project;
  56. import org.apache.tools.ant.ProjectHelper;
  57. import org.apache.tools.ant.BuildEvent;
  58. import org.apache.tools.ant.BuildException;
  59. import org.apache.tools.ant.BuildListener;
  60. import org.apache.tools.ant.gui.event.*;
  61. import java.io.File;
  62. import java.io.IOException;
  63. import javax.swing.tree.TreeModel;
  64. import javax.swing.text.Document;
  65. import java.util.Enumeration;
  66. /**
  67. * This class provides the gateway interface to the data model for
  68. * the application. The translation between the Ant datamodel,
  69. * (or other external datamodel) occurs. This class also provides various
  70. * views into the data model, such as TreeModel, Documenet, etc.
  71. *
  72. * @version $Revision$
  73. * @author Simeon Fitch
  74. */
  75. public class ProjectProxy {
  76. /** Application context */
  77. private AppContext _context = null;
  78. /** The file where the project was last saved. */
  79. private File _file = null;
  80. /** The real Ant Project instance. */
  81. private Project _project = null;
  82. /** Private the current thread executing a build. */
  83. private Thread _buildThread = null;
  84. /**
  85. * File loading ctor.
  86. *
  87. * @param file File containing build file to load.
  88. */
  89. public ProjectProxy(AppContext context, File file) throws IOException {
  90. _file = file;
  91. _context = context;
  92. loadProject();
  93. }
  94. /**
  95. * Load the project from the build file.
  96. *
  97. */
  98. private void loadProject() throws IOException {
  99. _project = new Project();
  100. synchronized(_project) {
  101. _project.init();
  102. // XXX there is a bunch of stuff in the class
  103. // org.apache.tools.ant.Main that needs to be
  104. // abstracted out so that it doesn't have to be
  105. // replicated here.
  106. // XXX need to provide a way to pass in externally
  107. // defined properties. Perhaps define an external
  108. // Antidote properties file.
  109. _project.setUserProperty("ant.file" , _file.getAbsolutePath());
  110. ProjectHelper.configureProject(_project, _file);
  111. }
  112. }
  113. /**
  114. * Build the project with the current target (or the default target
  115. * if none is selected. Build occurs on a separate thread, so method
  116. * returns immediately.
  117. *
  118. */
  119. public void build() throws BuildException {
  120. if(_project == null) return;
  121. _buildThread = new Thread(new BuildRunner());
  122. _buildThread.start();
  123. }
  124. /**
  125. * Get the file where the project is saved to. If the project
  126. * is a new one that has never been saved the this will return null.
  127. *
  128. * @return Project file, or null if not save yet.
  129. */
  130. public File getFile() {
  131. return _file;
  132. }
  133. /**
  134. * Get the TreeModel perspective on the data.
  135. *
  136. * @return TreeModel view on project.
  137. */
  138. public TreeModel getTreeModel() {
  139. if(_project != null) {
  140. return new ProjectTreeModel(_project);
  141. }
  142. return null;
  143. }
  144. /**
  145. * Get the Document perspective on the data.
  146. *
  147. * @return Document view on project.
  148. */
  149. public Document getDocument() {
  150. if(_project != null) {
  151. // This is what the call should look like
  152. //return new ProjectDocument(_project);
  153. return new ProjectDocument(_file);
  154. }
  155. return null;
  156. }
  157. /**
  158. * Convenience method for causeing the project to fire a build event.
  159. * Implemented because the corresponding method in the Project class
  160. * is not publically accessible.
  161. *
  162. * @param event Event to fire.
  163. */
  164. private void fireBuildEvent(BuildEvent event, BuildEventType type) {
  165. synchronized(_project) {
  166. Enumeration enum = _project.getBuildListeners().elements();
  167. while(enum.hasMoreElements()) {
  168. BuildListener l = (BuildListener) enum.nextElement();
  169. type.fireEvent(event, l);
  170. }
  171. }
  172. }
  173. /** Class for executing the build in a separate thread. */
  174. private class BuildRunner implements Runnable {
  175. public void run() {
  176. synchronized(_project) {
  177. // Add the build listener for
  178. // dispatching BuildEvent objects to the
  179. // EventBus.
  180. BuildEventForwarder handler =
  181. new BuildEventForwarder(_context);
  182. _project.addBuildListener(handler);
  183. try {
  184. fireBuildEvent(new BuildEvent(
  185. _project), BuildEventType.BUILD_STARTED);
  186. // XXX add code to indicate target execution
  187. // on the targets that are selected.
  188. _project.executeTarget(
  189. _project.getDefaultTarget());
  190. }
  191. catch(BuildException ex) {
  192. BuildEvent errorEvent = new BuildEvent(_project);
  193. errorEvent.setException(ex);
  194. errorEvent.setMessage(ex.getMessage(), Project.MSG_ERR);
  195. fireBuildEvent(errorEvent, BuildEventType.MESSAGE_LOGGED);
  196. }
  197. finally {
  198. fireBuildEvent(new BuildEvent(
  199. _project), BuildEventType.BUILD_FINISHED);
  200. _project.removeBuildListener(handler);
  201. _buildThread = null;
  202. }
  203. }
  204. }
  205. }
  206. }