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.TabbedPane;
009: import org.dijon.TextPane;
010:
011: import com.tc.admin.common.BrowserLauncher;
012: import com.tc.admin.common.InputStreamDrainer;
013: import com.tc.admin.common.Splash;
014: import com.tc.util.ResourceBundleHelper;
015: import com.tc.util.runtime.Os;
016:
017: import java.awt.BorderLayout;
018: import java.awt.Color;
019: import java.awt.Container;
020: import java.awt.Cursor;
021: import java.awt.event.ActionEvent;
022: import java.awt.event.ActionListener;
023: import java.awt.event.WindowAdapter;
024: import java.awt.event.WindowEvent;
025: import java.beans.PropertyChangeEvent;
026: import java.beans.PropertyChangeListener;
027: import java.io.File;
028: import java.io.IOException;
029: import java.util.ArrayList;
030:
031: import javax.swing.Timer;
032: import javax.swing.UIManager;
033: import javax.swing.event.HyperlinkEvent;
034: import javax.swing.event.HyperlinkListener;
035: import javax.swing.text.AttributeSet;
036: import javax.swing.text.Element;
037: import javax.swing.text.html.HTML;
038: import javax.swing.text.html.HTMLEditorKit;
039:
040: public class WelcomeFrame extends HyperlinkFrame implements
041: HyperlinkListener, PropertyChangeListener {
042: private static String[] PRODUCTS = { "Pojo", "Spring", "Sessions" };
043: private static ResourceBundleHelper m_bundleHelper = new ResourceBundleHelper(
044: WelcomeFrame.class);
045:
046: private TabbedPane m_tabbedPane;
047: private ArrayList m_startupList;
048:
049: public WelcomeFrame() {
050: super ();
051:
052: if (Os.isMac()) {
053: System.setProperty("com.apple.macos.useScreenMenuBar",
054: "true");
055: System.setProperty("apple.laf.useScreenMenuBar", "true");
056: System.setProperty("apple.awt.showGrowBox", "true");
057: System.setProperty(
058: "com.apple.mrj.application.growbox.intrudes",
059: "false");
060: }
061:
062: setTitle(getBundleString("welcome.title"));
063:
064: m_startupList = new ArrayList();
065:
066: Container cp = getContentPane();
067: cp.setLayout(new BorderLayout());
068: cp.add(m_tabbedPane = new TabbedPane());
069:
070: addWindowListener(new WindowAdapter() {
071: public void windowDeactivated(WindowEvent e) {
072: setTextPaneCursor(Cursor.DEFAULT_CURSOR);
073: }
074:
075: public void windowActivated(WindowEvent e) {
076: setTextPaneCursor(Cursor.DEFAULT_CURSOR);
077: }
078:
079: public void windowGainedFocus(WindowEvent e) {
080: setTextPaneCursor(Cursor.DEFAULT_CURSOR);
081: }
082: });
083:
084: for (int i = 0; i < PRODUCTS.length; i++) {
085: TextPane textPane = new TextPane();
086: m_startupList.add(textPane);
087: textPane.setBackground(Color.WHITE);
088: textPane.setEditable(false);
089: textPane.addHyperlinkListener(this );
090: textPane.addPropertyChangeListener("page", this );
091: try {
092: textPane.setPage(getClass().getResource(
093: "Welcome" + PRODUCTS[i] + ".html"));
094: } catch (IOException ioe) {
095: textPane.setText(ioe.getMessage());
096: }
097:
098: m_tabbedPane.add(PRODUCTS[i], textPane);
099: m_tabbedPane.setBackgroundAt(i, Color.WHITE);
100: }
101: }
102:
103: private String getBundleString(String key) {
104: return m_bundleHelper.getString(key);
105: }
106:
107: private TextPane getTextPane() {
108: return (TextPane) m_tabbedPane.getSelectedComponent();
109: }
110:
111: protected void setTextPaneCursor(int type) {
112: Cursor c = Cursor.getPredefinedCursor(type);
113: TextPane textPane = getTextPane();
114: HTMLEditorKit kit = (HTMLEditorKit) textPane.getEditorKit();
115:
116: textPane.setCursor(c);
117: kit.setDefaultCursor(c);
118:
119: int linkType = (type == Cursor.WAIT_CURSOR) ? Cursor.WAIT_CURSOR
120: : Cursor.HAND_CURSOR;
121: kit.setLinkCursor(Cursor.getPredefinedCursor(linkType));
122: }
123:
124: public void hyperlinkUpdate(HyperlinkEvent e) {
125: TextPane textPane = getTextPane();
126: HyperlinkEvent.EventType type = e.getEventType();
127: Element elem = e.getSourceElement();
128:
129: if (elem == null || type == HyperlinkEvent.EventType.ENTERED
130: || type == HyperlinkEvent.EventType.EXITED) {
131: return;
132: }
133:
134: if (textPane.getCursor().getType() != Cursor.WAIT_CURSOR) {
135: AttributeSet a = elem.getAttributes();
136: AttributeSet anchor = (AttributeSet) a
137: .getAttribute(HTML.Tag.A);
138: String action = (String) anchor
139: .getAttribute(HTML.Attribute.HREF);
140:
141: hyperlinkActivated(anchor, action);
142: }
143: }
144:
145: private String getProduct() {
146: return PRODUCTS[m_tabbedPane.getSelectedIndex()];
147: }
148:
149: private void runDSOSampleLauncher() {
150: setTextPaneCursor(Cursor.WAIT_CURSOR);
151:
152: try {
153: String[] cmdarray = {
154: getJavaCmd().getAbsolutePath(),
155: "-Dtc.config=tc-config.xml",
156: "-Dtc.install-root="
157: + getInstallRoot().getAbsolutePath(),
158: "-cp", getTCLib().getAbsolutePath(),
159: "com.tc.welcome.DSOSamplesFrame" };
160:
161: Process p = exec(cmdarray, null, getProductDirectory());
162: InputStreamDrainer errDrainer = new InputStreamDrainer(p
163: .getErrorStream());
164: InputStreamDrainer outDrainer = new InputStreamDrainer(p
165: .getInputStream());
166:
167: errDrainer.start();
168: outDrainer.start();
169: startFakeWaitPeriod();
170: } catch (Exception e) {
171: e.printStackTrace();
172: }
173: }
174:
175: protected void hyperlinkActivated(AttributeSet anchor, String action) {
176: if (action.equals("show_samples")) {
177: if ("Pojo".equals(getProduct())) {
178: runDSOSampleLauncher();
179: } else {
180: File file = new File(getProductDirectory(),
181: "samples.html");
182:
183: openURL("file://" + file.getAbsolutePath());
184: }
185: } else if (action.equals("run_console")) {
186: startFakeWaitPeriod();
187: runScript("admin");
188: } else if (action.equals("run_configurator")) {
189: startFakeWaitPeriod();
190: runScript("sessions-configurator", "tools"
191: + System.getProperty("file.separator") + "sessions");
192: } else if (action.equals("show_guide")) {
193: File file = new File(getProductDirectory(), "docs");
194: String doc = (String) anchor
195: .getAttribute(HTML.Attribute.NAME);
196:
197: file = new File(file, doc);
198:
199: openURL(file.getAbsolutePath());
200: } else {
201: openURL(action);
202: }
203: }
204:
205: protected File getProductDirectory() {
206: int index = m_tabbedPane.getSelectedIndex();
207: return new File(getSamplesDir(), PRODUCTS[index].toLowerCase());
208: }
209:
210: protected void openURL(String url) {
211: setTextPaneCursor(Cursor.WAIT_CURSOR);
212: BrowserLauncher.openURL(url);
213: startFakeWaitPeriod();
214: }
215:
216: protected void runSampleScript(String scriptPath) {
217: setTextPaneCursor(Cursor.WAIT_CURSOR);
218:
219: File dir = getProductDirectory();
220: String ext = Os.isWindows() ? ".bat" : ".sh";
221: File file = new File(dir, scriptPath + ext);
222: String[] cmd = { file.getAbsolutePath() };
223:
224: try {
225: Process p = Runtime.getRuntime().exec(cmd);
226:
227: InputStreamDrainer errDrainer = new InputStreamDrainer(p
228: .getErrorStream());
229: InputStreamDrainer outDrainer = new InputStreamDrainer(p
230: .getInputStream());
231:
232: errDrainer.start();
233: outDrainer.start();
234:
235: startFakeWaitPeriod();
236: } catch (IOException ioe) {
237: ioe.printStackTrace();
238: }
239: }
240:
241: protected void runScript(String scriptName) {
242: runScript(scriptName, "bin");
243: }
244:
245: protected void runScript(String scriptName, String scriptRoot) {
246: setTextPaneCursor(Cursor.WAIT_CURSOR);
247:
248: File dir = new File(getInstallRoot(), scriptRoot);
249: String ext = Os.isWindows() ? ".bat" : ".sh";
250: File file = new File(dir, scriptName + ext);
251: String[] cmd = { file.getAbsolutePath() };
252:
253: try {
254: Process p = Runtime.getRuntime().exec(cmd);
255:
256: InputStreamDrainer errDrainer = new InputStreamDrainer(p
257: .getErrorStream());
258: InputStreamDrainer outDrainer = new InputStreamDrainer(p
259: .getInputStream());
260:
261: errDrainer.start();
262: outDrainer.start();
263:
264: startFakeWaitPeriod();
265: } catch (IOException ioe) {
266: ioe.printStackTrace();
267: }
268: }
269:
270: protected void startFakeWaitPeriod() {
271: Timer t = new Timer(3000, new ActionListener() {
272: public void actionPerformed(ActionEvent ae) {
273: setTextPaneCursor(Cursor.DEFAULT_CURSOR);
274: }
275: });
276: t.setRepeats(false);
277: t.start();
278: }
279:
280: public void propertyChange(PropertyChangeEvent pce) {
281: TextPane textPane = (TextPane) pce.getSource();
282: m_startupList.remove(textPane);
283:
284: if (m_startupList.isEmpty()) {
285: pack();
286: center();
287: Timer t = new Timer(2000, new ActionListener() {
288: public void actionPerformed(ActionEvent ae) {
289: setVisible(true);
290: splashProc.destroy();
291: }
292: });
293: t.setRepeats(false);
294: t.start();
295: }
296: }
297:
298: private static Process splashProc;
299:
300: public static void main(final String[] args) throws Exception {
301: UIManager.setLookAndFeel(UIManager
302: .getSystemLookAndFeelClassName());
303:
304: splashProc = Splash.start("Starting Terracotta Welcome...",
305: new Runnable() {
306: public void run() {
307: ApplicationManager.parseLAFArgs(args);
308: WelcomeFrame welcome = new WelcomeFrame();
309: welcome.setResizable(false);
310: }
311: });
312: splashProc.waitFor();
313: }
314: }
|