home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / in4wjcxu / src / como / awt / smartframe.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  2.6 KB  |  114 lines

  1. package como.awt;
  2.  
  3. import java.awt.*;
  4. import como.*;
  5. import como.util.Debug;
  6.  
  7. public class SmartFrame extends java.awt.Frame {
  8.     
  9. private Component notify = null;
  10. private Button okbutton = null;
  11. private Button cancelbutton = null;
  12. private Event okevent = null;
  13. private Event cancelevent = null;
  14. private boolean notifyall = false;
  15.  
  16.     /**
  17.      * Constructs a new SmartFrame. This Frame only reacts to the
  18.      * WINDOW_DESTROY Event.
  19.      */
  20.     public SmartFrame() {
  21.     }
  22.  
  23.     /**
  24.      * Constructs a new SmartFrame. If a notifier Component is given,
  25.      * Events are posted there. Otherwise, this Frame only reacts to the
  26.      * WINDOW_DESTROY Event.
  27.      */
  28.     public SmartFrame(Component c) {
  29.         notify = c;
  30.         notifyall = true;
  31.     }
  32.  
  33.     /**
  34.     * Constructs a new SmartFrame with two Buttons: OK and Cancel. If OK is pressed,
  35.     * Evt is sent to component. If Cancel is pressed, evtcancel is sent.
  36.     * It is not pack()'ed or show()'n.
  37.     */
  38.     public SmartFrame(Component c,String ok,Event evtOk,String cancel,Event evtCancel) {
  39.         notify = c;
  40.         cancelbutton = new Button(cancel);
  41.         cancelevent = evtCancel;
  42.         cancelevent.target = this;
  43.         okbutton = new Button(ok);
  44.         okevent = evtOk;
  45.         okevent.target = this;
  46.         Panel p = new Panel();
  47.         p.setLayout( new FlowLayout( FlowLayout.CENTER ) );
  48.         p.add(okbutton);
  49.         p.add(cancelbutton);
  50.         add("South",p);
  51.     }
  52.  
  53.     /**
  54.      * Constructs a new SmartFrame. The Frame will be shown.
  55.      * The Component will be displayed in the Frame and an
  56.      * OK-Button will be added.
  57.      */
  58.     public SmartFrame(Component comp, String ok) {
  59.         notify = null;
  60.  
  61.         // This is interesting here. First I used a BorderLayout
  62.         // with a componen in the North, and the OK-button
  63.         // in the south. With Win95 the North was half offscreen!!!!
  64.  
  65.         setLayout( new VertLayout( VertLayout.STRETCH ) );
  66.         Panel okpanel = new Panel();
  67.         okpanel.setLayout( new FlowLayout( FlowLayout.CENTER ) );
  68.         okbutton = new Button( ok );
  69.         okpanel.add( okbutton );
  70.  
  71.         add( comp );
  72.         add( okpanel );
  73.         setTitle( "Information" );
  74.         pack();
  75.         show();
  76.     }
  77.  
  78.     /** 
  79.      * calls SmartFrame( new Label(msg), ok );
  80.      */
  81.     public SmartFrame(String msg, String ok) {
  82.         this( new Label( msg, Label.CENTER ), ok );
  83.     }
  84.  
  85.     /** 
  86.      * calls SmartFrame( msg, "OK" );
  87.      */
  88.     public SmartFrame(String msg) {
  89.         this( msg, "OK" );
  90.     }
  91.  
  92.     public boolean handleEvent(Event evt) {
  93.         if (notify != null) {
  94.             if (evt.target == okbutton) {
  95.                 notify.handleEvent(okevent);
  96.                 return true;
  97.             }
  98.             if (evt.target == cancelbutton) {
  99.                 notify.handleEvent(cancelevent);
  100.                 return true;
  101.             }
  102.             if (notifyall)
  103.                 if (notify.handleEvent(evt)) return true;
  104.         }
  105.         if (evt.id == Event.WINDOW_DESTROY || evt.target == okbutton) {
  106.             hide();
  107.             dispose();
  108.             return true;
  109.  
  110.         }
  111.         else return false;
  112.         }
  113. }
  114.