]>
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 | *******************************************************************************/ | |
11 | package org.eclipse.jdt.internal.ui.refactoring.nls; | |
12 | ||
13 | import org.eclipse.swt.widgets.Shell; | |
14 | ||
15 | import org.eclipse.core.runtime.IPath; | |
16 | ||
17 | import org.eclipse.core.resources.IWorkspaceRoot; | |
18 | ||
19 | import org.eclipse.jface.viewers.Viewer; | |
20 | import org.eclipse.jface.window.Window; | |
21 | ||
22 | import org.eclipse.ui.dialogs.ElementTreeSelectionDialog; | |
23 | ||
24 | import org.eclipse.jdt.core.IJavaElement; | |
25 | import org.eclipse.jdt.core.IJavaProject; | |
26 | import org.eclipse.jdt.core.IPackageFragmentRoot; | |
27 | import org.eclipse.jdt.core.JavaCore; | |
28 | import org.eclipse.jdt.core.JavaModelException; | |
29 | ||
30 | import org.eclipse.jdt.ui.JavaElementComparator; | |
31 | import org.eclipse.jdt.ui.JavaElementLabelProvider; | |
32 | import org.eclipse.jdt.ui.StandardJavaElementContentProvider; | |
33 | ||
34 | import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; | |
35 | import org.eclipse.jdt.internal.ui.wizards.TypedElementSelectionValidator; | |
36 | import org.eclipse.jdt.internal.ui.wizards.TypedViewerFilter; | |
37 | ||
38 | public class SourceContainerDialog extends ElementTreeSelectionDialog { | |
39 | ||
40 | private class PackageAndProjectSelectionValidator extends TypedElementSelectionValidator { | |
41 | ||
42 | public PackageAndProjectSelectionValidator() { | |
43 | super(new Class[]{IPackageFragmentRoot.class},false); | |
44 | } | |
45 | ||
46 | @Override | |
47 | public boolean isSelectedValid(Object element) { | |
48 | try { | |
49 | if (element instanceof IJavaProject) { | |
50 | IJavaProject jproject= (IJavaProject) element; | |
51 | IPath path= jproject.getProject().getFullPath(); | |
52 | return (jproject.findPackageFragmentRoot(path) != null); | |
53 | } else | |
54 | if (element instanceof IPackageFragmentRoot) { | |
55 | return (((IPackageFragmentRoot) element).getKind() == IPackageFragmentRoot.K_SOURCE); | |
56 | } | |
57 | return true; | |
58 | } catch (JavaModelException e) { | |
59 | // fall through returning false | |
60 | } | |
61 | return false; | |
62 | } | |
63 | } | |
64 | ||
65 | /** | |
66 | * A TypedViewerFilter that accepts only PackageFragments and JavaProjects. | |
67 | * PackageFragments are only accepted if they are of the kind K_SOURCE. | |
68 | */ | |
69 | private class JavaTypedViewerFilter extends TypedViewerFilter { | |
70 | ||
71 | public JavaTypedViewerFilter() { | |
72 | super(new Class[]{IPackageFragmentRoot.class, IJavaProject.class}); | |
73 | } | |
74 | ||
75 | @Override | |
76 | public boolean select(Viewer viewer, Object parent, Object element) { | |
77 | if (element instanceof IPackageFragmentRoot) { | |
78 | IPackageFragmentRoot fragmentRoot= (IPackageFragmentRoot)element; | |
79 | try { | |
80 | return (fragmentRoot.getKind() == IPackageFragmentRoot.K_SOURCE); | |
81 | } catch (JavaModelException e) { | |
82 | return false; | |
83 | } | |
84 | } | |
85 | return super.select(viewer, parent, element); | |
86 | } | |
87 | } | |
88 | ||
89 | private SourceContainerDialog(Shell shell) { | |
90 | super(shell,new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT),new StandardJavaElementContentProvider()); | |
91 | setValidator(new PackageAndProjectSelectionValidator()); | |
92 | setComparator(new JavaElementComparator()); | |
93 | setTitle(NewWizardMessages.NewContainerWizardPage_ChooseSourceContainerDialog_title); | |
94 | setMessage(NewWizardMessages.NewContainerWizardPage_ChooseSourceContainerDialog_description); | |
95 | addFilter(new JavaTypedViewerFilter()); | |
96 | } | |
97 | ||
98 | public static IPackageFragmentRoot getSourceContainer(Shell shell, IWorkspaceRoot workspaceRoot, IJavaElement initElement) { | |
99 | SourceContainerDialog dialog= new SourceContainerDialog(shell); | |
100 | dialog.setInput(JavaCore.create(workspaceRoot)); | |
101 | dialog.setInitialSelection(initElement); | |
102 | ||
103 | if (dialog.open() == Window.OK) { | |
104 | Object element= dialog.getFirstResult(); | |
105 | if (element instanceof IJavaProject) { | |
106 | IJavaProject jproject= (IJavaProject) element; | |
107 | return jproject.getPackageFragmentRoot(jproject.getProject()); | |
108 | } else | |
109 | if (element instanceof IPackageFragmentRoot) { | |
110 | return (IPackageFragmentRoot) element; | |
111 | } | |
112 | return null; | |
113 | } | |
114 | return null; | |
115 | } | |
116 | } |