1 /*******************************************************************************
2  *  Copyright (c) 2005, 2015 IBM Corporation and others.
3  *
4  *  This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License 2.0
6  *  which accompanies this distribution, and is available at
7  *  https://www.eclipse.org/legal/epl-2.0/
8  *
9  *  SPDX-License-Identifier: EPL-2.0
10  *
11  *  Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.pde.internal.ui.search;
15 
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.jface.action.IMenuManager;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.pde.core.IBaseModel;
21 import org.eclipse.pde.core.plugin.*;
22 import org.eclipse.pde.internal.core.PDECore;
23 import org.eclipse.pde.internal.core.plugin.ImportObject;
24 import org.eclipse.pde.internal.ui.editor.actions.OpenSchemaAction;
25 import org.eclipse.pde.internal.ui.search.dependencies.DependencyExtentAction;
26 import org.eclipse.ui.actions.ActionContext;
27 import org.eclipse.ui.actions.ActionGroup;
28 
29 public class PluginSearchActionGroup extends ActionGroup {
30 
31 	private IBaseModel fModel;
32 
setBaseModel(IBaseModel model)33 	public void setBaseModel(IBaseModel model) {
34 		fModel = model;
35 	}
36 
37 	@Override
fillContextMenu(IMenuManager menu)38 	public void fillContextMenu(IMenuManager menu) {
39 		ActionContext context = getContext();
40 		ISelection selection = context.getSelection();
41 		if (selection instanceof IStructuredSelection) {
42 			IStructuredSelection sSelection = (IStructuredSelection) selection;
43 			if (sSelection.size() == 1) {
44 				Object object = sSelection.getFirstElement();
45 				addShowDescriptionAction(object, menu);
46 				addOpenSchemaAction(object, menu);
47 				addFindDeclarationsAction(object, menu);
48 				addFindReferencesAction(object, menu);
49 				addDependencyExtentAction(object, menu);
50 			}
51 		}
52 	}
53 
54 	/**
55 	 * @param object
56 	 * @param menu
57 	 */
addOpenSchemaAction(Object object, IMenuManager menu)58 	private void addOpenSchemaAction(Object object, IMenuManager menu) {
59 		if (object instanceof IPluginExtension) {
60 			// From PDEOutlinePage
61 			OpenSchemaAction action = new OpenSchemaAction();
62 			action.setInput((IPluginExtension) object);
63 			action.setEnabled(true);
64 			menu.add(action);
65 		} else if (object instanceof IPluginExtensionPoint) {
66 			// From PluginSearchResultPage
67 			// From ExtensionPointsSection
68 			OpenSchemaAction action = new OpenSchemaAction();
69 			IPluginExtensionPoint point = (IPluginExtensionPoint) object;
70 			String pointID = point.getFullId();
71 			// Ensure the extension point ID is fully qualified
72 			if (pointID.indexOf('.') == -1) {
73 				// Point ID is not fully qualified
74 				action.setInput(fullyQualifyPointID(pointID));
75 			} else {
76 				action.setInput(pointID);
77 			}
78 			action.setEnabled(true);
79 			menu.add(action);
80 		}
81 	}
82 
addFindDeclarationsAction(Object object, IMenuManager menu)83 	private void addFindDeclarationsAction(Object object, IMenuManager menu) {
84 		if (object instanceof ImportObject)
85 			object = ((ImportObject) object).getImport();
86 
87 		if (object instanceof IPluginBase || object instanceof IPluginExtension || object instanceof IPluginImport) {
88 			menu.add(new FindDeclarationsAction(object));
89 		}
90 	}
91 
addShowDescriptionAction(Object object, IMenuManager menu)92 	private void addShowDescriptionAction(Object object, IMenuManager menu) {
93 		if (object instanceof IPluginExtensionPoint) {
94 			IPluginExtensionPoint extPoint = (IPluginExtensionPoint) object;
95 			String pointID = extPoint.getFullId();
96 			if (pointID.indexOf('.') == -1) {
97 				// Point ID is not fully qualified
98 				pointID = fullyQualifyPointID(pointID);
99 			}
100 			menu.add(new ShowDescriptionAction(extPoint, pointID));
101 		} else if (object instanceof IPluginExtension) {
102 			String point = ((IPluginExtension) object).getPoint();
103 			IPluginExtensionPoint extPoint = PDECore.getDefault().getExtensionsRegistry().findExtensionPoint(point);
104 			if (extPoint != null)
105 				menu.add(new ShowDescriptionAction(extPoint));
106 		}
107 	}
108 
fullyQualifyPointID(String pointID)109 	private String fullyQualifyPointID(String pointID) {
110 		if (fModel instanceof IPluginModelBase) {
111 			String basePointID = ((IPluginModelBase) fModel).getPluginBase().getId();
112 			pointID = basePointID + '.' + pointID;
113 		}
114 		return pointID;
115 	}
116 
addFindReferencesAction(Object object, IMenuManager menu)117 	private void addFindReferencesAction(Object object, IMenuManager menu) {
118 		if (object instanceof IPluginModelBase) {
119 			object = ((IPluginModelBase) object).getPluginBase();
120 		} else if (object instanceof ImportObject) {
121 			object = ((ImportObject) object).getImport();
122 		}
123 		if (object instanceof IPluginExtensionPoint || object instanceof IPluginImport || (object instanceof IPlugin) || (object instanceof IPluginExtension)) {
124 			String basePointID = fModel instanceof IPluginModelBase ? ((IPluginModelBase) fModel).getPluginBase().getId() : null;
125 			menu.add(new FindReferencesAction(object, basePointID));
126 		}
127 	}
128 
addDependencyExtentAction(Object object, IMenuManager menu)129 	private void addDependencyExtentAction(Object object, IMenuManager menu) {
130 		if (object instanceof ImportObject) {
131 			object = ((ImportObject) object).getImport();
132 		}
133 
134 		if (object instanceof IPluginImport) {
135 			String id = ((IPluginImport) object).getId();
136 			IResource resource = ((IPluginImport) object).getModel().getUnderlyingResource();
137 			if (resource != null) {
138 				menu.add(new DependencyExtentAction(resource.getProject(), id));
139 			}
140 		}
141 	}
142 
143 }
144