001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.refactoring.nls;
011:
012: import java.util.ArrayList;
013: import java.util.HashMap;
014: import java.util.List;
015:
016: import org.eclipse.swt.widgets.Display;
017:
018: import org.eclipse.jface.window.Window;
019:
020: import org.eclipse.ui.dialogs.ElementListSelectionDialog;
021:
022: import org.eclipse.jdt.core.ICompilationUnit;
023: import org.eclipse.jdt.core.IJavaElement;
024: import org.eclipse.jdt.core.IJavaProject;
025: import org.eclipse.jdt.core.IPackageFragment;
026: import org.eclipse.jdt.core.IPackageFragmentRoot;
027: import org.eclipse.jdt.core.JavaModelException;
028:
029: import org.eclipse.jdt.ui.JavaElementLabelProvider;
030:
031: import org.eclipse.jdt.internal.ui.JavaPlugin;
032: import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
033: import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter;
034:
035: public class PackageBrowseAdapter implements IStringButtonAdapter {
036:
037: PackageSelectionDialogButtonField fReceiver;
038: private ICompilationUnit fCu;
039:
040: public PackageBrowseAdapter(ICompilationUnit unit) {
041: fCu = unit;
042: }
043:
044: public void setReceiver(PackageSelectionDialogButtonField receiver) {
045: fReceiver = receiver;
046: }
047:
048: public void changeControlPressed(DialogField field) {
049: ElementListSelectionDialog dialog = new ElementListSelectionDialog(
050: Display.getCurrent().getActiveShell(),
051: new JavaElementLabelProvider());
052: dialog.setIgnoreCase(false);
053: dialog
054: .setTitle(NLSUIMessages.PackageBrowseAdapter_package_selection);
055: dialog
056: .setMessage(NLSUIMessages.PackageBrowseAdapter_choose_package);
057: dialog.setElements(createPackageListInput(fCu, null));
058: if (dialog.open() == Window.OK) {
059: IPackageFragment selectedPackage = (IPackageFragment) dialog
060: .getFirstResult();
061: if (selectedPackage != null) {
062: fReceiver.setPackage(selectedPackage);
063: }
064: }
065: }
066:
067: public static Object[] createPackageListInput(ICompilationUnit cu,
068: String elementNameMatch) {
069: try {
070: IJavaProject project = cu.getJavaProject();
071: IPackageFragmentRoot[] roots = project
072: .getPackageFragmentRoots();
073: List result = new ArrayList();
074: HashMap entered = new HashMap();
075: for (int i = 0; i < roots.length; i++) {
076: if (canAddPackageRoot(roots[i])) {
077: getValidPackages(roots[i], result, entered,
078: elementNameMatch);
079: }
080: }
081: return result.toArray();
082: } catch (JavaModelException e) {
083: JavaPlugin.log(e);
084: return new Object[0];
085: }
086: }
087:
088: static boolean canAddPackageRoot(IPackageFragmentRoot root)
089: throws JavaModelException {
090: if (!root.exists())
091: return false;
092: if (root.isArchive())
093: return false;
094: if (root.isExternal())
095: return false;
096: if (root.isReadOnly())
097: return false;
098: if (!root.isStructureKnown())
099: return false;
100: return true;
101: }
102:
103: static void getValidPackages(IPackageFragmentRoot root,
104: List result, HashMap entered, String elementNameMatch)
105: throws JavaModelException {
106: IJavaElement[] children = null;
107: try {
108: children = root.getChildren();
109: } catch (JavaModelException e) {
110: return;
111: }
112: for (int i = 0; i < children.length; i++) {
113: if (children[i] instanceof IPackageFragment) {
114: IPackageFragment packageFragment = (IPackageFragment) children[i];
115: String packageName = packageFragment.getElementName();
116:
117: if ((entered != null)
118: && (entered.containsKey(packageName)) == true) {
119: continue;
120: }
121:
122: if (canAddPackage(packageFragment)) {
123: if ((elementNameMatch == null)
124: || (elementNameMatch.equals(packageName))) {
125: result.add(packageFragment);
126: if (entered != null) {
127: entered.put(packageName, null);
128: }
129: }
130: }
131: }
132: }
133: }
134:
135: static boolean canAddPackage(IPackageFragment p)
136: throws JavaModelException {
137: if (!p.exists())
138: return false;
139: if (p.isReadOnly())
140: return false;
141: if (!p.isStructureKnown())
142: return false;
143: return true;
144: }
145:
146: public static List searchAllPackages(IJavaProject project,
147: String matcher) {
148: try {
149: IPackageFragmentRoot[] roots = project
150: .getPackageFragmentRoots();
151: List result = new ArrayList();
152: for (int i = 0; i < roots.length; i++) {
153: if (canAddPackageRoot(roots[i])) {
154: getValidPackages(roots[i], result, null, matcher);
155: }
156: }
157: return result;
158: } catch (JavaModelException e) {
159: JavaPlugin.log(e);
160: return new ArrayList(0);
161: }
162: }
163: }
|