home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / IFC_112 / netscape / application / FoundationPanel.java < prev    next >
Encoding:
Text File  |  1999-05-28  |  9.4 KB  |  292 lines  |  [TEXT/CWIE]

  1. // FoundationPanel.java
  2. // By Ned Etcode
  3. // Copyright 1996, 1997 Netscape Communications Corp.  All rights reserved.
  4.  
  5. package netscape.application;
  6.  
  7. import netscape.util.*;
  8.  
  9. import java.awt.Panel;
  10. import java.awt.Rectangle;
  11. import java.awt.Event;
  12.  
  13. /** java.awt.Panel subclass that glues the IFC View-based world to the
  14.   * AWT component-based world.  Each Application has at least one
  15.   * FoundationPanel instance. If you use ExternalWindows, you'll have an
  16.   * additional instance per Window.  You'll rarely access a FoundationPanel
  17.   * directly - instead you'll work with an Application or RootView.
  18.   * @see Application
  19.   * @see RootView
  20.   * @note 1.0 added support for JDK 1.1 printing
  21.   * @note 1.0 requesting focus in keydown because of JDK 1.1 incompatibility
  22.   *           with JDK 1.0.2
  23.   */
  24. public class FoundationPanel extends Panel {
  25.     RootView rootView;
  26.  
  27.     /** Constructs a FoundationPanel with a RootView. */
  28.     public FoundationPanel() {
  29.         if(Application.application().handleExtendedKeyEvent() &&
  30.            JDK11AirLock.hasOneOneEvents()) {
  31.             JDK11AirLock.createFoundationPanelListener(this);
  32.         }
  33.         setRootView(new RootView());
  34.     }
  35.  
  36.     /** Constructs a FoundationPanel and RootView of a specified size. */
  37.     public FoundationPanel(int width, int height) {
  38.         setRootView(new RootView(0, 0, width, height));
  39.         resize(width, height);
  40.     }
  41.  
  42.     /** Returns the Panel's RootView, the View that sits at the top
  43.       * of the View hierarchy.
  44.       * @see RootView
  45.       */
  46.     public RootView rootView() {
  47.         return rootView;
  48.     }
  49.  
  50.     /** Sets the Panel's RootView. */
  51.     public void setRootView(RootView rootView) {
  52.         Application application = Application.application();
  53.  
  54.         if (this.rootView != null) {
  55.             application.removeRootView(this.rootView);
  56.         }
  57.         this.rootView = rootView;
  58.         rootView.setPanel(this);
  59.         rootView.setVisible(!application.isPaused);
  60.         application.addRootView(rootView);
  61.     }
  62.  
  63.     /** Overridden to resize the RootView when the FoundationPanel changes
  64.       * size.
  65.       */
  66.     public void resize(int width, int height) {
  67.         Application app = Application.application();
  68.  
  69.         super.resize(width, height);
  70.         // It is possible for the app to be null.  Sometimes we get called
  71.         // from the main thread group.  This seems like a security hole, but
  72.         // it still happens.
  73.         if (app != null && app.eventLoop.shouldProcessSynchronously()) {
  74.             rootView.processEvent(
  75.                         ApplicationEvent.newResizeEvent(width, height));
  76.         } else {
  77.             addEvent(ApplicationEvent.newResizeEvent(width, height));
  78.         }
  79.     }
  80.  
  81.     /** Overridden to resize the RootView when the FoundationPanel changes
  82.       * size.
  83.       */
  84.     public void reshape(int x, int y, int width, int height) {
  85.         Application app = Application.application();
  86.  
  87.         super.reshape(x, y, width, height);
  88.         // System.err.println("AWT reshape: " + new Rect(x, y, width, height));
  89.         // It is possible for the app to be null.  Sometimes we get called
  90.         // from the main thread group.  This seems like a security hole, but
  91.         // it still happens.
  92.         if (app != null && app.eventLoop.shouldProcessSynchronously()) {
  93.             rootView.processEvent(
  94.                             ApplicationEvent.newResizeEvent(width, height));
  95.         } else {
  96.             addEvent(ApplicationEvent.newResizeEvent(width, height));
  97.         }
  98.     }
  99.  
  100.     /** Updates the Panel. Calls <b>paint()</b>.
  101.       */
  102.     public void update(java.awt.Graphics g) {
  103.         paint(g);
  104.     }
  105.  
  106.     /** Paints the Panel by eventually calling <b>redraw()</b> on the RootView.
  107.       */
  108.     public void paint(java.awt.Graphics g) {
  109.         if (JDK11AirLock.isPrintGraphics(g)) {
  110.             paintNow(g);
  111.         } else {
  112.             addEvent(ApplicationEvent.newUpdateEvent(g));
  113.             super.paint(g);
  114.         }
  115.     }
  116.  
  117.     /** Overridden to add a MouseEvent of type <b>MouseEvent.MOUSE_DOWN</b>
  118.       * to the Application's EventLoop.
  119.       */
  120.     public boolean mouseDown(java.awt.Event evt, int x, int y) {
  121.         requestFocus();
  122.         addEvent(new MouseEvent(evt.when, MouseEvent.MOUSE_DOWN, x, y,
  123.                                 evt.modifiers));
  124.         return true;
  125.     }
  126.  
  127.     /** Overridden to add a MouseEvent of type
  128.       * <b>MouseEvent.MOUSE_DRAGGED</b> to the Application's EventLoop.
  129.       */
  130.     public boolean mouseDrag(java.awt.Event evt, int x, int y) {
  131.       /* funnel window drag events to a separate event queue */
  132.         addEvent(new MouseEvent(evt.when, MouseEvent.MOUSE_DRAGGED, x, y,
  133.                                 evt.modifiers));
  134.  
  135.         return true;
  136.     }
  137.  
  138.     /** Overridden to add a MouseEvent of type <b>MouseEvent.MOUSE_UP</b> to
  139.       * the Application's EventLoop.
  140.       */
  141.     public boolean mouseUp(java.awt.Event evt, int x, int y) {
  142.       /* funnel window drag events to a separate event queue */
  143.         addEvent(new MouseEvent(evt.when, MouseEvent.MOUSE_UP, x, y,
  144.                                 evt.modifiers));
  145.  
  146.         return true;
  147.     }
  148.  
  149.     /** Overridden to add a MouseEvent of type
  150.       * <b>MouseEvent.MOUSE_ENTERED</b> to the Application's EventLoop.
  151.       */
  152.     public boolean mouseEnter(java.awt.Event evt, int x, int y) {
  153.         addEvent(new MouseEvent(evt.when, MouseEvent.MOUSE_ENTERED, x, y,
  154.                                 evt.modifiers));
  155.         return true;
  156.     }
  157.  
  158.     /** Overridden to add a MouseEvent of type <b>MouseEvent.MOUSE_MOVED</b>
  159.       * to the Application's EventLoop.
  160.       */
  161.     public boolean mouseMove(java.awt.Event evt, int x, int y) {
  162.         addEvent(new MouseEvent(evt.when, MouseEvent.MOUSE_MOVED, x, y,
  163.                                 evt.modifiers));
  164.         return true;
  165.     }
  166.  
  167.     /** Overridden to add a MouseEvent of type <b>MouseEvent.MOUSE_EXITED</b>
  168.       * to the Application's EventLoop.
  169.       */
  170.     public boolean mouseExit(java.awt.Event evt, int x, int y) {
  171.         addEvent(new MouseEvent(evt.when, MouseEvent.MOUSE_EXITED, x, y,
  172.                                 evt.modifiers));
  173.  
  174.         return true;
  175.     }
  176.  
  177.     /** Overridden to add a KeyEvent of type <b>KeyEvent.KEY_DOWN</b> to
  178.       * the Application's EventLoop.
  179.       */
  180.     public boolean keyDown(java.awt.Event evt, int key) {
  181.         if (evt.target == this) {
  182.             addEvent(new KeyEvent(evt.when, key, evt.modifiers, true));
  183.             return true;
  184.         } else {
  185.             return super.keyDown(evt, key);
  186.         }
  187.     }
  188.  
  189.     /** Overridden to add a KeyEvent of type <b>KeyEvent.KEY_UP</b> to
  190.       * the Application's EventLoop.
  191.       */
  192.     public boolean keyUp(java.awt.Event evt, int key) {
  193.         if (evt.target == this) {
  194.             addEvent(new KeyEvent(evt.when, key, evt.modifiers, false));
  195.             return true;
  196.         } else {
  197.             return super.keyUp(evt, key);
  198.         }
  199.     }
  200.  
  201.     /** Overridden to force the focused View to stop editing.
  202.       */
  203.     public synchronized boolean lostFocus(java.awt.Event evt, Object what) {
  204.         addEvent(ApplicationEvent.newFocusEvent(false));
  205.         return true;
  206.     }
  207.  
  208.     /** Overridden to force the focused View to start editing.
  209.       */
  210.     public boolean gotFocus(java.awt.Event evt, Object what) {
  211.         addEvent(ApplicationEvent.newFocusEvent(true));
  212.         return true;
  213.     }
  214.  
  215.     /** @private **/
  216.     public synchronized void addEvent(netscape.application.Event anEvent) {
  217.         if (anEvent.processor() == null) {
  218.             anEvent.setProcessor(rootView);
  219.         }
  220.         if (rootView != null) {
  221.             if (rootView.application() != null) {
  222.                 rootView.application().eventLoop().addEvent(anEvent);
  223.             } else {   // ALERT!  This should never happen.
  224. //                System.err.println("No Application on RootView");
  225.             }
  226.         } else {
  227. //            System.err.println("No RootView");
  228.         }
  229.     }
  230.  
  231.     java.awt.Frame frame() {
  232.         java.awt.Frame          frame;
  233.         java.awt.Component      parent;
  234.  
  235.         parent = getParent();
  236.         while (parent != null && !(parent instanceof java.awt.Frame)) {
  237.             parent = parent.getParent();
  238.         }
  239.  
  240.         if (parent != null) {
  241.             return (java.awt.Frame)parent;
  242.         }
  243.  
  244.         return null;
  245.     }
  246.  
  247.     /** @private */
  248.     public void setCursor(int cursor) {
  249.         java.awt.Frame          frame;
  250.  
  251.         frame = frame();
  252.         if (frame != null) {
  253.             frame.setCursor(cursor);
  254.         }
  255.     }
  256.  
  257.     /** @private */
  258.     public void layout() {
  259.     }
  260.  
  261.     /** @private */
  262.     public void paintNow(java.awt.Graphics g) {
  263.         Application app = Application.application();
  264.  
  265.         if (app == null && rootView != null) {
  266.             app = rootView.application();
  267.         }
  268.  
  269.         if (app != null) {
  270.             if (app.eventLoop.shouldProcessSynchronously()) {
  271.                 Rect rect = new Rect(0, 0, rootView.width(), rootView.height());
  272.                 Graphics ifcGraphics = new Graphics(rect, g);
  273.  
  274.                 rootView.redraw(ifcGraphics, rect);
  275.                 rootView.redrawTransparentWindows(ifcGraphics, rect, null);
  276.             } else {
  277.                 ApplicationEvent event = ApplicationEvent.newPrintEvent(g);
  278.  
  279.                 event.processor = rootView;
  280.                 app.eventLoop().addEventAndWait(event);
  281.             }
  282.         } else {
  283.             System.err.println("Can't print with no application");
  284.         }
  285.     }
  286.  
  287.     /** @private */
  288.     public void printAll(java.awt.Graphics g) {
  289.         paintNow(g);
  290.     }
  291. }
  292.