2 *******************************************************************************
3 * Copyright (C) 2007-2011, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.dev.demo;
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;
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;
30 * Application to provide a panel of demos to launch
32 public class Launcher extends DemoApplet {
33 private static final long serialVersionUID = -8054963875776183877L;
36 * base package of all demos
38 public static final String demoBase = "com.ibm.icu.dev.demo";
40 * list of classes, relative to the demoBase. all must have a static void main(String[])
42 public static final String demoList[] = {
43 "calendar.CalendarApp",
44 "charsetdet.DetectingViewer",
45 "holiday.HolidayCalendarDemo",
50 public class LauncherFrame extends Frame implements ActionListener {
51 private static final long serialVersionUID = -8054963875776183878L;
53 public Button buttonList[] = new Button[demoList.length]; // one button for each demo
54 public Label statusLabel;
55 private DemoApplet applet;
57 LauncherFrame(DemoApplet applet) {
63 // close down when close is clicked.
64 // TODO: this should be factored..
67 public void windowClosing(WindowEvent e) {
73 } else System.exit(0);
77 setBackground(DemoUtility.bgColor);
78 setLayout(new BorderLayout());
80 Panel topPanel = new Panel();
81 topPanel.setLayout(new GridLayout(5,3));
83 for(int i=0;i<buttonList.length;i++) {
84 String demo = demoList[i];
85 Button b = new Button(demo);
86 b.addActionListener(this);
90 add(BorderLayout.CENTER,topPanel);
91 statusLabel = new Label("");
92 statusLabel.setAlignment(Label.LEFT);
93 String javaVersion = "";
95 javaVersion = "* Java: "+System.getProperty("java.version");
96 } catch (Throwable t) {
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. ");
108 * Change the 'status' field, and set it to black
111 void showStatus(String status) {
112 statusLabel.setText(status);
113 statusLabel.setForeground(Color.BLACK);
114 statusLabel.setBackground(Color.WHITE);
115 // statusLabel.setFont(Font.PLAIN);
118 void showStatus(String demo, String status) {
119 showStatus(demo+": "+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);
128 void showFailure(String demo, String status) {
129 showFailure(demo+": "+status);
133 public void actionPerformed(ActionEvent e) {
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");
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();
175 /* This creates a Frame for the demo applet. */
176 protected Frame createDemoFrame(DemoApplet applet) {
177 return new LauncherFrame(applet);
181 * The main function which defines the behavior of the Demo
182 * applet when an applet is started.
184 public static void main(String[] args) {
185 new Launcher().showDemo();