View Javadoc

1   /*
2    * Created on Apr 27, 2005, Copyright UC Regents
3    */
4   package org.telscenter.pas.beans;
5   
6   import java.beans.PropertyChangeListener;
7   import java.beans.PropertyVetoException;
8   import java.beans.beancontext.BeanContext;
9   import java.beans.beancontext.BeanContextChildComponentProxy;
10  import java.beans.beancontext.BeanContextChildSupport;
11  import java.beans.beancontext.BeanContextServiceAvailableEvent;
12  import java.beans.beancontext.BeanContextServices;
13  import java.util.ArrayList;
14  import java.util.List;
15  
16  import net.sf.sail.common.beansupport.ITitleAware;
17  import net.sf.sail.core.util.SailBeanUtils;
18  
19  import org.telscenter.pas.hint.Hint;
20  import org.telscenter.pas.properties.HintSet;
21  
22  /***
23   * Abstract superclass of all beans representing Pas steps.
24   * 
25   * @author turadg
26   * @author Laurel Williams
27   * 
28   * @version $Id: PasStep.java 1817 2007-04-09 21:33:53 +0000 (Mon, 09 Apr 2007)
29   *          aperritano $
30   */
31  public abstract class PasStep extends BeanContextChildSupport implements
32  		BeanContextChildComponentProxy, IAuthorNotation, ITitleAware, IHelp {
33  
34  	private static final String HELP_ID = "step";
35  
36  	static int instanceCount;
37  
38  	// nth step in VM for identification in toString()
39  	int instanceNumber = instanceCount++;
40  
41  	String authorNotes = ""; //$NON-NLS-1$
42  
43  	boolean bcSet = false;
44  
45  	private HintSet hintSet = new HintSet();
46  
47  	String title = "UNDEFINED"; //$NON-NLS-1$
48  
49  	String hoverString = "Pas Step";
50  	
51  	/***
52  	 * Default constructor for Bean pattern
53  	 */
54  	public PasStep() {
55  		// nothing
56  	}
57  
58  	// helper
59  	public boolean hasHints() {
60  		if (hintSet == null) {
61  			return false;
62  		} else if (hintSet.isEmpty()) {
63  			return false;
64  		}// if
65  		return true;
66  	}
67  
68  	// added so the bean editor can detect which properties are bound
69  	public void addPropertyChangeListener(PropertyChangeListener listener) {
70  		pcSupport.addPropertyChangeListener(listener);
71  	}
72  
73  	/***
74  	 * <p>
75  	 * This gets called when the Component returned by getComponent() is added
76  	 * into a Container.
77  	 * </p>
78  	 * 
79  	 * @see beforeSessionStart
80  	 */
81  	public void afterComponentAdded() {
82  		// by default, do nothing
83  	}
84  
85  	/***
86  	 * <p>
87  	 * PasProject calls this method on each step after assembly() and before
88  	 * showing its frame. If the step has any heavy lifting to do, do it now so
89  	 * when the user reaches the step it comes up quickly.
90  	 * </p>
91  	 * <p>
92  	 * <b>NOTE:</b>: this will no be called in authoring, or any environments
93  	 * other than a learning session. It is only a hint and cannot be relied
94  	 * upon.
95  	 * </p>
96  	 */
97  	protected void beforeSessionStart() {
98  		// by default, do nothing
99  	}
100 
101 	protected void consumeService(BeanContextServices bcs, Class<? extends Object> serviceClass) {
102 		// by default do nothing
103 	}
104 
105 	// Navigation method - called by PasProject
106 	public void stepEnter() {
107 		// by default do nothing
108 	}
109 
110 	// Navigation method - called by PasProject
111 	public void stepExit() {
112 		// by default do nothing
113 	}
114 
115 	public String getAuthorNotes() {
116 		return authorNotes;
117 	}
118 
119 	public HintSet getHintSet() {
120 		return hintSet;
121 	}
122 
123 	public String getTitle() {
124 		return title;
125 	}
126 
127 	protected void initializeBeanContextResources() {
128 		if (!bcSet) {
129 			throw new IllegalStateException(
130 					"initializeBeanContextResources() called before setBeanContext()"); //$NON-NLS-1$
131 		}
132 		super.initializeBeanContextResources();
133 
134 		BeanContextServices bcs = SailBeanUtils.findServicesContextOf(this);
135 		bcs.addBeanContextServicesListener(this); // for consumeService
136 		initializePasStep(bcs);
137 	}
138 
139 	/***
140 	 * <p>
141 	 * Called by initializeBeanContextResources() during the unmarshalling
142 	 * phase, before the assembly phase. Thus, at this point the object can't
143 	 * make use of anything outside its own context Pod.
144 	 * </p>
145 	 * <p>
146 	 * By default, does nothing. Override in child.
147 	 * </p>
148 	 * 
149 	 * @param bcs
150 	 */
151 	protected void initializePasStep(BeanContextServices bcs) {
152 		// do nothing, override in child
153 	}
154 
155 	// added so the bean editor can detect which properties are bound
156 	public void removePropertyChangeListener(PropertyChangeListener listener) {
157 		pcSupport.removePropertyChangeListener(listener);
158 	}
159 
160 	public void serviceAvailable(BeanContextServiceAvailableEvent bcsae) {
161 		BeanContextServices bcs = SailBeanUtils.findServicesContextOf(this);
162 		Class<?> serviceClass = bcsae.getServiceClass();
163 
164 		consumeService(bcs, serviceClass);
165 	}
166 
167 	public void setAuthorNotes(String authorNotes) {
168 		Object old = this.authorNotes;
169 		this.authorNotes = authorNotes;
170 		firePropertyChange("authorNotes", old, authorNotes); //$NON-NLS-1$
171 	}
172 
173 	/***
174 	 * Replaced by <tt>hoverString</tt> property
175 	 * @deprecate
176 	 * @return
177 	 */
178 	public String getType() {
179 		return hoverString;
180 	}
181 
182 	/***
183 	 * @see org.telscenter.pas.beans.IHelp#getHelpId()
184 	 */
185 	public String getHelpId() {
186 		return HELP_ID;
187 	}
188 
189 	/***
190 	 * @see java.beans.beancontext.BeanContextChildSupport#setBeanContext(java.beans.beancontext.BeanContext)
191 	 */
192 	public synchronized void setBeanContext(BeanContext bc)
193 			throws PropertyVetoException {
194 		if (bc == null) {
195 			throw new NullPointerException("null BeanContext"); //$NON-NLS-1$
196 		}
197 		bcSet = true;
198 		super.setBeanContext(bc);
199 	}
200 
201 	public void setHintSet(HintSet hintSet) {
202 		HintSet old = this.hintSet;
203 		this.hintSet = hintSet;
204 		firePropertyChange("hintSet", old, hintSet); //$NON-NLS-1$
205 	}
206 
207 	public void setTitle(String title) {
208 		Object old = this.title;
209 		this.title = title;
210 		firePropertyChange("title", old, title);//$NON-NLS-1$
211 	}
212 
213 	public final String toString() {
214 		return getClass().getSimpleName()
215 				+ "(stepInstance#" + instanceNumber + ") " + getTitle(); //$NON-NLS-1$
216 	}
217 
218 	/***
219 	 * @return the hoverString
220 	 */
221 	public String getHoverString() {
222 		return hoverString;
223 	}
224 
225 	/***
226 	 * @param hoverString the hoverString to set
227 	 */
228 	public void setHoverString(String hoverString) {
229 		this.hoverString = hoverString;
230 	}
231 
232 }