[yocto] [PATCHv2 5/8] plugins/sdk.ide: Add command to switch the target profile

Timo Mueller mail at timomueller.eu
Fri Jun 21 05:44:44 PDT 2013


From: Timo Mueller <timo.mueller at bmw-carit.de>

The command can be used in a radio group to switch the target profile
of a selected project.

Radio items should call this command handing over the name of the
target profile as the command's parameter.

Signed-off-by: Timo Mueller <timo.mueller at bmw-carit.de>
---
 .../OSGI-INF/l10n/bundle.properties                |   3 +
 plugins/org.yocto.sdk.ide/plugin.xml               |  18 ++++
 .../org/yocto/sdk/ide/YoctoSDKMessages.properties  |   3 +
 .../sdk/ide/actions/ProfileSwitchHandler.java      | 110 +++++++++++++++++++++
 4 files changed, 134 insertions(+)
 create mode 100644 plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/actions/ProfileSwitchHandler.java

diff --git a/plugins/org.yocto.sdk.ide/OSGI-INF/l10n/bundle.properties b/plugins/org.yocto.sdk.ide/OSGI-INF/l10n/bundle.properties
index 48eb653..1191af6 100644
--- a/plugins/org.yocto.sdk.ide/OSGI-INF/l10n/bundle.properties
+++ b/plugins/org.yocto.sdk.ide/OSGI-INF/l10n/bundle.properties
@@ -6,6 +6,9 @@ extension.name.2 = YoctoSDKCMakeNature
 command.name = ReconfigureYoctoProject
 command.label.0 = Change Yocto Project Settings
 command.mnemonic = C
+command.targetProfileSwitch.name = Change Target Profile
+command.targetProfileSwitch.description = Changes the target profile of a selected project
+command.targetProfileSwitch.parameter.name = Selected Target Profile
 projectType.name.0 = Yocto Project ADT Autotools Project
 projectProperties.label.0 = Yocto Project Settings
 Bundle-Vendor = yoctoproject.org
diff --git a/plugins/org.yocto.sdk.ide/plugin.xml b/plugins/org.yocto.sdk.ide/plugin.xml
index 7ea55c7..62f1297 100644
--- a/plugins/org.yocto.sdk.ide/plugin.xml
+++ b/plugins/org.yocto.sdk.ide/plugin.xml
@@ -225,5 +225,23 @@
          </filter>
       </page>
    </extension>
+   <extension
+         point="org.eclipse.ui.commands">
+      <command
+            defaultHandler="org.yocto.sdk.ide.actions.ProfileSwitchHandler"
+            description="%command.targetProfileSwitch.description"
+            id="org.yocto.sdk.ide.targetProfile.switch"
+            name="%command.targetProfileSwitch.name">
+         <commandParameter
+               id="org.eclipse.ui.commands.radioStateParameter"
+               name="%command.targetProfileSwitch.parameter.name"
+               optional="false">
+         </commandParameter>
+         <state
+               class="org.eclipse.ui.handlers.RadioState:project-specific"
+               id="org.eclipse.ui.commands.radioState">
+         </state>
+      </command>
+   </extension>
 
 </plugin>
diff --git a/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/YoctoSDKMessages.properties b/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/YoctoSDKMessages.properties
index 4ff1574..d6b5cdb 100644
--- a/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/YoctoSDKMessages.properties
+++ b/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/YoctoSDKMessages.properties
@@ -89,6 +89,9 @@ Preferences.Profile.Standard.Modification.Message = Standard cross development p
 Preferences.Profile.ProjectSpecific.Title = Use project specific settings
 Preferences.Profile.ProjectSpecific.Group.Title = Project specific settings:
 
+Preferences.Profile.ProjectSpecific.Error.Title = Could not change to project specific target profile
+Preferences.Profile.ProjectSpecific.Error.Message = The project specific target profile is not defined for the project "{0}".\nYou can define it in the project's property page.  
+
 Console.SDK.Name = Yocto Project Console
 
 LaunchConfig.Type.Name = org.eclipse.ui.externaltools.ProgramLaunchConfigurationType
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..38e3e6d
--- /dev/null
+++ b/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/actions/ProfileSwitchHandler.java
@@ -0,0 +1,110 @@
+/*******************************************************************************
+ * 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 org.eclipse.cdt.core.model.ICElement;
+import org.eclipse.cdt.core.model.ICProject;
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+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.handlers.HandlerUtil;
+import org.eclipse.ui.handlers.RadioState;
+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 {
+	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"; //$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);
+		}
+	}
+}
-- 
1.8.1.4




More information about the yocto mailing list