KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > packageview > GotoTypeAction


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.packageview;
12
13 import org.eclipse.swt.widgets.Shell;
14
15 import org.eclipse.jface.action.Action;
16 import org.eclipse.jface.dialogs.IDialogConstants;
17 import org.eclipse.jface.dialogs.MessageDialog;
18 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.jface.viewers.StructuredSelection;
21
22 import org.eclipse.ui.PlatformUI;
23 import org.eclipse.ui.dialogs.SelectionDialog;
24
25 import org.eclipse.jdt.core.ICompilationUnit;
26 import org.eclipse.jdt.core.IJavaElement;
27 import org.eclipse.jdt.core.IType;
28 import org.eclipse.jdt.core.JavaModelException;
29 import org.eclipse.jdt.core.search.SearchEngine;
30
31 import org.eclipse.jdt.internal.corext.util.Messages;
32
33 import org.eclipse.jdt.ui.IJavaElementSearchConstants;
34 import org.eclipse.jdt.ui.JavaUI;
35
36 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
37 import org.eclipse.jdt.internal.ui.JavaPlugin;
38 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
39
40 class GotoTypeAction extends Action {
41     
42     private PackageExplorerPart fPackageExplorer;
43     
44     GotoTypeAction(PackageExplorerPart part) {
45         super();
46         setText(PackagesMessages.GotoType_action_label);
47         setDescription(PackagesMessages.GotoType_action_description);
48         fPackageExplorer= part;
49         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.GOTO_TYPE_ACTION);
50     }
51
52     public void run() {
53         Shell shell= JavaPlugin.getActiveWorkbenchShell();
54         SelectionDialog dialog= null;
55         try {
56             dialog= JavaUI.createTypeDialog(shell, new ProgressMonitorDialog(shell),
57                 SearchEngine.createWorkspaceScope(), IJavaElementSearchConstants.CONSIDER_ALL_TYPES, false);
58         } catch (JavaModelException e) {
59             String JavaDoc title= getDialogTitle();
60             String JavaDoc message= PackagesMessages.GotoType_error_message;
61             ExceptionHandler.handle(e, title, message);
62             return;
63         }
64     
65         dialog.setTitle(getDialogTitle());
66         dialog.setMessage(PackagesMessages.GotoType_dialog_message);
67         if (dialog.open() == IDialogConstants.CANCEL_ID) {
68             return;
69         }
70         
71         Object JavaDoc[] types= dialog.getResult();
72         if (types != null && types.length > 0) {
73             gotoType((IType) types[0]);
74         }
75     }
76     
77     private void gotoType(IType type) {
78         ICompilationUnit cu= (ICompilationUnit) type.getAncestor(IJavaElement.COMPILATION_UNIT);
79         IJavaElement element= null;
80         if (cu != null) {
81             element= cu.getPrimary();
82         }
83         else {
84             element= type.getAncestor(IJavaElement.CLASS_FILE);
85         }
86         if (element != null) {
87             PackageExplorerPart view= PackageExplorerPart.openInActivePerspective();
88             if (view != null) {
89                 view.selectReveal(new StructuredSelection(element));
90                 if (!element.equals(getSelectedElement(view))) {
91                     MessageDialog.openInformation(fPackageExplorer.getSite().getShell(),
92                         getDialogTitle(),
93                         Messages.format(PackagesMessages.PackageExplorer_element_not_present, element.getElementName()));
94                 }
95             }
96         }
97     }
98     
99     private Object JavaDoc getSelectedElement(PackageExplorerPart view) {
100         return ((IStructuredSelection)view.getSite().getSelectionProvider().getSelection()).getFirstElement();
101     }
102     
103     private String JavaDoc getDialogTitle() {
104         return PackagesMessages.GotoType_dialog_title;
105     }
106 }
107
Popular Tags