summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/TargetProfileContributionItem.java
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/TargetProfileContributionItem.java')
-rw-r--r--plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/TargetProfileContributionItem.java126
1 files changed, 126 insertions, 0 deletions
diff --git a/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/TargetProfileContributionItem.java b/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/TargetProfileContributionItem.java
new file mode 100644
index 0000000..95d8229
--- /dev/null
+++ b/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/TargetProfileContributionItem.java
@@ -0,0 +1,126 @@
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;
12
13import java.util.ArrayList;
14import java.util.HashMap;
15import java.util.TreeSet;
16
17import org.eclipse.cdt.core.model.ICElement;
18import org.eclipse.cdt.core.model.ICProject;
19import org.eclipse.core.commands.Command;
20import org.eclipse.core.commands.ExecutionException;
21import org.eclipse.core.resources.IProject;
22import org.eclipse.core.resources.IResource;
23import org.eclipse.core.runtime.IAdaptable;
24import org.eclipse.jface.action.IContributionItem;
25import org.eclipse.jface.viewers.ISelection;
26import org.eclipse.jface.viewers.ITreeSelection;
27import org.eclipse.ui.ISelectionService;
28import org.eclipse.ui.actions.CompoundContributionItem;
29import org.eclipse.ui.commands.ICommandService;
30import org.eclipse.ui.handlers.HandlerUtil;
31import org.eclipse.ui.handlers.RadioState;
32import org.eclipse.ui.menus.CommandContributionItem;
33import org.eclipse.ui.menus.CommandContributionItemParameter;
34import org.eclipse.ui.menus.IWorkbenchContribution;
35import org.eclipse.ui.services.IServiceLocator;
36import org.yocto.sdk.ide.actions.ProfileSwitchHandler;
37import org.yocto.sdk.ide.utils.ProjectPreferenceUtils;
38import org.yocto.sdk.ide.utils.YoctoSDKUtils;
39
40public class TargetProfileContributionItem extends CompoundContributionItem implements IWorkbenchContribution {
41 private IServiceLocator serviceLocator;
42
43 public TargetProfileContributionItem() {}
44
45 public TargetProfileContributionItem(String id) {
46 super(id);
47 }
48
49 protected CommandContributionItem createProfileItem(IServiceLocator serviceLocator,
50 String parameter, String label) {
51 CommandContributionItemParameter itemParameter;
52 itemParameter = new CommandContributionItemParameter(serviceLocator,
53 null,
54 ProfileSwitchHandler.PROFILE_SWITCH_COMMAND,
55 CommandContributionItem.STYLE_RADIO);
56
57 HashMap<String, String> params = new HashMap<String, String>();
58 params.put(RadioState.PARAMETER_ID, parameter);
59
60 itemParameter.label = label;
61 itemParameter.parameters = params;
62
63 return new CommandContributionItem(itemParameter);
64 }
65
66 @Override
67 protected IContributionItem[] getContributionItems() {
68 TreeSet<String> profiles = YoctoSDKUtils.getProfilesFromDefaultStore().getProfiles();
69 ArrayList<IContributionItem> items = new ArrayList<IContributionItem>();
70
71 for (String profile : profiles) {
72 items.add(createProfileItem(serviceLocator, profile, profile));
73 }
74
75 updateSelection(serviceLocator);
76
77 return items.toArray(new IContributionItem[profiles.size()]);
78 }
79
80 public IProject getSelectedProject(IServiceLocator serviceLocator) {
81 ISelectionService selectionService = (ISelectionService) serviceLocator.getService(ISelectionService.class);
82 ISelection selection = selectionService.getSelection();
83
84 if (selection instanceof ITreeSelection) {
85 Object selectedItem = ((ITreeSelection) selection).getFirstElement();
86 if (selectedItem instanceof IResource) {
87 return ((IResource) selectedItem).getProject();
88 } else if (selectedItem instanceof ICElement) {
89 ICProject cProject = ((ICElement) selectedItem).getCProject();
90 if (cProject != null) {
91 return cProject.getProject();
92 }
93 } else if (selectedItem instanceof IAdaptable) {
94 Object projectObject = ((IAdaptable) selectedItem).getAdapter(IProject.class);
95 if (projectObject != null && projectObject instanceof IProject) {
96 return ((IProject) projectObject);
97 }
98 }
99 }
100
101 return null;
102 }
103
104 @Override
105 public void initialize(IServiceLocator serviceLocator) {
106 this.serviceLocator = serviceLocator;
107 }
108
109 protected void updateSelection(IServiceLocator serviceLocator) {
110 ICommandService commandService = (ICommandService) serviceLocator.getService(ICommandService.class);
111 Command command = commandService.getCommand(ProfileSwitchHandler.PROFILE_SWITCH_COMMAND);
112 IProject project = getSelectedProject(serviceLocator);
113
114 try {
115 if (ProjectPreferenceUtils.getUseProjectSpecificOption(project)) {
116 HandlerUtil.updateRadioState(command, ProfileSwitchHandler.PROJECT_SPECIFIC_PARAMETER);
117 return;
118 }
119
120 String selectedProfile = ProjectPreferenceUtils.getProfiles(project).getSelectedProfile();
121 HandlerUtil.updateRadioState(command, selectedProfile);
122 } catch (ExecutionException e) {
123 // ignore
124 }
125 }
126}