diff options
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.java | 134 |
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 | *******************************************************************************/ | ||
11 | package org.yocto.sdk.ide.actions; | ||
12 | |||
13 | import java.util.Map; | ||
14 | |||
15 | import org.eclipse.cdt.core.model.ICElement; | ||
16 | import org.eclipse.cdt.core.model.ICProject; | ||
17 | import org.eclipse.core.commands.AbstractHandler; | ||
18 | import org.eclipse.core.commands.Command; | ||
19 | import org.eclipse.core.commands.ExecutionEvent; | ||
20 | import org.eclipse.core.commands.ExecutionException; | ||
21 | import org.eclipse.core.commands.State; | ||
22 | import org.eclipse.core.resources.IProject; | ||
23 | import org.eclipse.core.resources.IResource; | ||
24 | import org.eclipse.core.runtime.IAdaptable; | ||
25 | import org.eclipse.core.runtime.Status; | ||
26 | import org.eclipse.jface.dialogs.ErrorDialog; | ||
27 | import org.eclipse.jface.preference.IPreferenceStore; | ||
28 | import org.eclipse.jface.viewers.ISelection; | ||
29 | import org.eclipse.jface.viewers.ITreeSelection; | ||
30 | import org.eclipse.swt.widgets.Display; | ||
31 | import org.eclipse.ui.commands.ICommandService; | ||
32 | import org.eclipse.ui.commands.IElementUpdater; | ||
33 | import org.eclipse.ui.handlers.HandlerUtil; | ||
34 | import org.eclipse.ui.handlers.RadioState; | ||
35 | import org.eclipse.ui.menus.UIElement; | ||
36 | import org.yocto.sdk.ide.YoctoProfileElement; | ||
37 | import org.yocto.sdk.ide.YoctoSDKChecker; | ||
38 | import org.yocto.sdk.ide.YoctoSDKChecker.SDKCheckResults; | ||
39 | import org.yocto.sdk.ide.YoctoSDKMessages; | ||
40 | import org.yocto.sdk.ide.YoctoSDKPlugin; | ||
41 | import org.yocto.sdk.ide.YoctoUIElement; | ||
42 | import org.yocto.sdk.ide.utils.ProjectPreferenceUtils; | ||
43 | import org.yocto.sdk.ide.utils.YoctoSDKUtils; | ||
44 | |||
45 | public 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 | } | ||