View Javadoc

1   /*
2    * Created on Apr 25, 2005, Copyright UC Regents
3    */
4   package org.telscenter.pas.otrunk;
5   
6   import java.awt.event.ActionEvent;
7   import java.beans.beancontext.BeanContextServices;
8   import java.io.ByteArrayInputStream;
9   import java.io.ByteArrayOutputStream;
10  import java.io.StringReader;
11  import java.net.URL;
12  import java.util.TooManyListenersException;
13  import java.util.logging.Logger;
14  
15  import javax.swing.AbstractAction;
16  import javax.swing.Action;
17  
18  import net.sf.sail.common.beansupport.ITitleAware;
19  import net.sf.sail.common.beansupport.SailBeanContextChildSupport;
20  import net.sf.sail.core.beans.event.SessionEvent;
21  import net.sf.sail.core.beans.event.SessionEventListener;
22  import net.sf.sail.core.beans.service.AgentService;
23  import net.sf.sail.core.beans.service.SessionService;
24  import net.sf.sail.core.entity.IAgent;
25  import net.sf.sail.core.entity.ISock;
26  import net.sf.sail.core.entity.MismatchedAgentSetSizeException;
27  import net.sf.sail.core.entity.Rim;
28  import net.sf.sail.core.entity.Role;
29  import net.sf.sail.core.entity.UnsupportedRimShapeException;
30  
31  import org.concord.otrunk.datamodel.OTDatabase;
32  import org.concord.otrunk.view.OTViewer;
33  import org.concord.otrunk.view.OTViewerHelper;
34  import org.concord.otrunk.xml.XMLDatabase;
35  
36  /***
37   * Bean representing the PAS project.
38   * 
39   * @author turadg
40   */
41  public class PasOTViewer extends SailBeanContextChildSupport 
42  	implements ITitleAware
43  {
44  	static {
45  		// I don't know if this will be soon enough
46  		// but lets see
47          System.setProperty("apple.laf.useScreenMenuBar","true");
48  	}
49  	
50  	/***
51  	 * Logger for this class
52  	 */
53  	private static final Logger logger = Logger.getLogger(PasOTViewer.class
54  			.getName());
55  
56  	private static final long serialVersionUID = 1L;
57  	
58  	private URL authoredDataURL;
59  
60  	private AgentService agentService;
61  	private SessionService sessionService;
62  		
63  	private Rim workRim;
64  
65  	private ISock workSock;
66  
67  	private boolean hideTree = false;
68  	private int userMode = 0; 
69  		
70  	OTViewer viewer = null;
71  	OTViewerHelper viewerHelper = new OTViewerHelper();
72  	
73  	SessionEventListener sessionListener = new SessionEventListener() {
74  
75  		public void sessionInitiated(SessionEvent e) {
76  		}
77  
78  		public void sessionStarted(SessionEvent e) 
79  		{
80  			viewer = new OTViewer();
81  
82  			viewer.setUserMode(getUserMode());
83  			
84  			String authoredDataUrlStr = getAuthoredDataURL().toExternalForm();
85  			
86  			// Figure out if there is a url being set through the configuration
87  			String authoredDataConfigUrlStr = 
88  				sessionService.getProperty("sailotrunk.otmlurl", null);
89  			
90  			// In the future we can load both the config url string
91  			// and the interally set (pod) url string. So the data can 
92  			// be split up.  For now we'll just replace the old string
93  			// with the new one
94  			if(authoredDataConfigUrlStr != null) {
95  				authoredDataUrlStr = authoredDataConfigUrlStr;
96  			}
97  			
98  			
99  			viewer.init(authoredDataUrlStr);	
100 
101 			AbstractAction exitAction = new AbstractAction(){
102 				/***
103 				 * Not intended to be serialized, just added remove compile warning
104 				 */
105 				private static final long serialVersionUID = 1L;
106 				
107 				public void actionPerformed(ActionEvent e) {
108                     viewer.getViewContainerPanel().setCurrentObject(null);
109 					saveDataInternal();
110 					
111 					getSessionService().userRequestsTermination();
112 				}				
113 			};
114 			exitAction.putValue(Action.NAME, "Exit");		
115 			
116 			viewer.setExitAction(exitAction);
117 			
118 			viewer.updateMenuBar();
119 			
120 			if(viewer.getUserMode() == OTViewerHelper.NO_USER_MODE){
121 				// if we are in no user mode then we don't need to read data in from the 
122 				// sock or initialize the user data.
123 				return;
124 			}
125 			
126 			ISock sock = getWorkSock();
127 			
128 			Object lastEntry = null;
129 			if(sock != null && sock.size() > 0){
130 				lastEntry = sock.peek();
131 			}
132 			
133 			if(lastEntry != null){
134 				try {
135 					XMLDatabase userDB = null;
136 					if(lastEntry instanceof String) {
137 						StringReader reader = new StringReader((String)lastEntry);
138 
139 						// FIXME need to pass in some context url this is needed
140 						// for any referenced resources in the otml
141 						userDB = (XMLDatabase) viewerHelper.loadOTDatabase(reader, null);
142 
143 					} else if(lastEntry instanceof byte[]){
144 						ByteArrayInputStream inStream = new ByteArrayInputStream((byte[])lastEntry); 
145 
146 						userDB = (XMLDatabase) viewerHelper.loadOTDatabase(inStream, null);
147 					}										
148 					
149 					if(userDB != null){
150 						viewer.loadUserDataDb(userDB, null);
151 						((XMLDatabase)userDB).setDirty(false);
152 					} else {
153 						viewer.newAnonUserData();					
154 					}
155 				} catch (Exception exp) {
156 					exp.printStackTrace();
157 					viewer.newAnonUserData();
158 				}
159 			} else {
160 				viewer.newAnonUserData();
161 			}
162 		}
163 
164 		public void sessionStopped(SessionEvent e) {
165 			saveDataInternal();
166 		}
167 
168 	};
169 
170 	protected void registerDesiredServices(BeanContextServices bcs) {
171 	}
172 		
173 	
174 	/***
175 	 * This method can be called more than once in the authoring runtime, when
176 	 * the preview is shown more than once.
177 	 * 
178 	 * @param bcs
179 	 * @param serviceClass
180 	 * @param project
181 	 */
182 	protected void consumeService(BeanContextServices bcs, Class serviceClass) {
183 		if (serviceClass == SessionService.class) {
184 			try {
185 				sessionService = (SessionService) bcs.getService(this, this,
186 						SessionService.class, this, this);
187 
188 				sessionService.addSessionEventListener(sessionListener);
189 			} catch (TooManyListenersException e1) {
190 				// TODO Auto-generated catch block
191 				logger
192 						.severe("BeanContextServices, Class -  : exception: " + e1); //$NON-NLS-1$
193 			}
194 		}
195 		if (serviceClass == AgentService.class)
196 		{
197 			try
198 			{
199 				final AgentService p = (AgentService) bcs.getService(this,
200 						this, AgentService.class, this, this);
201 				agentService = p;
202 			}
203 			catch (TooManyListenersException e)
204 			{
205 				e.printStackTrace();
206 			}
207 		}
208 	}
209 	
210 	/***
211 	 * @return the sessionService
212 	 */
213 	public SessionService getSessionService() {
214 		return sessionService;
215 	}
216 
217 	/***
218 	 * @param sessionService the sessionService to set
219 	 */
220 	public void setSessionService(SessionService sessionService) {
221 		this.sessionService = sessionService;
222 	}
223 
224 	protected ISock getRimSock(Rim rim)
225 	{
226 		ISock sock = null;
227 		if (rim != null)
228 		{
229 			try {
230 				IAgent agent = agentService.getAgentsInRole(Role.RUN_WORKGROUP).getSingle();
231 				sock = agentService.getSock(rim, agent);
232 			} catch (MismatchedAgentSetSizeException e) {
233 				// TODO Auto-generated catch block
234 				e.printStackTrace();
235 			} catch (UnsupportedRimShapeException e) {
236 				// TODO Auto-generated catch block
237 				e.printStackTrace();
238 			}
239 		}
240 		return sock;
241 	}
242 
243 	public Rim getWorkRim() {
244 		return workRim;
245 	}
246 
247 	public void setWorkRim(Rim workRim) {
248 		this.workRim = workRim;
249 	}
250 
251 	public ISock getWorkSock() {
252 		if (workSock == null) {
253 			workSock = getRimSock(getWorkRim());
254 		}
255 		return workSock;
256 	}
257 
258 
259 	public URL getAuthoredDataURL()
260 	{
261 		return authoredDataURL;
262 	}
263 
264 	public void setAuthoredDataURL(URL url)
265 	{
266 		authoredDataURL = url;
267 	}
268 	
269 	public String getTitle() {
270 		return "SailOTViewer title";
271 	}
272 
273 
274 	public void setTitle(String title) {
275 	}
276 
277 
278 	public boolean isHideTree() {
279 		return hideTree;
280 	}
281 
282 
283 	public void setHideTree(boolean hideTree) {
284 		this.hideTree = hideTree;
285 	}
286 
287 
288 	public int getUserMode() {
289 		return userMode;
290 	}
291 
292 
293 	public void setUserMode(int userMode) {
294 		this.userMode = userMode;
295 	}	
296 
297 	private void saveDataInternal()
298 	{
299 		OTDatabase userDB = viewer.getUserDataDb();
300 
301 		if(userDB == null){
302 			// no user database was created 
303 			return;
304 		}
305 		
306 		// If the database hasn't been modified, don't
307 		// save anything
308 		if(userDB instanceof XMLDatabase &&
309 				!((XMLDatabase)userDB).isDirty()){
310 			return;
311 		}
312 		
313 		ISock sock = getWorkSock();
314 		try {
315 			if(sock != null && userDB != null){
316 
317 				// this is a hack to test this should be moved
318 				// to the converter
319 				//getWorkRim().setShape(bytes.getClass());
320 
321 				if(getWorkRim().getShape() == byte[].class){
322 					ByteArrayOutputStream out = new ByteArrayOutputStream();
323 					viewerHelper.saveOTDatabase(userDB, out);
324 					byte [] bytes = out.toByteArray();
325 					sock.add(bytes);
326 					if(userDB instanceof XMLDatabase){
327 						((XMLDatabase)userDB).setDirty(false);
328 					}
329 				} else {
330 					String userData = viewerHelper.saveOTDatabase(userDB);
331 					sock.add(userData);
332 				}
333 			} else {
334 				System.err.println("No sock in OTrunkStep - cannot save data");
335 			}
336 			
337 		} catch (Exception e1) {
338 			// TODO Auto-generated catch block
339 			e1.printStackTrace();
340 		}										
341 
342 	}
343 
344 }