Skip to main content
summaryrefslogtreecommitdiffstats
blob: 04ca28541f3c342708388dfd45bb592c63dfe7c4 (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
/*******************************************************************************
 * Copyright (c) 2011,2012 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.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 {
	private static final String BUNDLE_ID = "org.eclipse.e4.tools.css.spy";
	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 = "bundleclass://" + BUNDLE_ID
			+ "/"
			+ OpenSpyHandler.class.getName();

	public static final String OPEN_SCRATCHPAD_COMMAND_ID = "org.eclipse.e4.css.OpenSctachpad";
	private static final String SCRATCHPAD_HANDLER_ID = OpenScratchpadHandler.class
			.getName();
	private static final String SCRATCHPAD_HANDLER_URI = "bundleclass://"
			+ BUNDLE_ID + "/"
			+ OpenScratchpadHandler.class.getName();
	private static final String CONTRIBUTOR_URI = "platform:/plugin/"
			+ BUNDLE_ID;

	@Inject
	protected MApplication app;

	@Execute
	public void execute() {
		// rectify situation introduced by bug 376475
		removeBindingTable("bt.org.eclipse.e4.css.OpenSpy");

		MCommand openSpyCommand = installCommand("Open CSS Spy",
				OPEN_SPY_COMMAND_ID);
		installHandler(openSpyCommand, SPY_HANDLER_ID, SPY_HANDLER_URI);

		// M1 = Control or Cmd on MacOS X
		// M2 = Shift
		// M3 = Alt
		// M4 = Control on MacOS X, Command on others
		installBinding("org.eclipse.ui.contexts.dialogAndWindow",
				openSpyCommand, "M2+M3+F5"); // Alt-Shift-F5

		MCommand openScratchpadCommand = installCommand("Open CSS Scratchpad",
				OPEN_SCRATCHPAD_COMMAND_ID);
		installHandler(openScratchpadCommand, SCRATCHPAD_HANDLER_ID, SCRATCHPAD_HANDLER_URI);
		installBinding("org.eclipse.ui.contexts.dialogAndWindow",
				openScratchpadCommand, "M2+M3+F6"); // Alt-Shift-F5
	}

	private void removeBindingTable(String tableId) {
		for (MBindingTable table : app.getBindingTables()) {
			if (tableId.equals(table.getElementId())) {
				app.getBindingTables().remove(table);
				return;
			}
		}
	}

	private MCommand installCommand(String label, String commandId) {
		for(MCommand cmd : app.getCommands()) {
			if (commandId.equals(cmd.getElementId())) {
				return cmd;
			}
		}

		MCommand cmd = MCommandsFactory.INSTANCE.createCommand();
		cmd.setCommandName(label);
		cmd.setElementId(commandId);
		cmd.setContributorURI(CONTRIBUTOR_URI);
		app.getCommands().add(cmd);
		return cmd;
	}

	private MHandler installHandler(MCommand cmd, String handlerId,
			String handlerURI) {
		for(MHandler hdlr : app.getHandlers()) {
			if (handlerId.equals(hdlr.getElementId())) {
				return hdlr;
			}
		}

		MHandler hdlr = MCommandsFactory.INSTANCE.createHandler();
		hdlr.setElementId(handlerId);
		hdlr.setContributionURI(handlerURI);
		hdlr.setCommand(cmd);
		hdlr.setContributorURI(CONTRIBUTOR_URI);
		app.getHandlers().add(hdlr);
		return hdlr;
	}

	private void installBinding(String bindingContextId, MCommand cmd,
			String keySeq) {
		// there is a one-to-one mapping between binding contexts and
		// binding tables, though binding tables may not necessarily
		// guaranteed an element id.
		MBindingTable bindingTable = null;
		for(MBindingTable table : app.getBindingTables()) {
			for(MKeyBinding binding : table.getBindings()) {
				if(binding.getCommand() == cmd) {
					return;
				}
			}
			if (table.getBindingContext() != null
					&& bindingContextId.equals(table.getBindingContext()
							.getElementId())) {
				bindingTable = table;
			}
		}

		if(bindingTable == null) {
			System.err.println("Cannot find table for binding context: "
					+ bindingContextId);
			return;
		}

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

}

Back to the top