KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > mbtf > v1 > ant > TransitionType


1 /*
2  * @(#)TransitionType.java
3  *
4  * Copyright (C) 2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Part of the GroboUtils package at:
9  * http://groboutils.sourceforge.net
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in
19  * all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  */

29 package net.sourceforge.groboutils.mbtf.v1.ant;
30
31 import org.apache.tools.ant.Project;
32 import org.apache.tools.ant.ProjectComponent;
33 import org.apache.tools.ant.Task;
34 import org.apache.tools.ant.TaskAdapter;
35 import org.apache.tools.ant.BuildException;
36 import org.apache.tools.ant.types.Reference;
37 import org.apache.tools.ant.types.DataType;
38 import org.apache.tools.ant.taskdefs.Definer;
39
40 import java.util.Vector JavaDoc;
41
42 import net.sourceforge.groboutils.mbtf.v1.IValidate;
43 import net.sourceforge.groboutils.mbtf.v1.ISystem;
44 import net.sourceforge.groboutils.mbtf.v1.IAction;
45
46 import net.sourceforge.groboutils.mbtf.v1.assembler.AsmblTransition;
47
48 import org.apache.log4j.Logger;
49
50
51 /**
52  * An ant datatype encapsulating the IState instance. This includes an
53  * <tt>execute()</tt> method to allow for this to be defined in a
54  * &lt;typedef&gt; declaration, rather than inside a target as a
55  * &lt;datatype&gt; declaration.
56  *
57  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
58  * @version $Date: 2003/02/10 22:52:25 $
59  * @since June 13, 2002
60  */

61 public class TransitionType extends AbstractStateMachineType
62 {
63     private static final Logger LOG = Logger.getLogger( TransitionType.class );
64     
65     public static final String JavaDoc DEFAULT_ANT_NAME = "mbtf.transition";
66     
67     private Vector JavaDoc transitions = new Vector JavaDoc();
68     private String JavaDoc nextState;
69     private IAction action;
70     
71     
72     public void setNextState( String JavaDoc name )
73     {
74         this.nextState = name;
75     }
76     
77     
78     public String JavaDoc getNextState()
79     {
80         if (isReference())
81         {
82             return ((TransitionType)getCheckedRef(
83                 TransitionType.class, DEFAULT_ANT_NAME )).
84                 getNextState();
85         }
86         return this.nextState;
87     }
88     
89     
90     public void addAntAction( ValidateTaskType action )
91             throws BuildException
92     {
93         if (this.action != null)
94         {
95             throw new BuildException(
96                 "too many action elements: only one allowed" );
97         }
98         this.action = action;
99     }
100     
101     
102     public void addJavaAction( ValidateJavaType action )
103             throws BuildException
104     {
105         if (this.action != null)
106         {
107             throw new BuildException(
108                 "too many action elements: only one allowed" );
109         }
110         LOG.debug("Adding ValidateJavaType");
111         this.action = action;
112     }
113     
114     
115     public void setAntActionRef( Reference ref )
116     {
117         ValidateTaskType vtt = new ValidateTaskType();
118         vtt.setProject( project );
119         vtt.setRefid( ref );
120         
121         addAntAction( vtt );
122     }
123     
124     
125     public void setJavaActionRef( Reference ref )
126     {
127         Object JavaDoc o = ref.getReferencedObject( project );
128         if (o != null && o instanceof TaskAdapter)
129         {
130             ((TaskAdapter)o).execute();
131             o = ((TaskAdapter)o).getProxy();
132         }
133         addJavaAction( (ValidateJavaType)o );
134     }
135     
136     
137     public IAction getAction()
138     {
139         if (isReference())
140         {
141             return ((TransitionType)getCheckedRef(
142                 TransitionType.class, DEFAULT_ANT_NAME )).
143                 getAction();
144         }
145         if (project != null)
146         {
147             if (this.action instanceof ProjectComponent)
148             {
149                 LOG.debug("Setting action "+this.action+"'s project to "+
150                     getProject());
151                 ((ProjectComponent)this.action).setProject( getProject() );
152             }
153             else
154             {
155                 LOG.debug("Action "+this.action+
156                     " is not a ProjectComponent, but "+this.action.getClass().
157                     getName());
158             }
159         }
160         else
161         {
162             LOG.debug("Transition "+getName()+" has null project");
163         }
164         return this.action;
165     }
166     
167     
168     public AsmblTransition createTransition()
169     {
170         if (isReference())
171         {
172             return ((TransitionType)getCheckedRef(
173                 TransitionType.class, DEFAULT_ANT_NAME )).
174                 createTransition();
175         }
176         verifySettings();
177         
178         AsmblTransition at = new AsmblTransition();
179         at.setName( getName() );
180         at.setNextStateName( getNextState() );
181         at.setAction( getAction() );
182
183         IValidate v[] = getValidates();
184         for (int i = 0; i < v.length; ++i)
185         {
186             at.addValidate( v[i] );
187         }
188         
189         return at;
190     }
191     
192     
193     protected void verifySettings()
194             throws BuildException
195     {
196         if (isReference())
197         {
198             ((TransitionType)getCheckedRef(
199                 TransitionType.class, DEFAULT_ANT_NAME )).
200                 verifySettings();
201             return;
202         }
203         super.verifySettings();
204         if (this.nextState == null)
205         {
206             throw new BuildException( "next state name not set", location );
207         }
208         if (this.action == null)
209         {
210             throw new BuildException( "next state name not set", location );
211         }
212     }
213 }
214
215
Popular Tags