KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > nls > SourceContainerDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.core.runtime.IPath;
14
15 import org.eclipse.core.resources.IWorkspaceRoot;
16
17 import org.eclipse.swt.widgets.Shell;
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 JavaDoc[]{IPackageFragmentRoot.class},false);
44         }
45
46         public boolean isSelectedValid(Object JavaDoc element) {
47             try {
48                 if (element instanceof IJavaProject) {
49                     IJavaProject jproject= (IJavaProject) element;
50                     IPath path= jproject.getProject().getFullPath();
51                     return (jproject.findPackageFragmentRoot(path) != null);
52                 } else
53                     if (element instanceof IPackageFragmentRoot) {
54                         return (((IPackageFragmentRoot) element).getKind() == IPackageFragmentRoot.K_SOURCE);
55                     }
56                 return true;
57             } catch (JavaModelException e) {
58                 // fall through returning false
59
}
60             return false;
61         }
62     }
63     
64     /**
65      * A TypedViewerFilter that accepts only PackageFragments and JavaProjects.
66      * PackageFragments are only accepted if they are of the kind K_SOURCE.
67      */

68     private class JavaTypedViewerFilter extends TypedViewerFilter {
69
70         public JavaTypedViewerFilter() {
71             super(new Class JavaDoc[]{IPackageFragmentRoot.class, IJavaProject.class});
72         }
73
74         public boolean select(Viewer viewer, Object JavaDoc parent, Object JavaDoc element) {
75             if (element instanceof IPackageFragmentRoot) {
76                 IPackageFragmentRoot fragmentRoot= (IPackageFragmentRoot)element;
77                 try {
78                     return (fragmentRoot.getKind() == IPackageFragmentRoot.K_SOURCE);
79                 } catch (JavaModelException e) {
80                     return false;
81                 }
82             }
83             return super.select(viewer, parent, element);
84         }
85     }
86
87     private SourceContainerDialog(Shell shell) {
88         super(shell,new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT),new StandardJavaElementContentProvider());
89         setValidator(new PackageAndProjectSelectionValidator());
90         setComparator(new JavaElementComparator());
91         setTitle(NewWizardMessages.NewContainerWizardPage_ChooseSourceContainerDialog_title);
92         setMessage(NewWizardMessages.NewContainerWizardPage_ChooseSourceContainerDialog_description);
93         addFilter(new JavaTypedViewerFilter());
94     }
95
96     public static IPackageFragmentRoot getSourceContainer(Shell shell, IWorkspaceRoot workspaceRoot, IJavaElement initElement) {
97         SourceContainerDialog dialog= new SourceContainerDialog(shell);
98         dialog.setInput(JavaCore.create(workspaceRoot));
99         dialog.setInitialSelection(initElement);
100
101         if (dialog.open() == Window.OK) {
102             Object JavaDoc element= dialog.getFirstResult();
103             if (element instanceof IJavaProject) {
104                 IJavaProject jproject= (IJavaProject) element;
105                 return jproject.getPackageFragmentRoot(jproject.getProject());
106             } else
107                 if (element instanceof IPackageFragmentRoot) {
108                     return (IPackageFragmentRoot) element;
109                 }
110             return null;
111         }
112         return null;
113     }
114 }
115
Popular Tags