KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.search.internal.ui.text;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.resources.IFile;
17
18 import org.eclipse.swt.widgets.Table;
19 import org.eclipse.swt.widgets.TableItem;
20
21 import org.eclipse.jface.action.IMenuManager;
22 import org.eclipse.jface.action.MenuManager;
23 import org.eclipse.jface.operation.IRunnableWithProgress;
24 import org.eclipse.jface.util.Assert;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.ISelectionProvider;
27 import org.eclipse.jface.viewers.IStructuredSelection;
28 import org.eclipse.jface.viewers.StructuredSelection;
29
30 import org.eclipse.ui.IActionBars;
31 import org.eclipse.ui.IViewPart;
32 import org.eclipse.ui.IWorkbenchPage;
33 import org.eclipse.ui.IWorkbenchPartSite;
34 import org.eclipse.ui.actions.ActionFactory;
35 import org.eclipse.ui.actions.ActionGroup;
36 import org.eclipse.ui.actions.OpenWithMenu;
37 import org.eclipse.ui.dialogs.PropertyDialogAction;
38
39 import org.eclipse.search.ui.IContextMenuConstants;
40 import org.eclipse.search.ui.ISearchResultView;
41 import org.eclipse.search.ui.ISearchResultViewEntry;
42 import org.eclipse.search.ui.SearchUI;
43
44 import org.eclipse.search.internal.ui.SearchManager;
45 import org.eclipse.search.internal.ui.SearchMessages;
46 import org.eclipse.search.internal.ui.SearchPlugin;
47 import org.eclipse.search.internal.ui.SearchResultView;
48 import org.eclipse.search.internal.ui.SearchResultViewer;
49
50 /**
51  * Action group that adds the Text search actions to a context menu and
52  * the global menu bar.
53  *
54  * <p>
55  * This class may be instantiated; it is not intended to be subclassed.
56  * </p>
57  *
58  * @since 2.1
59  */

60 class TextSearchActionGroup extends ActionGroup {
61
62     private ISelectionProvider fSelectionProvider;
63     private IWorkbenchPage fPage;
64     private PropertyDialogAction fOpenPropertiesDialog;
65
66     public TextSearchActionGroup(IViewPart part) {
67         Assert.isNotNull(part);
68         IWorkbenchPartSite site= part.getSite();
69         fSelectionProvider= site.getSelectionProvider();
70         fPage= site.getPage();
71         fOpenPropertiesDialog= new PropertyDialogAction(site.getShell(), fSelectionProvider);
72
73         ISelection selection= fSelectionProvider.getSelection();
74
75         if (selection instanceof IStructuredSelection)
76             fOpenPropertiesDialog.selectionChanged((IStructuredSelection)selection);
77         else
78             fOpenPropertiesDialog.selectionChanged(selection);
79     }
80     
81     public void fillContextMenu(IMenuManager menu) {
82         if (!isTextSearch())
83             return;
84
85         // view must exist if we create a context menu for it.
86
ISearchResultView view= SearchUI.getSearchResultView();
87         IStructuredSelection selection= null;
88         if (getContext().getSelection() instanceof IStructuredSelection)
89             selection= (IStructuredSelection)getContext().getSelection();
90         else
91             selection= StructuredSelection.EMPTY;
92         
93         addOpenWithMenu(menu, selection);
94             
95         ReplaceAction replaceAll= new ReplaceAction(view.getSite(), getSearchResultEntries());
96         if (replaceAll.isEnabled())
97             menu.appendToGroup(IContextMenuConstants.GROUP_REORGANIZE, replaceAll);
98         ReplaceAction replaceSelected= new ReplaceAction(view.getSite(), selection);
99         if (replaceSelected.isEnabled())
100             menu.appendToGroup(IContextMenuConstants.GROUP_REORGANIZE, replaceSelected);
101
102         if (fOpenPropertiesDialog != null && fOpenPropertiesDialog.isEnabled() && selection != null &&fOpenPropertiesDialog.isApplicableForSelection(selection))
103             menu.appendToGroup(IContextMenuConstants.GROUP_PROPERTIES, fOpenPropertiesDialog);
104     }
105     
106     private List JavaDoc getSearchResultEntries() {
107         SearchResultView view= (SearchResultView) SearchPlugin.getSearchResultView();
108         // we can assume we have a view, how else would we be called?
109
SearchResultViewer viewer= view.getViewer();
110         Table table= viewer.getTable();
111         TableItem[] items= table.getItems();
112         List JavaDoc elements = new ArrayList JavaDoc(items.length);
113         for (int i = 0; i < items.length; i++) {
114             elements.add(items[i].getData());
115         }
116         return elements;
117     }
118
119     private boolean isTextSearch() {
120         IRunnableWithProgress operation= SearchManager.getDefault().getCurrentSearch().getOperation();
121         if (operation instanceof TextSearchOperation) {
122             String JavaDoc pattern= ((TextSearchOperation)operation).getPattern();
123             return pattern != null && pattern.length() > 0;
124         }
125         return false;
126     }
127
128     private void addOpenWithMenu(IMenuManager menu, IStructuredSelection selection) {
129         if (selection == null || selection.size() != 1)
130             return;
131
132         Object JavaDoc o= selection.getFirstElement();
133         if (!(o instanceof ISearchResultViewEntry))
134             return;
135
136         Object JavaDoc resource= ((ISearchResultViewEntry)o).getResource();
137         if (!(resource instanceof IFile))
138             return;
139
140         // Create menu
141
IMenuManager submenu= new MenuManager(SearchMessages.getString("OpenWithMenu.label")); //$NON-NLS-1$
142
submenu.add(new OpenWithMenu(fPage, (IFile)resource));
143
144         // Add the submenu.
145
menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, submenu);
146     }
147
148     /* (non-Javadoc)
149      * Method declared in ActionGroup
150      */

151     public void fillActionBars(IActionBars actionBar) {
152         super.fillActionBars(actionBar);
153         setGlobalActionHandlers(actionBar);
154     }
155     
156     private void setGlobalActionHandlers(IActionBars actionBars) {
157         actionBars.setGlobalActionHandler(ActionFactory.PROPERTIES.getId(), fOpenPropertiesDialog);
158     }
159 }
160
Popular Tags