Home | History | Annotate | Download | only in ui
      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.ui;
     18 
     19 import com.android.ide.eclipse.adt.internal.resources.IResourceRepository;
     20 import com.android.ide.eclipse.adt.internal.resources.ResourceItem;
     21 import com.android.ide.eclipse.adt.internal.resources.ResourceType;
     22 import com.android.ide.eclipse.adt.internal.resources.manager.ConfigurableResourceItem;
     23 import com.android.ide.eclipse.adt.internal.resources.manager.ResourceFile;
     24 
     25 import org.eclipse.jface.viewers.ITreeContentProvider;
     26 import org.eclipse.jface.viewers.Viewer;
     27 
     28 /**
     29  * Content provider for the Resource Explorer TreeView.
     30  * Each level of the tree is represented by a different class.
     31  * <ul>
     32  * <li>{@link ResourceType}. This represents the list of existing Resource Type present
     33  * in the resources. This can be matched to the subclasses inside the class <code>R</code>
     34  * </li>
     35  * <ul>
     36  * <li>{@link ResourceItem}. This represents one resource (which can existing in various alternate
     37  * versions). This is similar to the resource Ids defined as <code>R.sometype.id</code>.
     38  * </li>
     39  * <ul>
     40  * <li>{@link ResourceFile}. (optional) This represents a particular version of the
     41  * {@link ResourceItem}. It is displayed as a list of resource qualifier.
     42  * </li>
     43  * </ul>
     44  * </ul>
     45  * </ul>
     46  *
     47  * @see ResourceLabelProvider
     48  */
     49 public class ResourceContentProvider implements ITreeContentProvider {
     50 
     51     /**
     52      * The current ProjectResources being displayed.
     53      */
     54     private IResourceRepository mResources;
     55 
     56     private boolean mFullLevels;
     57 
     58    /**
     59      * Constructs a new content providers for resource display.
     60      * @param fullLevels if <code>true</code> the content provider will suppport all 3 levels. If
     61      * <code>false</code>, only two levels are provided.
     62      */
     63     public ResourceContentProvider(boolean fullLevels) {
     64         mFullLevels = fullLevels;
     65     }
     66 
     67     public Object[] getChildren(Object parentElement) {
     68         if (parentElement instanceof ResourceType) {
     69             return mResources.getResources((ResourceType)parentElement);
     70         } else if (mFullLevels && parentElement instanceof ConfigurableResourceItem) {
     71             return ((ConfigurableResourceItem)parentElement).getSourceFileArray();
     72         }
     73         return null;
     74     }
     75 
     76     public Object getParent(Object element) {
     77         // pass
     78         return null;
     79     }
     80 
     81     public boolean hasChildren(Object element) {
     82         if (element instanceof ResourceType) {
     83             return mResources.hasResources((ResourceType)element);
     84         } else if (mFullLevels && element instanceof ConfigurableResourceItem) {
     85             return ((ConfigurableResourceItem)element).hasAlternates();
     86         }
     87         return false;
     88     }
     89 
     90     public Object[] getElements(Object inputElement) {
     91         if (inputElement instanceof IResourceRepository) {
     92             if ((IResourceRepository)inputElement == mResources) {
     93                 // get the top level resources.
     94                 return mResources.getAvailableResourceTypes();
     95             }
     96         }
     97 
     98         return new Object[0];
     99     }
    100 
    101     public void dispose() {
    102         // pass
    103     }
    104 
    105     public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
    106         if (newInput instanceof IResourceRepository) {
    107              mResources = (IResourceRepository)newInput;
    108         }
    109     }
    110 }
    111