Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 884b6af580cfe40c48741f7ea0b879dc80108c41 (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
/*******************************************************************************
 * Copyright (c) 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.ui.controls.wire;

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

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.tcf.te.core.nodes.interfaces.wire.IWireTypeNetwork;
import org.eclipse.tcf.te.core.nodes.interfaces.wire.IWireTypeSerial;
import org.eclipse.tcf.te.ui.controls.BaseEditBrowseTextControl;
import org.eclipse.tcf.te.ui.controls.nls.Messages;
import org.eclipse.tcf.te.ui.swt.SWTControlUtil;

/**
 * Wire type control implementation.
 */
public class WireTypeControl extends BaseEditBrowseTextControl {

	private final static String[] WIRE_TYPES = new String[] {
										IWireTypeNetwork.PROPERTY_CONTAINER_NAME,
										IWireTypeSerial.PROPERTY_CONTAINER_NAME
									};

	/**
	 * Constructor.
	 *
	 * @param parentPage The parent dialog page this control is embedded in.
	 *                   Might be <code>null</code> if the control is not associated with a page.
	 */
	public WireTypeControl(IDialogPage parentPage) {
		super(parentPage);
		setIsGroup(false);
		setReadOnly(true);
		setHideBrowseButton(true);
		setEditFieldLabel(Messages.WireTypeControl_label);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.controls.BaseEditBrowseTextControl#setupPanel(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void setupPanel(Composite parent) {
		super.setupPanel(parent);

		List<String> wireTypeLabels = new ArrayList<String>();
		for (String wireType : WIRE_TYPES) {
			String label = getWireTypeLabel(wireType);
			if (label != null) wireTypeLabels.add(label);
		}

		setEditFieldControlHistory(wireTypeLabels.toArray(new String[wireTypeLabels.size()]));
		SWTControlUtil.select(getEditFieldControl(), 0);
	}

	/**
	 * Returns the list of supported wire types.
	 *
	 * @return The list of supported wire types.
	 */
	public static final String[] getSupportedWireTypes() {
		return Arrays.copyOf(WIRE_TYPES, WIRE_TYPES.length);
	}

	/**
	 * Returns the label of the given wire type.
	 *
	 * @param wireType The wire type. Must not be <code>null</code>.
	 * @return The corresponding label or <code>null</code> if the wire type is unknown.
	 */
	protected String getWireTypeLabel(String wireType) {
		Assert.isNotNull(wireType);

		if (IWireTypeNetwork.PROPERTY_CONTAINER_NAME.equals(wireType)) return Messages.WireTypeControl_networkType_label;
		if (IWireTypeSerial.PROPERTY_CONTAINER_NAME.equals(wireType)) return Messages.WireTypeControl_serialType_label;

		return null;
	}

	/**
	 * Returns the currently selected wire type.
	 *
	 * @return The currently selected wire type.
	 */
	public String getSelectedWireType() {
		String type = getEditFieldControlText();

		if (Messages.WireTypeControl_networkType_label.equals(type)) type = IWireTypeNetwork.PROPERTY_CONTAINER_NAME;
		else if (Messages.WireTypeControl_serialType_label.equals(type)) type = IWireTypeSerial.PROPERTY_CONTAINER_NAME;

		return type;
	}

	/**
	 * Sets the selected wire type to the specified one.
	 *
	 * @param wireType The wire type. Must not be <code>null</code>.
	 */
	public void setSelectedWireType(String wireType) {
		Assert.isNotNull(wireType);

		// Get the wire type label for given wire type
		String label = getWireTypeLabel(wireType);
		int index = SWTControlUtil.indexOf(getEditFieldControl(), label);
		if (index != -1) SWTControlUtil.select(getEditFieldControl(), index);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.controls.BaseEditBrowseTextControl#doRestoreWidgetValues(org.eclipse.jface.dialogs.IDialogSettings, java.lang.String)
	 */
	@Override
	public void doRestoreWidgetValues(IDialogSettings settings, String idPrefix) {
		// The widget is not user editable and the history is used
		// for presenting the available wire types. Neither save
		// or restore the history actively.
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.controls.BaseEditBrowseTextControl#doSaveWidgetValues(org.eclipse.jface.dialogs.IDialogSettings, java.lang.String)
	 */
	@Override
	public void doSaveWidgetValues(IDialogSettings settings, String idPrefix) {
		// The widget is not user editable and the history is used
		// for presenting the available wire types. Neither save
		// or restore the history actively.
	}
}

Back to the top