1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * ident "%Z%%M% %I% %E% SMI" 24 * 25 * Copyright (c) 2000 by Sun Microsystems, Inc. 26 * All rights reserved. 27 */ 28 29 /* 30 * Copyright (C) 1996 Active Software, Inc. 31 * All rights reserved. 32 * 33 * @(#) MainHelper.java 1.29 - last change made 07/25/97 34 */ 35 36 package sunsoft.jws.visual.rt.base; 37 38 import sunsoft.jws.visual.rt.awt.RootFrame; 39 import sunsoft.jws.visual.rt.awt.GBLayout; 40 import sunsoft.jws.visual.rt.awt.GBConstraints; 41 import sunsoft.jws.visual.rt.shadow.java.awt.PanelShadow; 42 import sunsoft.jws.visual.rt.shadow.java.awt.DialogShadow; 43 import sunsoft.jws.visual.rt.shadow.java.awt.FrameShadow; 44 import sunsoft.jws.visual.rt.shadow.java.awt.WindowShadow; 45 46 import java.awt.*; 47 import java.util.Date; 48 import java.applet.Applet; 49 50 /** 51 * Helper class for the generated main class. 52 * 53 * @version 1.29, 07/25/97 54 */ 55 public class MainHelper { 56 /** 57 * The base group 58 */ 59 private Group baseGroup; 60 61 /** 62 * Name of an external call-out class if there is one. 63 */ 64 private String ecoClassName; 65 66 /** 67 * Pointer to an external call-out class if there is one. 68 */ 69 private ExternalCallOut eco; 70 71 72 /** 73 * Checks the version of the runtime. 74 */ checkVersion(double version)75 public void checkVersion(double version) { 76 if (Global.getVersion() < version) { 77 Double d1 = new Double(version); 78 Double d2 = new Double(Global.getVersion()); 79 System.out.println(Global.fmtMsg( 80 "sunsoft.jws.visual.rt.base.MainHelper.NeedRuntimeVersion", 81 d1, d2)); 82 } 83 } 84 85 /** 86 * Accepts the command line arguments. 87 * Sets an external class callout 88 * name if there is one. Registers the remaining 89 * command line arguments 90 * with the Global class. 91 */ parseCmdLineArgs(String args[])92 private String[] parseCmdLineArgs(String args[]) { 93 String registerArgs[] = args; 94 // args that will be registered 95 96 if (args.length >= 2) { 97 for (int i = 0; i < args.length - 1; i++) { 98 if (args[i].equals(/* NOI18N */"-external")) { 99 // set the class name for use in external callout later 100 ecoClassName = args[i + 1]; 101 102 // create a new argument list from the remaining 103 // cmd line args 104 registerArgs = new String[args.length - 2]; 105 int count = 0; 106 for (int j = 0; j < args.length; j++) { 107 if (j < i || j > i + 1) { 108 registerArgs[count] = args[j]; 109 count++; 110 } 111 } 112 break; 113 } 114 } 115 } 116 117 return registerArgs; 118 } 119 120 /** 121 * Called when application is run from the command line. 122 */ main(Group group, String args[])123 public void main(Group group, String args[]) { 124 // Set the base group 125 baseGroup = group; 126 127 RootFrame frame = null; 128 129 // Set the environment information. This must be done before 130 // initialization. 131 args = parseCmdLineArgs(args); 132 baseGroup.setEnvironmentInfo(null, args); 133 134 // Initialize the group 135 baseGroup.initialize(); 136 137 WindowShadow win = baseGroup.getWindow(); 138 139 // The top level frame must be set before the group 140 // is created. 141 if (win instanceof FrameShadow) { 142 win.createBody(); 143 baseGroup.setTopLevel((Frame)win.getBody()); 144 } else if (win instanceof DialogShadow) { 145 // Create a frame for the dialog 146 // if there are no other frames 147 String title = win.getName(); 148 if (title == null || title.equals(/* NOI18N */"")) 149 title = /* NOI18N */"Unnamed"; 150 151 RootFrame f = new RootFrame(title); 152 f.setSubGroup(baseGroup); 153 154 // SGI and Windows have bad defaults for 155 // font and background 156 if (Global.isIrix()) { 157 f.setFont(new Font(/* NOI18N */"Sansserif", 158 Font.PLAIN, 12)); 159 } 160 if (Global.isWindows()) { 161 f.setBackground(Color.lightGray); 162 f.setFont(new Font(/* NOI18N */"Dialog", 163 Font.PLAIN, 12)); 164 } 165 166 baseGroup.setTopLevel(f); 167 168 f.resize(25, 25); 169 f.show(); 170 f.reshape(20, 20, 120, 80); 171 f.validate(); 172 } else if (baseGroup.getPanel() != null) { 173 // Put a frame around the group's panel 174 // if it is a panel group 175 String title = group.getPanel().getName(); 176 if (title == null || title.equals(/* NOI18N */"")) 177 title = /* NOI18N */"Unnamed"; 178 179 frame = new RootFrame(title); 180 frame.setSubGroup(baseGroup); 181 182 // SGI and Windows have bad defaults for 183 // font and background 184 if (Global.isIrix()) { 185 frame.setFont(new Font(/* NOI18N */"Sansserif", 186 Font.PLAIN, 12)); 187 } 188 if (Global.isWindows()) { 189 frame.setBackground(Color.lightGray); 190 frame.setFont(new Font(/* NOI18N */"Dialog", 191 Font.PLAIN, 12)); 192 } 193 194 baseGroup.setTopLevel(frame); 195 } else { 196 throw new Error(Global.fmtMsg( 197 "sunsoft.jws.visual.rt.base.MainHelper.BaseGroupMustBeNonVis", 198 group)); 199 } 200 201 // Create the group 202 baseGroup.create(); 203 204 // This part needs to happen after the group is created 205 if (frame != null) { 206 frame.add(/* NOI18N */"Center", 207 (Panel)baseGroup.getPanel().getBody()); 208 // Window managers get confused when a window is shown with 209 // zero width and height 210 frame.resize(25, 25); 211 frame.show(); 212 Dimension d = frame.preferredSize(); 213 frame.reshape(20, 20, d.width, d.height); 214 frame.validate(); 215 } 216 217 // Start the group 218 baseGroup.start(); 219 220 if (ecoClassName != null) { 221 initExternalCallOut(ecoClassName, baseGroup); 222 startExternalCallOut(); 223 stopExternalCallOut(); 224 } 225 } 226 227 /** 228 * Called when the applet is loaded. 229 */ init(Applet applet, Group group)230 public void init(Applet applet, Group group) { 231 // Set the base group 232 baseGroup = group; 233 234 // Set the environment information. This must be done before 235 // initialization. 236 baseGroup.setEnvironmentInfo(applet, null); 237 238 // Initialize the group 239 baseGroup.initialize(); 240 241 WindowShadow win = baseGroup.getWindow(); 242 243 // The top level frame must be set before the 244 // group is created. 245 if (win instanceof FrameShadow) { 246 win.createBody(); 247 baseGroup.setTopLevel((Frame)win.getBody()); 248 } else { 249 // Figure out the applet's frame 250 Component comp = applet; 251 while (comp != null && !(comp instanceof Frame)) 252 comp = comp.getParent(); 253 254 baseGroup.setTopLevel((Frame)comp); 255 } 256 257 // Create the group 258 baseGroup.create(); 259 260 // Adjust the font of the panel 261 if (Global.isIrix()) 262 applet.setFont(new Font(/* NOI18N */"Sansserif", 263 Font.PLAIN, 12)); 264 else if (Global.isWindows()) 265 applet.setFont(new Font(/* NOI18N */"Dialog", 266 Font.PLAIN, 12)); 267 268 // Set up the layout for the Applet panel 269 GBLayout gridbag = new GBLayout(); 270 applet.setLayout(gridbag); 271 GBConstraints c = new GBConstraints(); 272 c.fill = GBConstraints.BOTH; 273 c.weightx = 1; 274 c.weighty = 1; 275 276 // Add the group panel to the applet 277 PanelShadow panelshadow = baseGroup.getPanel(); 278 if (panelshadow != null) { 279 Panel panel = (Panel)panelshadow.getBody(); 280 gridbag.setConstraints(applet.add(panel), c); 281 } 282 283 // Check for and initialize an external call-out class 284 ecoClassName = applet.getParameter(/* NOI18N */"external"); 285 if (ecoClassName != null) 286 initExternalCallOut(ecoClassName, baseGroup); 287 } 288 289 /** 290 * Called whenever the applet's page is visited. 291 */ start()292 public void start() { 293 if (baseGroup != null) 294 baseGroup.start(); 295 startExternalCallOut(); 296 } 297 298 /** 299 * Called by the browser when the user leaves the page. 300 */ stop()301 public void stop() { 302 if (baseGroup != null) 303 baseGroup.stop(); 304 stopExternalCallOut(); 305 } 306 307 /** 308 * Called by the browser when the applet should be destroyed. 309 */ destroy()310 public void destroy() { 311 if (baseGroup != null) 312 baseGroup.destroy(); 313 } 314 315 /** 316 * Initializes a call-out to an external class. 317 * Gives the external 318 * class a reference to this main's Group object. 319 */ initExternalCallOut(String name, Group group)320 private void initExternalCallOut(String name, Group group) { 321 try { 322 Class c = Global.util.getClassLoader().loadClass(name); 323 eco = (ExternalCallOut) c.newInstance(); 324 } 325 catch (ClassNotFoundException e) { 326 throw new Error(Global.fmtMsg( 327 "sunsoft.jws.visual.rt.base.MainHelper.ClassNotFound", 328 name)); 329 } 330 catch (InstantiationException e) { 331 throw new Error(Global.fmtMsg( 332 "sunsoft.jws.visual.rt.base.MainHelper.InstantiationException", 333 name)); 334 } 335 catch (IllegalAccessException e) { 336 throw new Error(Global.fmtMsg( 337 "sunsoft.jws.visual.rt.base.MainHelper.illegalAccess", 338 name)); 339 } 340 if (eco != null) 341 eco.initExternal(group); 342 } 343 344 /** 345 * Starts the external call-out class, if there is one. 346 */ startExternalCallOut()347 private void startExternalCallOut() { 348 if (eco != null) 349 eco.startExternal(); 350 } 351 352 /** 353 * Stops the external call-out class, if there is one. 354 */ stopExternalCallOut()355 private void stopExternalCallOut() { 356 if (eco != null) 357 eco.stopExternal(); 358 } 359 } 360