View Javadoc

1   /***
2    * Created on Aug 5, 2005, Copyright UC Regents
3    */
4   package org.telscenter.pas.steps;
5   
6   import java.awt.BorderLayout;
7   import java.awt.Component;
8   import java.awt.image.BufferedImage;
9   import java.beans.beancontext.BeanContextServices;
10  import java.io.IOException;
11  import java.io.InputStream;
12  import java.io.StringBufferInputStream;
13  import java.util.EmptyStackException;
14  import java.util.TooManyListenersException;
15  import java.util.logging.Logger;
16  
17  import javax.swing.JEditorPane;
18  import javax.swing.JPanel;
19  import javax.swing.SwingUtilities;
20  
21  import net.sf.sail.core.beans.service.AgentService;
22  import net.sf.sail.core.beans.service.SessionService;
23  import net.sf.sail.core.entity.AgentSet;
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.User;
30  
31  import org.telscenter.pas.beans.PasStep;
32  import org.telstech.sensemaker.ControlPanel;
33  import org.telstech.sensemaker.DisplayPanel;
34  import org.telstech.sensemaker.SXML;
35  import org.telstech.sensemaker.SenseRoot;
36  import org.telstech.sensemaker.io.IDataHandler;
37  
38  /***
39   * @author turadg
40   */
41  public class Sensemaker extends PasStep implements IntroductionHtmlAware {
42  	/***
43  	 * Logger for this class
44  	 */
45  	private static final Logger logger = Logger.getLogger(Sensemaker.class
46  			.getName());
47  
48  	private static final long serialVersionUID = 1L;
49  
50  	Rim<String> starterRim;
51  
52  	Rim<String> workRim;
53  
54  	String introductionHtml = ""; //$NON-NLS-1$
55  
56  	String[] stockClaims = new String[0];
57  
58  	private JPanel _panel;
59  
60  	private ISock<String> starterSock;
61  
62  	private ISock<String> workSock;
63  
64  	private ControlPanel controlPanel;
65  
66  	private SessionService sessionService;
67  
68  	private AgentService agentService;
69  
70  	protected void initializePasStep(BeanContextServices bcs) {
71  		buildPanel();
72  	}
73  
74  	public String getType() {
75  		return "Sensemaker";
76  	}
77  
78  	protected void consumeService(BeanContextServices bcs, Class<? extends Object> serviceClass) {
79  		if (serviceClass == SessionService.class) {
80  			try {
81  				sessionService = (SessionService) bcs.getService(this, this,
82  						SessionService.class, this, this);
83  			} catch (TooManyListenersException e) {
84  				// TODO Auto-generated catch block
85  				logger
86  						.severe("BeanContextServices, Class -  : exception: " + e); //$NON-NLS-1$
87  			}
88  		} else if (serviceClass == AgentService.class) {
89  			try {
90  				agentService = (AgentService) bcs.getService(this, this,
91  						AgentService.class, this, this);
92  			} catch (TooManyListenersException e) {
93  				// TODO Auto-generated catch block
94  				logger
95  						.severe("BeanContextServices, Class -  : exception: " + e); //$NON-NLS-1$
96  			}
97  		}
98  
99  		if (agentService != null && sessionService != null)
100 			bindItemsToSocks();
101 	}
102 
103 	private void bindItemsToSocks() {
104 		// FIX assumes all users in the session are in the same workgroup
105 		User user = sessionService.getUsers().iterator().next();
106 		AgentSet agentsOfUser = agentService.getAgentsOfUser(user);
107 		AgentSet agentsOfUserInWorkgroup = agentsOfUser
108 				.select(Role.RUN_WORKGROUP);
109 		IAgent agent;
110 		try {
111 			agent = agentsOfUserInWorkgroup.getSingle();
112 		} catch (MismatchedAgentSetSizeException e1) {
113 			logger.severe("exception: " + e1); //$NON-NLS-1$
114 			return;
115 		}
116 
117 		try {
118 			workSock = agentService.getSock(workRim, agent);
119 			if (starterRim != null)
120 				starterSock = agentService.getSock(starterRim, agent);
121 			IDataHandler dataHandler = new RimDataHandler();
122 			controlPanel.setDataHandler(dataHandler);
123 		} catch (Exception e) {
124 			throw new RuntimeException("could not bind to rim", e); //$NON-NLS-1$
125 		}
126 	}
127 
128 	public Component getComponent() {
129 		return _panel;
130 	}
131 
132 	/***
133 	 * 
134 	 */
135 	private void buildPanel() {
136 		int width = 700;
137 		int height = 500;
138 		BufferedImage bimage = new BufferedImage(width, height,
139 				BufferedImage.TYPE_INT_RGB);
140 		BufferedImage bimage2 = new BufferedImage(width, height,
141 				BufferedImage.TYPE_INT_RGB);
142 
143 		JEditorPane introductionPane = new JEditorPane("text/html", //$NON-NLS-1$
144 				introductionHtml);
145 		introductionPane.setEditable(false);
146 		DisplayPanel displayPanel = new DisplayPanel(bimage, bimage2, width,
147 				height);
148 		controlPanel = new ControlPanel(displayPanel);
149 		controlPanel.setStockClaims(stockClaims);
150 
151 		// compose full UI
152 		_panel = new JPanel();
153 		_panel.setLayout(new BorderLayout());
154 		_panel.add(introductionPane, BorderLayout.NORTH);
155 		_panel.add(displayPanel, BorderLayout.CENTER);
156 		_panel.add(controlPanel, BorderLayout.SOUTH);
157 		_panel.validate();
158 	}
159 
160 	public String getIntroductionHtml() {
161 		return introductionHtml;
162 	}
163 
164 	public void setIntroductionHtml(String introductionHtml) {
165 		Object old = this.introductionHtml;
166 		this.introductionHtml = introductionHtml;
167 		firePropertyChange("introductionHtml", old, this.introductionHtml); //$NON-NLS-1$
168 
169 	}
170 
171 	public String[] getStockClaims() {
172 		return stockClaims;
173 	}
174 
175 	public void setStockClaims(String[] stockClaims) {
176 		String[] old = this.stockClaims;
177 		this.stockClaims = stockClaims;
178 		if (controlPanel != null)
179 			controlPanel.setStockClaims(stockClaims);
180 		firePropertyChange("introductionHtml", old, this.stockClaims); //$NON-NLS-1$
181 	}
182 
183 	public Rim<String> getStarterRim() {
184 		return starterRim;
185 	}
186 
187 	public void setStarterRim(Rim<String> starterRim) {
188 		Object old = this.starterRim;
189 		this.starterRim = starterRim;
190 		firePropertyChange("starterRim", old, starterRim); //$NON-NLS-1$
191 	}
192 
193 	public Rim<String> getWorkRim() {
194 		return workRim;
195 	}
196 
197 	public void setWorkRim(Rim<String> workRim) {
198 		Object old = this.workRim;
199 		this.workRim = workRim;
200 		firePropertyChange("workRim", old, workRim); //$NON-NLS-1$
201 	}
202 
203 	class RimDataHandler implements IDataHandler {
204 
205 		private ISock<String> sockToRead;
206 
207 		public RimDataHandler() {
208 			if (workSock == null)
209 				throw new IllegalStateException("workSock cannot be null"); //$NON-NLS-1$
210 			if (starterSock != null)
211 				sockToRead = starterSock;
212 			else
213 				sockToRead = workSock;
214 			logger.info("rim handler built, starterSock:" + starterSock //$NON-NLS-1$
215 					+ " workSock:" + workSock); //$NON-NLS-1$
216 		}
217 
218 		public void save(SenseRoot root) throws IOException {
219 			String data = root.toXML();
220 			workSock.add(data);
221 
222 			// since there's something in it now
223 			sockToRead = workSock;
224 		}
225 
226 		@SuppressWarnings("deprecation")
227 		public void load(final SenseRoot root) throws IOException {
228 			String s;
229 			try {
230 				s = sockToRead.peek();
231 			} catch (EmptyStackException e) {
232 				// nothin' there
233 				logger.severe("no document to load from sock " //$NON-NLS-1$
234 						+ sockToRead
235 						+ "\n// starterRim:" + starterRim + " workRim:" //$NON-NLS-1$ //$NON-NLS-2$
236 						+ workRim + "\n// starterSock:" + starterSock //$NON-NLS-1$
237 						+ " workSock:" + workSock); //$NON-NLS-1$
238 				return;
239 			}
240 			final InputStream is = new StringBufferInputStream(s);
241 			try {
242 				Class.forName("com.microstar.xml.XmlParser"); //$NON-NLS-1$
243 			} catch (ClassNotFoundException e1) {
244 				e1.printStackTrace();
245 				throw new RuntimeException(e1);
246 			}
247 
248 			Runnable updateDisplay = new Runnable() {
249 				public void run() {
250 					try {
251 						SXML.load(root, is);
252 						is.close();
253 					} catch (Exception e) {
254 						throw new RuntimeException(e);
255 					}
256 				}
257 
258 			};
259 			SwingUtilities.invokeLater(updateDisplay);
260 		}
261 
262 	}
263 
264 }