Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 088a5a178354c4c1c4db1ad663e80ff34a4d3feb (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
/*******************************************************************************
 *  Copyright (c) 2006, 2011, 2012 IBM Corporation and others.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 * 
 *  Contributors:
 *     IBM Corporation - initial API and implementation
 *     Alex Blewitt (alex_blewitt@yahoo.com) - contributed a patch for:
 *       o Add an 'Open Manifest' to projects to open the manifest editor
 *         (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=133692)
 *     Brian de Alwis - open manifest for the currently active editor
 *******************************************************************************/
package org.eclipse.pde.internal.ui.editor;

import java.util.HashSet;
import java.util.Iterator;
import org.eclipse.core.commands.*;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.osgi.util.NLS;
import org.eclipse.pde.internal.core.WorkspaceModelManager;
import org.eclipse.pde.internal.core.project.PDEProject;
import org.eclipse.pde.internal.ui.*;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.ui.*;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.model.IWorkbenchAdapter;

/**
 * Command handler that opens the manifest file for the plug-in in the
 * manifest editor.
 */
public class OpenManifestHandler extends AbstractHandler {

	/* (non-Javadoc)
	 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
	 */
	public Object execute(ExecutionEvent event) throws ExecutionException {
		final HashSet projects = new HashSet();
		if (HandlerUtil.getActivePart(event) instanceof IEditorPart) {
			IEditorInput input = ((IEditorPart) HandlerUtil.getActivePart(event)).getEditorInput();
			IProject proj = getProject(input);
			if (proj != null && WorkspaceModelManager.isPluginProject(proj)) {
				projects.add(proj);
			}
		}
		ISelection selection = HandlerUtil.getCurrentSelection(event);
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection ssel = (IStructuredSelection) selection;
			Iterator it = ssel.iterator();
			while (it.hasNext()) {
				Object element = it.next();
				IProject proj = null;
				if (element instanceof IFile)
					proj = ((IFile) element).getProject();
				if ((proj == null) && (element instanceof IProject))
					proj = (IProject) element;
				if ((proj == null) && (element instanceof IAdaptable)) {
					IResource resource = (IResource) ((IAdaptable) element).getAdapter(IResource.class);
					if (resource != null) {
						proj = resource.getProject();
					}
					if (proj == null) {
						IWorkbenchAdapter workbenchAdapter = (IWorkbenchAdapter) ((IAdaptable) element).getAdapter(IWorkbenchAdapter.class);
						if (workbenchAdapter != null) {
							Object o = workbenchAdapter.getParent(element);
							if (o instanceof IAdaptable) {
								resource = (IResource) ((IAdaptable) o).getAdapter(IResource.class);
								if (resource != null) {
									proj = resource.getProject();
								}
							}
						}
					}
				}

				if (proj != null && WorkspaceModelManager.isPluginProject(proj))
					projects.add(proj);
			}
		}
		if (projects.size() > 0) {
			BusyIndicator.showWhile(PDEPlugin.getActiveWorkbenchShell().getDisplay(), new Runnable() {
				public void run() {
					Iterator it = projects.iterator();
					while (it.hasNext()) {
						IProject project = (IProject) it.next();
						IFile file = PDEProject.getManifest(project);
						if (file == null || !file.exists())
							file = PDEProject.getPluginXml(project);
						if (file == null || !file.exists())
							file = PDEProject.getFragmentXml(project);
						if (file == null || !file.exists())
							MessageDialog.openError(PDEPlugin.getActiveWorkbenchShell(), PDEUIMessages.OpenManifestsAction_title, NLS.bind(PDEUIMessages.OpenManifestsAction_cannotFind, project.getName()));
						else
							try {
								IDE.openEditor(PDEPlugin.getActivePage(), file, IPDEUIConstants.MANIFEST_EDITOR_ID);
							} catch (PartInitException e) {
								MessageDialog.openError(PDEPlugin.getActiveWorkbenchShell(), PDEUIMessages.OpenManifestsAction_title, NLS.bind(PDEUIMessages.OpenManifestsAction_cannotOpen, project.getName()));
							}
					}
				}
			});
		} else
			MessageDialog.openInformation(PDEPlugin.getActiveWorkbenchShell(), PDEUIMessages.OpenManifestsAction_title, PDEUIMessages.OpenManifestAction_noManifest);
		return null;
	}

	private IProject getProject(IEditorInput input) {
		IAdapterManager adapterManager = Platform.getAdapterManager();
		Object o;
		if ((o = input.getAdapter(IResource.class)) != null || (o = adapterManager.getAdapter(input, IResource.class)) != null) {
			return ((IFile) o).getProject();
		}
		if ((o = input.getAdapter(IProject.class)) != null || (o = adapterManager.getAdapter(input, IProject.class)) != null) {
			return (IProject) o;
		}
		return null;
	}
}

Back to the top