View Javadoc

1   /*
2    * Created on Apr 28, 2005, Copyright UC Regents
3    */
4   package org.telscenter.pas.steps;
5   
6   import java.util.logging.Level;
7   import java.util.logging.Logger;
8   
9   import java.awt.BorderLayout;
10  import java.awt.Component;
11  import java.awt.Image;
12  import java.awt.Toolkit;
13  import java.beans.beancontext.BeanContextServices;
14  import java.io.IOException;
15  import java.io.Reader;
16  import java.io.StringReader;
17  import java.io.StringWriter;
18  import java.net.URL;
19  import java.util.EmptyStackException;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Locale;
23  import java.util.Map;
24  import java.util.MissingResourceException;
25  import java.util.Properties;
26  import java.util.ResourceBundle;
27  import java.util.TooManyListenersException;
28  
29  import javax.swing.BorderFactory;
30  import javax.swing.JEditorPane;
31  import javax.swing.JLabel;
32  import javax.swing.JPanel;
33  import javax.swing.JScrollPane;
34  import javax.swing.ScrollPaneConstants;
35  import javax.swing.SwingUtilities;
36  
37  import net.sf.sail.core.beans.service.AgentService;
38  import net.sf.sail.core.beans.service.AnnotationService;
39  import net.sf.sail.core.entity.IAgent;
40  import net.sf.sail.core.entity.ISock;
41  import net.sf.sail.core.entity.Rim;
42  import net.sf.sail.core.entity.Role;
43  
44  import org.apache.log4j.PropertyConfigurator;
45  import org.telscenter.pas.beans.IWorkReporter;
46  import org.telscenter.pas.beans.PasStep;
47  import org.telscenter.pas.service.NavigateAction;
48  import org.telscenter.pas.ui.util.PasColors;
49  import org.telstech.pedraw.ImageCache;
50  import org.telstech.pedraw.WDPanel;
51  import org.telstech.pedraw.io.IOHandler;
52  
53  
54  import com.lowagie.text.pdf.PdfPCell;
55  
56  /***
57   * Implementation of the Pas step type, <i>PasDraw 2</i>.
58   * 
59   * @author turadg
60   */
61  public class Pedraw extends PasStep implements IWorkReporter,
62  		IntroductionHtmlAware {
63  	
64  	private Integer possibleScore;
65  	
66  	/***
67  	 * Logger for this class
68  	 */
69  	private static final Logger logger = Logger.getLogger(Pedraw.class
70  			.getName());
71  
72  	private static final long serialVersionUID = 1L;
73  
74  	Rim<String> starterRim;
75  
76  	Rim<String> workRim;
77  
78  	String introductionHtml;
79  
80  	transient JPanel fullPanel;
81  
82  	public static final URL errorImageURL = ImageCache.class
83  			.getResource("images/error.gif"); //$NON-NLS-1$
84  
85  	private static final ResourceBundle languageStrings = ResourceBundle
86  			.getBundle("org.telstech.pedraw.l12n.language", new Locale("en", //$NON-NLS-1$ //$NON-NLS-2$
87  					"US")); //$NON-NLS-1$
88  
89  	private ISock<String> starterSock;
90  
91  	private ISock<String> workSock;
92  
93  	protected void initializePasStep(BeanContextServices bcs) {
94  		buildPanel();
95  	}
96  
97  	public String getType() {
98  		return "Pedraw";
99  	}
100 	
101 	protected void consumeService(BeanContextServices bcs, Class<? extends Object> serviceClass) {
102 		if (serviceClass == AgentService.class) {
103 			try {
104 				final AgentService p = (AgentService) bcs.getService(this,
105 						this, AgentService.class, this, this);
106 				setupIO(p);
107 			} catch (TooManyListenersException e) {
108 				// TODO Auto-generated catch block
109 				logger
110 						.severe("BeanContextServices, Class -  : exception: " + e); //$NON-NLS-1$
111 			}
112 		}
113 	}
114 
115 	class RimIOHandler extends IOHandler {
116 
117 		private ISock<String> sockToRead;
118 
119 		/***
120 		 * @param arg0
121 		 */
122 		public RimIOHandler() {
123 			super(languageStrings);
124 			if (workSock == null)
125 				throw new IllegalStateException("workSock cannot be null"); //$NON-NLS-1$
126 			if (starterSock != null)
127 				sockToRead = starterSock;
128 			else
129 				sockToRead = workSock;
130 		}
131 
132 		public void saveDocument() throws IOException {
133 			StringWriter sw = new StringWriter();
134 			writeDocument(sw);
135 			workSock.add(sw.toString());
136 
137 			// since there's something in it now
138 			sockToRead = workSock;
139 		}
140 
141 		public void loadDocument() throws IOException {
142 			String s;
143 			try {
144 				s = sockToRead.peek();
145 			} catch (EmptyStackException e) {
146 				// nothin' there
147 				return;
148 			}
149 			final Reader reader = new StringReader(s);
150 			try {
151 				Class.forName("com.microstar.xml.XmlParser"); //$NON-NLS-1$
152 			} catch (ClassNotFoundException e1) {
153 				e1.printStackTrace();
154 				throw new RuntimeException(e1);
155 			}
156 
157 			Runnable updateDisplay = new Runnable() {
158 				public void run() {
159 					try {
160 						readDocument(reader);
161 					} catch (IOException e) {
162 						throw new RuntimeException(e);
163 					}
164 				}
165 
166 			};
167 			SwingUtilities.invokeLater(updateDisplay);
168 		}
169 
170 	}
171 
172 	private void setupIO(AgentService p) {
173 		try {
174 			IAgent agent = p.getAgentsInRole(Role.RUN_WORKGROUP).getSingle();
175 			workSock = p.getSock(workRim, agent);
176 			if (starterRim != null)
177 				starterSock = p.getSock(starterRim, agent);
178 			org.telstech.pedraw.io.IOHandler ioh = new RimIOHandler();
179 			_wdPanel.setIOHandler(ioh);
180 		} catch (Exception e) {
181 			throw new RuntimeException("could not bind to rim", e); //$NON-NLS-1$
182 		}
183 	}
184 
185 	public Component getComponent() {
186 		if (fullPanel == null) {
187 			fullPanel = new JPanel();
188 			fullPanel.setLayout(new BorderLayout());
189 			JEditorPane instructionPane = new JEditorPane("text/html", //$NON-NLS-1$
190 					introductionHtml);
191 			instructionPane.setEditable(false);
192 			fullPanel.add(instructionPane, BorderLayout.NORTH);
193 			fullPanel.add(_wdPanel, BorderLayout.CENTER);
194 			try {
195 				IOHandler ioh = _wdPanel.getIOHandler();
196 				ioh.loadDocument();
197 			} catch (NullPointerException npe) {
198 				logger.severe("null IOHandler -  : exception: " + npe); //$NON-NLS-1$ 
199 			} catch (Exception e) {
200 				logger
201 						.severe("pedraw could not load from " + _wdPanel.getIOHandler() + " -  : exception: " + e); //$NON-NLS-1$ //$NON-NLS-2$
202 			}
203 		}
204 		
205 		JScrollPane spane  = new JScrollPane();
206 		spane.setOpaque(false);
207 		spane
208 				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
209 		
210 		spane
211 				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
212 		spane
213 				.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
214 		spane.getVerticalScrollBar().getModel().setValue(spane.getVerticalScrollBar().getModel().getMinimum());
215 		spane.getViewport().add(fullPanel);
216 		spane.requestFocusInWindow();
217 		spane.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
218 		
219 		return spane;
220 	}
221 
222 	WDPanel _wdPanel;
223 
224 	void buildPanel() {
225 
226 		if (logger.isLoggable(Level.CONFIG)) {
227 			logger.config("errorImageURL: " + errorImageURL); //$NON-NLS-1$
228 		} 
229 
230 		String[] toolNames = { "SelectTool", "PathTool", "LineTool", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
231 				"ArrowTool", "RectTool", "EllipseTool", "CircleTool", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
232 				"TextTool", "StampTool", "HyperlinkTool" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
233 
234 		// set up error image
235 		org.telstech.pedraw.ImageCache.clear();
236 		org.telstech.pedraw.ImageCache.setErrorImage(Toolkit
237 				.getDefaultToolkit().getImage(errorImageURL));
238 
239 		// setupBackgrounds();
240 		//
241 		// setupStamps();
242 
243 		// suppress logging in WDPanel
244 		Properties logProperties = new Properties();
245 		logProperties.put("log4j.rootLogger", "WARN"); //$NON-NLS-1$ //$NON-NLS-2$
246 		logProperties.put("log4j.appender.stdout", //$NON-NLS-1$
247 				"org.apache.log4j.ConsoleAppender"); //$NON-NLS-1$
248 		logProperties.put("log4j.appender.stdout.layout", //$NON-NLS-1$
249 				"org.apache.log4j.PatternLayout"); //$NON-NLS-1$
250 		PropertyConfigurator.configure(logProperties);
251 
252 		_wdPanel = new org.telstech.pedraw.WDPanel();
253 		_wdPanel.setLocaleResourceBundle(languageStrings);
254 
255 		_wdPanel.initialize();
256 
257 		setupTools(toolNames);
258 	}
259 
260 	/***
261 	 * set up tools and stamps
262 	 * 
263 	 * @param toolNames
264 	 */
265 	private void setupTools(String[] toolNames) {
266 		for (int i = 0; i < toolNames.length; ++i) {
267 			String key = "images/" + toolNames[i] + ".gif"; //$NON-NLS-1$ //$NON-NLS-2$
268 			URL resource = ImageCache.class.getResource(key);
269 			if (resource == null)
270 				throw new MissingResourceException("gif not available (" + key //$NON-NLS-1$
271 						+ ")", "Image", key); //$NON-NLS-1$ //$NON-NLS-2$
272 			Image toolImage = Toolkit.getDefaultToolkit().getImage(resource);
273 			_wdPanel.addTool(toolNames[i], toolImage);
274 			ImageCache.addStamp(toolNames[i], resource, toolImage);
275 		}
276 	}
277 
278 	public String getIntroductionHtml() {
279 		return introductionHtml;
280 	}
281 
282 	public void setIntroductionHtml(String introductionHtml) {
283 		Object old = this.introductionHtml;
284 		this.introductionHtml = introductionHtml;
285 		firePropertyChange("introductionHtml", old, introductionHtml); //$NON-NLS-1$
286 	}
287 
288 	public Rim<String> getStarterRim() {
289 		return starterRim;
290 	}
291 
292 	public void setStarterRim(Rim<String> starterRim) {
293 		Object old = this.starterRim;
294 		this.starterRim = starterRim;
295 		firePropertyChange("starterRim", old, starterRim); //$NON-NLS-1$
296 	}
297 
298 	public Rim<String> getWorkRim() {
299 		return workRim;
300 	}
301 
302 	public void setWorkRim(Rim<String> workRim) {
303 		Object old = this.workRim;
304 		this.workRim = workRim;
305 		firePropertyChange("workRim", old, workRim); //$NON-NLS-1$
306 	}
307 
308 	/***
309 	 * 
310 	 */
311 	public JPanel getReportForLearner() {
312 
313 		JPanel rootPanel = new JPanel(new BorderLayout(0, 0));
314 		rootPanel.setBackground(PasColors.pedDrawReportBackgroundColor);
315 
316 		if (workSock.isEmpty()) {
317 			JLabel noWorkLabel = new JLabel(Messages
318 					.getString("PasDraw2.NO_WORK_COMPLETED")); //$NON-NLS-1$
319 			noWorkLabel.setFont(noWorkLabel.getFont().deriveFont(14.0f));
320 			noWorkLabel.setForeground(PasColors.pedDrawReportNoWorkFontColor);
321 			noWorkLabel.setBorder(BorderFactory.createEmptyBorder(0, 8, 0, 0));
322 			rootPanel.add(noWorkLabel, BorderLayout.WEST);
323 		} else {
324 			rootPanel.add(new JLabel("SVG renderer broken"),
325 					BorderLayout.CENTER);
326 			// try {
327 			// Reader reader = SvgUtils.sockToReader(workSock);
328 			// SVGDocument document;
329 			// document = SvgUtils.read(reader);
330 			// JComponent component = SvgUtils.createSVGCanvas(document);
331 			// rootPanel.add(component,BorderLayout.CENTER);
332 			// } catch (Throwable t) {
333 			// t.printStackTrace();
334 			// rootPanel.add(new JLabel(t.toString()),BorderLayout.CENTER);
335 			// }
336 		}
337 		return rootPanel;
338 	}
339 
340 	/*
341 	 * (non-Javadoc)
342 	 * 
343 	 * @see pas2.beans.IWorkReporter#getReportForLearnerPDF()
344 	 */
345 	public PdfPCell getReportForLearnerPDF() {
346 		// TODO Auto-generated method stub
347 		return null;
348 	}
349 	
350 
351 	public JPanel getReportForLearnerWithAnnotations() {
352 		// TODO Auto-generated method stub
353 		return null;
354 	}
355 
356 	public List<JLabel> getCurrentAnswers() {
357 		// TODO Auto-generated method stub
358 		return null;
359 	}
360 
361 	public List<String> getCurrentPrompts() {
362 		// TODO Auto-generated method stub
363 		return null;
364 	}
365 
366 	public List<JPanel> getCurrentStepParts() {
367 		// TODO Auto-generated method stub
368 		return null;
369 	}
370 
371 	public void initStepParts() {
372 		// TODO Auto-generated method stub
373 		
374 	}
375 
376 	public Map<String, String> getEntityToPromptMap() {
377 		return new HashMap<String, String>();
378 	}
379 
380 	public Integer getPossibleScore() {
381 		return possibleScore;
382 	}
383 
384 	public void setPossibleScore(Integer possibleScore) {
385 		this.possibleScore = possibleScore;
386 	}
387 
388 	public JPanel getReportForLearner(NavigateAction navigateAction) {
389 		// TODO Auto-generated method stub
390 		return null;
391 	}
392 
393 	public JPanel getReportForLearnerWithAnnotations(
394 			NavigateAction navigateAction) {
395 		// TODO Auto-generated method stub
396 		return null;
397 	}
398 
399 	public PdfPCell getReportForLearnerPDF(AnnotationService annotationService) {
400 		return null;
401 	}
402 
403 }