2 *******************************************************************************
\r
3 * Copyright (C) 2007, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.demo;
\r
9 import java.awt.BorderLayout;
\r
10 import java.awt.Button;
\r
11 import java.awt.Color;
\r
12 import java.awt.Frame;
\r
13 import java.awt.GridLayout;
\r
14 import java.awt.Label;
\r
15 import java.awt.Panel;
\r
16 import java.awt.event.ActionEvent;
\r
17 import java.awt.event.ActionListener;
\r
18 import java.awt.event.WindowAdapter;
\r
19 import java.awt.event.WindowEvent;
\r
20 import java.lang.reflect.InvocationTargetException;
\r
21 import java.lang.reflect.Method;
\r
23 import com.ibm.icu.dev.demo.impl.DemoApplet;
\r
24 import com.ibm.icu.dev.demo.impl.DemoUtility;
\r
25 import com.ibm.icu.util.VersionInfo;
\r
30 * Application to provide a panel of demos to launch
\r
32 public class Launcher extends DemoApplet {
\r
33 private static final long serialVersionUID = -8054963875776183877L;
\r
36 * base package of all demos
\r
38 public static final String demoBase = "com.ibm.icu.dev.demo";
\r
40 * list of classes, relative to the demoBase. all must have a static void main(String[])
\r
42 public static final String demoList[] = {
\r
43 "calendar.CalendarApp",
\r
44 "charsetdet.DetectingViewer",
\r
45 "holiday.HolidayCalendarDemo",
\r
46 // "number.CurrencyDemo", -- console
\r
49 // "rbbi.TextBoundDemo",
\r
51 // "timescale.PivotDemo", -- console
\r
55 public class LauncherFrame extends Frame implements ActionListener {
\r
56 private static final long serialVersionUID = -8054963875776183878L;
\r
58 public Button buttonList[] = new Button[demoList.length]; // one button for each demo
\r
59 public Label statusLabel;
\r
60 private DemoApplet applet;
\r
62 LauncherFrame(DemoApplet applet) {
\r
64 this.applet = applet;
\r
67 public void init() {
\r
68 // close down when close is clicked.
\r
69 // TODO: this should be factored..
\r
71 new WindowAdapter() {
\r
72 public void windowClosing(WindowEvent e) {
\r
76 if (applet != null) {
\r
77 applet.demoClosed();
\r
78 } else System.exit(0);
\r
82 setBackground(DemoUtility.bgColor);
\r
83 setLayout(new BorderLayout());
\r
85 Panel topPanel = new Panel();
\r
86 topPanel.setLayout(new GridLayout(5,3));
\r
88 for(int i=0;i<buttonList.length;i++) {
\r
89 String demo = demoList[i];
\r
90 Button b = new Button(demo);
\r
91 b.addActionListener(this);
\r
95 add(BorderLayout.CENTER,topPanel);
\r
96 statusLabel = new Label("");
\r
97 statusLabel.setAlignment(Label.LEFT);
\r
98 String javaVersion = "";
\r
100 javaVersion = "* Java: "+System.getProperty("java.version");
\r
101 } catch (Throwable t) {
\r
104 add(BorderLayout.NORTH, new Label(
\r
105 "ICU Demos * ICU version "+VersionInfo.ICU_VERSION +
\r
106 " * http://icu-project.org "+javaVersion));
\r
107 add(BorderLayout.SOUTH,statusLabel);
\r
108 // set up an initial status.
\r
109 showStatus(buttonList.length+" demos ready. ");
\r
113 * Change the 'status' field, and set it to black
\r
116 void showStatus(String status) {
\r
117 statusLabel.setText(status);
\r
118 statusLabel.setForeground(Color.BLACK);
\r
119 statusLabel.setBackground(Color.WHITE);
\r
120 // statusLabel.setFont(Font.PLAIN);
\r
123 void showStatus(String demo, String status) {
\r
124 showStatus(demo+": "+status);
\r
126 void showFailure(String status) {
\r
127 statusLabel.setText(status);
\r
128 statusLabel.setBackground(Color.GRAY);
\r
129 statusLabel.setForeground(Color.RED);
\r
130 // statusLabel.setFont(Font.BOLD);
\r
133 void showFailure(String demo, String status) {
\r
134 showFailure(demo+": "+status);
\r
138 public void actionPerformed(ActionEvent e) {
\r
140 for(int i=0;i<buttonList.length;i++) {
\r
141 if(e.getSource() == buttonList[i]) {
\r
142 String demoShort = demoList[i];
\r
143 String demo = demoBase+'.'+demoShort;
\r
144 showStatus(demoShort, "launching");
\r
146 Class c = Class.forName(demo);
\r
147 String args[] = new String[0];
\r
148 Class params[] = new Class[1];
\r
149 params[0] = args.getClass();
\r
150 Method m = c.getMethod("main", params );
\r
151 Object[] argList = { args };
\r
152 m.invoke(null, argList);
\r
153 showStatus(demoShort, "launched.");
\r
154 } catch (ClassNotFoundException e1) {
\r
155 showFailure(demoShort,e1.toString());
\r
156 e1.printStackTrace();
\r
157 } catch (SecurityException se) {
\r
158 showFailure(demoShort,se.toString());
\r
159 se.printStackTrace();
\r
160 } catch (NoSuchMethodException nsme) {
\r
161 showFailure(demoShort,nsme.toString());
\r
162 nsme.printStackTrace();
\r
163 } catch (IllegalArgumentException iae) {
\r
164 showFailure(demoShort,iae.toString());
\r
165 iae.printStackTrace();
\r
166 } catch (IllegalAccessException iae) {
\r
167 showFailure(demoShort,iae.toString());
\r
168 iae.printStackTrace();
\r
169 } catch (InvocationTargetException ite) {
\r
170 showFailure(demoShort,ite.toString());
\r
171 ite.printStackTrace();
\r
180 /* This creates a Frame for the demo applet. */
\r
181 protected Frame createDemoFrame(DemoApplet applet) {
\r
182 return new LauncherFrame(applet);
\r
186 * The main function which defines the behavior of the Demo
\r
187 * applet when an applet is started.
\r
189 public static void main(String[] args) {
\r
190 new Launcher().showDemo();
\r