summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/actions/ProfileSwitchHandler.java
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/actions/ProfileSwitchHandler.java')
-rw-r--r--plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/actions/ProfileSwitchHandler.java134
1 files changed, 134 insertions, 0 deletions
diff --git a/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/actions/ProfileSwitchHandler.java b/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/actions/ProfileSwitchHandler.java
new file mode 100644
index 0000000..e3e7e60
--- /dev/null
+++ b/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/actions/ProfileSwitchHandler.java
@@ -0,0 +1,134 @@
1/*******************************************************************************
2 * Copyright (c) 2013 BMW Car IT GmbH.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * BMW Car IT - initial implementation
10 *******************************************************************************/
11package org.yocto.sdk.ide.actions;
12
13import java.util.Map;
14
15import org.eclipse.cdt.core.model.ICElement;
16import org.eclipse.cdt.core.model.ICProject;
17import org.eclipse.core.commands.AbstractHandler;
18import org.eclipse.core.commands.Command;
19import org.eclipse.core.commands.ExecutionEvent;
20import org.eclipse.core.commands.ExecutionException;
21import org.eclipse.core.commands.State;
22import org.eclipse.core.resources.IProject;
23import org.eclipse.core.resources.IResource;
24import org.eclipse.core.runtime.IAdaptable;
25import org.eclipse.core.runtime.Status;
26import org.eclipse.jface.dialogs.ErrorDialog;
27import org.eclipse.jface.preference.IPreferenceStore;
28import org.eclipse.jface.viewers.ISelection;
29import org.eclipse.jface.viewers.ITreeSelection;
30import org.eclipse.swt.widgets.Display;
31import org.eclipse.ui.commands.ICommandService;
32import org.eclipse.ui.commands.IElementUpdater;
33import org.eclipse.ui.handlers.HandlerUtil;
34import org.eclipse.ui.handlers.RadioState;
35import org.eclipse.ui.menus.UIElement;
36import org.yocto.sdk.ide.YoctoProfileElement;
37import org.yocto.sdk.ide.YoctoSDKChecker;
38import org.yocto.sdk.ide.YoctoSDKChecker.SDKCheckResults;
39import org.yocto.sdk.ide.YoctoSDKMessages;
40import org.yocto.sdk.ide.YoctoSDKPlugin;
41import org.yocto.sdk.ide.YoctoUIElement;
42import org.yocto.sdk.ide.utils.ProjectPreferenceUtils;
43import org.yocto.sdk.ide.utils.YoctoSDKUtils;
44
45public class ProfileSwitchHandler extends AbstractHandler implements IElementUpdater {
46 private static final String PROJECT_SPECIFIC_ERROR = "Preferences.Profile.ProjectSpecific.Error.Title";
47 private static final String PROJECT_SPECIFIC_ERROR_MESSAGE = "Preferences.Profile.ProjectSpecific.Error.Message";
48
49 public static final String PROFILE_SWITCH_COMMAND = "org.yocto.sdk.ide.targetProfile.switch"; //$NON-NLS-N$
50 public static final String PROJECT_SPECIFIC_PARAMETER = "##PROJECT_SPECIFIC_PROFILE##"; //$NON-NLS-N$
51
52 @Override
53 public Object execute(ExecutionEvent event) throws ExecutionException {
54 if(HandlerUtil.matchesRadioState(event)) {
55 return null;
56 }
57
58 String currentState = event.getParameter(RadioState.PARAMETER_ID);
59 HandlerUtil.updateRadioState(event.getCommand(), currentState);
60
61 switchProfile(getSelectedProject(event), currentState);
62
63 return null;
64 }
65
66 public IProject getSelectedProject(ExecutionEvent event) {
67 ISelection selection = HandlerUtil.getCurrentSelection(event);
68
69 if (selection instanceof ITreeSelection) {
70 Object selectedItem = ((ITreeSelection) selection).getFirstElement();
71 if (selectedItem instanceof IResource) {
72 return ((IResource) selectedItem).getProject();
73 } else if (selectedItem instanceof ICElement) {
74 ICProject cProject = ((ICElement) selectedItem).getCProject();
75 if (cProject != null) {
76 return cProject.getProject();
77 }
78 } else if (selectedItem instanceof IAdaptable) {
79 Object projectObject = ((IAdaptable) selectedItem).getAdapter(IProject.class);
80 if (projectObject != null && projectObject instanceof IProject) {
81 return ((IProject) projectObject);
82 }
83 }
84 }
85
86 return null;
87 }
88
89 private void switchProfile(IProject project, String selectedProfile) {
90 if (PROJECT_SPECIFIC_PARAMETER.equals(selectedProfile)) {
91 YoctoUIElement yoctoUIElement = ProjectPreferenceUtils.getElem(project);
92 SDKCheckResults result = YoctoSDKChecker.checkYoctoSDK(yoctoUIElement);
93
94 if ((result != SDKCheckResults.SDK_PASS)) {
95 Display display = Display.getCurrent();
96 ErrorDialog.openError(display.getActiveShell(),
97 YoctoSDKMessages.getString(PROJECT_SPECIFIC_ERROR),
98 YoctoSDKMessages.getFormattedString(PROJECT_SPECIFIC_ERROR_MESSAGE,
99 project.getName()),
100 new Status(Status.ERROR, YoctoSDKPlugin.PLUGIN_ID, result.getMessage()));
101 return;
102 }
103
104 ProjectPreferenceUtils.saveElemToProjectEnv(yoctoUIElement, project);
105 ProjectPreferenceUtils.saveUseProjectSpecificOption(project, true);
106 } else {
107 IPreferenceStore store = YoctoSDKPlugin.getProfilePreferenceStore(selectedProfile);
108 YoctoUIElement yoctoUIElement = YoctoSDKUtils.getElemFromStore(store);
109 ProjectPreferenceUtils.saveElemToProjectEnv(yoctoUIElement, project);
110 ProjectPreferenceUtils.saveUseProjectSpecificOption(project, false);
111
112 YoctoProfileElement profileSettings = ProjectPreferenceUtils.getProfiles(project);
113 profileSettings.setSelectedProfile(selectedProfile);
114 ProjectPreferenceUtils.saveProfiles(profileSettings, project);
115 }
116 }
117
118 /*
119 * Workaround for BUG 398647 to allow checking radio items
120 * in a dynamic contribution
121 *
122 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=398647
123 */
124 @Override
125 public void updateElement(UIElement element, @SuppressWarnings("rawtypes") Map parameters) {
126 ICommandService service = (ICommandService) element.getServiceLocator().getService(ICommandService.class);
127 String state = (String) parameters.get(RadioState.PARAMETER_ID);
128 Command command = service.getCommand(PROFILE_SWITCH_COMMAND);
129 State commandState = command.getState(RadioState.STATE_ID);
130 if (commandState.getValue().equals(state)) {
131 element.setChecked(true);
132 }
133 }
134}