Skip to main content
summaryrefslogtreecommitdiffstats
blob: cb9d5874a73d5ecb977dcb84113707b0ba16a0d9 (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
140
141
142
143
144
145
/*******************************************************************************
 * Copyright (c) 2010 BestSolution.at 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:
 * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
 * Sopot Cela <sopotcela@gmail.com>
 ******************************************************************************/
package org.eclipse.e4.tools.emf.editor3x.extension;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.e4.internal.tools.wizards.classes.NewHandlerClassWizard;
import org.eclipse.e4.tools.emf.editor3x.Messages;
import org.eclipse.e4.tools.emf.ui.common.IContributionClassCreator;
import org.eclipse.e4.ui.model.application.MContribution;
import org.eclipse.e4.ui.model.application.commands.impl.CommandsPackageImpl;
import org.eclipse.e4.ui.model.application.impl.ApplicationPackageImpl;
import org.eclipse.e4.ui.model.application.ui.menu.impl.MenuPackageImpl;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.edit.command.SetCommand;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PartInitException;

@SuppressWarnings("restriction")
public class HandlerContributionEditor implements IContributionClassCreator {
	@Override
	public void createOpen(MContribution contribution, EditingDomain domain, IProject project, Shell shell) {
		createOpen(contribution, domain, project, shell, false);
	}

	private void createOpen(MContribution contribution, EditingDomain domain, IProject project, Shell shell,
		boolean forceNew) {
		if (forceNew || contribution.getContributionURI() == null
			|| contribution.getContributionURI().trim().length() == 0
			|| !contribution.getContributionURI().startsWith("bundleclass:")) { //$NON-NLS-1$
			final NewHandlerClassWizard wizard = new NewHandlerClassWizard(contribution.getContributionURI());
			wizard.init(null, new StructuredSelection(project));
			final WizardDialog dialog = new WizardDialog(shell, wizard);
			if (dialog.open() == Window.OK) {
				final IFile f = wizard.getFile();
				final ICompilationUnit el = JavaCore.createCompilationUnitFrom(f);
				try {
					String fullyQualified;
					if (el.getPackageDeclarations() != null && el.getPackageDeclarations().length > 0) {
						final String packageName = el.getPackageDeclarations()[0].getElementName();
						final String className = wizard.getDomainClass().getName();
						if (packageName.trim().length() > 0) {
							fullyQualified = packageName + "." + className; //$NON-NLS-1$
						} else {
							fullyQualified = className;
						}
					} else {
						fullyQualified = wizard.getDomainClass().getName();
					}

					final Command cmd = SetCommand.create(domain, contribution,
						ApplicationPackageImpl.Literals.CONTRIBUTION__CONTRIBUTION_URI,
						"bundleclass://" + Util.getBundleSymbolicName(f.getProject()) + "/" + fullyQualified); //$NON-NLS-1$//$NON-NLS-2$
					if (cmd.canExecute()) {
						domain.getCommandStack().execute(cmd);
					}
				} catch (final JavaModelException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} else {
			final URI uri = URI.createURI(contribution.getContributionURI());
			if (uri.hasAuthority() && uri.segmentCount() == 1) {
				final String symbolicName = uri.authority();
				final String fullyQualified = uri.segment(0);
				IProject p = ResourcesPlugin.getWorkspace().getRoot()
					.getProject(symbolicName);

				if (!p.exists()) {
					for (final IProject check : ResourcesPlugin.getWorkspace().getRoot().getProjects()) {
						final String name = Util.getBundleSymbolicName(check);
						if (symbolicName.equals(name)) {
							p = check;
							break;
						}
					}
				}

				// TODO If this is not a WS-Resource we need to open differently
				if (p != null) {
					final IJavaProject jp = JavaCore.create(p);
					IType t = null;
					try {
						if (p.exists()) {
							t = jp.findType(fullyQualified);
						}
						else
						{
							final IJavaProject pprim = JavaCore.create(project);
							t = pprim.findType(fullyQualified);
						}
						if (t != null) {
							JavaUI.openInEditor(t);
						} else {
							createOpen(contribution, domain, project, shell, true);
						}
					} catch (final JavaModelException e) {
						createOpen(contribution, domain, project, shell, true);
					} catch (final PartInitException e) {
						MessageDialog.openError(shell, Messages.ContributionEditor_FailedToOpenEditor, e.getMessage());
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			} else {
				MessageDialog.openError(shell, Messages.ContributionEditor_InvalidURL,
					Messages.ContributionEditor_CurrentURLIsInvalid);
			}
		}
	}

	@Override
	public boolean isSupported(EClass element) {
		return Util.isTypeOrSuper(CommandsPackageImpl.Literals.HANDLER, element)
			||
			Util.isTypeOrSuper(MenuPackageImpl.Literals.DIRECT_MENU_ITEM, element)
			||
			Util.isTypeOrSuper(MenuPackageImpl.Literals.DIRECT_TOOL_ITEM, element);
	}

}

Back to the top