Skip to main content
summaryrefslogtreecommitdiffstats
blob: be06428bdc62189d8f32bdca51ea7b76d48123d7 (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
/*******************************************************************************
 * Copyright (c) 2004, 2008 Jingwen Ou 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:
 *     Jingwen Ou - initial API and implementation
 *     Tasktop Technologies - improvements
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.ui.commands;

import java.util.Map;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.HandlerEvent;
import org.eclipse.jface.action.IAction;
import org.eclipse.mylyn.internal.tasks.ui.editors.EditorUtil;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchSite;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.IElementUpdater;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.editor.IFormPage;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.menus.UIElement;

/**
 * @author Jingwen Ou
 * @author Steffen Pingel
 */
public class ViewSourceHandler extends AbstractHandler implements IElementUpdater {

	private static boolean checked;

	private static ViewSourceHandler instance;

	public ViewSourceHandler() {
		instance = this;
	}

	public boolean isChecked() {
		return checked;
	}

	public static void setChecked(boolean checked) {
		ViewSourceHandler.checked = checked;
		if (instance != null) {
			instance.fireHandlerChanged(new HandlerEvent(instance, true, false));
		}
	}

	private Control getFocusControl() {
		return PlatformUI.getWorkbench().getDisplay().getFocusControl();
	}

	@Override
	public boolean isEnabled() {
		Control focusControl = getFocusControl();
		if (focusControl instanceof StyledText && focusControl.getData(VIEW_SOURCE_ACTION) instanceof IAction) {
			return true;
		}

		return false;
	}

	public static final String VIEW_SOURCE_ACTION = "viewSourceAction"; //$NON-NLS-1$

	public Object execute(ExecutionEvent event) throws ExecutionException {
		IWorkbenchSite site = HandlerUtil.getActiveSite(event);
		if (site instanceof IEditorSite) {
			IWorkbenchPart part = ((IEditorSite) site).getPart();
			if (part instanceof FormEditor) {
				IFormPage page = ((FormEditor) part).getActivePageInstance();
				Control focusedControl = EditorUtil.getFocusControl(page);
				if (focusedControl != null) {
					Object data = focusedControl.getData(VIEW_SOURCE_ACTION);
					if (data instanceof IAction) {
						IAction action = (IAction) data;
						action.setChecked(!action.isChecked());
						action.run();
						setChecked(action.isChecked());
					}
				}
			}
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public void updateElement(UIElement element, Map parameters) {
		element.setChecked(checked);
	}

}

Back to the top