Home | History | Annotate | Download | only in tree
      1 /*
      2  * Copyright (C) 2007 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.ui.tree;
     18 
     19 import com.android.ide.eclipse.adt.internal.editors.descriptors.ElementDescriptor;
     20 import com.android.ide.eclipse.adt.internal.editors.uimodel.UiElementNode;
     21 
     22 import org.eclipse.jface.viewers.ITreeContentProvider;
     23 import org.eclipse.jface.viewers.Viewer;
     24 
     25 import java.util.ArrayList;
     26 
     27 /**
     28  * UiModelTreeContentProvider is a trivial implementation of {@link ITreeContentProvider}
     29  * where elements are expected to be instances of {@link UiElementNode}.
     30  */
     31 class UiModelTreeContentProvider implements ITreeContentProvider {
     32 
     33     /** The descriptor of the elements to be displayed as root in this tree view. All elements
     34      *  of the same type in the root will be displayed. */
     35     private ElementDescriptor[] mDescriptorFilters;
     36     /** The uiRootNode of the model. */
     37     private final UiElementNode mUiRootNode;
     38 
     39     public UiModelTreeContentProvider(UiElementNode uiRootNode,
     40             ElementDescriptor[] descriptorFilters) {
     41         mUiRootNode = uiRootNode;
     42         mDescriptorFilters = descriptorFilters;
     43     }
     44 
     45     /* (non-java doc)
     46      * Returns all the UI node children of the given element or null if not the right kind
     47      * of object. */
     48     @Override
     49     public Object[] getChildren(Object parentElement) {
     50         if (parentElement instanceof UiElementNode) {
     51             UiElementNode node = (UiElementNode) parentElement;
     52             return node.getUiChildren().toArray();
     53         }
     54         return null;
     55     }
     56 
     57     /* (non-java doc)
     58      * Returns the parent of a given UI node or null if it's a root node or it's not the
     59      * right kind of node. */
     60     @Override
     61     public Object getParent(Object element) {
     62         if (element instanceof UiElementNode) {
     63             UiElementNode node = (UiElementNode) element;
     64             return node.getUiParent();
     65         }
     66         return null;
     67     }
     68 
     69     /* (non-java doc)
     70      * Returns true if the UI node has any UI children nodes. */
     71     @Override
     72     public boolean hasChildren(Object element) {
     73         if (element instanceof UiElementNode) {
     74             UiElementNode node = (UiElementNode) element;
     75             return node.getUiChildren().size() > 0;
     76         }
     77         return false;
     78     }
     79 
     80     /* (non-java doc)
     81      * Get root elements for the tree. These are all the UI nodes that
     82      * match the filter descriptor in the current root node.
     83      * <p/>
     84      * Although not documented, it seems this method should not return null.
     85      * At worse, it should return new Object[0].
     86      * <p/>
     87      * inputElement is not currently used. The root node and the filter are given
     88      * by the enclosing class.
     89      */
     90     @Override
     91     public Object[] getElements(Object inputElement) {
     92         ArrayList<UiElementNode> roots = new ArrayList<UiElementNode>();
     93         if (mUiRootNode != null) {
     94             for (UiElementNode ui_node : mUiRootNode.getUiChildren()) {
     95                 if (mDescriptorFilters == null || mDescriptorFilters.length == 0) {
     96                     roots.add(ui_node);
     97                 } else {
     98                     for (ElementDescriptor filter : mDescriptorFilters) {
     99                         if (ui_node.getDescriptor() == filter) {
    100                             roots.add(ui_node);
    101                         }
    102                     }
    103                 }
    104             }
    105         }
    106 
    107         return roots.toArray();
    108     }
    109 
    110     @Override
    111     public void dispose() {
    112         // pass
    113     }
    114 
    115     @Override
    116     public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
    117         // pass
    118     }
    119 }
    120 
    121