Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a4d7baa12b82b8e508cdaae5c5d7c9d76b4b5584 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*******************************************************************************
 * Copyright (c) 2013 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.controls;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.tcf.te.runtime.services.ServiceManager;
import org.eclipse.tcf.te.runtime.services.interfaces.IService;
import org.eclipse.tcf.te.runtime.services.interfaces.ISimulatorService;
import org.eclipse.tcf.te.runtime.services.interfaces.IUIService;
import org.eclipse.tcf.te.tcf.ui.nls.Messages;
import org.eclipse.tcf.te.tcf.ui.sections.SimulatorTypeSelectionSection;
import org.eclipse.tcf.te.ui.controls.BaseEditBrowseTextControl;
import org.eclipse.tcf.te.ui.interfaces.services.ISimulatorServiceUIDelegate;
import org.eclipse.tcf.te.ui.jface.interfaces.IValidatingContainer;
import org.eclipse.tcf.te.ui.swt.SWTControlUtil;

/**
 * Simulator type selection control implementation.
 */
public class SimulatorTypeSelectionControl extends BaseEditBrowseTextControl {
	private final Map<String, ISimulatorServiceUIDelegate> id2delegate = new HashMap<String, ISimulatorServiceUIDelegate>();
	private final Map<String, String> name2id = new HashMap<String, String>();
	private final Map<String, String> id2name = new HashMap<String, String>();
	private final Map<String, String> id2config = new HashMap<String, String>();

	private final SimulatorTypeSelectionSection parentSection;

	/**
	 * Constructor.
	 *
	 * @param parentSection The parent section.
	 */
	public SimulatorTypeSelectionControl(SimulatorTypeSelectionSection parentSection) {
		super(null);

		Assert.isNotNull(parentSection);
		this.parentSection = parentSection;

		setIsGroup(false);
		setEditFieldLabel(Messages.SimulatorTypeSelectionControl_label);
		setButtonLabel(Messages.SimulatorTypeSelectionControl_button_configure);
		setHasHistory(true);
		setReadOnly(true);
	}

