KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > buildpaths > newsourcepage > BuildActionSelectionContext


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage;
12
13 import java.util.Collections JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.IAdaptable;
17
18 import org.eclipse.core.resources.IResource;
19
20 import org.eclipse.jface.viewers.IStructuredSelection;
21
22 import org.eclipse.jdt.core.IJavaElement;
23 import org.eclipse.jdt.core.IJavaProject;
24 import org.eclipse.jdt.core.JavaCore;
25 import org.eclipse.jdt.core.JavaModelException;
26
27 import org.eclipse.jdt.internal.ui.JavaPlugin;
28 import org.eclipse.jdt.internal.ui.packageview.ClassPathContainer;
29
30 public class BuildActionSelectionContext {
31
32     private IStructuredSelection fSelection;
33     private IJavaProject fJavaProject;
34     private List JavaDoc fElements;
35     private int[] fTypes;
36
37     public BuildActionSelectionContext() {
38         fSelection= null;
39         fElements= null;
40         fTypes= null;
41     }
42     
43     public void init(IStructuredSelection selection) {
44         if (selection == null || !selection.equals(fSelection)) {
45             initContextValues(selection);
46         }
47     }
48     
49     private void initContextValues(IStructuredSelection selection) {
50         fSelection= selection;
51
52         fJavaProject= null;
53         fElements= Collections.EMPTY_LIST;
54         fTypes= new int[0];
55         
56         IJavaProject project= getJavaProjectFromSelection(selection);
57         if (project != null && project.exists()) {
58             List JavaDoc elements= selection.toList();
59             try {
60                 int[] types= new int[elements.size()];
61                 for (int i= 0; i < elements.size(); i++) {
62                     Object JavaDoc curr= elements.get(i);
63                     if (i > 0 && !project.equals(getJavaProjectFromSelectedElement(curr))) {
64                         return;
65                     }
66                     
67                     types[i]= DialogPackageExplorerActionGroup.getType(elements.get(i), project);
68                 }
69                 
70                 fJavaProject= project;
71                 fElements= elements;
72                 fTypes= types;
73             } catch (JavaModelException e) {
74                 JavaPlugin.log(e);
75             }
76         }
77
78     }
79
80     
81     /**
82      * Get the Java project from the first element in the provided selection.
83      *
84      * @param selection the selection containing a list of elements
85      * @return the Java project of the first element of the selection, or
86      * <code>null</code> if the selection is empty or no Java project could
87      * be found.
88      */

89     private IJavaProject getJavaProjectFromSelection(IStructuredSelection selection) {
90         if (selection.isEmpty())
91             return null;
92         Object JavaDoc element= selection.getFirstElement();
93         return getJavaProjectFromSelectedElement(element);
94     }
95
96     /**
97      * For a given element, try to get it's Java project
98      *
99      * @param element the element to get the Java project from
100      *
101      * @return the Java project of the provided element, or
102      * <code>null</code> if no Java project could be found.
103      */

104     private IJavaProject getJavaProjectFromSelectedElement(Object JavaDoc element) {
105
106         if (element instanceof IJavaElement)
107             return ((IJavaElement) element).getJavaProject();
108         if (element instanceof ClassPathContainer)
109             return ((ClassPathContainer) element).getJavaProject();
110         if (element instanceof IResource)
111             return JavaCore.create(((IResource) element).getProject());
112         
113         if (element instanceof IAdaptable) {
114             IResource resource= (IResource) ((IAdaptable) element).getAdapter(IResource.class);
115             if (resource != null) {
116                 return JavaCore.create(resource.getProject());
117             }
118         }
119         return null;
120     }
121
122     public List JavaDoc getElements() {
123         return fElements;
124     }
125     
126
127     public IJavaProject getJavaProject() {
128         return fJavaProject;
129     }
130     
131
132     public IStructuredSelection getSelection() {
133         return fSelection;
134     }
135     
136
137     public int[] getTypes() {
138         return fTypes;
139     }
140
141 }
142
Popular Tags