PageRenderTime 31ms CodeModel.GetById 17ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/eclipse-plugin/plugins/com.google.test.metric.eclipse.ui/src/main/java/com/google/test/metric/eclipse/ui/internal/JavaPackageElementContentProvider.java

http://testability-explorer.googlecode.com/
Java | 94 lines | 60 code | 13 blank | 21 comment | 8 complexity | a29fb7a5838c99ecf6d0a7e514abe8dd MD5 | raw file
 1/*
 2 * Copyright 2009 Google Inc.
 3 *
 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 5 * use this file except in compliance with the License. You may obtain a copy of
 6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.google.test.metric.eclipse.ui.internal;
18
19import com.google.test.metric.eclipse.internal.util.JavaProjectHelper;
20import com.google.test.metric.eclipse.internal.util.Logger;
21
22import org.eclipse.core.runtime.CoreException;
23import org.eclipse.jdt.core.IJavaElement;
24import org.eclipse.jdt.core.IJavaProject;
25import org.eclipse.jdt.core.IPackageFragment;
26import org.eclipse.jdt.core.IPackageFragmentRoot;
27import org.eclipse.jdt.core.IParent;
28import org.eclipse.jdt.core.JavaModelException;
29import org.eclipse.jface.viewers.ITreeContentProvider;
30import org.eclipse.jface.viewers.Viewer;
31
32import java.util.ArrayList;
33import java.util.List;
34
35/**
36 * Provides the list of packages to be displayed in the whitelist selection dialog.
37 * 
38 * @author shyamseshadri@google.com (Shyam Seshadri)
39 */
40public class JavaPackageElementContentProvider implements ITreeContentProvider {
41
42  private JavaProjectHelper javaProjectHelper;
43  private final static Logger logger = new Logger();
44
45  public JavaPackageElementContentProvider() {
46    javaProjectHelper = new JavaProjectHelper();
47  }
48
49  public Object[] getChildren(Object element) {
50    if (element instanceof IJavaProject) {
51      try {
52        return javaProjectHelper.getAllJavaPackageFragmentRoots((IJavaProject) element).toArray();
53      } catch (JavaModelException e) {
54        logger.logException(e);
55      } catch (CoreException e) {
56        logger.logException(e);
57      } 
58    } else if (element instanceof IPackageFragmentRoot || element instanceof IPackageFragment) {
59      try {
60        List<IJavaElement> javaElements = new ArrayList<IJavaElement>();
61        IJavaElement[] children = ((IParent) element).getChildren();
62        for (IJavaElement javaElement : children) {
63          if (javaElement instanceof IPackageFragment && !javaElement.getElementName().equals("")) {
64            javaElements.add(javaElement);
65          }
66        }
67        return javaElements.toArray();
68      } catch (JavaModelException e) {
69        logger.logException(e);
70      }
71    }
72    return null;
73  }
74
75  public Object getParent(Object element) {
76    if (element instanceof IJavaElement) {
77      return ((IJavaElement) element).getParent();
78    }
79    return null;
80  }
81
82  public boolean hasChildren(Object element) {
83    // TODO(shyamseshadri): Figure out a better, faster, more optimum way to do this.
84    return getChildren(element).length > 0;
85  }
86
87  public Object[] getElements(Object inputElement) {
88    return getChildren(inputElement);
89  }
90
91  public void dispose() {}
92
93  public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
94}