Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e1b90b2a3ae33fb69acd1073b792b8df00138aed (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
/*******************************************************************************
 *  Copyright (c) 2009, 2016 QNX Software Systems 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:
 *      QNX Software Systems - initial API and implementation
 *      Freescale Semiconductor
 *******************************************************************************/
package org.eclipse.debug.internal.ui.groups;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;

/**
 * Stack Composite - Switch between panes controlled by combo box
 * <p>
 * Copied from CDT (org.eclipse.cdt.launch)
 */
class ComboControlledStackComposite extends Composite {
	private Composite fArea;
	private Combo fCombo;
	private Map<String, Composite> tabMap; // label ==> tab
	private Map<String, String> capMap = new TreeMap<>();
	private StackLayout layout;
	private Label fLabel;

	public ComboControlledStackComposite(Composite parent, int style) {
		super(parent, style);
		tabMap = new LinkedHashMap<String, Composite>();
		setLayout(new GridLayout(2, false));
		createContents(this);
	}

	public void setLabelText(String label) {
		fLabel.setText(label);
	}

	private static String capitalize(String l) {
		return l.substring(0, 1).toUpperCase() + l.substring(1);
	}

	public void addItem(String label, Composite tab) {
		tabMap.put(label, tab);
		String cap = capitalize(label);
		fCombo.add(cap);
		capMap.put(cap, label);
		if (layout.topControl==null) {
			layout.topControl = tab;
			fCombo.setText(cap);
		}
	}

	public void deleteItem(String label) {
		if (capMap.get(fCombo.getText()).equals(label)) {
			setSelection(fCombo.getItem(0));
		}
		Composite tab = tabMap.get(label);
		if (tab != null) {
			tab.dispose();
			tabMap.remove(label);
			capMap.remove(capitalize(label));
		}
	}

	public void setSelection(String label) {
		fCombo.setText(capitalize(label));
		setPage(label);
	}

	protected void createContents(Composite parent) {
		fLabel = createLabel(this);
		fCombo = createCombo(this);
		GridData cgd = new GridData(GridData.FILL_HORIZONTAL);

		fCombo.setLayoutData(cgd);
		fArea = createTabArea(this);
		GridData agd = new GridData(GridData.FILL_BOTH);
		agd.horizontalSpan = 2;
		fArea.setLayoutData(agd);
	}


	public Composite getStackParent() {
		return fArea;
	}

	public Label getLabel() {
		return fLabel;
	}

	/**
	 * @return the underlying combo, should NOT be used to get the actual text,
	 *         use {@link #getSelection()} instead.
	 */
	public Combo getCombo() {
		return fCombo;
	}

	public String getSelection() {
		return capMap.get(fCombo.getText());
	}

	protected Composite createTabArea(Composite parent) {
		Composite comp = new Composite(parent, SWT.NONE);
		layout = new StackLayout();
		comp.setLayout(layout);

		return comp;
	}


	protected Label createLabel(Composite parent) {
		Label label = new Label(parent, SWT.WRAP);
	    return label;
    }

	protected Combo createCombo(Composite parent) {
		Combo box = new Combo(parent, SWT.READ_ONLY);
		box.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String name = fCombo.getText();
				comboSelected(name);
			}
		});
		return box;
	}

	protected void comboSelected(String label) {
		setPage(capMap.get(label));
	}

	protected void setPage(String label) {
		layout.topControl = tabMap.get(label);
		getStackParent().layout();
	}

	public Control getTopControl() {
		return layout != null ? layout.topControl : null;
	}
}

Back to the top