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