KickJava   Java API By Example, From Geeks To Geeks.

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


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.MessageDialog;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.jface.viewers.StructuredSelection;
19
20 import org.eclipse.ui.PlatformUI;
21 import org.eclipse.ui.dialogs.SelectionDialog;
22 import org.eclipse.ui.progress.IProgressService;
23
24 import org.eclipse.jdt.core.IPackageFragment;
25 import org.eclipse.jdt.core.JavaModelException;
26 import org.eclipse.jdt.core.search.IJavaSearchScope;
27 import org.eclipse.jdt.core.search.SearchEngine;
28
29 import org.eclipse.jdt.internal.corext.util.Messages;
30
31 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
32 import org.eclipse.jdt.internal.ui.JavaPlugin;
33 import org.eclipse.jdt.internal.ui.dialogs.PackageSelectionDialog;
34
35 class GotoPackageAction extends Action {
36     
37     private PackageExplorerPart fPackageExplorer;
38     
39     GotoPackageAction(PackageExplorerPart part) {
40         super(PackagesMessages.GotoPackage_action_label);
41         setDescription(PackagesMessages.GotoPackage_action_description);
42         fPackageExplorer= part;
43         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.GOTO_PACKAGE_ACTION);
44     }
45  
46     public void run() {
47         try {
48             Shell shell= JavaPlugin.getActiveWorkbenchShell();
49             SelectionDialog dialog= createAllPackagesDialog(shell);
50             dialog.setTitle(getDialogTitle());
51             dialog.setMessage(PackagesMessages.GotoPackage_dialog_message);
52             dialog.open();
53             Object JavaDoc[] res= dialog.getResult();
54             if (res != null && res.length == 1)
55                 gotoPackage((IPackageFragment)res[0]);
56         } catch (JavaModelException e) {
57         }
58     }
59     
60     private SelectionDialog createAllPackagesDialog(Shell shell) throws JavaModelException{
61         IProgressService progressService= PlatformUI.getWorkbench().getProgressService();
62         IJavaSearchScope scope= SearchEngine.createWorkspaceScope();
63         int flag= PackageSelectionDialog.F_HIDE_EMPTY_INNER;
64         PackageSelectionDialog dialog= new PackageSelectionDialog(shell, progressService, flag, scope);
65         dialog.setFilter(""); //$NON-NLS-1$
66
dialog.setIgnoreCase(false);
67         dialog.setMultipleSelection(false);
68         return dialog;
69     }
70                 
71     private void gotoPackage(IPackageFragment p) {
72         fPackageExplorer.selectReveal(new StructuredSelection(p));
73         if (!p.equals(getSelectedElement())) {
74             MessageDialog.openInformation(fPackageExplorer.getSite().getShell(),
75                 getDialogTitle(),
76                 Messages.format(PackagesMessages.PackageExplorer_element_not_present, p.getElementName()));
77         }
78     }
79     
80     private Object JavaDoc getSelectedElement() {
81         return ((IStructuredSelection)fPackageExplorer.getSite().getSelectionProvider().getSelection()).getFirstElement();
82     }
83     
84     private String JavaDoc getDialogTitle() {
85         return PackagesMessages.GotoPackage_dialog_title;
86     }
87     
88 }
89
Popular Tags