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 Dmitry A. Durnev
   19    */
   20   package java.awt;
   21   
   22   import org.apache.harmony.awt.gl.MultiRectArea;
   23   import org.apache.harmony.awt.wtk.NativeWindow;
   24   
   25   
   26   /**
   27    * A lightweight component specific behaviour.
   28    *
   29    */
   30   
   31   class LWBehavior implements ComponentBehavior {
   32   
   33       /*
   34        * only lightweights store their position/size, others get it from native
   35        * resources each time
   36        */
   37   
   38       private int x, y, w, h;
   39   
   40       private final Component component;
   41   
   42       private boolean visible;
   43   
   44       private boolean displayable;
   45   
   46       LWBehavior(Component comp) {
   47           component = comp;
   48           /* lightweight components are initially visible */
   49           visible = component.isVisible();
   50       }
   51   
   52       public void addNotify() {
   53           displayable = true;
   54           if (visible) {
   55               component.repaint();
   56           }
   57       }
   58   
   59       public void setVisible(boolean b) {
   60           visible = b;
   61           if (visible) {
   62               component.repaint();
   63           } else if (isParentShowing()) {
   64               component.parent.repaint(x, y, w, h);
   65           }
   66       }
   67   
   68       public Graphics getGraphics(int translationX, int translationY, int width,
   69               int height) {
   70           Graphics g = null;
   71           Container parent = component.getParent();
   72   
   73           if (parent != null) {
   74               g = parent.behaviour.getGraphics(translationX + component.x,
   75                       translationY + component.y, width, height);
   76           }
   77   
   78           return g;
   79       }
   80   
   81       public int getX() {
   82           return x;
   83       }
   84   
   85       public int getY() {
   86           return y;
   87       }
   88   
   89       public void setBounds(int x, int y, int width, int height) {
   90           Rectangle oldBounds = new Rectangle(this.x, this.y, this.w, this.h);
   91           Rectangle newBounds = new Rectangle(x, y, width, height);
   92   
   93           if (oldBounds.equals(newBounds)) {
   94               return;
   95           }
   96           w = width;
   97           h = height;
   98           this.x = x;
   99           this.y = y;
  100   
  101           boolean grow = newBounds.contains(oldBounds);
  102           boolean shrink = oldBounds.contains(newBounds);
  103           if (isParentShowing() && !grow) {
  104               Component parent = component.parent;
  105               if (!shrink) {
  106                   parent.repaintRegion = new MultiRectArea(oldBounds);
  107                   parent.repaintRegion.substract(newBounds);
  108               }
  109               parent.repaint(oldBounds.x, oldBounds.y, oldBounds.width, oldBounds.height);
  110           }
  111           if (visible && displayable && !shrink) {
  112               component.repaint();
  113           }
  114       }
  115   
  116       public boolean isLightweight() {
  117           return true;
  118       }
  119   
  120       public boolean isOpaque() {
  121           return false;
  122       }
  123   
  124       public void setBounds(int x, int y, int w, int h, int bMask) {
  125           setBounds(component.x, component.y, component.w, component.h);
  126           onMove(x, y);
  127       }
  128   
  129       public boolean isDisplayable() {
  130           return displayable;
  131       }
  132   
  133       public void setEnabled(boolean value) {
  134       }
  135   
  136       public void removeNotify() {
  137           displayable = false;
  138           if (visible && isParentShowing()) {
  139               component.parent.repaint(x, y, w, h);
  140           }
  141       }
  142   
  143       public NativeWindow getNativeWindow() {
  144           return component.getHWAncestor().getNativeWindow();
  145       }
  146   
  147       public void onMove(int x, int y) {
  148           if (component instanceof Container) {
  149               Container cont = (Container) component;
  150               for (int i = 0; i < cont.getComponentCount(); i++) {
  151                   cont.getComponent(i).behaviour.onMove(x, y);
  152               }
  153           }
  154   
  155       }
  156   
  157       public void setZOrder(int newIndex, int oldIndex) {
  158           // do nothing
  159       }
  160   
  161       public boolean setFocus(boolean focus, Component opposite) {
  162   
  163           // request [native] focus from the nearest heavyweight ancestor
  164           // don't post event, so call only cb
  165           Component hw = component.getHWAncestor();
  166           if (hw != null) {
  167               hw.behaviour.setFocus(focus, opposite);
  168           }
  169   
  170           return true;
  171       }
  172   
  173       private boolean isParentShowing() {
  174           return component.parent != null && component.parent.visible
  175               && component.parent.behaviour.isDisplayable();
  176       }
  177   }

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