Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8d83bd93ebdadce75e6896455af473e02b1cc650 (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
/*******************************************************************************
 * Copyright (c) 2010 - 2013 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
 *     Lars Vogel <lars.vogel@gmail.com> - Bug 419723, 422644
 *     Markus A. Kuppe <bugs.eclipse.org@lemmster.de> - Bug 421259
 ******************************************************************************/
package org.eclipse.e4.tools.emf.liveeditor;

import java.util.List;

import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.commands.MBindingContext;
import org.eclipse.e4.ui.model.application.commands.MBindingTable;
import org.eclipse.e4.ui.model.application.commands.MCommand;
import org.eclipse.e4.ui.model.application.commands.MHandler;
import org.eclipse.e4.ui.model.application.commands.MKeyBinding;
import org.eclipse.e4.ui.model.application.descriptor.basic.MPartDescriptor;
import org.eclipse.e4.ui.workbench.modeling.EModelService;

public class ModelProcessor {

	private static final String E4_TOOLING_LIVEMODEL_HANDLER = "e4.tooling.livemodel.handler";
	private static final String E4_TOOLING_LIVEMODEL = "e4.tooling.livemodel";

	@Execute
	public void process(MApplication application, EModelService modelService) {

		MCommand command = null;
		for (MCommand cmd : application.getCommands()) {
			if (E4_TOOLING_LIVEMODEL.equals(cmd.getElementId())) {
				command = cmd;
			}
		}
		// List<MCommand> commands = modelService.findElements(application,
		// E4_TOOLING_LIVEMODEL, MCommand.class, null);

		if (command == null) {
			command = modelService.createModelElement(MCommand.class);
			command.setElementId(E4_TOOLING_LIVEMODEL);
			command.setCommandName("Show running app model");
			command.setDescription("Show the running application model");
			application.getCommands().add(command);
		}

		MHandler handler = null;

		for (MHandler hdl : application.getHandlers()) {
			if (E4_TOOLING_LIVEMODEL_HANDLER.equals(handler.getElementId())) {
				handler = hdl;
			}
		}

		if (handler == null) {
			handler = modelService.createModelElement(MHandler.class);
			handler.setElementId(E4_TOOLING_LIVEMODEL_HANDLER);
			handler.setContributionURI("bundleclass://org.eclipse.e4.tools.emf.liveeditor/org.eclipse.e4.tools.emf.liveeditor.OpenLiveDialogHandler");
			application.getHandlers().add(handler);
		}

		handler.setCommand(command);

		MKeyBinding binding = null;

		if (application.getBindingTables().size() <= 0) {
			MBindingContext bc = null;
			final List<MBindingContext> bindingContexts = application
					.getBindingContexts();
			if (bindingContexts.size() == 0) {
				bc = modelService.createModelElement(MBindingContext.class);
				bc.setElementId("org.eclipse.ui.contexts.window");
			} else {
				// Prefer org.eclipse.ui.contexts.dialogAndWindow but randomly
				// select another one
				// if org.eclipse.ui.contexts.dialogAndWindow cannot be found
				for (MBindingContext aBindingContext : bindingContexts) {
					bc = aBindingContext;
					if ("org.eclipse.ui.contexts.dialogAndWindow"
							.equals(aBindingContext.getElementId())) {
						break;
					}
				}
			}
			MBindingTable bt = modelService
					.createModelElement(MBindingTable.class);
			bt.setElementId("e4.tooling.livemodel.bindingTable");
			bt.setBindingContext(bc);
			application.getBindingTables().add(bt);
		}
		List<MKeyBinding> keyBindings = modelService.findElements(application,
				"e4.tooling.livemodel.binding", MKeyBinding.class, null);

		if (keyBindings.size() == 0) {
			binding = modelService.createModelElement(MKeyBinding.class);
			binding.setElementId("e4.tooling.livemodel.binding");
			binding.setKeySequence("M2+M3+F9");
			if (application.getBindingTables().size() > 0) {
				application.getBindingTables().get(0).getBindings()
						.add(binding);
			}
		} else {
			binding = keyBindings.get(0);
		}
		binding.setCommand(command);

		MPartDescriptor descriptor = null;
		List<MPartDescriptor> descriptors = modelService.findElements(
				application, "org.eclipse.e4.tools.emf.liveeditor.view",
				MPartDescriptor.class, null);

		if (descriptors.size() == 0) {
			descriptor = modelService.createModelElement(MPartDescriptor.class);
			descriptor.setCategory("org.eclipse.e4.secondaryDataStack");
			descriptor.setElementId("org.eclipse.e4.tools.emf.liveeditor.view");
			descriptor.getTags().add("View");
			descriptor.getTags().add("categoryTag:General");

			descriptor.setLabel("Live Application Model");
			descriptor
					.setContributionURI("bundleclass://org.eclipse.e4.tools.emf.liveeditor/org.eclipse.e4.tools.emf.liveeditor.LivePartDelegator");
			descriptor
					.setContributorURI("bundleclass://org.eclipse.e4.tools.emf.liveeditor");
			descriptor
					.setIconURI("platform:/plugin/org.eclipse.e4.tools.emf.liveeditor/icons/full/obj16/application_lightning.png");
			application.getDescriptors().add(descriptor);
		}
	}

}

Back to the top