]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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 | * Rodrigo Kumpera <kumpera AT gmail.com> - bug 95232 | |
11 | * | |
12 | *******************************************************************************/ | |
13 | package org.eclipse.jdt.internal.ui.workingsets; | |
14 | ||
15 | import org.eclipse.swt.custom.BusyIndicator; | |
16 | import org.eclipse.swt.widgets.Composite; | |
17 | ||
18 | import org.eclipse.core.runtime.IAdaptable; | |
19 | ||
20 | import org.eclipse.core.resources.IProject; | |
21 | import org.eclipse.core.resources.IResource; | |
22 | import org.eclipse.core.resources.ResourcesPlugin; | |
23 | ||
24 | import org.eclipse.jface.viewers.ISelection; | |
25 | import org.eclipse.jface.viewers.IStructuredSelection; | |
26 | import org.eclipse.jface.viewers.StructuredSelection; | |
27 | import org.eclipse.jface.viewers.TableViewer; | |
28 | import org.eclipse.jface.viewers.TreeViewer; | |
29 | ||
30 | import org.eclipse.ui.IWorkbenchPage; | |
31 | import org.eclipse.ui.IWorkbenchPart; | |
32 | import org.eclipse.ui.IWorkbenchWindow; | |
33 | import org.eclipse.ui.IWorkingSet; | |
34 | import org.eclipse.ui.PlatformUI; | |
35 | ||
36 | import org.eclipse.jdt.core.IJavaElement; | |
37 | import org.eclipse.jdt.core.IJavaProject; | |
38 | import org.eclipse.jdt.core.JavaCore; | |
39 | import org.eclipse.jdt.core.JavaModelException; | |
40 | ||
41 | import org.eclipse.jdt.ui.JavaElementComparator; | |
42 | import org.eclipse.jdt.ui.JavaElementLabels; | |
43 | ||
44 | import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; | |
45 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
46 | import org.eclipse.jdt.internal.ui.JavaPluginImages; | |
47 | import org.eclipse.jdt.internal.ui.actions.SelectionConverter; | |
48 | import org.eclipse.jdt.internal.ui.filters.EmptyInnerPackageFilter; | |
49 | import org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart; | |
50 | import org.eclipse.jdt.internal.ui.packageview.PackageFragmentRootContainer; | |
51 | import org.eclipse.jdt.internal.ui.viewsupport.AppearanceAwareLabelProvider; | |
52 | import org.eclipse.jdt.internal.ui.viewsupport.DecoratingJavaLabelProvider; | |
53 | import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider; | |
54 | import org.eclipse.jdt.internal.ui.wizards.NewJavaWorkingSetWizard; | |
55 | ||
56 | /** | |
57 | * The Java working set page allows the user to create | |
58 | * and edit a Java working set. | |
59 | * <p> | |
60 | * Workspace elements can be added/removed from a tree into | |
61 | * a list. | |
62 | * </p> | |
63 | * | |
64 | * @since 2.0 | |
65 | */ | |
66 | public class JavaWorkingSetPage extends AbstractWorkingSetWizardPage { | |
67 | ||
68 | final private static String PAGE_TITLE= WorkingSetMessages.JavaWorkingSetPage_title; | |
69 | final private static String PAGE_NAME= "javaWorkingSetPage"; //$NON-NLS-1$ | |
70 | ||
71 | private IStructuredSelection fInitialSelection; | |
72 | ||
73 | /** | |
74 | * Default constructor. | |
75 | */ | |
76 | public JavaWorkingSetPage() { | |
77 | super(PAGE_NAME, PAGE_TITLE, JavaPluginImages.DESC_WIZBAN_JAVA_WORKINGSET); | |
78 | setDescription(WorkingSetMessages.JavaWorkingSetPage_workingSet_description); | |
79 | IWorkbenchWindow activeWorkbenchWindow= JavaPlugin.getActiveWorkbenchWindow(); | |
80 | if (activeWorkbenchWindow != null) { | |
81 | ISelection selection= activeWorkbenchWindow.getSelectionService().getSelection(); | |
82 | if (selection instanceof IStructuredSelection) | |
83 | fInitialSelection= (IStructuredSelection)selection; | |
84 | } | |
85 | } | |
86 | ||
87 | public void setInitialSelection(IStructuredSelection selection) { | |
88 | fInitialSelection= selection; | |
89 | } | |
90 | ||
91 | @Override | |
92 | protected String getPageId() { | |
93 | return "org.eclipse.jdt.ui.JavaWorkingSetPage"; //$NON-NLS-1$ | |
94 | } | |
95 | ||
96 | /** | |
97 | * {@inheritDoc} | |
98 | */ | |
99 | @Override | |
100 | public void createControl(Composite parent) { | |
101 | super.createControl(parent); | |
102 | ||
103 | // Set help for the page | |
104 | PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IJavaHelpContextIds.JAVA_WORKING_SET_PAGE); | |
105 | } | |
106 | ||
107 | /** | |
108 | * {@inheritDoc} | |
109 | */ | |
110 | @Override | |
111 | protected void configureTree(TreeViewer tree) { | |
112 | tree.setContentProvider(new JavaWorkingSetPageContentProvider()); | |
113 | ||
114 | AppearanceAwareLabelProvider javaElementLabelProvider= | |
115 | new AppearanceAwareLabelProvider( | |
116 | AppearanceAwareLabelProvider.DEFAULT_TEXTFLAGS | JavaElementLabels.P_COMPRESSED, | |
117 | AppearanceAwareLabelProvider.DEFAULT_IMAGEFLAGS | JavaElementImageProvider.SMALL_ICONS | |
118 | ); | |
119 | ||
120 | tree.setLabelProvider(new DecoratingJavaLabelProvider(javaElementLabelProvider)); | |
121 | tree.setComparator(new JavaElementComparator()); | |
122 | tree.addFilter(new EmptyInnerPackageFilter()); | |
123 | ||
124 | tree.setInput(JavaCore.create(ResourcesPlugin.getWorkspace().getRoot())); | |
125 | ||
126 | if (getSelection() == null) | |
127 | return; | |
128 | ||
129 | Object[] selection= getInitialTreeSelection(); | |
130 | if (selection.length > 0) { | |
131 | try { | |
132 | tree.getTree().setRedraw(false); | |
133 | ||
134 | for (int i= 0; i < selection.length; i++) { | |
135 | tree.expandToLevel(selection[i], 0); | |
136 | } | |
137 | tree.setSelection(new StructuredSelection(selection)); | |
138 | } finally { | |
139 | tree.getTree().setRedraw(true); | |
140 | } | |
141 | } | |
142 | } | |
143 | ||
144 | /** | |
145 | * {@inheritDoc} | |
146 | */ | |
147 | @Override | |
148 | protected void configureTable(TableViewer table) { | |
149 | AppearanceAwareLabelProvider javaElementLabelProvider= new AppearanceAwareLabelProvider( | |
150 | AppearanceAwareLabelProvider.DEFAULT_TEXTFLAGS | JavaElementLabels.P_COMPRESSED | |
151 | | JavaElementLabels.ROOT_POST_QUALIFIED | JavaElementLabels.P_POST_QUALIFIED, | |
152 | AppearanceAwareLabelProvider.DEFAULT_IMAGEFLAGS | JavaElementImageProvider.SMALL_ICONS | |
153 | ); | |
154 | ||
155 | table.setLabelProvider(new DecoratingJavaLabelProvider(javaElementLabelProvider)); | |
156 | table.setComparator(new JavaElementComparator()); | |
157 | } | |
158 | ||
159 | /** | |
160 | * {@inheritDoc} | |
161 | */ | |
162 | @Override | |
163 | protected Object[] getInitialWorkingSetElements(IWorkingSet workingSet) { | |
164 | Object[] elements; | |
165 | if (workingSet == null) { | |
166 | if (fInitialSelection == null) | |
167 | return new IAdaptable[0]; | |
168 | ||
169 | elements= fInitialSelection.toArray(); | |
170 | } else { | |
171 | elements= workingSet.getElements(); | |
172 | } | |
173 | ||
174 | // Use closed project for elements in closed project and remove PackageFragmentRootContainer elements | |
175 | int deletedElements= 0; | |
176 | for (int i= 0; i < elements.length; i++) { | |
177 | Object element= elements[i]; | |
178 | if (element instanceof IResource) { | |
179 | IProject project= ((IResource)element).getProject(); | |
180 | if (!project.isAccessible()) | |
181 | elements[i]= project; | |
182 | } else if (element instanceof PackageFragmentRootContainer) { | |
183 | for (int j= i; j < elements.length - 1; j++) | |
184 | elements[j]= elements[j + 1]; | |
185 | deletedElements++; | |
186 | } else if (element instanceof IJavaElement) { | |
187 | IJavaProject jProject= ((IJavaElement)element).getJavaProject(); | |
188 | if (jProject != null && !jProject.getProject().isAccessible()) | |
189 | elements[i]= jProject.getProject(); | |
190 | } else if (!(element instanceof IAdaptable) || ((IAdaptable)element).getAdapter(IProject.class) == null) { | |
191 | for (int j= i; j < elements.length - 1; j++) | |
192 | elements[j]= elements[j + 1]; | |
193 | deletedElements++; | |
194 | } | |
195 | } | |
196 | ||
197 | if (deletedElements == 0) | |
198 | return elements; | |
199 | ||
200 | IAdaptable[] result= new IAdaptable[elements.length - deletedElements]; | |
201 | System.arraycopy(elements, 0, result, 0, result.length); | |
202 | return result; | |
203 | } | |
204 | ||
205 | private Object[] getInitialTreeSelection() { | |
206 | final Object[][] result= new Object[1][]; | |
207 | BusyIndicator.showWhile(getShell().getDisplay(), new Runnable() { | |
208 | public void run() { | |
209 | IStructuredSelection selection= fInitialSelection; | |
210 | if (selection == null) { | |
211 | ||
212 | IWorkbenchPage page= JavaPlugin.getActivePage(); | |
213 | if (page == null) | |
214 | return; | |
215 | ||
216 | IWorkbenchPart part= page.getActivePart(); | |
217 | if (part == null) | |
218 | return; | |
219 | ||
220 | try { | |
221 | selection= SelectionConverter.getStructuredSelection(part); | |
222 | } catch (JavaModelException e) { | |
223 | return; | |
224 | } | |
225 | } | |
226 | ||
227 | Object[] elements= selection.toArray(); | |
228 | for (int i= 0; i < elements.length; i++) { | |
229 | if (elements[i] instanceof IResource) { | |
230 | IJavaElement je= (IJavaElement)((IResource)elements[i]).getAdapter(IJavaElement.class); | |
231 | if (je != null && je.exists() && je.getJavaProject().isOnClasspath((IResource)elements[i])) | |
232 | elements[i]= je; | |
233 | } | |
234 | } | |
235 | result[0]= elements; | |
236 | } | |
237 | }); | |
238 | ||
239 | if (result[0] == null) | |
240 | return new Object[0]; | |
241 | ||
242 | return result[0]; | |
243 | } | |
244 | ||
245 | public void generated_2166086965901552550(NewJavaWorkingSetWizard newjavaworkingsetwizard) { | |
246 | finish(); | |
247 | ||
248 | IWorkingSet ws= getSelection(); | |
249 | if (ws != null) { | |
250 | PlatformUI.getWorkbench().getWorkingSetManager().addWorkingSet(ws); | |
251 | ||
252 | PackageExplorerPart explorer= newjavaworkingsetwizard.getActivePackageExplorer(); | |
253 | if (explorer != null) { | |
254 | if (explorer.getRootMode() == PackageExplorerPart.WORKING_SETS_AS_ROOTS) { | |
255 | explorer.selectReveal(new StructuredSelection(ws)); | |
256 | } | |
257 | } | |
258 | } | |
259 | } | |
260 | } |