1 5 package com.tc.welcome; 6 7 import org.dijon.ApplicationManager; 8 import org.dijon.Frame; 9 import org.dijon.ScrollPane; 10 import org.dijon.TextPane; 11 12 import com.tc.admin.TCStop; 13 import com.tc.admin.common.BrowserLauncher; 14 import com.tc.admin.common.Splash; 15 import com.tc.admin.common.StreamReader; 16 import com.tc.admin.common.TextPaneUpdater; 17 import com.tc.admin.common.XTextPane; 18 import com.tc.util.ResourceBundleHelper; 19 20 import java.awt.BorderLayout ; 21 import java.awt.Color ; 22 import java.awt.Container ; 23 import java.awt.Cursor ; 24 import java.awt.Dimension ; 25 import java.awt.Rectangle ; 26 import java.awt.event.ActionEvent ; 27 import java.awt.event.ActionListener ; 28 import java.awt.event.WindowAdapter ; 29 import java.awt.event.WindowEvent ; 30 import java.beans.PropertyChangeEvent ; 31 import java.beans.PropertyChangeListener ; 32 import java.io.File ; 33 import java.io.IOException ; 34 import java.io.InputStream ; 35 import java.util.ArrayList ; 36 import java.util.Iterator ; 37 38 import javax.swing.Timer ; 39 import javax.swing.UIManager ; 40 import javax.swing.event.HyperlinkEvent ; 41 import javax.swing.event.HyperlinkListener ; 42 import javax.swing.text.AttributeSet ; 43 import javax.swing.text.Document ; 44 import javax.swing.text.Element ; 45 import javax.swing.text.html.HTML ; 46 import javax.swing.text.html.HTMLEditorKit ; 47 48 public class DSOSamplesFrame extends HyperlinkFrame implements HyperlinkListener , PropertyChangeListener { 49 private ResourceBundleHelper m_bundleHelper = new ResourceBundleHelper(DSOSamplesFrame.class); 50 51 private TextPane m_textPane; 52 private TextPane m_outputPane; 53 private ArrayList m_processList; 54 55 public DSOSamplesFrame() { 56 super(); 57 58 setTitle(m_bundleHelper.getString("frame.title")); 59 60 Container cp = getContentPane(); 61 cp.setLayout(new BorderLayout ()); 62 63 m_textPane = new TextPane(); 64 m_textPane.setBackground(Color.WHITE); 65 cp.add(new ScrollPane(m_textPane)); 66 m_textPane.setEditable(false); 67 m_textPane.addHyperlinkListener(this); 68 m_textPane.addPropertyChangeListener("page", this); 69 70 m_processList = new ArrayList (); 71 m_outputPane = new XTextPane(); 72 m_outputPane.setForeground(Color.GREEN); 73 m_outputPane.setBackground(Color.BLACK); 74 ScrollPane scroller = new ScrollPane(m_outputPane); 75 scroller.setPreferredSize(new Dimension (500, 200)); 76 cp.add(scroller, BorderLayout.SOUTH); 77 runServer(); 78 79 try { 80 m_textPane.setPage(getClass().getResource("SamplesPojo.html")); 81 } catch (IOException ioe) { 82 m_textPane.setText(ioe.getMessage()); 83 } 84 } 85 86 private void toOutputPane(String s) { 87 Document doc = m_outputPane.getDocument(); 88 89 try { 90 doc.insertString(doc.getLength(), s + "\n", null); 91 } catch (Exception e) { 92 } 93 } 94 95 protected void quit() { 96 stopProcesses(); 97 } 98 99 private StreamReader createStreamReader(InputStream stream) { 100 return new StreamReader(stream, new TextPaneUpdater(m_outputPane), null, null); 101 } 102 103 private StreamReader createStreamReader(InputStream stream, TextPane textPane) { 104 return new StreamReader(stream, new TextPaneUpdater(textPane), null, null); 105 } 106 107 private void stopProcesses() { 108 String host = "localhost"; 109 int port = 9520; 110 111 try { 112 new TCStop(host, port).stop(); 113 } catch (Exception e) { 114 toOutputPane(e.getMessage()); 115 } 116 117 Iterator iter = m_processList.iterator(); 118 while (iter.hasNext()) { 119 Process p = (Process ) iter.next(); 120 121 try { 122 p.exitValue(); 123 } catch (Exception e) { 124 p.destroy(); 125 } 126 } 127 128 new Thread () { 129 public void run() { 130 TCStop tester = new TCStop("localhost", 9520); 131 132 while (true) { 133 try { 134 sleep(2000); 135 tester.stop(); 136 } catch (Exception e) { 137 DSOSamplesFrame.super.quit(); 138 } 139 } 140 } 141 }.start(); 142 } 143 144 private void runServer() { 145 try { 146 File bootFile = new File (getBootPath()); 147 148 if (bootFile.exists()) { 149 internalRunServer(); 150 } else { 151 createBootJar(); 152 } 153 } catch (Exception e) { 154 e.printStackTrace(); 155 } 156 } 157 158 private void createBootJar() { 159 String [] cmdarray = { getJavaCmd().getAbsolutePath(), "-Dtc.install-root=" + getInstallRoot().getAbsolutePath(), 160 "-cp", getTCLib().getAbsolutePath(), "com.tc.object.tools.BootJarTool" }; 161 162 final Process p = exec(cmdarray, null, getSamplesDir()); 163 StreamReader errDrainer = createStreamReader(p.getErrorStream()); 164 StreamReader outDrainer = createStreamReader(p.getInputStream()); 165 166 errDrainer.start(); 167 outDrainer.start(); 168 169 new Thread () { 170 public void run() { 171 while (true) { 172 try { 173 p.waitFor(); 174 break; 175 } catch (Exception e) { 176 } 177 } 178 internalRunServer(); 179 } 180 }.start(); 181 } 182 183 private void internalRunServer() { 184 String [] cmdarray = { getJavaCmd().getAbsolutePath(), "-Dtc.config=tc-config.xml", 185 "-Dtc.install-root=" + getInstallRoot().getAbsolutePath(), "-cp", getTCLib().getAbsolutePath(), 186 "com.tc.server.TCServerMain" }; 187 188 Process p = exec(cmdarray, null, getSamplesDir()); 189 StreamReader errDrainer = createStreamReader(p.getErrorStream()); 190 StreamReader outDrainer = createStreamReader(p.getInputStream()); 191 192 errDrainer.start(); 193 outDrainer.start(); 194 } 195 196 private void runSample(String dirName, String className) { 197 setTextPaneCursor(Cursor.WAIT_CURSOR); 198 try { 199 String bootPath = getBootPath(); 200 String [] cmdarray = { getJavaCmd().getAbsolutePath(), "-Dtc.config=tc-config.xml", 201 "-Djava.awt.Window.locationByPlatform=true", "-Dtc.install-root=" + getInstallRoot().getAbsolutePath(), 202 "-Xbootclasspath/p:" + bootPath, "-cp", "classes", className }; 203 204 Process p = exec(cmdarray, null, new File (getProductDirectory(), dirName)); 205 StreamReader errDrainer = createStreamReader(p.getErrorStream()); 206 StreamReader outDrainer = createStreamReader(p.getInputStream()); 207 208 errDrainer.start(); 209 outDrainer.start(); 210 211 m_processList.add(p); 212 startFakeWaitPeriod(); 213 } catch (Exception e) { 214 e.printStackTrace(); 215 } 216 } 217 218 private void runCoordinationSample() { 219 setTextPaneCursor(Cursor.WAIT_CURSOR); 220 try { 221 String bootPath = getBootPath(); 222 File dir = new File (getProductDirectory(), "coordination"); 223 File lib = new File (dir, "lib"); 224 File oswego = new File (lib, "concurrent-1.3.4.jar"); 225 String classpath = "classes" + System.getProperty("path.separator") + oswego.getAbsolutePath(); 226 String [] cmdarray = { getJavaCmd().getAbsolutePath(), "-Dtc.config=tc-config.xml", 227 "-Djava.awt.Window.locationByPlatform=true", "-Dtc.install-root=" + getInstallRoot().getAbsolutePath(), 228 "-Xbootclasspath/p:" + bootPath, "-cp", classpath, "demo.coordination.Main" }; 229 230 final Process p = exec(cmdarray, null, dir); 231 XTextPane textPane = new XTextPane(); 232 StreamReader errDrainer = createStreamReader(p.getErrorStream(), textPane); 233 StreamReader outDrainer = createStreamReader(p.getInputStream(), textPane); 234 235 errDrainer.start(); 236 outDrainer.start(); 237 238 m_processList.add(p); 239 startFakeWaitPeriod(); 240 241 Frame frame = new SampleFrame(this, m_bundleHelper.getString("jvm.coordination")); 242 frame.getContentPane().add(new ScrollPane(textPane)); 243 frame.setSize(new Dimension (500, 300)); 244 frame.setVisible(true); 245 frame.addWindowListener(new WindowAdapter () { 246 public void windowClosing(WindowEvent we) { 247 try { 248 p.destroy(); 249 } catch (Exception e) { 250 } 251 m_processList.remove(p); 252 } 253 }); 254 } catch (Exception e) { 255 e.printStackTrace(); 256 } 257 } 258 259 private void runSharedQueueSample() { 260 setTextPaneCursor(Cursor.WAIT_CURSOR); 261 try { 262 String bootPath = getBootPath(); 263 File dir = new File (getProductDirectory(), "sharedqueue"); 264 String pathSep = System.getProperty("path.separator"); 265 String fileSep = System.getProperty("file.separator"); 266 String classpath = "classes" + pathSep + "lib" + fileSep + "javax.servlet.jar" + pathSep + "lib" + fileSep 267 + "org.mortbay.jetty-4.2.20.jar"; 268 String [] cmdarray = { getJavaCmd().getAbsolutePath(), "-Dtc.config=tc-config.xml", 269 "-Djava.awt.Window.locationByPlatform=true", "-Dtc.install-root=" + getInstallRoot().getAbsolutePath(), 270 "-Xbootclasspath/p:" + bootPath, "-cp", classpath, "demo.sharedqueue.Main" }; 271 272 final Process p = exec(cmdarray, null, dir); 273 XTextPane textPane = new XTextPane(); 274 StreamReader errDrainer = createStreamReader(p.getErrorStream(), textPane); 275 StreamReader outDrainer = createStreamReader(p.getInputStream(), textPane); 276 277 errDrainer.start(); 278 outDrainer.start(); 279 280 m_processList.add(p); 281 startFakeWaitPeriod(); 282 283 Frame frame = new SampleFrame(this, m_bundleHelper.getString("shared.work.queue")); 284 frame.getContentPane().add(new ScrollPane(textPane)); 285 frame.setSize(new Dimension (500, 300)); 286 frame.setVisible(true); 287 frame.addWindowListener(new WindowAdapter () { 288 public void windowClosing(WindowEvent we) { 289 try { 290 p.destroy(); 291 } catch (Exception e) { 292 } 293 m_processList.remove(p); 294 } 295 }); 296 } catch (Exception e) { 297 e.printStackTrace(); 298 } 299 } 300 301 public void hyperlinkUpdate(HyperlinkEvent e) { 302 HyperlinkEvent.EventType type = e.getEventType(); 303 Element elem = e.getSourceElement(); 304 305 if (elem == null || 306 type == HyperlinkEvent.EventType.ENTERED || 307 type == HyperlinkEvent.EventType.EXITED) { 308 return; 309 } 310 311 if (m_textPane.getCursor().getType() != Cursor.WAIT_CURSOR) { 312 AttributeSet a = elem.getAttributes(); 313 AttributeSet anchor = (AttributeSet ) a.getAttribute(HTML.Tag.A); 314 String action = (String ) anchor.getAttribute(HTML.Attribute.HREF); 315 316 hyperlinkActivated(anchor, action); 317 } 318 } 319 320 private void hyperlinkActivated(AttributeSet anchor, String action) { 321 if (action.equals("run_jtable")) { 322 toOutputPane(m_bundleHelper.getString("starting.jtable")); 323 runSample("jtable", "demo.jtable.Main"); 324 } else if (action.equals("run_sharededitor")) { 325 toOutputPane(m_bundleHelper.getString("starting.shared.editor")); 326 runSample("sharededitor", "demo.sharededitor.Main"); 327 } else if (action.equals("run_chatter")) { 328 toOutputPane(m_bundleHelper.getString("starting.chatter")); 329 runSample("chatter", "demo.chatter.Main"); 330 } else if (action.equals("run_coordination")) { 331 toOutputPane(m_bundleHelper.getString("starting.jvm.coordination")); 332 runCoordinationSample(); 333 } else if (action.equals("run_sharedqueue")) { 334 toOutputPane(m_bundleHelper.getString("starting.shared.queue")); 335 runSharedQueueSample(); 336 } else if (action.equals("view_readme")) { 337 String name = (String ) anchor.getAttribute(HTML.Attribute.NAME); 338 File dir = new File (getProductDirectory(), name); 339 File file = new File (dir, "readme.html"); 340 341 openURL("file://" + file.getAbsolutePath()); 342 } else if (action.equals("browse_source")) { 343 String name = (String ) anchor.getAttribute(HTML.Attribute.NAME); 344 File sampleDir = new File (getProductDirectory(), name); 345 File sampleDocs = new File (sampleDir, "docs"); 346 File index = new File (sampleDocs, "source.html"); 347 348 openURL("file://" + index.getAbsolutePath()); 349 } else { 350 openURL(action); 351 } 352 } 353 354 private void setTextPaneCursor(int type) { 355 Cursor c = Cursor.getPredefinedCursor(type); 356 HTMLEditorKit kit = (HTMLEditorKit ) m_textPane.getEditorKit(); 357 358 m_textPane.setCursor(c); 359 kit.setDefaultCursor(c); 360 361 int linkType = (type == Cursor.WAIT_CURSOR) ? Cursor.WAIT_CURSOR : Cursor.HAND_CURSOR; 362 kit.setLinkCursor(Cursor.getPredefinedCursor(linkType)); 363 } 364 365 private void openURL(String url) { 366 setTextPaneCursor(Cursor.WAIT_CURSOR); 367 BrowserLauncher.openURL(url); 368 startFakeWaitPeriod(); 369 } 370 371 private void startFakeWaitPeriod() { 372 Timer t = new Timer (3000, new ActionListener () { 373 public void actionPerformed(ActionEvent ae) { 374 setTextPaneCursor(Cursor.DEFAULT_CURSOR); 375 } 376 }); 377 t.setRepeats(false); 378 t.start(); 379 } 380 381 private File getProductDirectory() { 382 return new File (getSamplesDir(), "pojo"); 383 } 384 385 public void propertyChange(PropertyChangeEvent pce) { 386 Timer t = new Timer (2000, new ActionListener () { 387 public void actionPerformed(ActionEvent ae) { 388 pack(); 389 center(); 390 setVisible(true); 391 splashProc.destroy(); 392 } 393 }); 394 t.setRepeats(false); 395 t.start(); 396 } 397 398 private static Process splashProc; 399 400 public static void main(final String [] args) throws Exception { 401 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 402 403 splashProc = Splash.start("Starting Pojo Sample Launcher...", new Runnable () { 404 public void run() { 405 ApplicationManager.parseLAFArgs(args); 406 new DSOSamplesFrame(); 407 } 408 }); 409 splashProc.waitFor(); 410 } 411 } 412 413 class SampleFrame extends Frame { 414 public SampleFrame(Frame parentFrame, String title) { 415 super(title); 416 getContentPane().setLayout(new BorderLayout ()); 417 setParentFrame(parentFrame); 418 } 419 420 public Rectangle getPreferredBounds() { 421 return null; 422 } 423 424 public Integer getPreferredState() { 425 return null; 426 } 427 428 public void storeBounds() { 429 } 430 431 public void storeState() { 432 } 433 } 434 | Popular Tags |