]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/demos/src/com/ibm/icu/dev/demo/Launcher.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / demos / src / com / ibm / icu / dev / demo / Launcher.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2007, International Business Machines Corporation and         *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.dev.demo;\r
8 \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
22 \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
26 \r
27 \r
28 /**\r
29  * @author srl\r
30  * Application to provide a panel of demos to launch\r
31  */\r
32 public class Launcher extends DemoApplet {\r
33     private static final long serialVersionUID = -8054963875776183877L;\r
34     \r
35     /**\r
36      * base package of all demos\r
37      */\r
38     public static final String demoBase = "com.ibm.icu.dev.demo";\r
39     /**\r
40      * list of classes, relative to the demoBase. all must have a static void main(String[])\r
41      */\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
47 //        "rbbi.DBBIDemo",\r
48 //        "rbbi.RBBIDemo",\r
49 //        "rbbi.TextBoundDemo",\r
50         "rbnf.RbnfDemo",\r
51 //        "timescale.PivotDemo",  -- console\r
52         "translit.Demo",\r
53     };\r
54 \r
55     public class LauncherFrame extends Frame implements ActionListener {\r
56         private static final long serialVersionUID = -8054963875776183878L;\r
57         \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
61         \r
62         LauncherFrame(DemoApplet applet) {\r
63             init();\r
64             this.applet = applet;\r
65         }\r
66         \r
67         public void init() {\r
68             // close down when close is clicked.\r
69             // TODO: this should be factored..\r
70             addWindowListener(\r
71                     new WindowAdapter() {\r
72                         public void windowClosing(WindowEvent e) {\r
73                             setVisible(false);\r
74                             dispose();\r
75 \r
76                             if (applet != null) {\r
77                                 applet.demoClosed();\r
78                             } else System.exit(0);\r
79                         }\r
80                     } );\r
81 \r
82             setBackground(DemoUtility.bgColor);\r
83             setLayout(new BorderLayout());\r
84 \r
85             Panel topPanel = new Panel();\r
86             topPanel.setLayout(new GridLayout(5,3));\r
87 \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
92                 buttonList[i]=b;\r
93                 topPanel.add(b);\r
94             }\r
95             add(BorderLayout.CENTER,topPanel);\r
96             statusLabel = new Label("");\r
97             statusLabel.setAlignment(Label.LEFT);\r
98             String javaVersion = "";\r
99             try { \r
100                 javaVersion = "* Java: "+System.getProperty("java.version");\r
101             } catch (Throwable t) {\r
102                 javaVersion = "";\r
103             }\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
110         }\r
111         \r
112         /**\r
113          * Change the 'status' field, and set it to black\r
114          * @param status\r
115          */\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
121             doLayout();\r
122         }\r
123         void showStatus(String demo, String status) {\r
124             showStatus(demo+": "+status);\r
125         }\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
131             doLayout();\r
132         }\r
133         void showFailure(String demo, String status) {\r
134             showFailure(demo+": "+status);\r
135         }\r
136 \r
137         \r
138         public void actionPerformed(ActionEvent e) {\r
139             // find button\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
145                     try {\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
172                     }\r
173                     repaint();\r
174                 }\r
175             }\r
176         }\r
177 \r
178     }\r
179     \r
180     /* This creates a Frame for the demo applet. */\r
181     protected Frame createDemoFrame(DemoApplet applet) {\r
182         return new LauncherFrame(applet);\r
183     }\r
184 \r
185     /**\r
186      * The main function which defines the behavior of the Demo\r
187      * applet when an applet is started.\r
188      */\r
189     public static void main(String[] args) {\r
190         new Launcher().showDemo();\r
191     }\r
192 }\r