1 /* 2 ******************************************************************************* 3 * Copyright (C) 2007-2011, International Business Machines Corporation and * 4 * others. All Rights Reserved. * 5 ******************************************************************************* 6 */ 7 package com.ibm.icu.dev.demo; 8 9 import java.awt.BorderLayout; 10 import java.awt.Button; 11 import java.awt.Color; 12 import java.awt.Frame; 13 import java.awt.GridLayout; 14 import java.awt.Label; 15 import java.awt.Panel; 16 import java.awt.event.ActionEvent; 17 import java.awt.event.ActionListener; 18 import java.awt.event.WindowAdapter; 19 import java.awt.event.WindowEvent; 20 import java.lang.reflect.InvocationTargetException; 21 import java.lang.reflect.Method; 22 23 import com.ibm.icu.dev.demo.impl.DemoApplet; 24 import com.ibm.icu.dev.demo.impl.DemoUtility; 25 import com.ibm.icu.util.VersionInfo; 26 27 28 /** 29 * @author srl 30 * Application to provide a panel of demos to launch 31 */ 32 public class Launcher extends DemoApplet { 33 private static final long serialVersionUID = -8054963875776183877L; 34 35 /** 36 * base package of all demos 37 */ 38 public static final String demoBase = "com.ibm.icu.dev.demo"; 39 /** 40 * list of classes, relative to the demoBase. all must have a static void main(String[]) 41 */ 42 public static final String demoList[] = { 43 "calendar.CalendarApp", 44 "charsetdet.DetectingViewer", 45 "holiday.HolidayCalendarDemo", 46 "rbnf.RbnfDemo", 47 "translit.Demo", 48 }; 49 50 public class LauncherFrame extends Frame implements ActionListener { 51 private static final long serialVersionUID = -8054963875776183878L; 52 53 public Button buttonList[] = new Button[demoList.length]; // one button for each demo 54 public Label statusLabel; 55 private DemoApplet applet; 56 LauncherFrame(DemoApplet applet)57 LauncherFrame(DemoApplet applet) { 58 init(); 59 this.applet = applet; 60 } 61 init()62 public void init() { 63 // close down when close is clicked. 64 // TODO: this should be factored.. 65 addWindowListener( 66 new WindowAdapter() { 67 public void windowClosing(WindowEvent e) { 68 setVisible(false); 69 dispose(); 70 71 if (applet != null) { 72 applet.demoClosed(); 73 } else System.exit(0); 74 } 75 } ); 76 77 setBackground(DemoUtility.bgColor); 78 setLayout(new BorderLayout()); 79 80 Panel topPanel = new Panel(); 81 topPanel.setLayout(new GridLayout(5,3)); 82 83 for(int i=0;i<buttonList.length;i++) { 84 String demo = demoList[i]; 85 Button b = new Button(demo); 86 b.addActionListener(this); 87 buttonList[i]=b; 88 topPanel.add(b); 89 } 90 add(BorderLayout.CENTER,topPanel); 91 statusLabel = new Label(""); 92 statusLabel.setAlignment(Label.LEFT); 93 String javaVersion = ""; 94 try { 95 javaVersion = "* Java: "+System.getProperty("java.version"); 96 } catch (Throwable t) { 97 javaVersion = ""; 98 } 99 add(BorderLayout.NORTH, new Label( 100 "ICU Demos * ICU version "+VersionInfo.ICU_VERSION + 101 " * http://icu-project.org "+javaVersion)); 102 add(BorderLayout.SOUTH,statusLabel); 103 // set up an initial status. 104 showStatus(buttonList.length+" demos ready. "); 105 } 106 107 /** 108 * Change the 'status' field, and set it to black 109 * @param status 110 */ showStatus(String status)111 void showStatus(String status) { 112 statusLabel.setText(status); 113 statusLabel.setForeground(Color.BLACK); 114 statusLabel.setBackground(Color.WHITE); 115 // statusLabel.setFont(Font.PLAIN); 116 doLayout(); 117 } showStatus(String demo, String status)118 void showStatus(String demo, String status) { 119 showStatus(demo+": "+status); 120 } showFailure(String status)121 void showFailure(String status) { 122 statusLabel.setText(status); 123 statusLabel.setBackground(Color.GRAY); 124 statusLabel.setForeground(Color.RED); 125 // statusLabel.setFont(Font.BOLD); 126 doLayout(); 127 } showFailure(String demo, String status)128 void showFailure(String demo, String status) { 129 showFailure(demo+": "+status); 130 } 131 132 actionPerformed(ActionEvent e)133 public void actionPerformed(ActionEvent e) { 134 // find button 135 for(int i=0;i<buttonList.length;i++) { 136 if(e.getSource() == buttonList[i]) { 137 String demoShort = demoList[i]; 138 String demo = demoBase+'.'+demoShort; 139 showStatus(demoShort, "launching"); 140 try { 141 Class c = Class.forName(demo); 142 String args[] = new String[0]; 143 Class params[] = new Class[1]; 144 params[0] = args.getClass(); 145 Method m = c.getMethod("main", params ); 146 Object[] argList = { args }; 147 m.invoke(null, argList); 148 showStatus(demoShort, "launched."); 149 } catch (ClassNotFoundException e1) { 150 showFailure(demoShort,e1.toString()); 151 e1.printStackTrace(); 152 } catch (SecurityException se) { 153 showFailure(demoShort,se.toString()); 154 se.printStackTrace(); 155 } catch (NoSuchMethodException nsme) { 156 showFailure(demoShort,nsme.toString()); 157 nsme.printStackTrace(); 158 } catch (IllegalArgumentException iae) { 159 showFailure(demoShort,iae.toString()); 160 iae.printStackTrace(); 161 } catch (IllegalAccessException iae) { 162 showFailure(demoShort,iae.toString()); 163 iae.printStackTrace(); 164 } catch (InvocationTargetException ite) { 165 showFailure(demoShort,ite.toString()); 166 ite.printStackTrace(); 167 } 168 repaint(); 169 } 170 } 171 } 172 173 } 174 175 /* This creates a Frame for the demo applet. */ createDemoFrame(DemoApplet applet)176 protected Frame createDemoFrame(DemoApplet applet) { 177 return new LauncherFrame(applet); 178 } 179 180 /** 181 * The main function which defines the behavior of the Demo 182 * applet when an applet is started. 183 */ main(String[] args)184 public static void main(String[] args) { 185 new Launcher().showDemo(); 186 } 187 } 188