Skip to main content
summaryrefslogtreecommitdiffstats
blob: 87f65e547ddcfb668f5eec0cd5c6905b8eae1cc9 (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
/*******************************************************************************
 * Copyright (c) 2011 Manumitting Technologies, Inc.
 * 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:
 *     Brian de Alwis (MT) - initial API and implementation
 *******************************************************************************/
package org.eclipse.e4.tools.css.spy;

import javax.inject.Inject;

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.MCommandsFactory;
import org.eclipse.e4.ui.model.application.commands.MHandler;
import org.eclipse.e4.ui.model.application.commands.MKeyBinding;

public class SpyInstaller {
	public static final String OPEN_SPY_COMMAND_ID = "org.eclipse.e4.css.OpenSpy";
	private static final String SPY_HANDLER_ID = OpenSpyHandler.class.getName();
    private static final String SPY_HANDLER_URI = "platform:/plugin/org.eclipse.e4.tools.css.spy/"
			+ OpenSpyHandler.class.getName();

	@Inject
	protected MApplication app;

	@Execute
	public void execute() {
		MCommand cmd = installSpyCommand();
		installSpyHandler(cmd);
        installSpyBinding("org.eclipse.ui.contexts.dialogAndWindow", cmd, "M2+M3+F4");
	}

	private MCommand installSpyCommand() {
		for(MCommand cmd : app.getCommands()) {
			if(OPEN_SPY_COMMAND_ID.equals(cmd.getElementId())) {
				System.err.println("CSS Spy command already setup");
				return cmd;
			}
		}

		MCommand cmd = MCommandsFactory.INSTANCE.createCommand();
		cmd.setCommandName("Open CSS Spy");
		cmd.setElementId(OPEN_SPY_COMMAND_ID);
		app.getCommands().add(cmd);
		return cmd;
	}

	private MHandler installSpyHandler(MCommand cmd) {
		for(MHandler hdlr : app.getHandlers()) {
			if(SPY_HANDLER_ID.equals(hdlr.getElementId())) {
				System.err.println("CSS Spy handler already setup");
				return hdlr;
			}
		}

		MHandler hdlr = MCommandsFactory.INSTANCE.createHandler();
		hdlr.setElementId(SPY_HANDLER_ID);
		hdlr.setContributionURI(SPY_HANDLER_URI);
		hdlr.setCommand(cmd);
		app.getHandlers().add(hdlr);
		return hdlr;
	}

	private void installSpyBinding(String bindingContextId, MCommand cmd, String keySeq) {
		for(MBindingTable table : app.getBindingTables()) {
			for(MKeyBinding binding : table.getBindings()) {
				if(binding.getCommand() == cmd) {
					System.err.println("Spy binding already installed");
					return;
				}
			}
		}

		MBindingContext context = null;
		for(MBindingContext ctxt : app.getBindingContexts()) {
			if(ctxt.getElementId().equals(bindingContextId)) {
				context = ctxt;
				break;
			}
		}
		if(context == null) {
			System.err.println("Cannot find binding context: " + bindingContextId);
			return;
		}

		MBindingTable bindingTable = null;
		String tableId = "bt." + cmd.getElementId();
		for(MBindingTable table : app.getBindingTables()) {
			if(tableId.equals(table.getElementId())) {
				bindingTable = table;
			}
		}

		if(bindingTable == null) {
			bindingTable = MCommandsFactory.INSTANCE.createBindingTable();
			bindingTable.setElementId(tableId);
			bindingTable.setBindingContext(context);
			app.getBindingTables().add(bindingTable);
		}

		MKeyBinding binding = MCommandsFactory.INSTANCE.createKeyBinding();
		binding.setCommand(cmd);
		binding.setKeySequence(keySeq);
		binding.setElementId("kb." + cmd.getElementId());
		bindingTable.getBindings().add(binding);
	}

}

Back to the top