1 5 package com.tc.welcome; 6 7 import org.dijon.ApplicationManager; 8 import org.dijon.TabbedPane; 9 import org.dijon.TextPane; 10 11 import com.tc.admin.common.BrowserLauncher; 12 import com.tc.admin.common.InputStreamDrainer; 13 import com.tc.admin.common.Splash; 14 import com.tc.util.ResourceBundleHelper; 15 import com.tc.util.runtime.Os; 16 17 import java.awt.BorderLayout ; 18 import java.awt.Color ; 19 import java.awt.Container ; 20 import java.awt.Cursor ; 21 import java.awt.event.ActionEvent ; 22 import java.awt.event.ActionListener ; 23 import java.awt.event.WindowAdapter ; 24 import java.awt.event.WindowEvent ; 25 import java.beans.PropertyChangeEvent ; 26 import java.beans.PropertyChangeListener ; 27 import java.io.File ; 28 import java.io.IOException ; 29 import java.util.ArrayList ; 30 31 import javax.swing.Timer ; 32 import javax.swing.UIManager ; 33 import javax.swing.event.HyperlinkEvent ; 34 import javax.swing.event.HyperlinkListener ; 35 import javax.swing.text.AttributeSet ; 36 import javax.swing.text.Element ; 37 import javax.swing.text.html.HTML ; 38 import javax.swing.text.html.HTMLEditorKit ; 39 40 public class WelcomeFrame extends HyperlinkFrame 41 implements HyperlinkListener , 42 PropertyChangeListener 43 { 44 private static String [] PRODUCTS = { "Pojo", "Spring", "Sessions" }; 45 private static ResourceBundleHelper m_bundleHelper = new ResourceBundleHelper(WelcomeFrame.class); 46 47 private TabbedPane m_tabbedPane; 48 private ArrayList m_startupList; 49 50 public WelcomeFrame() { 51 super(); 52 53 setTitle(getBundleString("welcome.title")); 54 55 m_startupList = new ArrayList (); 56 57 Container cp = getContentPane(); 58 cp.setLayout(new BorderLayout ()); 59 cp.add(m_tabbedPane = new TabbedPane()); 60 61 addWindowListener(new WindowAdapter () { 62 public void windowDeactivated(WindowEvent e) { 63 setTextPaneCursor(Cursor.DEFAULT_CURSOR); 64 } 65 66 public void windowActivated(WindowEvent e) { 67 setTextPaneCursor(Cursor.DEFAULT_CURSOR); 68 } 69 70 public void windowGainedFocus(WindowEvent e) { 71 setTextPaneCursor(Cursor.DEFAULT_CURSOR); 72 } 73 }); 74 75 for (int i = 0; i < PRODUCTS.length; i++) { 76 TextPane textPane = new TextPane(); 77 m_startupList.add(textPane); 78 textPane.setBackground(Color.WHITE); 79 textPane.setEditable(false); 80 textPane.addHyperlinkListener(this); 81 textPane.addPropertyChangeListener("page", this); 82 try { 83 textPane.setPage(getClass().getResource("Welcome" + PRODUCTS[i] + ".html")); 84 } catch (IOException ioe) { 85 textPane.setText(ioe.getMessage()); 86 } 87 88 m_tabbedPane.add(PRODUCTS[i], textPane); 89 m_tabbedPane.setBackgroundAt(i, Color.WHITE); 90 } 91 } 92 93 private String getBundleString(String key) { 94 return m_bundleHelper.getString(key); 95 } 96 97 private TextPane getTextPane() { 98 return (TextPane) m_tabbedPane.getSelectedComponent(); 99 } 100 101 protected void setTextPaneCursor(int type) { 102 Cursor c = Cursor.getPredefinedCursor(type); 103 TextPane textPane = getTextPane(); 104 HTMLEditorKit kit = (HTMLEditorKit ) textPane.getEditorKit(); 105 106 textPane.setCursor(c); 107 kit.setDefaultCursor(c); 108 109 int linkType = (type == Cursor.WAIT_CURSOR) ? Cursor.WAIT_CURSOR : Cursor.HAND_CURSOR; 110 kit.setLinkCursor(Cursor.getPredefinedCursor(linkType)); 111 } 112 113 public void hyperlinkUpdate(HyperlinkEvent e) { 114 TextPane textPane = getTextPane(); 115 HyperlinkEvent.EventType type = e.getEventType(); 116 Element elem = e.getSourceElement(); 117 118 if (elem == null || 119 type == HyperlinkEvent.EventType.ENTERED || 120 type == HyperlinkEvent.EventType.EXITED) { 121 return; 122 } 123 124 if (textPane.getCursor().getType() != Cursor.WAIT_CURSOR) { 125 AttributeSet a = elem.getAttributes(); 126 AttributeSet anchor = (AttributeSet ) a.getAttribute(HTML.Tag.A); 127 String action = (String ) anchor.getAttribute(HTML.Attribute.HREF); 128 129 hyperlinkActivated(anchor, action); 130 } 131 } 132 133 private String getProduct() { 134 return PRODUCTS[m_tabbedPane.getSelectedIndex()]; 135 } 136 137 private void runDSOSampleLauncher() { 138 setTextPaneCursor(Cursor.WAIT_CURSOR); 139 140 try { 141 String [] cmdarray = { getJavaCmd().getAbsolutePath(), "-Dtc.config=tc-config.xml", 142 "-Dtc.install-root=" + getInstallRoot().getAbsolutePath(), 143 "-cp", getTCLib().getAbsolutePath(), 144 "com.tc.welcome.DSOSamplesFrame" }; 145 146 Process p = exec(cmdarray, null, getProductDirectory()); 147 InputStreamDrainer errDrainer = new InputStreamDrainer(p.getErrorStream()); 148 InputStreamDrainer outDrainer = new InputStreamDrainer(p.getInputStream()); 149 150 errDrainer.start(); 151 outDrainer.start(); 152 startFakeWaitPeriod(); 153 } catch (Exception e) { 154 e.printStackTrace(); 155 } 156 } 157 158 protected void hyperlinkActivated(AttributeSet anchor, String action) { 159 if (action.equals("show_samples")) { 160 if ("Pojo".equals(getProduct())) { 161 runDSOSampleLauncher(); 162 } else { 163 File file = new File (getProductDirectory(), "samples.html"); 164 165 openURL("file://" + file.getAbsolutePath()); 166 } 167 } else if (action.equals("run_console")) { 168 startFakeWaitPeriod(); 169 runScript("admin"); 170 } else if (action.equals("run_configurator")) { 171 startFakeWaitPeriod(); 172 runScript("sessions-configurator", "tools" + System.getProperty("file.separator") + "sessions"); 173 } else if (action.equals("show_guide")) { 174 File file = new File (getProductDirectory(), "docs"); 175 String doc = (String ) anchor.getAttribute(HTML.Attribute.NAME); 176 177 file = new File (file, doc); 178 179 openURL(file.getAbsolutePath()); 180 } else { 181 openURL(action); 182 } 183 } 184 185 protected File getProductDirectory() { 186 int index = m_tabbedPane.getSelectedIndex(); 187 return new File (getSamplesDir(), PRODUCTS[index].toLowerCase()); 188 } 189 190 protected void openURL(String url) { 191 setTextPaneCursor(Cursor.WAIT_CURSOR); 192 BrowserLauncher.openURL(url); 193 startFakeWaitPeriod(); 194 } 195 196 protected void runSampleScript(String scriptPath) { 197 setTextPaneCursor(Cursor.WAIT_CURSOR); 198 199 File dir = getProductDirectory(); 200 String ext = Os.isWindows() ? ".bat" : ".sh"; 201 File file = new File (dir, scriptPath + ext); 202 String [] cmd = { file.getAbsolutePath() }; 203 204 try { 205 Process p = Runtime.getRuntime().exec(cmd); 206 207 InputStreamDrainer errDrainer = new InputStreamDrainer(p.getErrorStream()); 208 InputStreamDrainer outDrainer = new InputStreamDrainer(p.getInputStream()); 209 210 errDrainer.start(); 211 outDrainer.start(); 212 213 startFakeWaitPeriod(); 214 } catch (IOException ioe) { 215 ioe.printStackTrace(); 216 } 217 } 218 219 protected void runScript(String scriptName) { 220 runScript(scriptName, "bin"); 221 } 222 223 protected void runScript(String scriptName, String scriptRoot) { 224 setTextPaneCursor(Cursor.WAIT_CURSOR); 225 226 File dir = new File (getInstallRoot(), scriptRoot); 227 String ext = Os.isWindows() ? ".bat" : ".sh"; 228 File file = new File (dir, scriptName + ext); 229 String [] cmd = { file.getAbsolutePath() }; 230 231 try { 232 Process p = Runtime.getRuntime().exec(cmd); 233 234 InputStreamDrainer errDrainer = new InputStreamDrainer(p.getErrorStream()); 235 InputStreamDrainer outDrainer = new InputStreamDrainer(p.getInputStream()); 236 237 errDrainer.start(); 238 outDrainer.start(); 239 240 startFakeWaitPeriod(); 241 } catch (IOException ioe) { 242 ioe.printStackTrace(); 243 } 244 } 245 246 protected void startFakeWaitPeriod() { 247 Timer t = new Timer (3000, new ActionListener () { 248 public void actionPerformed(ActionEvent ae) { 249 setTextPaneCursor(Cursor.DEFAULT_CURSOR); 250 } 251 }); 252 t.setRepeats(false); 253 t.start(); 254 } 255 256 public void propertyChange(PropertyChangeEvent pce) { 257 TextPane textPane = (TextPane) pce.getSource(); 258 m_startupList.remove(textPane); 259 260 if (m_startupList.isEmpty()) { 261 pack(); 262 center(); 263 Timer t = new Timer (2000, new ActionListener () { 264 public void actionPerformed(ActionEvent ae) { 265 setVisible(true); 266 splashProc.destroy(); 267 } 268 }); 269 t.setRepeats(false); 270 t.start(); 271 } 272 } 273 274 private static Process splashProc; 275 276 public static void main(final String [] args) throws Exception { 277 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 278 279 splashProc = Splash.start("Starting Terracotta Welcome...", new Runnable () { 280 public void run() { 281 ApplicationManager.parseLAFArgs(args); 282 WelcomeFrame welcome = new WelcomeFrame(); 283 welcome.setResizable(false); 284 } 285 }); 286 splashProc.waitFor(); 287 } 288 } 289 | Popular Tags |