summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/actions/ProfileSwitchHandler.java
blob: e3e7e60b4cfc4e93348c88f944cb43d6897c09dc (plain)
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
/*******************************************************************************
 * Copyright (c) 2013 BMW Car IT GmbH.
 * 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:
 * BMW Car IT - initial implementation
 *******************************************************************************/
package org.yocto.sdk.ide.actions;

import java.util.Map;

import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.State;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.commands.IElementUpdater;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.handlers.RadioState;
import org.eclipse.ui.menus.UIElement;
import org.yocto.sdk.ide.YoctoProfileElement;
import org.yocto.sdk.ide.YoctoSDKChecker;
import org.yocto.sdk.ide.YoctoSDKChecker.SDKCheckResults;
import org.yocto.sdk.ide.YoctoSDKMessages;
import org.yocto.sdk.ide.YoctoSDKPlugin;
import org.yocto.sdk.ide.YoctoUIElement;
import org.yocto.sdk.ide.utils.ProjectPreferenceUtils;
import org.yocto.sdk.ide.utils.YoctoSDKUtils;

public class ProfileSwitchHandler extends AbstractHandler implements IElementUpdater {
	private static final String PROJECT_SPECIFIC_ERROR = "Preferences.Profile.ProjectSpecific.Error.Title";
	private static final String PROJECT_SPECIFIC_ERROR_MESSAGE = "Preferences.Profile.ProjectSpecific.Error.Message";

	public static final String PROFILE_SWITCH_COMMAND = "org.yocto.sdk.ide.targetProfile.switch"; //$NON-NLS-N$
	public static final String PROJECT_SPECIFIC_PARAMETER = "##PROJECT_SPECIFIC_PROFILE##"; //$NON-NLS-N$

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		if(HandlerUtil.matchesRadioState(event)) {
			return null;
		}

		String currentState = event.getParameter(RadioState.PARAMETER_ID);
		HandlerUtil.updateRadioState(event.getCommand(), currentState);

		switchProfile(getSelectedProject(event), currentState);

		return null;
	}

	public IProject getSelectedProject(ExecutionEvent event) {
		ISelection selection = HandlerUtil.getCurrentSelection(event);

		if (selection instanceof ITreeSelection) {
			Object selectedItem = ((ITreeSelection) selection).getFirstElement();
			if (selectedItem instanceof IResource) {
				return ((IResource) selectedItem).getProject();
			} else if (selectedItem instanceof ICElement) {
				ICProject cProject = ((ICElement) selectedItem).getCProject();
				if (cProject != null) {
					return cProject.getProject();
				}
			} else if (selectedItem instanceof IAdaptable) {
				Object projectObject = ((IAdaptable) selectedItem).getAdapter(IProject.class);
				if (projectObject != null && projectObject instanceof IProject) {
					return ((IProject) projectObject);
				}
			}
		}

		return null;
	}

	private void switchProfile(IProject project, String selectedProfile) {
		if (PROJECT_SPECIFIC_PARAMETER.equals(selectedProfile)) {
			YoctoUIElement yoctoUIElement = ProjectPreferenceUtils.getElem(project);
			SDKCheckResults result = YoctoSDKChecker.checkYoctoSDK(yoctoUIElement);

			if ((result != SDKCheckResults.SDK_PASS)) {
				Display display = Display.getCurrent();
				ErrorDialog.openError(display.getActiveShell(),
										YoctoSDKMessages.getString(PROJECT_SPECIFIC_ERROR),
										YoctoSDKMessages.getFormattedString(PROJECT_SPECIFIC_ERROR_MESSAGE,
															project.getName()),
										new Status(Status.ERROR, YoctoSDKPlugin.PLUGIN_ID, result.getMessage()));
				return;
			}

			ProjectPreferenceUtils.saveElemToProjectEnv(yoctoUIElement, project);
			ProjectPreferenceUtils.saveUseProjectSpecificOption(project, true);
		} else {
			IPreferenceStore store = YoctoSDKPlugin.getProfilePreferenceStore(selectedProfile);
			YoctoUIElement yoctoUIElement = YoctoSDKUtils.getElemFromStore(store);
			ProjectPreferenceUtils.saveElemToProjectEnv(yoctoUIElement, project);
			ProjectPreferenceUtils.saveUseProjectSpecificOption(project, false);

			YoctoProfileElement profileSettings = ProjectPreferenceUtils.getProfiles(project);
			profileSettings.setSelectedProfile(selectedProfile);
			ProjectPreferenceUtils.saveProfiles(profileSettings, project);
		}
	}

	/*
	 * Workaround for BUG 398647 to allow checking radio items
	 * in a dynamic contribution
	 *
	 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=398647
	 */
	@Override
	public void updateElement(UIElement element, @SuppressWarnings("rawtypes") Map parameters) {
			ICommandService service = (ICommandService) element.getServiceLocator().getService(ICommandService.class);
			String state = (String) parameters.get(RadioState.PARAMETER_ID);
			Command command = service.getCommand(PROFILE_SWITCH_COMMAND);
			State commandState = command.getState(RadioState.STATE_ID);
			if (commandState.getValue().equals(state)) {
				element.setChecked(true);
			}
	}
}