Home | History | Annotate | Download | only in gle2
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.eclipse.org/org/documents/epl-v10.php
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.ide.eclipse.adt.internal.editors.layout.gle2;
     18 
     19 import org.eclipse.swt.graphics.Color;
     20 import org.eclipse.swt.graphics.Device;
     21 import org.eclipse.swt.graphics.GC;
     22 import org.eclipse.swt.graphics.Rectangle;
     23 
     24 /**
     25  * The {@link EmptyViewsOverlay} paints bounding rectangles for any of the empty and
     26  * invisible container views in the scene.
     27  */
     28 public class EmptyViewsOverlay extends Overlay {
     29     /** The {@link ViewHierarchy} containing visible view information. */
     30     private final ViewHierarchy mViewHierarchy;
     31 
     32     /** Border color to paint the bounding boxes with. */
     33     private Color mBorderColor;
     34 
     35     /** Vertical scaling & scrollbar information. */
     36     private CanvasTransform mVScale;
     37 
     38     /** Horizontal scaling & scrollbar information. */
     39     private CanvasTransform mHScale;
     40 
     41     /**
     42      * Constructs a new {@link EmptyViewsOverlay} linked to the given view hierarchy.
     43      *
     44      * @param viewHierarchy The {@link ViewHierarchy} to render.
     45      * @param hScale The {@link CanvasTransform} to use to transfer horizontal layout
     46      *            coordinates to screen coordinates.
     47      * @param vScale The {@link CanvasTransform} to use to transfer vertical layout coordinates
     48      *            to screen coordinates.
     49      */
     50     public EmptyViewsOverlay(
     51             ViewHierarchy viewHierarchy,
     52             CanvasTransform hScale,
     53             CanvasTransform vScale) {
     54         super();
     55         mViewHierarchy = viewHierarchy;
     56         mHScale = hScale;
     57         mVScale = vScale;
     58     }
     59 
     60     @Override
     61     public void create(Device device) {
     62         mBorderColor = new Color(device, SwtDrawingStyle.EMPTY.getStrokeColor());
     63     }
     64 
     65     @Override
     66     public void dispose() {
     67         if (mBorderColor != null) {
     68             mBorderColor.dispose();
     69             mBorderColor = null;
     70         }
     71     }
     72 
     73     @Override
     74     public void paint(GC gc) {
     75         gc.setForeground(mBorderColor);
     76         gc.setLineDash(null);
     77         gc.setLineStyle(SwtDrawingStyle.EMPTY.getLineStyle());
     78         int oldAlpha = gc.getAlpha();
     79         gc.setAlpha(SwtDrawingStyle.EMPTY.getStrokeAlpha());
     80         gc.setLineWidth(SwtDrawingStyle.EMPTY.getLineWidth());
     81 
     82         for (CanvasViewInfo info : mViewHierarchy.getInvisibleViews()) {
     83             Rectangle r = info.getAbsRect();
     84 
     85             int x = mHScale.translate(r.x);
     86             int y = mVScale.translate(r.y);
     87             int w = mHScale.scale(r.width);
     88             int h = mVScale.scale(r.height);
     89 
     90             // +1: See explanation in equivalent code in {@link OutlineOverlay#paint}
     91             gc.drawRectangle(x, y, w + 1, h + 1);
     92         }
     93 
     94         gc.setAlpha(oldAlpha);
     95     }
     96 }
     97