Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d8f166f15e8ba9fb5788be048c5874384cde2b8d (plain) (blame)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*******************************************************************************
 * Copyright (c) 2013, 2014 Wind River Systems, Inc. and others. 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.tcf.ui.handler;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer;
import org.eclipse.tcf.te.runtime.persistence.history.HistoryManager;
import org.eclipse.tcf.te.runtime.persistence.utils.DataHelper;
import org.eclipse.tcf.te.runtime.properties.PropertiesContainer;
import org.eclipse.tcf.te.runtime.services.ServiceManager;
import org.eclipse.tcf.te.runtime.services.interfaces.IService;
import org.eclipse.tcf.te.runtime.stepper.interfaces.IStepperOperationService;
import org.eclipse.tcf.te.tcf.core.interfaces.IContextDataProperties;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerNode;
import org.eclipse.tcf.te.tcf.locator.interfaces.services.IDefaultContextService;
import org.eclipse.tcf.te.tcf.locator.services.selection.RuntimeServiceContextFilter;
import org.eclipse.tcf.te.tcf.locator.utils.PeerNodeDataHelper;
import org.eclipse.tcf.te.ui.handler.AbstractStepperCommandHandler;
import org.eclipse.tcf.te.ui.interfaces.IDataExchangeDialog;
import org.eclipse.tcf.te.ui.views.interfaces.IUIConstants;
import org.eclipse.ui.handlers.HandlerUtil;

/**
 * Abstract stepper command handler for load/unload.
 */
public abstract class AbstractContextStepperCommandHandler extends AbstractStepperCommandHandler {

	protected void calculateSelectionData(ExecutionEvent event, IPropertiesContainer data) {

		setContextList(event, data);

		List<IStructuredSelection> selections = getSelections(event);

		for (IStructuredSelection selection : selections) {
			if (selection != null) {
				addSelection(selection, data);
			}
		}
	}

	protected abstract String[] getServices();

	protected abstract void addSelection(IStructuredSelection selection, IPropertiesContainer data);

	protected boolean doShowDisconnectedContexts() {
		return false;
	}

	protected IPropertiesContainer getDataFromHistory(ExecutionEvent event) {
		IPeerNode context = getContext(event);
		if (context != null) {
			IService[] stepperOpServices = ServiceManager.getInstance().getServices(context, IStepperOperationService.class, false);
			for (IService stepperOpService : stepperOpServices) {
				if (stepperOpService instanceof IStepperOperationService && ((IStepperOperationService)stepperOpService).isHandledOperation(context, operation)) {
					String groupId = ((IStepperOperationService)stepperOpService).getStepGroupId(context, operation);
					if (groupId != null) {
						String history = HistoryManager.getInstance().getFirst(groupId + "@" + context.getPeerId()); //$NON-NLS-1$
						if (history != null) {
							return DataHelper.decodePropertiesContainer(history);
						}
					}
				}
			}
		}

		return getDefaultData();
	}

	protected IPropertiesContainer getDefaultData() {
		return new PropertiesContainer();
	}

	protected IPeerNode getContext(ExecutionEvent event) {
		IDefaultContextService selService = ServiceManager.getInstance().getService(IDefaultContextService.class);
		if (selService != null) {
			IPeerNode[] peerModels = selService.getCandidates(getSelection(event),
							new RuntimeServiceContextFilter(getServices(), doShowDisconnectedContexts()));
			if (peerModels.length > 0) {
				return peerModels[0];
			}
		}
		return null;
	}

	protected void setContextList(ExecutionEvent event, IPropertiesContainer data) {
		IPeerNode context = getContext(event);
		if (context != null) {
			data.setProperty(IContextDataProperties.PROPERTY_CONTEXT_LIST, PeerNodeDataHelper.encodeContextList(new IPeerNode[]{context}));
		}
	}

	protected String getContextName(IPropertiesContainer data) {
		String encoded = data.getStringProperty(IContextDataProperties.PROPERTY_CONTEXT_LIST);
		IPeerNode[] contexts = PeerNodeDataHelper.decodeContextList(encoded);
		if (contexts != null && contexts.length > 0) {
			return contexts[0].getName();
		}
		return null;
	}

	protected abstract IDataExchangeDialog getDialog(ExecutionEvent event, IPropertiesContainer data);

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.handler.AbstractStepperCommandHandler#getData(org.eclipse.core.commands.ExecutionEvent)
	 */
	@Override
	protected final IPropertiesContainer getData(ExecutionEvent event) {

		IPropertiesContainer data = getDataFromHistory(event);
		calculateSelectionData(event, data);

		IDataExchangeDialog dialog = getDialog(event, data);

		if (dialog != null) {
			dialog.setupData(data);
			if (dialog.open() == Window.OK) {
				String contextList = data.getStringProperty(IContextDataProperties.PROPERTY_CONTEXT_LIST);
				data = new PropertiesContainer();
				dialog.extractData(data);
				if (!data.containsKey(IContextDataProperties.PROPERTY_CONTEXT_LIST)) {
					data.setProperty(IContextDataProperties.PROPERTY_CONTEXT_LIST, contextList);
				}
				return data;
			}
			return null;
		}

		return data;
	}


	protected List<IStructuredSelection> getSelections(ExecutionEvent event) {
		List<IStructuredSelection> selections = new ArrayList<IStructuredSelection>();
		String partId = HandlerUtil.getActivePartId(event);
		selections.add(getSelection(event));
		if (!PART_ID_PROJECT_VIEW.equals(partId)) {
			selections.add(getPartSelection(PART_ID_PROJECT_VIEW));
		}
		if (!IUIConstants.ID_EXPLORER.equals(partId)) {
			selections.add(getEditorInputSelection());
			selections.add(getPartSelection(IUIConstants.ID_EXPLORER));
		}

		return selections;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.handler.AbstractStepperCommandHandler#getContext(org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer)
	 */
	@Override
	protected Object getContext(IPropertiesContainer data) {
		IPeerNode[] contexts = PeerNodeDataHelper.decodeContextList(data.getStringProperty(IContextDataProperties.PROPERTY_CONTEXT_LIST));
		data.setProperty(IContextDataProperties.PROPERTY_CONTEXT_LIST, null);
		return contexts.length == 1 ? contexts[0] : null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.handler.AbstractStepperCommandHandler#cleanupData(org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer)
	 */
	@Override
	protected IPropertiesContainer cleanupData(IPropertiesContainer data) {
		data = super.cleanupData(data);
		data.setProperty(IContextDataProperties.PROPERTY_CONTEXT_LIST, null);
		return data;
	}
}

Back to the top