View Javadoc

1   /*
2    * Created on Apr 27, 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.Dimension;
9   import java.awt.FlowLayout;
10  import java.awt.event.ActionEvent;
11  import java.awt.event.ActionListener;
12  import java.io.ByteArrayInputStream;
13  import java.io.File;
14  import java.io.IOException;
15  import java.io.InputStream;
16  import java.net.MalformedURLException;
17  import java.net.URL;
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.Properties;
21  import java.util.StringTokenizer;
22  import java.util.logging.Level;
23  import java.util.logging.Logger;
24  import java.util.zip.ZipEntry;
25  import java.util.zip.ZipInputStream;
26  
27  import javax.swing.JButton;
28  import javax.swing.JFrame;
29  import javax.swing.JPanel;
30  import javax.swing.JScrollPane;
31  
32  import net.sf.sail.common.beansupport.DynamicObject;
33  import net.sf.sail.core.beans.InvalidPropertyException;
34  import net.sf.sail.core.util.BinaryUtils;
35  
36  /***
37   * Implementation of the Pas step type, <i>Pedagogica</i>.
38   * 
39   * @author turadg
40   */
41  public class ConcordModelState extends AbstractUrlStep implements
42  		IntroductionHtmlAware {
43  	/***
44  	 * Logger for this class
45  	 */
46  	private static final Logger logger = Logger
47  			.getLogger(ConcordModelState.class.getName());
48  
49  	DynamicObject dynamic = new DynamicObject();
50  
51  	String classRunActivity = "org.concord.pedagogica.ui.RunActivity"; //$NON-NLS-1$
52  
53  	String activityPath;
54  
55  	Boolean authoring;
56  
57  	private static final long serialVersionUID = 1L;
58  
59  	protected JButton launchButton = new JButton(Messages
60  			.getString("ConcordModelState.launch_button")); //$NON-NLS-1$
61  
62  	protected JPanel buttonPanel = new JPanel();
63  
64  	Content content;
65  
66  	String codeBasePath;
67  
68  	private boolean initialized;
69  
70  	private String introductionHtml;
71  
72  	public ConcordModelState() {
73  	}
74  
75  	public Component getComponent() {
76  		if (!initialized)
77  			initialize();
78  		return new JScrollPane(content);
79  	}
80  
81  	public String getCodeBase() {
82  		return codeBasePath;
83  	}
84  
85  	public void setCodeBase(String path) {
86  		Object old = this.codeBasePath;
87  		this.codeBasePath = path;
88  		firePropertyChange("codeBase", old, codeBasePath); //$NON-NLS-1$
89  	}
90  
91  	// this gets called before the project UI shows up so users don't
92  	// have to wait for it when they click this step -turadg
93  	public void beforeSessionStart() {
94  		initialize();
95  		dynamic.invoke("start"); //$NON-NLS-1$
96  	}
97  
98  	protected void initialize() {
99  		if (initialized)
100 			return;
101 		if (codeBasePath == null)
102 			throw new InvalidPropertyException(this, "codeBasePath"); //$NON-NLS-1$
103 		try {
104 			File tempFile = BinaryUtils.tempFileForUrl(getUrl());
105 			activityPath = tempFile.toString();
106 			if (logger.isLoggable(Level.CONFIG)) {
107 				logger.config("activityPath = " + activityPath); //$NON-NLS-1$
108 			}
109 			dynamic.setInstanceClass(getClass().getClassLoader(),
110 					classRunActivity);
111 			content = new Content();
112 			Object[] paneArgs = { content };
113 			dynamic.invoke("setContentPane", paneArgs); //$NON-NLS-1$
114 			if (activityPath != null) {
115 				Object[] pathArgs = { activityPath };
116 				dynamic.invoke("setActivityPath", pathArgs); //$NON-NLS-1$
117 			}
118 			if (authoring != null) {
119 				Object[] modeArgs = { authoring };
120 				dynamic.invoke("setAuthoringMode", modeArgs); //$NON-NLS-1$
121 			}
122 			initialized = true;
123 		} catch (IOException e) {
124 			// TODO Auto-generated catch block
125 			logger.severe("exception: " + e); //$NON-NLS-1$
126 		}
127 	}
128 
129 	public class Content extends JPanel implements ActionListener {
130 		public Content() {
131 			setPreferredSize(new Dimension(800, 600));
132 			setLayout(new BorderLayout());
133 			buttonPanel.setLayout(new FlowLayout());
134 			buttonPanel.add(launchButton);
135 			add(buttonPanel, BorderLayout.NORTH);
136 			launchButton.addActionListener(this);
137 		}
138 
139 		public void addNotify() {
140 			super.addNotify();
141 		}
142 
143 		public void actionPerformed(ActionEvent event) {
144 			dynamic.invoke("start"); //$NON-NLS-1$
145 			JFrame frame = (JFrame) dynamic.invoke("getFrame"); //$NON-NLS-1$
146 			frame.setVisible(true);
147 		}
148 	}
149 
150 	public String getIntroductionHtml() {
151 		return introductionHtml;
152 	}
153 
154 	public void setIntroductionHtml(String introductionHtml) {
155 		Object old = this.introductionHtml;
156 		this.introductionHtml = introductionHtml;
157 		firePropertyChange("introductionHtml", old, this.introductionHtml); //$NON-NLS-1$
158 	}
159 
160 	protected InputStream getURLStream(URL url) {
161 		try {
162 			return url.openStream();
163 		} catch (IOException e) {
164 			logger.severe("URL -  : exception: " + e); //$NON-NLS-1$
165 		}
166 		return null;
167 	}
168 
169 	protected InputStream getURLStream(String urlString) {
170 		try {
171 			URL url = new URL(urlString);
172 			return getURLStream(url);
173 		} catch (MalformedURLException e) {
174 			logger.severe("String -  : exception: " + e); //$NON-NLS-1$
175 		}
176 		return null;
177 	}
178 	
179 	public String getType() {
180 		return "Concord Model";
181 	}
182 	
183 
184 	protected static List<String> getJarList(String path) {
185 		List<String> jarList = new ArrayList<String>();
186 		try {
187 			URL url = new URL(path);
188 			ZipInputStream zipInput = new ZipInputStream(url.openStream());
189 			ZipEntry entry = zipInput.getNextEntry();
190 			while (entry != null) {
191 				if (entry.getName().endsWith("classes.properties")) //$NON-NLS-1$
192 				{
193 					int size = (int) entry.getSize();
194 					byte[] bytes = new byte[size];
195 					zipInput.read(bytes);
196 					ByteArrayInputStream bytesIn = new ByteArrayInputStream(
197 							bytes);
198 					Properties properties = new Properties();
199 					properties.load(bytesIn);
200 					@SuppressWarnings("unused")
201 					String classPath = properties.getProperty("class.path"); //$NON-NLS-1$
202 					StringTokenizer tokens = new StringTokenizer(";"); //$NON-NLS-1$
203 					while (tokens.hasMoreTokens()) {
204 						jarList.add(tokens.nextToken());
205 					}
206 					return jarList;
207 				}
208 			}
209 		} catch (MalformedURLException e) {
210 			logger.severe("String -  : exception: " + e); //$NON-NLS-1$
211 		} catch (IOException e) {
212 			logger.severe("String -  : exception: " + e); //$NON-NLS-1$
213 		}
214 		return jarList;
215 	}
216 
217 }