KickJava   Java API By Example, From Geeks To Geeks.

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


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.refactoring.nls;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.swt.widgets.Display;
18
19 import org.eclipse.jface.window.Window;
20
21 import org.eclipse.ui.dialogs.ElementListSelectionDialog;
22
23 import org.eclipse.jdt.core.ICompilationUnit;
24 import org.eclipse.jdt.core.IJavaElement;
25 import org.eclipse.jdt.core.IJavaProject;
26 import org.eclipse.jdt.core.IPackageFragment;
27 import org.eclipse.jdt.core.IPackageFragmentRoot;
28 import org.eclipse.jdt.core.JavaModelException;
29
30 import org.eclipse.jdt.ui.JavaElementLabelProvider;
31
32 import org.eclipse.jdt.internal.ui.JavaPlugin;
33 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
34 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
35
36
37 public class PackageBrowseAdapter implements IStringButtonAdapter {
38     
39     PackageSelectionDialogButtonField fReceiver;
40     private ICompilationUnit fCu;
41     
42     public PackageBrowseAdapter(ICompilationUnit unit) {
43         fCu = unit;
44     }
45     
46     public void setReceiver(PackageSelectionDialogButtonField receiver) {
47        fReceiver = receiver;
48     }
49     
50     public void changeControlPressed(DialogField field) {
51         ElementListSelectionDialog dialog= new ElementListSelectionDialog(
52             Display.getCurrent().getActiveShell(), new JavaElementLabelProvider());
53         dialog.setIgnoreCase(false);
54         dialog.setTitle(NLSUIMessages.PackageBrowseAdapter_package_selection);
55         dialog.setMessage(NLSUIMessages.PackageBrowseAdapter_choose_package);
56         dialog.setElements(createPackageListInput(fCu, null));
57         if (dialog.open() == Window.OK) {
58             IPackageFragment selectedPackage= (IPackageFragment)dialog.getFirstResult();
59             if (selectedPackage != null) {
60                 fReceiver.setPackage(selectedPackage);
61             }
62         }
63     }
64     public static Object JavaDoc[] createPackageListInput(ICompilationUnit cu, String JavaDoc elementNameMatch){
65         try{
66             IJavaProject project= cu.getJavaProject();
67             IPackageFragmentRoot[] roots= project.getPackageFragmentRoots();
68             List JavaDoc result= new ArrayList JavaDoc();
69             HashMap JavaDoc entered =new HashMap JavaDoc();
70             for (int i= 0; i < roots.length; i++){
71                 if (canAddPackageRoot(roots[i])){
72                     getValidPackages(roots[i], result, entered, elementNameMatch);
73                 }
74             }
75             return result.toArray();
76         } catch (JavaModelException e){
77             JavaPlugin.log(e);
78             return new Object JavaDoc[0];
79         }
80     }
81
82     static boolean canAddPackageRoot(IPackageFragmentRoot root) throws JavaModelException{
83         if (! root.exists())
84             return false;
85         if (root.isArchive())
86             return false;
87         if (root.isExternal())
88             return false;
89         if (root.isReadOnly())
90             return false;
91         if (! root.isStructureKnown())
92             return false;
93         return true;
94     }
95     
96     static void getValidPackages(IPackageFragmentRoot root, List JavaDoc result, HashMap JavaDoc entered, String JavaDoc elementNameMatch) throws JavaModelException {
97         IJavaElement[] children= null;
98         try {
99             children= root.getChildren();
100         } catch (JavaModelException e){
101             return;
102         }
103         for (int i= 0; i < children.length; i++){
104             if (children[i] instanceof IPackageFragment) {
105                 IPackageFragment packageFragment = (IPackageFragment)children[i];
106                 String JavaDoc packageName = packageFragment.getElementName();
107                 
108                 if ((entered != null) && (entered.containsKey(packageName)) == true) {
109                     continue;
110                 }
111                 
112                 if (canAddPackage(packageFragment)) {
113                     if ((elementNameMatch == null) || (elementNameMatch.equals(packageName))) {
114                         result.add(packageFragment);
115                         if (entered != null) {
116                             entered.put(packageName, null);
117                         }
118                     }
119                 }
120             }
121         }
122     }
123
124     static boolean canAddPackage(IPackageFragment p) throws JavaModelException{
125         if (! p.exists())
126             return false;
127         if (p.isReadOnly())
128             return false;
129         if (! p.isStructureKnown())
130             return false;
131         return true;
132     }
133
134     public static List JavaDoc searchAllPackages(IJavaProject project, String JavaDoc matcher) {
135         try{
136             IPackageFragmentRoot[] roots= project.getPackageFragmentRoots();
137             List JavaDoc result= new ArrayList JavaDoc();
138             for (int i= 0; i < roots.length; i++){
139                 if (canAddPackageRoot(roots[i])){
140                     getValidPackages(roots[i], result, null, matcher);
141                 }
142             }
143             return result;
144         } catch (JavaModelException e) {
145             JavaPlugin.log(e);
146             return new ArrayList JavaDoc(0);
147         }
148     }
149 }
150
Popular Tags