001/*
002 * WrapperApp.java
003 * $Header$
004 * Copyright (c) 2002 Tom B. Gutwin P.Eng.
005 *
006 * This program is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU General Public License
008 * as published by the Free Software Foundation; either version 2
009 * of the License, or any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program; if not, write to the Free Software
018 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
019 */
020
021package ca.bc.webarts.widgets;
022import java.applet.Applet;
023
024import java.awt.*;
025import java.awt.event.*;
026import java.lang.System;
027import java.util.*;
028import javax.swing.*;
029import javax.swing.event.*;
030
031
032
033/**
034 *  The WrapperApp Frame is a generic JFrame that holds a JApplet or AWT Applet.
035 *  <PRE>
036 *  EXAMPLE:
037 *  WrapperApp app = new WrapperApp(" MyWrapped Applet ", new MyApplet());
038 *  app.pack(); 
039 *  app.setSize(800,600);
040 *  app.setVisible(true); 
041 *  app.setScreenLocation(2,2,0,0);</PRE>
042 *
043 * @author     tgutwin
044 * @created    July 10, 2002
045 */
046public class WrapperApp extends JFrame
047{
048  /**  The applet that the JFrame will contain. */
049  private Applet applet;
050  
051  /**  Description of the Field */
052  Toolkit toolkit = Toolkit.getDefaultToolkit();
053  
054  /**  Debug level */
055  int debug = 1;
056
057
058  /**
059   *  Constructor with a jApplet .
060   *
061   * @param  frameTitle  Description of the Parameter
062   * @param  jApplet     Description of the Parameter
063   */
064  public WrapperApp(String frameTitle, JApplet jApplet)
065  {
066    /*
067     *  get the super class frame to build a frame with a title
068     */
069    super(frameTitle);
070
071    try
072    {
073      String vers = System.getProperty("java.version");
074      debugOutput(1, System.getProperty("java.vendor") + " Runtime Version: " + vers);
075      if (vers.compareTo("1.1.2") < 0)
076      {
077        System.out.println("!!!WARNING: Swing must be run with a " +
078            "1.1.2 or higher version VM!!!");
079      }
080
081      /*
082       *  Instantiate the APPLET
083       */
084      applet = jApplet;
085
086      /*
087       *  place it into the app frame
088       */
089      getContentPane().add(applet, "Center");
090
091      /*
092       *  Init the applet
093       */
094      applet.init();
095
096      /*
097       *  Start the applet
098       */
099      applet.start();
100
101      /*
102       *  listen for closing
103       */
104      addWindowListener(
105        new WindowAdapter()
106        {
107          public void windowClosing(WindowEvent event)
108          {
109            /*
110             *  Stop the applet
111             */
112            applet.stop();
113
114            dispose();
115            System.exit(0);
116          }
117        });
118        
119      pack();
120      show();
121      toFront();
122      //setSize(300,300);
123      setVisible(true);
124    }
125    catch (Throwable t)
126    {
127      System.out.println("caught exception: " + t);
128      t.printStackTrace();
129    }
130  }
131
132
133  /**
134   *  Constructor with an Applet .
135   *
136   * @param  frameTitle  Description of the Parameter
137   * @param  awtApplet   Description of the Parameter
138   */
139  public WrapperApp(String frameTitle, Applet awtApplet)
140  {
141    /*
142     *  get the super class frame to build a frame with a title
143     */
144    super(frameTitle);
145
146    try
147    {
148      String vers = System.getProperty("java.version");
149      debugOutput(1, System.getProperty("java.vendor") + " Runtime Version: " + vers);
150      if (vers.compareTo("1.1.2") < 0)
151      {
152        System.out.println("!!!WARNING: Swing must be run with a " +
153            "1.1.2 or higher version VM!!!");
154      }
155
156      /*
157       *  Instantiate the APPLET
158       */
159      applet = awtApplet;
160
161      /*
162       *  place it into the app frame
163       */
164      getContentPane().add(applet, "Center");
165
166      /*
167       *  Init the applet
168       */
169      applet.init();
170
171      /*
172       *  Start the applet
173       */
174      applet.start();
175
176      /*
177       *  listen for closing
178       */
179      addWindowListener(
180        new WindowAdapter()
181        {
182          public void windowClosing(WindowEvent event)
183          {
184            /*
185             *  Stop the applet
186             */
187            applet.stop();
188
189            dispose();
190            System.exit(0);
191          }
192        });
193        
194      pack();
195      show();
196      toFront();
197      //setSize(300,300);
198      setVisible(true);
199    }
200    catch (Throwable t)
201    {
202      System.out.println("caught exception: " + t);
203      t.printStackTrace();
204    }
205  }
206
207
208  /**
209   *  Constructor for the WrapperApp object
210   *
211   * @param  frameTitle  Description of the Parameter
212   */
213  public WrapperApp(String frameTitle)
214  {
215    /*
216     *  get the super class frame to build a frame with a title
217     */
218    super(frameTitle);
219
220    try
221    {
222      String vers = System.getProperty("java.version");
223      debugOutput(1, System.getProperty("java.vendor") + " Runtime Version: " + vers);
224      if (vers.compareTo("1.1.2") < 0)
225      {
226        System.out.println("!!!WARNING: Swing must be run with a " +
227            "1.1.2 or higher version VM!!!");
228      }
229
230      /*
231       *  listen for closing
232       */
233      addWindowListener(
234        new WindowAdapter()
235        {
236          public void windowClosing(WindowEvent event)
237          {
238            /*
239             *  Stop the applet
240             */
241            applet.stop();
242
243            dispose();
244            System.exit(0);
245          }
246        });
247      /*
248       *  inner WindowAdapter class
249       */
250    }
251    catch (Throwable t)
252    {
253      System.out.println("caught exception: " + t);
254      t.printStackTrace();
255    }
256  }
257
258
259  /**
260   *  Gets the debug attribute of the WrapperApp object
261   *
262   * @return    The debug value
263   */
264  public int getDebug()
265  {
266    return debug;
267  }
268
269
270  /**
271   *  Sets the debug attribute of the WrapperApp object
272   *
273   * @param  l  The new debug value
274   */
275  public void setDebug(int l)
276  {
277    debug = l;
278  }
279
280
281  /**
282   *  handles debug messages by comparing the app debug level to the message
283   *  level. if the message level is less or == then the message get outout to
284   *  System.out.
285   *
286   * @param  l       Description of the Parameter
287   * @param  output  Description of the Parameter
288   */
289  private void debugOutput(int l, String output)
290  {
291
292    if (l <= debug)
293    {
294      System.out.println(output);
295    }
296  }
297
298
299
300  /**
301   *  Sets the screenLocation attribute of the WrapperApp object
302   *
303   * @param  xPosition  The new screenLocation value
304   * @param  yPosition  The new screenLocation value
305   * @param  xOffset    The new screenLocation value
306   * @param  yOffset    The new screenLocation value
307   */
308  public void setScreenLocation(int xPosition, int yPosition, int xOffset, int yOffset)
309  {
310    /*
311     *  the xPosition can be 1,2, or 3 for left center and right justified
312     */
313    /*
314     *  theyPosition can be 1,2, or 3 for Top center and bottom justified
315     */
316    Dimension scrnSize = toolkit.getScreenSize();
317    int scrnWidth = scrnSize.width;
318    int scrnHeight = scrnSize.height;
319    Frame topFrame = this;
320    int frameWidth = topFrame.getSize().width;
321    int frameHeight = topFrame.getSize().height;
322    Point startPoint = topFrame.getLocationOnScreen();
323    Point newPoint = new Point(startPoint);
324
325    debugOutput(3, "Existing pos       " + " " + (new Integer(xPosition)).toString() +
326        " " + (new Integer(yPosition)).toString() +
327        " " + (new Integer(frameWidth)).toString() +
328        " " + (new Integer(frameHeight)).toString() +
329        " " + (new Integer(newPoint.x)).toString() +
330        " " + (new Integer(newPoint.y)).toString());
331
332    switch (xPosition)
333    {
334        case 1:
335          newPoint.x = 0 + xOffset;
336          break;
337        case 2:
338          newPoint.x = scrnWidth / 2 - frameWidth / 2 + xOffset;
339          break;
340        case 3:
341          newPoint.x = scrnWidth - frameWidth;
342          break;
343        default:
344          debugOutput(3, "ERROR in Setting x Location");
345    }
346    switch (yPosition)
347    {
348        case 1:
349          newPoint.y = 0 + yOffset;
350          break;
351        case 2:
352          newPoint.y = scrnHeight / 2 - frameHeight / 2 + yOffset;
353          break;
354        case 3:
355          newPoint.y = scrnHeight - frameHeight;
356          break;
357        default:
358          debugOutput(3, "ERROR in Setting y Location");
359    }
360    debugOutput(3, "NEW pos            " + " " + (new Integer(xPosition)).toString() +
361        " " + (new Integer(yPosition)).toString() +
362        " " + (new Integer(frameWidth)).toString() +
363        " " + (new Integer(frameHeight)).toString() +
364        " " + (new Integer(newPoint.x)).toString() +
365        " " + (new Integer(newPoint.y)).toString());
366    topFrame.setLocation(newPoint);
367  }
368
369}
370