KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > typehierarchy > FocusOnSelectionAction


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.typehierarchy;
12
13 import org.eclipse.jface.action.Action;
14 import org.eclipse.jface.viewers.ISelection;
15 import org.eclipse.jface.viewers.ISelectionProvider;
16
17 import org.eclipse.ui.PlatformUI;
18
19 import org.eclipse.jdt.core.IType;
20
21 import org.eclipse.jdt.internal.corext.util.Messages;
22
23 import org.eclipse.jdt.ui.ITypeHierarchyViewPart;
24 import org.eclipse.jdt.ui.JavaElementLabels;
25
26 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
27 import org.eclipse.jdt.internal.ui.util.SelectionUtil;
28
29 /**
30  * Refocuses the type hierarchy on the currently selection type.
31  */

32 public class FocusOnSelectionAction extends Action {
33         
34     private ITypeHierarchyViewPart fViewPart;
35     
36     public FocusOnSelectionAction(ITypeHierarchyViewPart part) {
37         super(TypeHierarchyMessages.FocusOnSelectionAction_label);
38         setDescription(TypeHierarchyMessages.FocusOnSelectionAction_description);
39         setToolTipText(TypeHierarchyMessages.FocusOnSelectionAction_tooltip);
40         fViewPart= part;
41         
42         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.FOCUS_ON_SELECTION_ACTION);
43     }
44     
45     private ISelection getSelection() {
46         ISelectionProvider provider= fViewPart.getSite().getSelectionProvider();
47         if (provider != null) {
48             return provider.getSelection();
49         }
50         return null;
51     }
52     
53
54     /*
55      * @see Action#run
56      */

57     public void run() {
58         Object JavaDoc element= SelectionUtil.getSingleElement(getSelection());
59         if (element instanceof IType) {
60             fViewPart.setInputElement((IType)element);
61         }
62     }
63     
64     public boolean canActionBeAdded() {
65         Object JavaDoc element= SelectionUtil.getSingleElement(getSelection());
66         if (element instanceof IType) {
67             IType type= (IType)element;
68             setText(Messages.format(
69                     TypeHierarchyMessages.FocusOnSelectionAction_label,
70                     JavaElementLabels.getTextLabel(type, 0)));
71             return true;
72         }
73         return false;
74     }
75 }
76
Popular Tags