summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/TargetProfileContributionItem.java
blob: 95d8229ebf290936a0b223bbb2a473a9c3ffcb77 (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
/*******************************************************************************
 * 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;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.TreeSet;

import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.core.commands.Command;
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.jface.action.IContributionItem;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.actions.CompoundContributionItem;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.handlers.RadioState;
import org.eclipse.ui.menus.CommandContributionItem;
import org.eclipse.ui.menus.CommandContributionItemParameter;
import org.eclipse.ui.menus.IWorkbenchContribution;
import org.eclipse.ui.services.IServiceLocator;
import org.yocto.sdk.ide.actions.ProfileSwitchHandler;
import org.yocto.sdk.ide.utils.ProjectPreferenceUtils;
import org.yocto.sdk.ide.utils.YoctoSDKUtils;

public class TargetProfileContributionItem extends CompoundContributionItem implements IWorkbenchContribution {
	private IServiceLocator serviceLocator;

	public TargetProfileContributionItem() {}

	public TargetProfileContributionItem(String id) {
		super(id);
	}

	protected CommandContributionItem createProfileItem(IServiceLocator serviceLocator,
														String parameter, String label) {
		CommandContributionItemParameter itemParameter;
		itemParameter = new CommandContributionItemParameter(serviceLocator,
														null,
														ProfileSwitchHandler.PROFILE_SWITCH_COMMAND,
														CommandContributionItem.STYLE_RADIO);

		HashMap<String, String> params = new HashMap<String, String>();
		params.put(RadioState.PARAMETER_ID, parameter);

		itemParameter.label = label;
		itemParameter.parameters = params;

		return new CommandContributionItem(itemParameter);
	}

	@Override
	protected IContributionItem[] getContributionItems() {
		TreeSet<String> profiles = YoctoSDKUtils.getProfilesFromDefaultStore().getProfiles();
		ArrayList<IContributionItem> items = new ArrayList<IContributionItem>();

		for (String profile : profiles) {
			items.add(createProfileItem(serviceLocator, profile, profile));
		}

		updateSelection(serviceLocator);

		return items.toArray(new IContributionItem[profiles.size()]);
	}

	public IProject getSelectedProject(IServiceLocator serviceLocator) {
		ISelectionService selectionService = (ISelectionService) serviceLocator.getService(ISelectionService.class);
		ISelection selection = selectionService.getSelection();

		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;
	}

	@Override
	public void initialize(IServiceLocator serviceLocator) {
		this.serviceLocator = serviceLocator;
	}

	protected void updateSelection(IServiceLocator serviceLocator) {
		ICommandService commandService = (ICommandService) serviceLocator.getService(ICommandService.class);
		Command command = commandService.getCommand(ProfileSwitchHandler.PROFILE_SWITCH_COMMAND);
		IProject project = getSelectedProject(serviceLocator);

		try {
			if (ProjectPreferenceUtils.getUseProjectSpecificOption(project)) {
				HandlerUtil.updateRadioState(command, ProfileSwitchHandler.PROJECT_SPECIFIC_PARAMETER);
				return;
			}

			String selectedProfile = ProjectPreferenceUtils.getProfiles(project).getSelectedProfile();
			HandlerUtil.updateRadioState(command, selectedProfile);
		} catch (ExecutionException e) {
			// ignore
		}
	}
}