KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > ui > text > NewTextSearchActionGroup


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.search.internal.ui.text;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.IAdaptable;
15
16 import org.eclipse.jface.action.IMenuManager;
17 import org.eclipse.jface.action.MenuManager;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.ISelectionProvider;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21
22 import org.eclipse.ui.IActionBars;
23 import org.eclipse.ui.IViewPart;
24 import org.eclipse.ui.IWorkbenchPage;
25 import org.eclipse.ui.IWorkbenchPartSite;
26 import org.eclipse.ui.actions.ActionFactory;
27 import org.eclipse.ui.actions.ActionGroup;
28 import org.eclipse.ui.actions.OpenFileAction;
29 import org.eclipse.ui.actions.OpenWithMenu;
30 import org.eclipse.ui.dialogs.PropertyDialogAction;
31
32 import org.eclipse.search.internal.ui.SearchMessages;
33 import org.eclipse.search.ui.IContextMenuConstants;
34
35 /**
36  * Action group that adds the Text search actions to a context menu and
37  * the global menu bar.
38  *
39  * <p>
40  * This class may be instantiated; it is not intended to be subclassed.
41  * </p>
42  *
43  * @since 2.1
44  */

45 public class NewTextSearchActionGroup extends ActionGroup {
46
47     private ISelectionProvider fSelectionProvider;
48     private IWorkbenchPage fPage;
49     private OpenFileAction fOpenAction;
50     private PropertyDialogAction fOpenPropertiesDialog;
51
52     public NewTextSearchActionGroup(IViewPart part) {
53         Assert.isNotNull(part);
54         IWorkbenchPartSite site= part.getSite();
55         fSelectionProvider= site.getSelectionProvider();
56         fPage= site.getPage();
57         fOpenPropertiesDialog= new PropertyDialogAction(site, fSelectionProvider);
58         fOpenAction= new OpenFileAction(fPage);
59         ISelection selection= fSelectionProvider.getSelection();
60
61         if (selection instanceof IStructuredSelection)
62             fOpenPropertiesDialog.selectionChanged((IStructuredSelection)selection);
63         else
64             fOpenPropertiesDialog.selectionChanged(selection);
65         
66     }
67     
68     public void fillContextMenu(IMenuManager menu) {
69         // view must exist if we create a context menu for it.
70

71         ISelection selection= getContext().getSelection();
72         if (selection instanceof IStructuredSelection) {
73             addOpenWithMenu(menu, (IStructuredSelection) selection);
74             if (fOpenPropertiesDialog != null && fOpenPropertiesDialog.isEnabled() && fOpenPropertiesDialog.isApplicableForSelection((IStructuredSelection) selection))
75                 menu.appendToGroup(IContextMenuConstants.GROUP_PROPERTIES, fOpenPropertiesDialog);
76         }
77             
78     }
79     
80     private void addOpenWithMenu(IMenuManager menu, IStructuredSelection selection) {
81         if (selection == null || selection.size() != 1)
82             return;
83     
84         Object JavaDoc o= selection.getFirstElement();
85     
86         if (!(o instanceof IAdaptable))
87             return;
88     
89         fOpenAction.selectionChanged(selection);
90         menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, fOpenAction);
91     
92         // Create menu
93
IMenuManager submenu= new MenuManager(SearchMessages.OpenWithMenu_label);
94         submenu.add(new OpenWithMenu(fPage, (IAdaptable)o));
95     
96         // Add the submenu.
97
menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, submenu);
98     }
99
100     /* (non-Javadoc)
101      * Method declared in ActionGroup
102      */

103     public void fillActionBars(IActionBars actionBar) {
104         super.fillActionBars(actionBar);
105         setGlobalActionHandlers(actionBar);
106     }
107     
108     private void setGlobalActionHandlers(IActionBars actionBars) {
109         actionBars.setGlobalActionHandler(ActionFactory.PROPERTIES.getId(), fOpenPropertiesDialog);
110     }
111 }
112
Popular Tags