001 package com.croftsoft.core.gui.multi; 002 003 import java.applet.*; 004 import java.awt.*; 005 import java.io.*; 006 import java.net.*; 007 import java.util.*; 008 import javax.swing.*; 009 import javax.swing.event.*; 010 011 import com.croftsoft.core.CroftSoftConstants; 012 import com.croftsoft.core.awt.image.ImageLib; 013 import com.croftsoft.core.gui.FullScreenToggler; 014 import com.croftsoft.core.gui.LifecycleWindowListener; 015 import com.croftsoft.core.lang.NullArgumentException; 016 import com.croftsoft.core.lang.Pair; 017 import com.croftsoft.core.lang.lifecycle.Lifecycle; 018 019 /********************************************************************* 020 * An applet that contains multiple applets. 021 * 022 * @version 023 * 2003-08-02 024 * @since 025 * 2002-02-25 026 * @author 027 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 028 *********************************************************************/ 029 030 public class MultiApplet 031 extends JApplet 032 implements Lifecycle 033 ////////////////////////////////////////////////////////////////////// 034 ////////////////////////////////////////////////////////////////////// 035 { 036 037 public static final String DEFAULT_NEWS_NAME = "News"; 038 039 // 040 041 private final String appletInfo; 042 043 private final Pair [ ] appletPairs; 044 045 private final String newsName; 046 047 private final String newsHTML; 048 049 private final String newsPage; 050 051 // 052 053 private JTabbedPane jTabbedPane; 054 055 private Component appletComponent; 056 057 private boolean isStarted; 058 059 private int index; 060 061 private MultiAppletStub multiAppletStub; 062 063 ////////////////////////////////////////////////////////////////////// 064 ////////////////////////////////////////////////////////////////////// 065 066 /********************************************************************* 067 * Test method. 068 *********************************************************************/ 069 public static void main ( String [ ] args ) 070 ////////////////////////////////////////////////////////////////////// 071 { 072 launch ( 073 CroftSoftConstants.DEFAULT_APPLET_INFO, 074 new Pair [ ] { 075 new Pair ( "Applet1", "javax.swing.JApplet" ), 076 new Pair ( "Applet2", "javax.swing.JApplet" ) }, 077 DEFAULT_NEWS_NAME, 078 ( String ) null, 079 CroftSoftConstants.HOME_PAGE, 080 "CroftSoft MultiApplet", 081 CroftSoftConstants.FRAME_ICON_FILENAME, 082 MultiApplet.class.getClassLoader ( ), 083 ( Dimension ) null, 084 "Close CroftSoft MultiApplet?" ); 085 } 086 087 public static void launch ( 088 String appletInfo, 089 Pair [ ] appletPairs, 090 String newsName, 091 String newsHTML, 092 String newsPage, 093 String frameTitle, 094 String frameIconFilename, 095 ClassLoader frameIconClassLoader, 096 Dimension frameSize, 097 String shutdownConfirmationPrompt ) 098 ////////////////////////////////////////////////////////////////////// 099 { 100 JFrame jFrame = new JFrame ( frameTitle ); 101 102 try 103 { 104 // Using ImageLib.loadBufferedImage() 105 // since ImageIO.read(URL) buggy 106 107 Image iconImage = ImageLib.loadBufferedImage ( 108 frameIconFilename, frameIconClassLoader ); 109 110 if ( iconImage != null ) 111 { 112 jFrame.setIconImage ( iconImage ); 113 } 114 } 115 catch ( Exception ex ) 116 { 117 } 118 119 MultiApplet multiApplet = new MultiApplet ( 120 appletInfo, appletPairs, newsName, newsHTML, newsPage ); 121 122 jFrame.setContentPane ( multiApplet ); 123 124 FullScreenToggler.monitor ( jFrame ); 125 126 LifecycleWindowListener.launchFrameAsDesktopApp ( 127 jFrame, 128 new Lifecycle [ ] { multiApplet }, 129 frameSize, 130 shutdownConfirmationPrompt ); 131 } 132 133 ////////////////////////////////////////////////////////////////////// 134 // constructor methods 135 ////////////////////////////////////////////////////////////////////// 136 137 public MultiApplet ( 138 String appletInfo, 139 Pair [ ] appletPairs, 140 String newsName, 141 String newsHTML, 142 String newsPage ) 143 ////////////////////////////////////////////////////////////////////// 144 { 145 NullArgumentException.check ( this.appletInfo = appletInfo ); 146 147 NullArgumentException.check ( this.appletPairs = appletPairs ); 148 149 NullArgumentException.check ( this.newsName = newsName ); 150 151 this.newsHTML = newsHTML; 152 153 this.newsPage = newsPage; 154 } 155 156 ////////////////////////////////////////////////////////////////////// 157 ////////////////////////////////////////////////////////////////////// 158 159 public String getAppletInfo ( ) 160 ////////////////////////////////////////////////////////////////////// 161 { 162 return appletInfo; 163 } 164 165 ////////////////////////////////////////////////////////////////////// 166 ////////////////////////////////////////////////////////////////////// 167 168 public void init ( ) 169 ////////////////////////////////////////////////////////////////////// 170 { 171 Container contentPane = getContentPane ( ); 172 173 contentPane.setLayout ( new BorderLayout ( ) ); 174 175 jTabbedPane = new JTabbedPane ( 176 JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT ); 177 178 contentPane.add ( jTabbedPane, BorderLayout.CENTER ); 179 180 jTabbedPane.add ( 181 new MultiAppletNews ( newsHTML, newsPage, this ), newsName ); 182 183 for ( int i = 0; i < appletPairs.length; i++ ) 184 { 185 jTabbedPane.add ( new JPanel ( ), appletPairs [ i ].name ); 186 } 187 188 jTabbedPane.addChangeListener ( 189 new ChangeListener ( ) 190 { 191 public void stateChanged ( ChangeEvent changeEvent ) 192 { 193 handleStateChange ( ); 194 } 195 } ); 196 197 multiAppletStub = new MultiAppletStub ( this ); 198 } 199 200 public void start ( ) 201 ////////////////////////////////////////////////////////////////////// 202 { 203 multiAppletStub.setActive ( true ); 204 205 try 206 { 207 if ( appletComponent instanceof Applet ) 208 { 209 ( ( Applet ) appletComponent ).start ( ); 210 } 211 else if ( appletComponent instanceof Lifecycle ) 212 { 213 ( ( Lifecycle ) appletComponent ).start ( ); 214 } 215 } 216 catch ( Exception ex ) 217 { 218 ex.printStackTrace ( ); 219 } 220 221 isStarted = true; 222 } 223 224 public void stop ( ) 225 ////////////////////////////////////////////////////////////////////// 226 { 227 multiAppletStub.setActive ( false ); 228 229 try 230 { 231 if ( appletComponent instanceof Applet ) 232 { 233 ( ( Applet ) appletComponent ).stop ( ); 234 } 235 else if ( appletComponent instanceof Lifecycle ) 236 { 237 ( ( Lifecycle ) appletComponent ).stop ( ); 238 } 239 } 240 catch ( Exception ex ) 241 { 242 ex.printStackTrace ( ); 243 } 244 245 isStarted = false; 246 } 247 248 public synchronized void destroy ( ) 249 ////////////////////////////////////////////////////////////////////// 250 { 251 try 252 { 253 if ( appletComponent instanceof Applet ) 254 { 255 ( ( Applet ) appletComponent ).destroy ( ); 256 } 257 else if ( appletComponent instanceof Lifecycle ) 258 { 259 ( ( Lifecycle ) appletComponent ).destroy ( ); 260 } 261 } 262 catch ( Exception ex ) 263 { 264 ex.printStackTrace ( ); 265 } 266 } 267 268 ////////////////////////////////////////////////////////////////////// 269 ////////////////////////////////////////////////////////////////////// 270 271 private void handleStateChange ( ) 272 ////////////////////////////////////////////////////////////////////// 273 { 274 if ( isStarted ) 275 { 276 stop ( ); 277 } 278 279 if ( index > 0 ) 280 { 281 jTabbedPane.setComponentAt ( index, new JPanel ( ) ); 282 283 destroy ( ); 284 285 appletComponent = null; 286 287 System.gc ( ); 288 } 289 290 index = jTabbedPane.getSelectedIndex ( ); 291 292 if ( index > 0 ) 293 { 294 try 295 { 296 appletComponent = ( Component ) Class.forName ( 297 appletPairs [ index - 1 ].value ).newInstance ( ); 298 299 if ( appletComponent instanceof Applet ) 300 { 301 ( ( Applet ) appletComponent ).setStub ( multiAppletStub ); 302 } 303 304 if ( appletComponent instanceof JComponent ) 305 { 306 FullScreenToggler.monitor ( ( JComponent ) appletComponent ); 307 } 308 309 jTabbedPane.setComponentAt ( index, appletComponent ); 310 311 try 312 { 313 if ( appletComponent instanceof Applet ) 314 { 315 ( ( Applet ) appletComponent ).init ( ); 316 } 317 else if ( appletComponent instanceof Lifecycle ) 318 { 319 ( ( Lifecycle ) appletComponent ).init ( ); 320 } 321 } 322 catch ( Exception ex ) 323 { 324 ex.printStackTrace ( ); 325 } 326 327 start ( ); 328 } 329 catch ( Exception ex ) 330 { 331 ex.printStackTrace ( ); 332 } 333 } 334 } 335 336 ////////////////////////////////////////////////////////////////////// 337 ////////////////////////////////////////////////////////////////////// 338 }