Home | History | Annotate | Download | only in repository
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
      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.sdkuilib.internal.repository;
     18 
     19 import com.android.sdklib.internal.repository.IDescription;
     20 import com.android.sdklib.internal.repository.Package;
     21 import com.android.sdklib.internal.repository.RepoSource;
     22 import com.android.sdkuilib.internal.repository.icons.ImageFactory;
     23 
     24 import org.eclipse.jface.viewers.IContentProvider;
     25 import org.eclipse.jface.viewers.ILabelProvider;
     26 import org.eclipse.jface.viewers.IStructuredContentProvider;
     27 import org.eclipse.jface.viewers.LabelProvider;
     28 import org.eclipse.jface.viewers.Viewer;
     29 import org.eclipse.swt.graphics.Image;
     30 
     31 /**
     32  * Table adapters to use the local SDK list.
     33  */
     34 class LocalSdkAdapter  {
     35 
     36     private final UpdaterData mUpdaterData;
     37 
     38     public LocalSdkAdapter(UpdaterData updaterData) {
     39         mUpdaterData = updaterData;
     40     }
     41 
     42     public ILabelProvider getLabelProvider() {
     43         return new ViewerLabelProvider();
     44     }
     45 
     46 
     47     public IContentProvider getContentProvider() {
     48         return new TableContentProvider();
     49     }
     50 
     51     // ------------
     52 
     53     public class ViewerLabelProvider extends LabelProvider {
     54         /** Returns an image appropriate for this element. */
     55         @Override
     56         public Image getImage(Object element) {
     57             ImageFactory imgFactory = mUpdaterData.getImageFactory();
     58 
     59             if (imgFactory != null) {
     60                 return imgFactory.getImageForObject(element);
     61             }
     62 
     63             return super.getImage(element);
     64         }
     65 
     66         /** Returns the toString of the element. */
     67         @Override
     68         public String getText(Object element) {
     69             if (element instanceof IDescription) {
     70                 return ((IDescription) element).getShortDescription();
     71             }
     72             return super.getText(element);
     73         }
     74     }
     75 
     76     // ------------
     77 
     78     private class TableContentProvider implements IStructuredContentProvider {
     79 
     80         // Called when the viewer is disposed
     81         public void dispose() {
     82             // pass
     83         }
     84 
     85         // Called when the input is set or changed on the provider
     86         public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
     87             assert newInput == LocalSdkAdapter.this;
     88         }
     89 
     90         /**
     91          * Called to collect the root elements for the given input.
     92          * The input here is a {@link LocalSdkAdapter} object, this returns an array
     93          * of {@link RepoSource}.
     94          */
     95         public Object[] getElements(Object inputElement) {
     96             if (inputElement == LocalSdkAdapter.this) {
     97                 Package[] packages = mUpdaterData.getInstalledPackage();
     98 
     99                 if (packages != null) {
    100                     return packages;
    101                 }
    102             }
    103 
    104             return new Object[0];
    105         }
    106     }
    107 
    108 }
    109