001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.welcome;
006:
007: import org.dijon.ApplicationManager;
008: import org.dijon.Frame;
009: import org.dijon.Menu;
010: import org.dijon.ScrollPane;
011: import org.dijon.TextPane;
012:
013: import com.tc.admin.TCStop;
014: import com.tc.admin.common.BrowserLauncher;
015: import com.tc.admin.common.Splash;
016: import com.tc.admin.common.StreamReader;
017: import com.tc.admin.common.TextPaneUpdater;
018: import com.tc.admin.common.XAbstractAction;
019: import com.tc.admin.common.XTextPane;
020: import com.tc.server.ServerConstants;
021: import com.tc.util.ResourceBundleHelper;
022: import com.tc.util.runtime.Os;
023:
024: import java.awt.BorderLayout;
025: import java.awt.Color;
026: import java.awt.Container;
027: import java.awt.Cursor;
028: import java.awt.Dimension;
029: import java.awt.FlowLayout;
030: import java.awt.GridLayout;
031: import java.awt.Rectangle;
032: import java.awt.event.ActionEvent;
033: import java.awt.event.ActionListener;
034: import java.awt.event.WindowAdapter;
035: import java.awt.event.WindowEvent;
036: import java.beans.PropertyChangeEvent;
037: import java.beans.PropertyChangeListener;
038: import java.io.File;
039: import java.io.FilenameFilter;
040: import java.io.IOException;
041: import java.io.InputStream;
042: import java.util.ArrayList;
043: import java.util.Iterator;
044:
045: import javax.swing.JCheckBox;
046: import javax.swing.JLabel;
047: import javax.swing.JOptionPane;
048: import javax.swing.JPanel;
049: import javax.swing.JTextField;
050: import javax.swing.JToggleButton;
051: import javax.swing.Timer;
052: import javax.swing.UIManager;
053: import javax.swing.border.EmptyBorder;
054: import javax.swing.event.HyperlinkEvent;
055: import javax.swing.event.HyperlinkListener;
056: import javax.swing.text.AttributeSet;
057: import javax.swing.text.Document;
058: import javax.swing.text.Element;
059: import javax.swing.text.html.HTML;
060: import javax.swing.text.html.HTMLEditorKit;
061:
062: public class DSOSamplesFrame extends HyperlinkFrame implements
063: HyperlinkListener, PropertyChangeListener {
064: private ResourceBundleHelper m_bundleHelper;
065: private TextPane m_textPane;
066: private TextPane m_outputPane;
067: private ArrayList m_processList;
068:
069: public DSOSamplesFrame() {
070: super ();
071:
072: setTitle(getResourceBundleHelper().getString("frame.title"));
073:
074: Container cp = getContentPane();
075: cp.setLayout(new BorderLayout());
076:
077: m_textPane = new TextPane();
078: m_textPane.setBackground(Color.WHITE);
079: cp.add(new ScrollPane(m_textPane));
080: m_textPane.setEditable(false);
081: m_textPane.addHyperlinkListener(this );
082: m_textPane.addPropertyChangeListener("page", this );
083:
084: m_processList = new ArrayList();
085: m_outputPane = new XTextPane();
086: m_outputPane.setForeground(Color.GREEN);
087: m_outputPane.setBackground(Color.BLACK);
088: ScrollPane scroller = new ScrollPane(m_outputPane);
089: scroller.setPreferredSize(new Dimension(500, 200));
090: cp.add(scroller, BorderLayout.SOUTH);
091: runServer();
092:
093: try {
094: m_textPane.setPage(getClass().getResource(
095: "SamplesPojo.html"));
096: } catch (IOException ioe) {
097: m_textPane.setText(ioe.getMessage());
098: }
099: }
100:
101: private ResourceBundleHelper getResourceBundleHelper() {
102: if (m_bundleHelper == null) {
103: m_bundleHelper = new ResourceBundleHelper(
104: DSOSamplesFrame.class);
105: }
106: return m_bundleHelper;
107: }
108:
109: protected void initFileMenu(Menu fileMenu) {
110: fileMenu.add(new ServersAction());
111: super .initFileMenu(fileMenu);
112: }
113:
114: class ServersAction extends XAbstractAction {
115: JPanel m_panel;
116: JToggleButton m_useLocalToggle;
117: JTextField m_serversListField;
118:
119: ServersAction() {
120: super (getResourceBundleHelper().getString(
121: "servers.action.name"));
122: }
123:
124: private JPanel createPanel() {
125: if (m_panel == null) {
126: m_panel = new JPanel();
127: m_panel.setLayout(new GridLayout(2, 1));
128: m_panel.add(m_useLocalToggle = new JCheckBox(
129: m_bundleHelper.getString("servers.use.local")));
130: m_useLocalToggle
131: .addActionListener(new ActionListener() {
132: public void actionPerformed(ActionEvent ae) {
133: m_serversListField
134: .setEnabled(!m_useLocalToggle
135: .isSelected());
136: }
137: });
138: JPanel bottomPanel = new JPanel(new BorderLayout());
139: JPanel otherPanel = new JPanel();
140: otherPanel.setLayout(new FlowLayout());
141: otherPanel.add(new JLabel(m_bundleHelper
142: .getString("servers.use.remote")));
143: m_serversListField = new JTextField(
144: "server1:9510,server2:9510,server3:9510");
145: m_serversListField.setToolTipText(m_bundleHelper
146: .getString("servers.field.tip"));
147: m_serversListField.setPreferredSize(m_serversListField
148: .getPreferredSize());
149: String prop = System.getProperty("tc.server");
150: m_serversListField.setText(prop);
151: otherPanel.add(m_serversListField);
152: bottomPanel.add(otherPanel, BorderLayout.CENTER);
153: JLabel serversListDescription = new JLabel(
154: m_bundleHelper
155: .getString("servers.field.description"));
156: serversListDescription.setBorder(new EmptyBorder(0, 20,
157: 0, 0));
158: bottomPanel.add(serversListDescription,
159: BorderLayout.SOUTH);
160: m_panel.add(bottomPanel);
161:
162: m_serversListField.setEnabled(prop != null);
163: m_useLocalToggle.setSelected(prop == null);
164: }
165: return m_panel;
166: }
167:
168: public void actionPerformed(ActionEvent ae) {
169: JPanel panel = createPanel();
170: int result;
171:
172: result = JOptionPane.showConfirmDialog(
173: DSOSamplesFrame.this , panel, DSOSamplesFrame.this
174: .getTitle(), JOptionPane.OK_CANCEL_OPTION);
175: if (result == JOptionPane.OK_OPTION) {
176: if (!m_useLocalToggle.isSelected()) {
177: System.setProperty("tc.server", m_serversListField
178: .getText());
179: } else {
180: System.getProperties().remove("tc.server");
181: }
182: }
183: }
184: }
185:
186: private void toOutputPane(String s) {
187: Document doc = m_outputPane.getDocument();
188:
189: try {
190: doc.insertString(doc.getLength(), s + "\n", null);
191: } catch (Exception e) {/**/
192: }
193: }
194:
195: protected void quit() {
196: stopProcesses();
197: }
198:
199: private StreamReader createStreamReader(InputStream stream) {
200: return new StreamReader(stream, new TextPaneUpdater(
201: m_outputPane), null, null);
202: }
203:
204: private StreamReader createStreamReader(InputStream stream,
205: TextPane textPane) {
206: return new StreamReader(stream, new TextPaneUpdater(textPane),
207: null, null);
208: }
209:
210: private void stopProcesses() {
211: String host = "localhost";
212: int port = 9520;
213:
214: try {
215: new TCStop(host, port).stop();
216: } catch (Exception e) {
217: toOutputPane(e.getMessage());
218: }
219:
220: Iterator iter = m_processList.iterator();
221: while (iter.hasNext()) {
222: Process p = (Process) iter.next();
223:
224: try {
225: p.exitValue();
226: } catch (Exception e) {
227: p.destroy();
228: }
229: }
230:
231: new Thread() {
232: public void run() {
233: TCStop tester = new TCStop("localhost", 9520);
234:
235: while (true) {
236: try {
237: sleep(2000);
238: tester.stop();
239: } catch (Exception e) {
240: DSOSamplesFrame.super .quit();
241: }
242: }
243: }
244: }.start();
245: }
246:
247: private void runServer() {
248: try {
249: File bootFile = new File(getBootPath());
250:
251: if (bootFile.exists()) {
252: internalRunServer();
253: } else {
254: createBootJar();
255: }
256: } catch (Exception e) {
257: e.printStackTrace();
258: }
259: }
260:
261: private void createBootJar() {
262: String[] cmdarray = {
263: getJavaCmd().getAbsolutePath(),
264: "-Dtc.install-root="
265: + getInstallRoot().getAbsolutePath(), "-cp",
266: getTCLib().getAbsolutePath(),
267: "com.tc.object.tools.BootJarTool" };
268:
269: final Process p = exec(cmdarray, null, getSamplesDir());
270: StreamReader errDrainer = createStreamReader(p.getErrorStream());
271: StreamReader outDrainer = createStreamReader(p.getInputStream());
272:
273: errDrainer.start();
274: outDrainer.start();
275:
276: new Thread() {
277: public void run() {
278: while (true) {
279: try {
280: p.waitFor();
281: break;
282: } catch (Exception e) {/**/
283: }
284: }
285: internalRunServer();
286: }
287: }.start();
288: }
289:
290: private void internalRunServer() {
291: String[] cmdarray = {
292: getJavaCmd().getAbsolutePath(),
293: "-Dtc.config=tc-config.xml",
294: "-Dtc.install-root="
295: + getInstallRoot().getAbsolutePath(), "-cp",
296: getTCLib().getAbsolutePath(),
297: ServerConstants.SERVER_MAIN_CLASS_NAME };
298:
299: Process p = exec(cmdarray, null, getSamplesDir());
300: StreamReader errDrainer = createStreamReader(p.getErrorStream());
301: StreamReader outDrainer = createStreamReader(p.getInputStream());
302:
303: errDrainer.start();
304: outDrainer.start();
305: }
306:
307: private void runSample(String dirName, String className) {
308: setTextPaneCursor(Cursor.WAIT_CURSOR);
309: try {
310: String bootPath = getBootPath();
311: String[] cmdarray = {
312: getJavaCmd().getAbsolutePath(),
313: "-Dcom.tc.l1.max.connect.retries=3",
314: "-Dtc.config=tc-config.xml",
315: "-Djava.awt.Window.locationByPlatform=true",
316: "-Dtc.install-root="
317: + getInstallRoot().getAbsolutePath(),
318: "-Dtc.server="
319: + System.getProperty("tc.server", ""),
320: "-Xbootclasspath/p:" + bootPath, "-cp", "classes",
321: className };
322:
323: Process p = exec(cmdarray, null, new File(
324: getProductDirectory(), dirName));
325: StreamReader errDrainer = createStreamReader(p
326: .getErrorStream());
327: StreamReader outDrainer = createStreamReader(p
328: .getInputStream());
329:
330: errDrainer.start();
331: outDrainer.start();
332:
333: m_processList.add(p);
334: startFakeWaitPeriod();
335: } catch (Exception e) {
336: e.printStackTrace();
337: }
338: }
339:
340: private void runCoordinationSample() {
341: setTextPaneCursor(Cursor.WAIT_CURSOR);
342: try {
343: String bootPath = getBootPath();
344: File dir = new File(getProductDirectory(), "coordination");
345: String classpath = getLibClassPath(dir, "classes");
346: String[] cmdarray = {
347: getJavaCmd().getAbsolutePath(),
348: "-Dcom.tc.l1.max.connect.retries=3",
349: "-Dtc.config=tc-config.xml",
350: "-Djava.awt.Window.locationByPlatform=true",
351: "-Dtc.install-root="
352: + getInstallRoot().getAbsolutePath(),
353: "-Xbootclasspath/p:" + bootPath, "-cp", classpath,
354: "demo.coordination.Main" };
355:
356: final Process p = exec(cmdarray, null, dir);
357: XTextPane textPane = new XTextPane();
358: StreamReader errDrainer = createStreamReader(p
359: .getErrorStream(), textPane);
360: StreamReader outDrainer = createStreamReader(p
361: .getInputStream(), textPane);
362:
363: errDrainer.start();
364: outDrainer.start();
365:
366: m_processList.add(p);
367: startFakeWaitPeriod();
368:
369: Frame frame = new SampleFrame(this ,
370: getResourceBundleHelper().getString(
371: "jvm.coordination"));
372: frame.getContentPane().add(new ScrollPane(textPane));
373: frame.setSize(new Dimension(500, 300));
374: frame.setVisible(true);
375: frame.addWindowListener(new WindowAdapter() {
376: public void windowClosing(WindowEvent we) {
377: try {
378: p.destroy();
379: } catch (Exception e) {/**/
380: }
381: m_processList.remove(p);
382: }
383: });
384: } catch (Exception e) {
385: e.printStackTrace();
386: }
387: }
388:
389: private String getLibClassPath(final File dir,
390: final String defaultPath) {
391: final String pathSep = System.getProperty("path.separator");
392: final String fileSep = System.getProperty("file.separator");
393: final String LIB_DIR = "lib";
394: final File libdir = new File(dir, LIB_DIR);
395: String classpath = defaultPath;
396: if (libdir.exists()) {
397: final String[] jars = libdir.list(new FilenameFilter() {
398: public boolean accept(File directory, String name) {
399: return name.endsWith(".jar");
400: }
401: });
402: for (int i = 0; i < jars.length; i++) {
403: classpath += (pathSep + LIB_DIR + fileSep + jars[i]);
404: }
405: }
406: return classpath;
407: }
408:
409: private void runSharedQueueSample() {
410: setTextPaneCursor(Cursor.WAIT_CURSOR);
411: try {
412: String bootPath = getBootPath();
413: File dir = new File(getProductDirectory(), "sharedqueue");
414: String classpath = getLibClassPath(dir, "classes");
415: String[] cmdarray = {
416: getJavaCmd().getAbsolutePath(),
417: "-Dcom.tc.l1.max.connect.retries=3",
418: "-Dtc.config=tc-config.xml",
419: "-Djava.awt.Window.locationByPlatform=true",
420: "-Dtc.install-root="
421: + getInstallRoot().getAbsolutePath(),
422: "-Xbootclasspath/p:" + bootPath, "-cp", classpath,
423: "demo.sharedqueue.Main" };
424:
425: final Process p = exec(cmdarray, null, dir);
426: XTextPane textPane = new XTextPane();
427: StreamReader errDrainer = createStreamReader(p
428: .getErrorStream(), textPane);
429: StreamReader outDrainer = createStreamReader(p
430: .getInputStream(), textPane);
431:
432: errDrainer.start();
433: outDrainer.start();
434:
435: m_processList.add(p);
436: startFakeWaitPeriod();
437:
438: Frame frame = new SampleFrame(this ,
439: getResourceBundleHelper().getString(
440: "shared.work.queue"));
441: frame.getContentPane().add(new ScrollPane(textPane));
442: frame.setSize(new Dimension(500, 300));
443: frame.setVisible(true);
444: frame.addWindowListener(new WindowAdapter() {
445: public void windowClosing(WindowEvent we) {
446: try {
447: p.destroy();
448: } catch (Exception e) {/**/
449: }
450: m_processList.remove(p);
451: }
452: });
453: } catch (Exception e) {
454: e.printStackTrace();
455: }
456: }
457:
458: public void hyperlinkUpdate(HyperlinkEvent e) {
459: HyperlinkEvent.EventType type = e.getEventType();
460: Element elem = e.getSourceElement();
461:
462: if (elem == null || type == HyperlinkEvent.EventType.ENTERED
463: || type == HyperlinkEvent.EventType.EXITED) {
464: return;
465: }
466:
467: if (m_textPane.getCursor().getType() != Cursor.WAIT_CURSOR) {
468: AttributeSet a = elem.getAttributes();
469: AttributeSet anchor = (AttributeSet) a
470: .getAttribute(HTML.Tag.A);
471: String action = (String) anchor
472: .getAttribute(HTML.Attribute.HREF);
473:
474: hyperlinkActivated(anchor, action);
475: }
476: }
477:
478: private void hyperlinkActivated(AttributeSet anchor, String action) {
479: if (action.equals("run_jtable")) {
480: toOutputPane(m_bundleHelper.getString("starting.jtable"));
481: runSample("jtable", "demo.jtable.Main");
482: } else if (action.equals("run_sharededitor")) {
483: toOutputPane(m_bundleHelper
484: .getString("starting.shared.editor"));
485: runSample("sharededitor", "demo.sharededitor.Main");
486: } else if (action.equals("run_chatter")) {
487: toOutputPane(m_bundleHelper.getString("starting.chatter"));
488: runSample("chatter", "demo.chatter.Main");
489: } else if (action.equals("run_coordination")) {
490: toOutputPane(m_bundleHelper
491: .getString("starting.jvm.coordination"));
492: runCoordinationSample();
493: } else if (action.equals("run_sharedqueue")) {
494: toOutputPane(m_bundleHelper
495: .getString("starting.shared.queue"));
496: runSharedQueueSample();
497: } else if (action.equals("view_readme")) {
498: String name = (String) anchor
499: .getAttribute(HTML.Attribute.NAME);
500: File dir = new File(getProductDirectory(), name);
501: File file = new File(dir, "readme.html");
502:
503: openURL("file://" + file.getAbsolutePath());
504: } else if (action.equals("browse_source")) {
505: String name = (String) anchor
506: .getAttribute(HTML.Attribute.NAME);
507: File sampleDir = new File(getProductDirectory(), name);
508: File sampleDocs = new File(sampleDir, "docs");
509: File index = new File(sampleDocs, "source.html");
510:
511: openURL("file://" + index.getAbsolutePath());
512: } else {
513: openURL(action);
514: }
515: }
516:
517: private void setTextPaneCursor(int type) {
518: Cursor c = Cursor.getPredefinedCursor(type);
519: HTMLEditorKit kit = (HTMLEditorKit) m_textPane.getEditorKit();
520:
521: m_textPane.setCursor(c);
522: kit.setDefaultCursor(c);
523:
524: int linkType = (type == Cursor.WAIT_CURSOR) ? Cursor.WAIT_CURSOR
525: : Cursor.HAND_CURSOR;
526: kit.setLinkCursor(Cursor.getPredefinedCursor(linkType));
527: }
528:
529: private void openURL(String url) {
530: setTextPaneCursor(Cursor.WAIT_CURSOR);
531: BrowserLauncher.openURL(url);
532: startFakeWaitPeriod();
533: }
534:
535: private void startFakeWaitPeriod() {
536: Timer t = new Timer(3000, new ActionListener() {
537: public void actionPerformed(ActionEvent ae) {
538: setTextPaneCursor(Cursor.DEFAULT_CURSOR);
539: }
540: });
541: t.setRepeats(false);
542: t.start();
543: }
544:
545: private File getProductDirectory() {
546: return new File(getSamplesDir(), "pojo");
547: }
548:
549: public void propertyChange(PropertyChangeEvent pce) {
550: Timer t = new Timer(2000, new ActionListener() {
551: public void actionPerformed(ActionEvent ae) {
552: pack();
553: center();
554: setVisible(true);
555: splashProc.destroy();
556: }
557: });
558: t.setRepeats(false);
559: t.start();
560: }
561:
562: private static Process splashProc;
563:
564: public static void main(final String[] args) throws Exception {
565: UIManager.setLookAndFeel(UIManager
566: .getSystemLookAndFeelClassName());
567:
568: splashProc = Splash.start("Starting Pojo Sample Launcher...",
569: new Runnable() {
570: public void run() {
571: ApplicationManager.parseLAFArgs(args);
572: new DSOSamplesFrame();
573: }
574: });
575: splashProc.waitFor();
576: }
577: }
578:
579: class SampleFrame extends Frame {
580: public SampleFrame(Frame parentFrame, String title) {
581: super (title);
582:
583: if (Os.isMac()) {
584: System.setProperty("com.apple.macos.useScreenMenuBar",
585: "true");
586: System.setProperty("apple.laf.useScreenMenuBar", "true");
587: System.setProperty("apple.awt.showGrowBox", "true");
588: System.setProperty(
589: "com.apple.mrj.application.growbox.intrudes",
590: "false");
591: }
592:
593: getContentPane().setLayout(new BorderLayout());
594: setParentFrame(parentFrame);
595: }
596:
597: public Rectangle getPreferredBounds() {
598: return null;
599: }
600:
601: public Integer getPreferredState() {
602: return null;
603: }
604:
605: public void storeBounds() {/**/
606: }
607:
608: public void storeState() {/**/
609: }
610: }
|