Home | History | Annotate | Download | only in gle2
      1 /*
      2  * Copyright (C) 2009 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 com.android.ide.common.api.ResizePolicy;
     20 import com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditor;
     21 import com.android.ide.eclipse.adt.internal.editors.layout.gre.NodeProxy;
     22 import com.android.ide.eclipse.adt.internal.editors.layout.gre.ViewMetadataRepository;
     23 import com.android.ide.eclipse.adt.internal.editors.layout.uimodel.UiViewElementNode;
     24 
     25 import org.eclipse.swt.graphics.Rectangle;
     26 import org.w3c.dom.Node;
     27 
     28 import java.util.ArrayList;
     29 import java.util.List;
     30 
     31 /**
     32  * Represents one selection in {@link LayoutCanvas}.
     33  */
     34 class SelectionItem {
     35 
     36     /** The associated {@link LayoutCanvas} */
     37     private LayoutCanvas mCanvas;
     38 
     39     /** Current selected view info. Can be null. */
     40     private final CanvasViewInfo mCanvasViewInfo;
     41 
     42     /** Current selection border rectangle. Null when mCanvasViewInfo is null . */
     43     private final Rectangle mRect;
     44 
     45     /** The node proxy for drawing the selection. Null when mCanvasViewInfo is null. */
     46     private final NodeProxy mNodeProxy;
     47 
     48     /** The resize policy for this selection item */
     49     private ResizePolicy mResizePolicy;
     50 
     51     /** The selection handles for this item */
     52     private SelectionHandles mHandles;
     53 
     54     /**
     55      * Creates a new {@link SelectionItem} object.
     56      * @param canvas the associated canvas
     57      * @param canvasViewInfo The view info being selected. Must not be null.
     58      */
     59     public SelectionItem(LayoutCanvas canvas, CanvasViewInfo canvasViewInfo) {
     60         assert canvasViewInfo != null;
     61 
     62         mCanvas = canvas;
     63         mCanvasViewInfo = canvasViewInfo;
     64 
     65         if (canvasViewInfo == null) {
     66             mRect = null;
     67             mNodeProxy = null;
     68         } else {
     69             Rectangle r = canvasViewInfo.getSelectionRect();
     70             mRect = new Rectangle(r.x, r.y, r.width, r.height);
     71             mNodeProxy = mCanvas.getNodeFactory().create(canvasViewInfo);
     72         }
     73     }
     74 
     75     /**
     76      * Returns true when this selection item represents the root, the top level
     77      * layout element in the editor.
     78      *
     79      * @return True if and only if this element is at the root of the hierarchy
     80      */
     81     public boolean isRoot() {
     82         return mCanvasViewInfo.isRoot();
     83     }
     84 
     85     /**
     86      * Returns true if this item represents a widget that should not be manipulated by the
     87      * user.
     88      *
     89      * @return True if this widget should not be manipulated directly by the user
     90      */
     91     public boolean isHidden() {
     92         return mCanvasViewInfo.isHidden();
     93     }
     94 
     95     /**
     96      * Returns the selected view info. Cannot be null.
     97      *
     98      * @return the selected view info. Cannot be null.
     99      */
    100     public CanvasViewInfo getViewInfo() {
    101         return mCanvasViewInfo;
    102     }
    103 
    104     /**
    105      * Returns the selection border rectangle. Cannot be null.
    106      *
    107      * @return the selection border rectangle, never null
    108      */
    109     public Rectangle getRect() {
    110         return mRect;
    111     }
    112 
    113     /** Returns the node associated with this selection (may be null) */
    114     NodeProxy getNode() {
    115         return mNodeProxy;
    116     }
    117 
    118     /** Returns the canvas associated with this selection (never null) */
    119     LayoutCanvas getCanvas() {
    120         return mCanvas;
    121     }
    122 
    123     //----
    124 
    125     /**
    126      * Gets the XML text from the given selection for a text transfer.
    127      * The returned string can be empty but not null.
    128      */
    129     static String getAsText(LayoutCanvas canvas, List<SelectionItem> selection) {
    130         StringBuilder sb = new StringBuilder();
    131 
    132         LayoutEditor layoutEditor = canvas.getLayoutEditor();
    133         for (SelectionItem cs : selection) {
    134             CanvasViewInfo vi = cs.getViewInfo();
    135             UiViewElementNode key = vi.getUiViewNode();
    136             Node node = key.getXmlNode();
    137             String t = layoutEditor.getXmlText(node);
    138             if (t != null) {
    139                 if (sb.length() > 0) {
    140                     sb.append('\n');
    141                 }
    142                 sb.append(t);
    143             }
    144         }
    145 
    146         return sb.toString();
    147     }
    148 
    149     /**
    150      * Returns elements representing the given selection of canvas items.
    151      *
    152      * @param items Items to wrap in elements
    153      * @return An array of wrapper elements. Never null.
    154      */
    155     static SimpleElement[] getAsElements(List<SelectionItem> items) {
    156         ArrayList<SimpleElement> elements = new ArrayList<SimpleElement>();
    157 
    158         for (SelectionItem cs : items) {
    159             CanvasViewInfo vi = cs.getViewInfo();
    160 
    161             SimpleElement e = vi.toSimpleElement();
    162             elements.add(e);
    163         }
    164 
    165         return elements.toArray(new SimpleElement[elements.size()]);
    166     }
    167 
    168     /**
    169      * Returns true if this selection item is a layout
    170      *
    171      * @return true if this selection item is a layout
    172      */
    173     public boolean isLayout() {
    174         UiViewElementNode node = mCanvasViewInfo.getUiViewNode();
    175         if (node != null) {
    176             return node.getDescriptor().hasChildren();
    177         } else {
    178             return false;
    179         }
    180     }
    181 
    182     /**
    183      * Returns the {@link SelectionHandles} for this {@link SelectionItem}. Never null.
    184      *
    185      * @return the {@link SelectionHandles} for this {@link SelectionItem}, never null
    186      */
    187     public SelectionHandles getSelectionHandles() {
    188         if (mHandles == null) {
    189             mHandles = new SelectionHandles(this);
    190         }
    191 
    192         return mHandles;
    193     }
    194 
    195     /**
    196      * Returns the {@link ResizePolicy} for this item
    197      *
    198      * @return the {@link ResizePolicy} for this item, never null
    199      */
    200     public ResizePolicy getResizePolicy() {
    201         if (mResizePolicy == null && mNodeProxy != null) {
    202             mResizePolicy = ViewMetadataRepository.get().getResizePolicy(mNodeProxy.getFqcn());
    203         }
    204 
    205         return mResizePolicy;
    206     }
    207 }
    208