Home » openjdk-7 » java » awt » [javadoc | source]

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   /**
   18    * @author Pavel Dolgov
   19    *
   20    */
   21   
   22   package java.awt;
   23   
   24   import org.apache.harmony.awt.gl.MultiRectArea;
   25   import org.apache.harmony.awt.wtk.NativeWindow;
   26   
   27   /**
   28    * Non-component window for displaying menus and drop-down lists
   29    */
   30   abstract class PopupBox {
   31   
   32       NativeWindow nativeWindow;
   33   
   34       final Dimension size = new Dimension();
   35       final Point location = new Point();
   36       private Window owner;
   37       private PopupBox parent;
   38       private PopupBox activeChild;
   39       private boolean visible;
   40       private ModalContext modalContext;
   41   
   42       final Toolkit toolkit = Toolkit.getDefaultToolkit();
   43   
   44       PopupBox() {
   45       }
   46   
   47       void addNotify() {
   48           if (nativeWindow == null) {
   49               nativeWindow = toolkit.createPopupNativeWindow(this);
   50           }
   51       }
   52   
   53       void removeNotify() {
   54           if (nativeWindow != null) {
   55               toolkit.removePopupNativeWindow(nativeWindow);
   56               NativeWindow temp = nativeWindow;
   57               nativeWindow = null;
   58               temp.dispose();
   59           }
   60       }
   61   
   62       Dimension getSize() {
   63           return new Dimension(size);
   64       }
   65   
   66       int getHeight() {
   67           return getSize().height;
   68       }
   69   
   70       Point getLocation() {
   71           return new Point(location);
   72       }
   73   
   74       Point getScreenLocation() {
   75           return new Point(location);
   76       }
   77   
   78       boolean contains(Point p) {
   79           Rectangle r = new Rectangle(getScreenLocation(), getSize());
   80           return r.contains(p);
   81       }
   82   
   83       Window getOwner() {
   84           return owner;
   85       }
   86   
   87       PopupBox getParent() {
   88           return parent;
   89       }
   90   
   91       void setParent(PopupBox p) {
   92           parent = p;
   93       }
   94   
   95       final PopupBox getActiveChild() {
   96           return activeChild;
   97       }
   98   
   99       final NativeWindow getNativeWindow() {
  100           return nativeWindow;
  101       }
  102   
  103       final boolean isVisible() {
  104           return visible;
  105       }
  106   
  107       boolean isMenu() {
  108           return false;
  109       }
  110   
  111       final void setModal(boolean modal) {
  112           if (modal) {
  113               if (modalContext == null) {
  114                   modalContext = new ModalContext();
  115               }
  116           } else {
  117               if (modalContext != null) {
  118                   if (modalContext.isModalLoopRunning()) {
  119                       modalContext.endModalLoop();
  120                   }
  121                   modalContext = null;
  122               }
  123           }
  124       }
  125   
  126       final boolean isModal() {
  127           return modalContext != null;
  128       }
  129   
  130       boolean isMenuBar() {
  131           return false;
  132       }
  133   
  134       Rectangle calculateBounds() {
  135           return new Rectangle(location, size);
  136       }
  137   
  138       void paint(MultiRectArea clipArea) {
  139           if (nativeWindow != null) {
  140               Graphics gr = getGraphics(clipArea);
  141               if (gr != null) {
  142                   paint(gr);
  143               }
  144               gr.dispose();
  145           }
  146       }
  147   
  148       abstract void paint(Graphics gr);
  149   
  150       /**
  151        * Mouse events handler
  152        * @param eventId - one of the MouseEvent.MOUSE_* constants
  153        * @param where - mouse location
  154        * @param mouseButton - mouse button that was pressed or released
  155        * @param when - event time
  156        * @param modifiers - input event modifiers
  157        */
  158       abstract void onMouseEvent(int eventId, Point where, int mouseButton,
  159               long when, int modifiers, int wheelRotation);
  160   
  161       /**
  162        * Keyboard events handler
  163        * @param eventId - one of the KeyEvent.KEY_* constants
  164        * @param vKey - the key code
  165        * @param when - event time
  166        * @param modifiers - input event modifiers
  167        */
  168       abstract void onKeyEvent(int eventId, int vKey, long when, int modifiers);
  169   
  170       /**
  171        * Determine if the pop-up box should be hidden if the mouse button was 
  172        * pressed at the <code>start</code> position 
  173        * and released at the <code>end</code> position
  174        * @param start - the location where mouse button was pressed
  175        * @param end - the location where mouse button was released
  176        * @return true if the pop-up box should be hidden; false otherwise 
  177        */
  178       boolean closeOnUngrab(Point start, Point end) {
  179           return true;
  180       }
  181   
  182       /**
  183        * Pass the keyboard event to the topmost active pop-up box
  184        * @param eventId - one of the KeyEvent.KEY_* constants
  185        * @param vKey - the key code
  186        * @param when - event time
  187        * @param modifiers - input event modifiers
  188        */
  189       final void dispatchKeyEvent(int eventId, int vKey, long when, int modifiers) {
  190           if (activeChild != null) {
  191               activeChild.dispatchKeyEvent(eventId, vKey, when, modifiers);
  192           } else {
  193               onKeyEvent(eventId, vKey, when, modifiers);
  194           }
  195       }
  196   
  197       void setDefaultCursor() {
  198           Cursor.getDefaultCursor().getNativeCursor().setCursor(nativeWindow.getId());
  199       }
  200   
  201       private void show(int x, int y, int w, int h) {
  202           visible = true;
  203           addNotify();
  204   
  205           location.setLocation(x, y);
  206           size.setSize(w, h);
  207           nativeWindow.setBounds(x, y, w, h, 0);
  208           nativeWindow.setVisible(true);
  209           setDefaultCursor();
  210       }
  211   
  212       void show(Frame owner) {
  213           this.owner = owner;
  214   
  215           Rectangle bounds = calculateBounds();
  216           show(bounds.x, bounds.y, bounds.width, bounds.height);
  217       }
  218   
  219       void show(Point location, Dimension size, Window owner) {
  220           this.owner = owner;
  221           if (parent != null) {
  222               parent.activeChild = this;
  223           }
  224           show(location.x, location.y, size.width, size.height);
  225   
  226           toolkit.dispatcher.popupDispatcher.activate(this);
  227           if (modalContext != null) {
  228               modalContext.runModalLoop();
  229           }
  230       }
  231   
  232       void hide() {
  233           if (!visible) {
  234               return;
  235           }
  236           visible = false;
  237   
  238           if (activeChild != null) {
  239               activeChild.hide();
  240           }
  241   
  242           if (nativeWindow != null) {
  243               nativeWindow.setVisible(false);
  244           }
  245   
  246           if (parent != null) {
  247               parent.activeChild = null;
  248           }
  249   
  250           if (modalContext != null) {
  251               modalContext.endModalLoop();
  252           }
  253   
  254           toolkit.dispatcher.popupDispatcher.deactivate(this);
  255       }
  256   
  257       Graphics getGraphics(MultiRectArea clip) {
  258           Dimension size = getSize();
  259           Graphics gr = toolkit.getGraphicsFactory().getGraphics2D(nativeWindow, 0, 0, size.width, size.height);
  260           if (gr != null && clip != null) {
  261               gr.setClip(clip);
  262           }
  263           return gr;
  264       }
  265   
  266   }

Home » openjdk-7 » java » awt » [javadoc | source]