Skip to main content
summaryrefslogtreecommitdiffstats
blob: e1d3c5dd0dd1f076bf3cbe56d0b22d4d3f755957 (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
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.search.internal.ui.text;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
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.search.internal.ui.SearchMessages;
import org.eclipse.search.ui.IContextMenuConstants;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ActionGroup;
import org.eclipse.ui.actions.OpenFileAction;
import org.eclipse.ui.actions.OpenWithMenu;
import org.eclipse.ui.dialogs.PropertyDialogAction;

/**
 * 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 NewTextSearchActionGroup extends ActionGroup {

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

	public NewTextSearchActionGroup(IViewPart part) {
		Assert.isNotNull(part);
		IWorkbenchPartSite site= part.getSite();
		fSelectionProvider= site.getSelectionProvider();
		fPage= site.getPage();
		fOpenPropertiesDialog= new PropertyDialogAction(site.getShell(), fSelectionProvider);
		fOpenAction= new OpenFileAction(fPage);
		ISelection selection= fSelectionProvider.getSelection();

		if (selection instanceof IStructuredSelection)
			fOpenPropertiesDialog.selectionChanged((IStructuredSelection)selection);
		else
			fOpenPropertiesDialog.selectionChanged(selection);
		
	}
	
	public void fillContextMenu(IMenuManager menu) {
		// view must exist if we create a context menu for it.
		
		ISelection selection= getContext().getSelection();
		if (selection instanceof IStructuredSelection) {
			addOpenWithMenu(menu, (IStructuredSelection) selection);
			if (fOpenPropertiesDialog != null && fOpenPropertiesDialog.isEnabled() && selection != null &&fOpenPropertiesDialog.isApplicableForSelection((IStructuredSelection) selection))
				menu.appendToGroup(IContextMenuConstants.GROUP_PROPERTIES, fOpenPropertiesDialog);
		}
			
	}
	
	private void addOpenWithMenu(IMenuManager menu, IStructuredSelection selection) {
		if (selection == null || selection.size() != 1)
			return;
	
		Object o= selection.getFirstElement();
	
		if (!(o instanceof IAdaptable))
			return; 
	
		fOpenAction.selectionChanged(selection);
		menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, fOpenAction);
	
		// Create menu
		IMenuManager submenu= new MenuManager(SearchMessages.getString("OpenWithMenu.label")); //$NON-NLS-1$
		submenu.add(new OpenWithMenu(fPage, (IAdaptable)o));
	
		// 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(ActionFactory.PROPERTIES.getId(), fOpenPropertiesDialog);		
	}
}	

Back to the top