Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8656e1714f4df975551a428b75440151558d4a92 (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
/*******************************************************************************
 * Copyright (c) 2004, 2011 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corp. - Rational Software - initial implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.search.actions;

import java.util.List;

import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IWorkbenchSite;
import org.eclipse.ui.actions.ActionGroup;
import org.eclipse.ui.part.Page;

public class SelectionSearchGroup extends ActionGroup {

	private CEditor fEditor;

	private DeclarationsSearchGroup fDeclarationsSearchGroup;
	private ReferencesSearchGroup fRefSearchGroup;

	public SelectionSearchGroup(CEditor editor) {
		Assert.isNotNull(editor);
		fEditor = editor;

		fDeclarationsSearchGroup = new DeclarationsSearchGroup(fEditor);
		fRefSearchGroup = new ReferencesSearchGroup(fEditor);
	}

	/**
	 * @param page
	 */
	public SelectionSearchGroup(Page page) {
		this(page.getSite());
	}

	/**
	 * @param site
	 */
	public SelectionSearchGroup(IWorkbenchSite site) {
		fDeclarationsSearchGroup = new DeclarationsSearchGroup(site);
		fRefSearchGroup = new ReferencesSearchGroup(site);
	}

	/*
	 * Method declared on ActionGroup.
	 */
	@Override
	public void fillContextMenu(IMenuManager menu) {
		super.fillContextMenu(menu);

		fDeclarationsSearchGroup.fillContextMenu(menu);
		fRefSearchGroup.fillContextMenu(menu);
	}

	@Override
	public void fillActionBars(IActionBars actionBars) {
		if (fEditor == null) {
			fDeclarationsSearchGroup.fillActionBars(actionBars);
			fRefSearchGroup.fillActionBars(actionBars);
		}
	}

	public static boolean canActionBeAdded(ISelection selection) {
		if (selection instanceof ITextSelection) {
			return (((ITextSelection) selection).getLength() > 0);
		}
		return getElement(selection) != null;
	}

	private static ICElement getElement(ISelection sel) {
		if (!sel.isEmpty() && sel instanceof IStructuredSelection) {
			List<?> list = ((IStructuredSelection) sel).toList();
			if (list.size() == 1) {
				Object element = list.get(0);
				if (element instanceof ICElement) {
					return (ICElement) element;
				}
			}
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.actions.ActionGroup#dispose()
	 */
	@Override
	public void dispose() {
		if (fDeclarationsSearchGroup != null) {
			fDeclarationsSearchGroup.dispose();
			fDeclarationsSearchGroup = null;
		}

		if (fRefSearchGroup != null) {
			fRefSearchGroup.dispose();
			fRefSearchGroup = null;
		}

		fEditor = null;

		super.dispose();
	}
}

Back to the top