1 /*
2 * Copyright (c) 2001, 2002 The XDoclet team
3 * All rights reserved.
4 */
5 package xdoclet.ant;
6
7 import org.apache.commons.logging.LogFactory;
8 import org.apache.tools.ant.*;
9
10 import xdoclet.beans.BeanContextSupportEx;
11 import xdoclet.XDoclet;
12 import xdoclet.XDocletException;
13 import xdoclet.gui.swing.BeanContextConfigurationPanel;
14
15 import javax.swing.AbstractAction;
16 import javax.swing.JButton;
17 import javax.swing.JFrame;
18 import javax.swing.JPanel;
19
20 import java.awt.BorderLayout;
21 import java.awt.event.ActionEvent;
22 import java.awt.event.WindowAdapter;
23 import java.awt.event.WindowEvent;
24
25 /***
26 * Ant task for XDoclet. It's just a thin adapter. Let's keep it that way!
27 * We're using Ant's {@link org.apache.tools.ant.IntrospectionHelper} to set attributes in
28 * the XDoclet object (which is totally decoupled from Ant).
29 *
30 * If you need to adapt a particular subclass of {@link XDoclet}, subclass this class
31 * and override {@link #createXDoclet}.
32 *
33 * @author <a href="mailto:aslak.hellesoy at netcom.no">Aslak Hellesøy</a>
34 * @version $Revision: 1.14 $
35 */
36 public class XDocletTask extends Task implements DynamicConfigurator {
37 /***
38 * Sync lock used to keep Ant waiting until gui is closed.
39 * @todo we should make a separate XDocletGuiTask with no preconfigured XDoclets.
40 */
41 private final Object _guiLock = new Object();
42 private boolean _isGui = false;
43 private XDoclet _xdoclet;
44 private AntProxy _antProxy;
45
46 public final void setDynamicAttribute(String name, String value)
47 throws BuildException {
48 _antProxy.setDynamicAttribute(name, value);
49 }
50
51 public final Object createDynamicElement(String name)
52 throws BuildException {
53 return _antProxy.createDynamicElement(name);
54 }
55
56 /***
57 * Callback from Ant. This method is called right after a new XDocletTask
58 * object is created.
59 *
60 * @param project the Ant project.
61 */
62 public final void setProject(Project project) {
63 super.setProject(project);
64
65 // Configure commons-logging to reroute all logging to Ant's logger.
66 // This makes it very easy to turn on/off.
67 AntLogFactory.setProject(project);
68 System.setProperty(LogFactory.FACTORY_PROPERTY, AntLogFactory.class.getName());
69
70 // Now that logging is properly set up we can initialise the rest of the XDoclet gang.
71 _xdoclet = createXDoclet();
72 try {
73 _xdoclet.setClasspath(getClasspath());
74 } catch( XDocletException e ) {
75 throw new BuildException( e );
76 }
77 }
78
79 /***
80 * Callback from Ant. This will be called after
81 * {@link #setProject(org.apache.tools.ant.Project)}.
82 *
83 * @param taskName the name of the task.
84 */
85 public void setTaskName(String taskName) {
86 super.setTaskName(taskName);
87 _antProxy = new AntProxy(_xdoclet, _xdoclet, getProject(), getTaskName());
88 }
89
90 /***
91 * Subclasses should override this method if they need to adapt a different
92 * subclass of XDoclet.
93 *
94 * @return an instance of an XDoclet
95 */
96 protected XDoclet createXDoclet() {
97 return new XDoclet();
98 }
99
100 protected final XDoclet getXDoclet() {
101 return _xdoclet;
102 }
103
104 /***
105 * Callback method called by our superclass' execute() method. Override
106 * this method if you want to add aditional plugins or other functionality.
107 *
108 * @throws BuildException
109 */
110 public void execute() throws BuildException {
111 if (_isGui) {
112 showGui();
113 }
114
115 try {
116 _xdoclet.execute();
117 } catch (Exception e) {
118 LogFactory.getLog(XDoclet.class).error(e.getMessage(), e);
119 throw new BuildException(e.getMessage(), e);
120 }
121
122 // Unset the Ant Logger
123 System.getProperties().remove(LogFactory.FACTORY_PROPERTY);
124 }
125
126 /***
127 * Returns the classpath
128 *
129 * @return the classpath
130 */
131 protected String getClasspath()
132 {
133 return ( ( AntClassLoader ) getClass().getClassLoader() ).getClasspath();
134 }
135
136 ///////////// GUI stuff. Will be moved out /////////////
137
138 public void setGui(boolean flag) {
139 _isGui = flag;
140 }
141
142 private Object getGuiLock() {
143 return _guiLock;
144 }
145
146 private void showGui() {
147 BeanContextSupportEx xdocletContainer = new BeanContextSupportEx();
148
149 xdocletContainer.add(_xdoclet);
150
151 BeanContextConfigurationPanel bccp = new BeanContextConfigurationPanel(xdocletContainer);
152 JButton plus = new JButton(new PlusAction(bccp));
153
154 JPanel p = new JPanel(new BorderLayout());
155
156 p.add(plus, BorderLayout.NORTH);
157 p.add(bccp, BorderLayout.CENTER);
158
159 JFrame frame = new JFrame("XDoclet Configuration");
160
161 frame.addWindowListener(new WindowAdapter() {
162 public void windowClosing(WindowEvent evt) {
163 // Tell the halted Ant task to resume.
164 synchronized (getGuiLock()) {
165 getGuiLock().notify();
166 }
167 }
168 });
169 frame.getContentPane().add(p);
170 frame.pack();
171 frame.setVisible(true);
172
173 synchronized (getGuiLock()) {
174 try {
175 // wait here until gui notifies us (when it quits)
176 getGuiLock().wait();
177 } catch (InterruptedException e) {
178 e.printStackTrace();
179 }
180 }
181 }
182
183 public static class PlusAction extends AbstractAction {
184 private final BeanContextConfigurationPanel _bccp;
185
186 public PlusAction(BeanContextConfigurationPanel bccp) {
187 super("+");
188 _bccp = bccp;
189 }
190
191 public void actionPerformed(ActionEvent evt) {
192 _bccp.createChildInSelectedNode();
193 }
194 }
195 }
This page was automatically generated by Maven