1 /***
2 * Created on Nov 17, 2005, Copyright UC Regents
3 */
4 package org.telscenter.pas.authortool.context;
5
6 import java.io.IOException;
7 import java.lang.reflect.Constructor;
8 import java.lang.reflect.InvocationTargetException;
9 import java.net.URL;
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.HashMap;
13 import java.util.Iterator;
14
15 import javax.swing.Action;
16 import javax.swing.JFrame;
17
18 import net.sf.sail.common.apps.LaunchGenericSession;
19 import net.sf.sail.common.apps.preview.PreviewSessionConfiguration;
20 import net.sf.sail.core.beans.Pod;
21 import net.sf.sail.core.beans.assembly.PodRegistry;
22 import net.sf.sail.core.bundle.BundleManager;
23 import net.sf.sail.core.curnit.Curnit;
24 import net.sf.sail.core.service.ServiceContext;
25 import net.sf.sail.core.service.SessionManager;
26 import net.sf.sail.core.session.ICurnitProvider;
27 import net.sf.sail.core.session.SessionConfiguration;
28 import net.sf.sail.core.uuid.CurnitUuid;
29 import net.sf.sail.core.uuid.PodUuid;
30
31 import org.telscenter.pas.authortool.actions.HelpContextAction;
32 import org.telscenter.pas.authortool.actions.PasCustomizerAction;
33 import org.telscenter.pas.authortool.actions.PasLivePreviewCurnitAction;
34 import org.telscenter.pas.authortool.actions.PreferenceAction;
35 import org.telscenter.pas.authortool.actions.ReportIssueAction;
36 import org.telscenter.pas.authortool.cards.CardProvider;
37 import org.telscenter.pas.authortool.cards.customizer.pas.PasCustomizerCardX;
38 import org.telscenter.pas.authortool.cards.help.HelpCard;
39 import org.telscenter.pas.authortool.menu.EditMenu;
40 import org.telscenter.pas.authortool.menu.FileMenu;
41 import org.telscenter.pas.authortool.menu.HelpMenu;
42 import org.telscenter.pas.authortool.menu.MenuProvider;
43 import org.telscenter.pas.authortool.menu.ToolsMenu;
44 import org.telscenter.pas.authortool.statusbar.StatusBarProvider;
45
46 /***
47 * @author turadg
48 */
49 public class CurnitAuthoringContext {
50
51 private static final long serialVersionUID = 1L;
52
53 private Curnit workingCurnit;
54
55 private CardProvider cardProvider;
56
57 private Class[] cardClasses;
58
59 private IAuthoringSessionManager sessionManager;
60
61 private JFrame mainAuthoringFrame;
62
63 private MenuProvider menuProvider;
64
65 private Class[] menuClasses;
66
67 private StatusBarProvider statusBarProvider;
68
69 private HashMap<Class, Action> actions = new HashMap<Class, Action>();
70
71 public Class[] getCardClasses() {
72 return cardClasses;
73 }
74
75 public Class[] getMenuClasses(){
76 return menuClasses;
77 }
78 /***
79 * @throws IOException
80 */
81 public CurnitAuthoringContext(final IAuthoringSessionManager sessionManager)
82 throws IOException {
83 this.sessionManager = sessionManager;
84 this.assemble();
85
86 }
87
88 public void assemble() {
89 sessionManager.startAuthoringSession();
90 workingCurnit = sessionManager.getCurnit();
91 workingCurnit.assemble();
92
93
94 cardClasses = new Class[] { PasCustomizerCardX.class, HelpCard.class };
95 menuClasses = new Class[] {FileMenu.class, EditMenu.class, ToolsMenu.class,HelpMenu.class};
96
97
98
99 addActionClass(PasLivePreviewCurnitAction.class);
100 addActionClass(PasCustomizerAction.class);
101 addActionClass(HelpContextAction.class);
102
103 addActionClass(PreferenceAction.class);
104 addActionClass(ReportIssueAction.class);
105 }
106
107 public Curnit getAssembledCurnit() {
108 return workingCurnit;
109 }
110
111 /***
112 * Save the curnit to where it was last saved to (or loaded from)
113 */
114 public void saveCurnit() {
115 try {
116 sessionManager.save(workingCurnit);
117 } catch (IOException e) {
118
119 e.printStackTrace();
120 }
121 }
122
123 /***
124 * Return the first non Pod child of the root pod of
125 * the curnit
126 *
127 * @return
128 */
129 public Object getFirstRootBean() {
130 PodUuid rootPodId = workingCurnit.getRootPodId();
131 Pod rootPod = PodRegistry.getRegistry().getPod(rootPodId);
132 Iterator children = rootPod.iterator();
133 while(children.hasNext()) {
134 Object child = children.next();
135 if(!(child instanceof Pod)) {
136 return child;
137 }
138 }
139
140 return null;
141 }
142
143 /***
144 * Save the curnit to a new location
145 */
146 public void saveCurnitAs() {
147 try {
148 sessionManager.saveAs(workingCurnit);
149 } catch (IOException e) {
150
151 e.printStackTrace();
152 }
153 }
154
155 public CardProvider getCardProvider() {
156 if (cardProvider == null)
157 cardProvider = new CardProvider(this);
158 return cardProvider;
159 }
160
161 public void setCardProvider(CardProvider cardProvider) {
162 this.cardProvider = cardProvider;
163 }
164 public MenuProvider getMenuProvider() {
165 if( menuProvider == null ) {
166 menuProvider = new MenuProvider(this);
167 }
168 return menuProvider;
169 }
170
171 /***
172 * Preview the whole curnit, as if the learner had begun it
173 * @throws IOException
174 */
175 public void previewCurnitOld() throws IOException {
176 SessionConfiguration sp = new PreviewSessionConfiguration(workingCurnit);
177 LaunchGenericSession.launchSession(sp);
178 }
179
180 /***
181 * Preview the whole curnit, as if the learner had begun it, using
182 * the services architecture.
183 *
184 * @throws IOException
185 */
186 public void previewCurnit() throws IOException {
187 BundleManager bundleManager = new BundleManager();
188 ServiceContext serviceContext = bundleManager.getServiceContext();
189
190
191 /***
192 * Create the curnit provider for this curnit and add it to the
193 * service context
194 */
195 ICurnitProvider provider = new ICurnitProvider(){
196
197 public Curnit getCurnit(CurnitUuid curnitId) {
198 return workingCurnit;
199 }
200
201 public Collection getRegisteredCurnitUuids() {
202 ArrayList curnits = new ArrayList();
203 curnits.add(workingCurnit.getCurnitId());
204 return curnits;
205 }
206 };
207
208 serviceContext.addService(ICurnitProvider.class, provider);
209
210 /***
211 * Add the bundles configured in this xml file. The format of the file is XMLEncoder
212 */
213 URL bundlesXML = getClass().getResource("previewBundles.xml");
214 bundleManager.addBundles(bundlesXML.openStream());
215
216 /***
217 * Have all the bundles register their services, and then do any linking to other
218 * registered services
219 */
220 bundleManager.initializeBundles();
221
222 /***
223 * Start the session manager
224 */
225 SessionManager manager = (SessionManager)
226 serviceContext.getService(SessionManager.class);
227 manager.start(serviceContext);
228 }
229
230 /***
231 * @return the mainAuthoringFrame
232 */
233 public JFrame getMainAuthoringFrame() {
234 return mainAuthoringFrame;
235 }
236
237 /***
238 * @param mainAuthoringFrame the mainAuthoringFrame to set
239 */
240 public void setMainAuthoringFrame(JFrame mainAuthoringFrame) {
241 this.mainAuthoringFrame = mainAuthoringFrame;
242 }
243
244 public Action getAction(Class actionClass){
245 return actions.get(actionClass);
246 }
247
248 protected void addActionClass(Class actionClass){
249
250 try{
251 Class[] parameters = new Class[] { CurnitAuthoringContext.class };
252 Constructor constr = actionClass.getConstructor(parameters);
253 Object[] initargs = new Object[] { this };
254 Object instance = constr.newInstance(initargs);
255 actions.put(actionClass, (Action)instance);
256 } catch (SecurityException e) {
257 e.printStackTrace();
258 } catch (NoSuchMethodException e) {
259 e.printStackTrace();
260 } catch (IllegalArgumentException e) {
261 e.printStackTrace();
262 } catch (InstantiationException e) {
263 e.printStackTrace();
264 } catch (IllegalAccessException e) {
265 e.printStackTrace();
266 } catch (InvocationTargetException e) {
267 e.printStackTrace();
268 }
269 }
270
271 /***
272 * @return the statusBarProvider
273 */
274 public StatusBarProvider getStatusBarProvider() {
275 return statusBarProvider;
276 }
277
278 /***
279 * @param statusBarProvider the statusBarProvider to set
280 */
281 public void setStatusBarProvider(StatusBarProvider statusBarProvider) {
282 this.statusBarProvider = statusBarProvider;
283 }
284
285
286 }