	/**
	 * Initialize the control based on the given context.
	 *
	 * @param context The context or <code>null</code>.
	 */
	public void initialize(Object context) {
		SWTControlUtil.removeAll(getEditFieldControl());
		id2delegate.clear();
		name2id.clear();
		id2name.clear();
		id2config.clear();

		List<String> entries = new ArrayList<String>();

		if (context != null) {
			// Get all simulator services for the given context
			IService[] services = ServiceManager.getInstance().getServices(context, ISimulatorService.class, false);
			for (IService service : services) {
				Assert.isTrue(service instanceof ISimulatorService);
				// Get the UI service which is associated with the simulator service
				IUIService uiService = ServiceManager.getInstance().getService(service, IUIService.class);
				if (uiService == null) {
					continue;
				}
				// Get the simulator service UI delegate
				ISimulatorServiceUIDelegate uiDelegate = uiService.getDelegate(service, ISimulatorServiceUIDelegate.class);
				String id = service.getId();
				String name = uiDelegate != null ? uiDelegate.getName() : id;
				id2delegate.put(id, uiDelegate);
				name2id.put(name, id);
				id2name.put(id, name);
				if (!entries.contains(name)) {
					entries.add(name);
				}
			}
		}

		SWTControlUtil.setItems(getEditFieldControl(), entries.toArray(new String[entries.size()]));
		SWTControlUtil.select(getEditFieldControl(), 0);

		if (getEditFieldControl() != null && getEditFieldControl().getLayoutData() instanceof GridData) {
			doAdjustEditFieldControlLayoutData((GridData)getEditFieldControl().getLayoutData());
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.controls.BaseEditBrowseTextControl#doAdjustEditFieldControlLayoutData(org.eclipse.swt.layout.GridData)
	 */
	@Override
	protected void doAdjustEditFieldControlLayoutData(GridData layoutData) {
		super.doAdjustEditFieldControlLayoutData(layoutData);

		int maxWidth = -1;
		for (String item : SWTControlUtil.getItems(getEditFieldControl())) {
			maxWidth = Math.max(item.length(), maxWidth);
		}

		if (maxWidth != -1) {
			layoutData.horizontalAlignment = GridData.HORIZONTAL_ALIGN_BEGINNING;
			layoutData.grabExcessHorizontalSpace = false;
			layoutData.widthHint = SWTControlUtil.convertWidthInCharsToPixels(getEditFieldControl(), maxWidth + 10);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.controls.BaseDialogPageControl#getValidatingContainer()
	 */
	@Override
	public IValidatingContainer getValidatingContainer() {
		Object container = parentSection.getManagedForm().getContainer();
		return container instanceof IValidatingContainer ? (IValidatingContainer)container : null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.controls.BaseEditBrowseTextControl#onButtonControlSelected()
	 */
	@Override
	protected void onButtonControlSelected() {
		// Get the corresponding simulator service UI delegate
		ISimulatorServiceUIDelegate uiDelegate = id2delegate.get(getSelectedSimulatorId());
		if (uiDelegate != null) {
			String oldConfig = getSimulatorConfig();
			String newConfig = uiDelegate.configure(parentSection.getOriginalData(), oldConfig);
			if ((oldConfig != null && !oldConfig.equals(newConfig)) || (newConfig != null && !newConfig.equals(oldConfig))) {
				id2config.put(getSelectedSimulatorId(), newConfig);
				parentSection.dataChanged(null);
			}
		}
	}

	/**
	 * Set the selected simulator id.
	 * @param id The selected simulator id or <code>null</code>.
	 */
	public void setSelectedSimulatorId(String id) {
		String name = id2name.get(id);
		if (name != null && name.trim().length() > 0) {
			int index = ((Combo)getEditFieldControl()).indexOf(name);
			if (index >= 0) {
				SWTControlUtil.select(getEditFieldControl(), index);
			}
		}
	}

	/**
	 * Get the selected simulator id.
	 * @return The selected simulator id or <code>null</code>.
	 */
	public String getSelectedSimulatorId() {
		return name2id.get(getEditFieldControlText());
	}

	/**
	 * Set the simulator configuration for the selected simulator.
	 *
	 * @param config The simulator configuration or <code>null</code>.
	 */
	public void setSimulatorConfig(String config) {
		id2config.put(getSelectedSimulatorId(), config);
	}

	/**
	 * Get the simulator configuration for the selected simulator.
	 * If no configuration was set, the default configuration or <code>null</code> will be returned.
	 *
	 * @return The simulator configuration or <code>null</code>.
	 */
	public String getSimulatorConfig() {
		String config = id2config.get(getSelectedSimulatorId());
		ISimulatorServiceUIDelegate uiDelegate = id2delegate.get(getSelectedSimulatorId());
		if (uiDelegate != null && (config == null || config.trim().length() == 0)) {
			config = uiDelegate.getService().getDefaultConfig();
		}
		return config;
	}

	private void setConfigureEnabled(boolean enabled) {
		// Get the currently selected simulator type name
		String id = getSelectedSimulatorId();
		// Get the corresponding simulator service UI delegate
		ISimulatorServiceUIDelegate uiDelegate = id2delegate.get(id);
		if (getButtonControl() != null) {
			getButtonControl().setEnabled(enabled && uiDelegate != null && uiDelegate.canConfigure());
			if (getButtonControl().isEnabled() && uiDelegate != null) {
				String config = id2config.get(getSelectedSimulatorId());
				String description = uiDelegate.getDescription(config);
				getButtonControl().setToolTipText(description);
			}
			else {
				getButtonControl().setToolTipText(null);
			}
		}

	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.controls.BaseEditBrowseTextControl#widgetSelected(org.eclipse.swt.events.SelectionEvent)
	 */
	@Override
	public void widgetSelected(SelectionEvent e) {
		super.widgetSelected(e);
		setConfigureEnabled(isEnabled());
		parentSection.dataChanged(e);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.controls.BaseEditBrowseTextControl#setEnabled(boolean)
	 */
	@Override
	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);
		setConfigureEnabled(enabled);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.controls.BaseEditBrowseTextControl#onLabelControlSelectedChanged()
	 */
	@Override
	protected void onLabelControlSelectedChanged() {
		super.onLabelControlSelectedChanged();
		setConfigureEnabled(isLabelControlSelected());
		parentSection.dataChanged(null);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.controls.BaseEditBrowseTextControl#isValid()
	 */
	@Override
	public boolean isValid() {
		boolean valid = super.isValid();

		if (valid) {
			// Get the corresponding simulator service UI delegate
			ISimulatorServiceUIDelegate uiDelegate = id2delegate.get(getSelectedSimulatorId());
			String config = getSimulatorConfig();
			String defaultConfig = uiDelegate != null ? uiDelegate.getService().getDefaultConfig() : null;
			valid = uiDelegate != null ? uiDelegate.getService().isValidConfig(parentSection.getOriginalData(), config) : true;
			boolean isDefaultConfig = (defaultConfig != null && defaultConfig.equals(config)) || (defaultConfig == null && config == null);
			if (!valid) {
				setMessage(Messages.SimulatorTypeSelectionControl_error_invalidConfiguration, isDefaultConfig ? IMessageProvider.INFORMATION : IMessageProvider.ERROR);
			}
		}

		if (getControlDecoration() != null) {
			// Setup and show the control decoration if necessary
			if (isEnabled() && (!valid || (getMessage() != null && getMessageType() != IMessageProvider.NONE))) {
				// Update the control decorator
				updateControlDecoration(getMessage(), getMessageType());
			} else {
				updateControlDecoration(null, IMessageProvider.NONE);
			}
		}

		return valid;
	}
}

Back to the top