Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8da2e51406b04a44a05d8aa6dab4c595bc1c63bd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*******************************************************************************
 * Copyright (c) 2002 International Business Machines Corp. and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v0.5 
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 ******************************************************************************/
package org.eclipse.search.internal.ui.text;

import java.util.List;

import org.eclipse.core.resources.IFile;

import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.util.Assert;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;

import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.ui.actions.ActionGroup;
import org.eclipse.ui.actions.OpenWithMenu;
import org.eclipse.ui.dialogs.PropertyDialogAction;

import org.eclipse.search.ui.IContextMenuConstants;
import org.eclipse.search.ui.ISearchResultView;
import org.eclipse.search.ui.ISearchResultViewEntry;
import org.eclipse.search.ui.SearchUI;

import org.eclipse.search.internal.ui.SearchManager;
import org.eclipse.search.internal.ui.SearchMessages;

/**
 * Action group that adds the Text search actions to a context menu and
 * the global menu bar.
 * 
 * <p>
 * This class may be instantiated; it is not intended to be subclassed.
 * </p>
 * 
 * @since 2.1
 */
class TextSearchActionGroup extends ActionGroup {

	private ISelectionProvider fSelectionProvider;		
	private IWorkbenchPage fPage;
	private PropertyDialogAction fOpenPropertiesDialog;

	public TextSearchActionGroup(IViewPart part) {
		Assert.isNotNull(part);
		IWorkbenchPartSite site= part.getSite();
		fSelectionProvider= site.getSelectionProvider();
		fPage= site.getPage();
		fOpenPropertiesDialog= new PropertyDialogAction(site.getShell(), fSelectionProvider);

		ISelection selection= fSelectionProvider.getSelection();

		if (selection instanceof IStructuredSelection)
			fOpenPropertiesDialog.selectionChanged((IStructuredSelection)selection);
		else
			fOpenPropertiesDialog.selectionChanged(selection);
	}
	
	public void fillContextMenu(IMenuManager menu) {
		if (!isTextSearch())
			return;

		// view must exist if we create a context menu for it.
		ISearchResultView view= SearchUI.getSearchResultView();
		IStructuredSelection selection= null;
		if (getContext().getSelection() instanceof IStructuredSelection)
			selection= (IStructuredSelection)getContext().getSelection();
		else
			selection= StructuredSelection.EMPTY;
		
		addOpenWithMenu(menu, selection);
			
		ReplaceAction replaceAll= new ReplaceAction(view.getSite(), (List)getContext().getInput());
		if (replaceAll.isEnabled())
			menu.appendToGroup(IContextMenuConstants.GROUP_REORGANIZE, replaceAll);
		ReplaceAction replaceSelected= new ReplaceAction(view.getSite(), selection);
		if (replaceSelected.isEnabled())
			menu.appendToGroup(IContextMenuConstants.GROUP_REORGANIZE, replaceSelected);

		if (fOpenPropertiesDialog != null && fOpenPropertiesDialog.isEnabled() && selection != null &&fOpenPropertiesDialog.isApplicableForSelection(selection))
			menu.appendToGroup(IContextMenuConstants.GROUP_PROPERTIES, fOpenPropertiesDialog);
	}
	
	private boolean isTextSearch() {
		IRunnableWithProgress operation= SearchManager.getDefault().getCurrentSearch().getOperation();
		if (operation instanceof TextSearchOperation) {
			String pattern= ((TextSearchOperation)operation).getPattern();
			return pattern != null && pattern.length() > 0;
		}
		return false;
	}

	private void addOpenWithMenu(IMenuManager menu, IStructuredSelection selection) {
		if (selection == null || selection.size() != 1)
			return;

		Object o= selection.getFirstElement();
		if (!(o instanceof ISearchResultViewEntry))
			return;

		Object resource= ((ISearchResultViewEntry)o).getResource();
		if (!(resource instanceof IFile))
			return; 

		// Create menu
		IMenuManager submenu= new MenuManager(SearchMessages.getString("OpenWithMenu.label")); //$NON-NLS-1$
		submenu.add(new OpenWithMenu(fPage, (IFile)resource));

		// Add the submenu.
		menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, submenu);
	}

	/* (non-Javadoc)
	 * Method declared in ActionGroup
	 */
	public void fillActionBars(IActionBars actionBar) {
		super.fillActionBars(actionBar);
		setGlobalActionHandlers(actionBar);
	}
	
	private void setGlobalActionHandlers(IActionBars actionBars) {
		actionBars.setGlobalActionHandler(IWorkbenchActionConstants.PROPERTIES, fOpenPropertiesDialog);		
	}
}	

Back to the top