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