调用eclipse自身OpenType

import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.JavaUIMessages;
import org.eclipse.jdt.internal.ui.dialogs.OpenTypeSelectionDialog;
import org.eclipse.jdt.internal.ui.search.JavaSearchScopeFactory;
import org.eclipse.ui.IWorkingSet;
import org.eclipse.ui.PlatformUI;

public class OpenTypeDialog extends OpenTypeSelectionDialog {

	public OpenTypeDialog(IWorkingSet ws) {
		super(JavaPlugin.getActiveWorkbenchShell(), true, PlatformUI.getWorkbench().getProgressService(), JavaSearchScopeFactory.getInstance().createJavaSearchScope(ws, true), 0);
		setTitle(JavaUIMessages.OpenTypeAction_dialogTitle);
		setMessage(JavaUIMessages.OpenTypeAction_dialogMessage);
		applyFilter();
	}

	public OpenTypeDialog(IWorkingSet ws, String searchText) {
		this(ws);
		setInitialPattern(searchText);
	}
}

然后调用OpenTypeDialog:

IProject[] projects = new IProject[] { project };
				IAdaptable[] elements = new IAdaptable[projects.length];
				for (int i = 0; i < elements.length; i++) {
					elements[i] = projects[i];
				}
				IWorkingSet ws = new WorkingSet("source", "source", elements);
				SelectionDialog dialog = new OpenTypeDialog(ws);
				int result = dialog.open();
				if (result != IDialogConstants.OK_ID)
					return;

				Object[] obj = dialog.getResult();
				if (obj == null || obj.length == 0)
					return;
				String className = null;
				String packageName = null;
				for (Object objs : obj) {
					className = ((SourceType) objs).getElementName();
					PackageFragment packet = (PackageFragment) ((SourceType) objs).getParent().getParent();
					packageName = packet.getElementName();
				}

相关推荐