Home | History | Annotate | Download | only in api
      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.common.api;
     18 
     19 import com.android.annotations.NonNull;
     20 import com.android.annotations.Nullable;
     21 
     22 import java.util.List;
     23 
     24 
     25 /**
     26  * An {@link IViewRule} describes the rules that apply to a given Layout or View object
     27  * in the Graphical Layout Editor.
     28  * <p/>
     29  * Rules are implemented by builtin layout helpers, or 3rd party layout rule implementations
     30  * provided with or for a given 3rd party widget.
     31  * <p/>
     32  * A 3rd party layout rule should use the same fully qualified class name as the layout it
     33  * represents, plus "Rule" as a suffix. For example, the layout rule for the
     34  * LinearLayout class is LinearLayoutRule, in the same package.
     35  * <p/>
     36  * Rule instances are stateless. They are created once per View class to handle and are shared
     37  * across platforms or editor instances. As such, rules methods should never cache editor-specific
     38  * arguments that they might receive.
     39  * <p/>
     40  * <b>NOTE: This is not a public or final API; if you rely on this be prepared
     41  * to adjust your code for the next tools release.</b>
     42  * </p>
     43  */
     44 public interface IViewRule {
     45 
     46     /**
     47      * This method is called by the rule engine when the script is first loaded.
     48      * It gives the rule a chance to initialize itself.
     49      *
     50      * @param fqcn The fully qualified class name of the Layout or View that will be managed by
     51      *   this rule. This can be cached as it will never change for the lifetime of this rule
     52      *   instance. This may or may not match the script's filename as it may be the fqcn of a
     53      *   class derived from the one this rule can handle.
     54      * @param engine The engine that is managing the rules. A rule can store a reference to
     55      *   the engine during initialization and then use it later to invoke some of the
     56      *   {@link IClientRulesEngine} methods for example to request user input.
     57      * @return True if this rule can handle the given FQCN. False if the rule can't handle the
     58      *   given FQCN, in which case the rule engine will find another rule matching a parent class.
     59      */
     60     boolean onInitialize(@NonNull String fqcn, @NonNull IClientRulesEngine engine);
     61 
     62     /**
     63      * This method is called by the rules engine just before the script is unloaded.
     64      */
     65     void onDispose();
     66 
     67     /**
     68      * Returns the class name to display when an element is selected in the layout editor.
     69      * <p/>
     70      * If null is returned, the layout editor will automatically shorten the class name using its
     71      * own heuristic, which is to keep the first 2 package components and the class name.
     72      * The class name is the <code>fqcn</code> argument that was given
     73      * to {@link #onInitialize(String,IClientRulesEngine)}.
     74      *
     75      * @return Null for the default behavior or a shortened string.
     76      */
     77     @Nullable
     78     String getDisplayName();
     79 
     80     /**
     81      * Invoked by the Rules Engine to produce a set of actions to customize
     82      * the context menu displayed for this view. The result is not cached and the
     83      * method is invoked every time the context menu is about to be shown.
     84      * <p>
     85      * The order of the menu items is determined by the sort priority set on
     86      * the actions.
     87      * <p/>
     88      * Most rules should consider calling super.{@link #addContextMenuActions(List, INode)}
     89      * as well.
     90      * <p/>
     91      * Menu actions are either toggles or fixed lists with one currently-selected
     92      * item. It is expected that the rule will need to recreate the actions with
     93      * different selections when a menu is going to shown, which is why the result
     94      * is not cached. However rules are encouraged to cache some or all of the result
     95      * to speed up following calls if it makes sense.
     96      *
     97      * @param actions a list of actions to add new context menu actions into. The order
     98      *    of the actions in this list is not important; it will be sorted by
     99      *    {@link RuleAction#getSortPriority()} later.
    100      * @param node the node to add actions for.
    101      */
    102     void addContextMenuActions(@NonNull List<RuleAction> actions, @NonNull INode node);
    103 
    104     /**
    105      * Invoked by the Rules Engine to ask the parent layout for the set of layout actions
    106      * to display in the layout bar. The layout rule should add these into the provided
    107      * list. The order the items are added in does not matter; the
    108      * {@link RuleAction#getSortPriority()} values will be used to sort the actions prior
    109      * to display, which makes it easier for parent rules and deriving rules to interleave
    110      * their respective actions.
    111      *
    112      * @param actions the list of actions to add newly registered actions into
    113      * @param parentNode the parent of the selection, or the selection itself if the root
    114      * @param targets the targeted/selected nodes, if any
    115      */
    116     void addLayoutActions(
    117             @NonNull List<RuleAction> actions,
    118             @NonNull INode parentNode,
    119             @NonNull List<? extends INode> targets);
    120 
    121     // ==== Selection ====
    122 
    123     /**
    124      * Returns a list of strings that will be displayed when a single child is being
    125      * selected in a layout corresponding to this rule. This gives the container a chance
    126      * to describe the child's layout attributes or other relevant information.
    127      * <p/>
    128      * Note that this is called only for single selections.
    129      * <p/>
    130      *
    131      * @param parentNode The parent of the node selected. Never null.
    132      * @param childNode The child node that was selected. Never null.
    133      * @return a list of strings to be displayed, or null or empty to display nothing
    134      */
    135     @Nullable
    136     List<String> getSelectionHint(@NonNull INode parentNode, @NonNull INode childNode);
    137 
    138     /**
    139      * Paints any layout-specific selection feedback for the given parent layout.
    140      *
    141      * @param graphics the graphics context to paint into
    142      * @param parentNode the parent layout node
    143      * @param childNodes the child nodes selected in the parent layout
    144      * @param view An instance of the view to be painted (may be null)
    145      */
    146     void paintSelectionFeedback(
    147             @NonNull IGraphics graphics,
    148             @NonNull INode parentNode,
    149             @NonNull List<? extends INode> childNodes,
    150             @Nullable Object view);
    151 
    152     // ==== Drag'n'drop support ====
    153 
    154     /**
    155      * Called when the d'n'd starts dragging over the target node. If
    156      * interested, returns a DropFeedback passed to onDrop/Move/Leave/Paint. If
    157      * not interested in drop, return null. Followed by a paint.
    158      *
    159      * @param targetNode the {@link INode} for the target layout receiving a
    160      *            drop event
    161      * @param targetView the corresponding View object for the target layout, or
    162      *            null if not known
    163      * @param elements an array of {@link IDragElement} element descriptors for
    164      *            the dragged views
    165      * @return a {@link DropFeedback} object with drop state (which will be
    166      *         supplied to a follow-up {@link #onDropMove} call), or null if the
    167      *         drop should be ignored
    168      */
    169     @Nullable
    170     DropFeedback onDropEnter(@NonNull INode targetNode, @Nullable Object targetView,
    171             @Nullable IDragElement[] elements);
    172 
    173     /**
    174      * Called after onDropEnter. Returns a DropFeedback passed to
    175      * onDrop/Move/Leave/Paint (typically same as input one). Returning null
    176      * will invalidate the drop workflow.
    177      *
    178      * @param targetNode the {@link INode} for the target layout receiving a
    179      *            drop event
    180      * @param elements an array of {@link IDragElement} element descriptors for
    181      *            the dragged views
    182      * @param feedback the {@link DropFeedback} object created by
    183      *            {@link #onDropEnter(INode, Object, IDragElement[])}
    184      * @param where the current mouse drag position
    185      * @return a {@link DropFeedback} (which is usually just the same one passed
    186      *         into this method)
    187      */
    188     @Nullable
    189     DropFeedback onDropMove(
    190             @NonNull INode targetNode,
    191             @NonNull IDragElement[] elements,
    192             @Nullable DropFeedback feedback,
    193             @NonNull Point where);
    194 
    195     /**
    196      * Called when drop leaves the target without actually dropping.
    197      * <p/>
    198      * When switching between views, onDropLeave is called on the old node *after* onDropEnter
    199      * is called after a new node that returned a non-null feedback. The feedback received here
    200      * is the one given by the previous onDropEnter on the same target.
    201      * <p/>
    202      * E.g. call order is:
    203      * <pre>
    204      * - onDropEnter(node1) => feedback1
    205      * <i>...user moves to new view...</i>
    206      * - onDropEnter(node2) => feedback2
    207      * - onDropLeave(node1, feedback1)
    208      * <i>...user leaves canvas...</i>
    209      * - onDropLeave(node2, feedback2)
    210      * </pre>
    211      * @param targetNode the {@link INode} for the target layout receiving a
    212      *            drop event
    213      * @param elements an array of {@link IDragElement} element descriptors for
    214      *            the dragged views
    215      * @param feedback the {@link DropFeedback} object created by
    216      *            {@link #onDropEnter(INode, Object, IDragElement[])}
    217      */
    218     void onDropLeave(
    219             @NonNull INode targetNode,
    220             @NonNull IDragElement[] elements,
    221             @Nullable DropFeedback feedback);
    222 
    223     /**
    224      * Called when drop is released over the target to perform the actual drop.
    225      * <p>
    226      * TODO: Document that this method will be called under an edit lock so you can
    227      * directly manipulate the nodes without wrapping it in an
    228      * {@link INode#editXml(String, INodeHandler)} call.
    229      *
    230      * @param targetNode the {@link INode} for the target layout receiving a
    231      *            drop event
    232      * @param elements an array of {@link IDragElement} element descriptors for
    233      *            the dragged views
    234      * @param feedback the {@link DropFeedback} object created by
    235      *            {@link #onDropEnter(INode, Object, IDragElement[])}
    236      * @param where the mouse drop position
    237      */
    238     void onDropped(
    239             @NonNull INode targetNode,
    240             @NonNull IDragElement[] elements,
    241             @Nullable DropFeedback feedback,
    242             @NonNull Point where);
    243 
    244     /**
    245      * Called when pasting elements in an existing document on the selected target.
    246      *
    247      * @param targetNode The first node selected.
    248      * @param targetView the corresponding View object for the target layout, or
    249      *            null if not known
    250      * @param pastedElements The elements being pasted.
    251      */
    252     void onPaste(@NonNull INode targetNode, @Nullable Object targetView,
    253             @NonNull IDragElement[] pastedElements);
    254 
    255     // ==== XML Creation ====
    256 
    257     /**
    258      * Called when a view for this rule is being created. This allows for the rule to
    259      * customize the newly created object. Note that this method is called not just when a
    260      * view is created from a palette drag, but when views are constructed via a drag-move
    261      * (where views are created in the destination and then deleted from the source), and
    262      * even when views are constructed programmatically from other view rules. The
    263      * {@link InsertType} parameter can be used to distinguish the context for the
    264      * insertion. For example, the <code>DialerFilterRule</code> will insert EditText children
    265      * when a DialerFilter is first created, but not during a copy/paste or a move.
    266      *
    267      * @param node the newly created node (which will always be a View that applies to
    268      *            this {@link IViewRule})
    269      * @param parent the parent of the node (which may not yet contain the newly created
    270      *            node in its child list)
    271      * @param insertType whether this node was created as part of a newly created view, or
    272      *            as a copy, or as a move, etc.
    273      */
    274     void onCreate(@NonNull INode node, @NonNull INode parent, @NonNull InsertType insertType);
    275 
    276     /**
    277      * Called when a child for this view has been created and is being inserted into the
    278      * view parent for which this {@link IViewRule} applies. Allows the parent to perform
    279      * customizations of the object. As with {@link #onCreate}, the {@link InsertType}
    280      * parameter can be used to handle new creation versus moves versus copy/paste
    281      * operations differently.
    282      *
    283      * @param child the newly created node
    284      * @param parent the parent of the newly created node (which may not yet contain the
    285      *            newly created node in its child list)
    286      * @param insertType whether this node was created as part of a newly created view, or
    287      *            as a copy, or as a move, etc.
    288      */
    289     void onChildInserted(@NonNull INode child, @NonNull INode parent,
    290             @NonNull InsertType insertType);
    291 
    292     /**
    293      * Called when one or more children are about to be deleted by the user. Note that
    294      * children deleted programmatically from view rules (via
    295      * {@link INode#removeChild(INode)}) will not notify about deletion.
    296      * <p>
    297      * Note that this method will be called under an edit lock, so rules can directly
    298      * add/remove nodes and attributes as part of the deletion handling (and their
    299      * actions will be part of the same undo-unit.)
    300      *
    301      * @param deleted a nonempty list of children about to be deleted
    302      * @param parent the parent of the deleted children (which still contains the children
    303      *            since this method is called before the deletion is performed)
    304      */
    305     void onRemovingChildren(@NonNull List<INode> deleted, @NonNull INode parent);
    306 
    307     /**
    308      * Called by the IDE on the parent layout when a child widget is being resized. This
    309      * is called once at the beginning of the resizing operation. A horizontal edge,
    310      * or a vertical edge, or both, can be resized simultaneously.
    311      *
    312      * @param child the widget being resized
    313      * @param parent the layout containing the child
    314      * @param horizEdge The horizontal edge being resized, or null
    315      * @param verticalEdge the vertical edge being resized, or null
    316      * @param childView an instance of the resized node view, or null if not known
    317      * @param parentView an instance of the parent layout view object, or null if not known
    318      * @return a {@link DropFeedback} object which performs an update painter callback
    319      *         etc.
    320      */
    321     @Nullable
    322     DropFeedback onResizeBegin(
    323             @NonNull INode child,
    324             @NonNull INode parent,
    325             @Nullable SegmentType horizEdge,
    326             @Nullable SegmentType verticalEdge,
    327             @Nullable Object childView,
    328             @Nullable Object parentView);
    329 
    330     /**
    331      * Called by the IDE on the parent layout when a child widget is being resized. This
    332      * is called repeatedly during the resize as the mouse is dragged to update the drag
    333      * bounds, recompute guidelines, etc. The resize has not yet been "committed" so the
    334      * XML should not be edited yet.
    335      *
    336      * @param feedback the {@link DropFeedback} object created in {@link #onResizeBegin}
    337      * @param child the widget being resized
    338      * @param parent the layout containing the child
    339      * @param newBounds the new bounds the user has chosen to resize the widget to,
    340      *    in absolute coordinates
    341      * @param modifierMask The modifier keys currently pressed by the user, as a bitmask
    342      *    of the constants {@link DropFeedback#MODIFIER1}, {@link DropFeedback#MODIFIER2}
    343      *    and {@link DropFeedback#MODIFIER3}.
    344      */
    345     void onResizeUpdate(
    346             @Nullable DropFeedback feedback,
    347             @NonNull INode child,
    348             @NonNull INode parent,
    349             @NonNull Rect newBounds,
    350             int modifierMask);
    351 
    352     /**
    353      * Called by the IDE on the parent layout when a child widget is being resized. This
    354      * is called once at the end of the resize operation, if it was not canceled.
    355      * This method can call {@link INode#editXml} to update the node to reflect the
    356      * new bounds.
    357      *
    358      * @param feedback the {@link DropFeedback} object created in {@link #onResizeBegin}
    359      * @param child the widget being resized
    360      * @param parent the layout containing the child
    361      * @param newBounds the new bounds the user has chosen to resize the widget to,
    362      *    in absolute coordinates
    363      */
    364     void onResizeEnd(
    365             @Nullable DropFeedback feedback,
    366             @NonNull INode child,
    367             @NonNull INode parent,
    368             @NonNull Rect newBounds);
    369 }
    370