Skip to main content
summaryrefslogtreecommitdiffstats
blob: 021ed469d884317da288ef0ee99d63a066743c28 (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
/*******************************************************************************
 * Copyright (c) 2009 Oracle. 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:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.ui.tests.internal.utility.swt;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.jface.window.Window;
import org.eclipse.jpt.ui.internal.utility.swt.SWTTools;
import org.eclipse.jpt.utility.internal.ClassTools;
import org.eclipse.jpt.utility.internal.model.value.SimpleListValueModel;
import org.eclipse.jpt.utility.internal.model.value.SimplePropertyValueModel;
import org.eclipse.jpt.utility.model.value.WritablePropertyValueModel;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
 * Play around with boolean state controllers ('enabled' and 'visible').
 */
@SuppressWarnings("nls")
public class BooleanStateControllerUITest
	extends ApplicationWindow
{
	private final WritablePropertyValueModel<Boolean> enabledHolder;
	private final WritablePropertyValueModel<Boolean> visibleHolder;
	private final SimpleListValueModel<String> listHolder;
	private final WritablePropertyValueModel<String> listSelectionHolder;

	public static void main(String[] args) throws Exception {
		Window window = new BooleanStateControllerUITest(args);
		window.setBlockOnOpen(true);
		window.open();
		Display.getCurrent().dispose();
		System.exit(0);
	}

	private BooleanStateControllerUITest(@SuppressWarnings("unused") String[] args) {
		super(null);
		this.enabledHolder = new SimplePropertyValueModel<Boolean>(Boolean.TRUE);
		this.visibleHolder = new SimplePropertyValueModel<Boolean>(Boolean.TRUE);
		this.listHolder = this.buildListHolder();
		this.listSelectionHolder = new SimplePropertyValueModel<String>(null);
	}

	private SimpleListValueModel<String> buildListHolder() {
		SimpleListValueModel<String> result = new SimpleListValueModel<String>();
		result.add("zero");
		result.add("one");
		result.add("two");
		result.add("three");
		result.add("four");
		result.add("five");
		result.add("six");
		result.add("seven");
		return result;
	}

	@Override
	protected Control createContents(Composite parent) {
		((Shell) parent).setText(ClassTools.shortClassNameForObject(this));
		parent.setSize(500, 100);
		Composite mainPanel = new Composite(parent, SWT.NONE);
		mainPanel.setLayout(new FormLayout());
		Control widgetPanel = this.buildWidgetPanel(mainPanel);
		this.buildControlPanel(mainPanel, widgetPanel);
		return mainPanel;
	}

	private Control buildWidgetPanel(Composite parent) {
		Composite panel = new Composite(parent, SWT.NONE);

		FormData fd = new FormData();
			fd.top = new FormAttachment(0);
			fd.bottom = new FormAttachment(100, -35);
			fd.left = new FormAttachment(0);
			fd.right = new FormAttachment(100);
		panel.setLayoutData(fd);

		panel.setLayout(new FillLayout());

		Button enabledComboBoxCheckBox = this.buildEnabledComboBoxCheckBox(panel);
		SWTTools.bind(this.enabledHolder, enabledComboBoxCheckBox);

		Button visibleComboBoxCheckBox = this.buildVisibleComboBoxCheckBox(panel);
		SWTTools.bind(this.visibleHolder, visibleComboBoxCheckBox);

		Label comboBoxLabel = this.buildComboBoxLabel(panel);
		Combo comboBox = this.buildComboBox(panel);
		SWTTools.bind(this.listHolder, this.listSelectionHolder, comboBox);
		SWTTools.controlEnabledState(this.enabledHolder, comboBoxLabel, comboBox);
		SWTTools.controlVisibleState(this.visibleHolder, comboBoxLabel, comboBox);

		return panel;
	}

	private Button buildEnabledComboBoxCheckBox(Composite parent) {
		Button checkBox = new Button(parent, SWT.CHECK);
		checkBox.setText("enabled");
		return checkBox;
	}

	private Button buildVisibleComboBoxCheckBox(Composite parent) {
		Button checkBox = new Button(parent, SWT.CHECK);
		checkBox.setText("visible");
		return checkBox;
	}

	private Label buildComboBoxLabel(Composite parent) {
		Label label = new Label(parent, SWT.LEFT);
		label.setText("list:");
		return label;
	}

	private Combo buildComboBox(Composite parent) {
		return new Combo(parent, SWT.READ_ONLY);
	}

	private void buildControlPanel(Composite parent, Control widgetPanel) {
		Composite panel = new Composite(parent, SWT.NONE);
		FormData fd = new FormData();
			fd.top = new FormAttachment(widgetPanel);
			fd.bottom = new FormAttachment(100);
			fd.left = new FormAttachment(0);
			fd.right = new FormAttachment(100);
		panel.setLayoutData(fd);

		panel.setLayout(new FillLayout());
		this.buildClearEnabledModelButton(panel);
		this.buildClearVisibleModelButton(panel);
		this.buildNullSelectionModelButton(panel);
		this.buildNextButton(panel);
	}

	private void buildClearEnabledModelButton(Composite parent) {
		this.buildClearEnabledModelACI().fill(parent);
	}

	private ActionContributionItem buildClearEnabledModelACI() {
		Action action = new Action("clear enabled model", IAction.AS_PUSH_BUTTON) {
			@Override
			public void run() {
				BooleanStateControllerUITest.this.clearEnabledModel();
			}
		};
		action.setToolTipText("clear enabled model");
		return new ActionContributionItem(action);
	}

	void clearEnabledModel() {
		this.enabledHolder.setValue(null);
	}

	private void buildClearVisibleModelButton(Composite parent) {
		this.buildClearVisibleModelACI().fill(parent);
	}

	private ActionContributionItem buildClearVisibleModelACI() {
		Action action = new Action("clear visible model", IAction.AS_PUSH_BUTTON) {
			@Override
			public void run() {
				BooleanStateControllerUITest.this.clearVisibleModel();
			}
		};
		action.setToolTipText("clear visible model");
		return new ActionContributionItem(action);
	}

	void clearVisibleModel() {
		this.visibleHolder.setValue(null);
	}

	private void buildNullSelectionModelButton(Composite parent) {
		this.buildNullSelectionModelACI().fill(parent);
	}

	private ActionContributionItem buildNullSelectionModelACI() {
		Action action = new Action("null selection model", IAction.AS_PUSH_BUTTON) {
			@Override
			public void run() {
				BooleanStateControllerUITest.this.setSelectionModelNull();
			}
		};
		action.setToolTipText("null selection model");
		return new ActionContributionItem(action);
	}

	void setSelectionModelNull() {
		this.listSelectionHolder.setValue(null);
	}

	private void buildNextButton(Composite parent) {
		this.buildNextACI().fill(parent);
	}

	private ActionContributionItem buildNextACI() {
		Action action = new Action("next", IAction.AS_PUSH_BUTTON) {
			@Override
			public void run() {
				BooleanStateControllerUITest.this.next();
			}
		};
		action.setToolTipText("next");
		return new ActionContributionItem(action);
	}

	void next() {
		this.listSelectionHolder.setValue(this.getNextListSelection());
	}

	private String getNextListSelection() {
		return this.listHolder.get(this.getNextListSelectionIndex());
	}

	private int getNextListSelectionIndex() {
		int index = this.getListSelectionIndex();
		if (index == -1) {
			return 0;
		}
		index++;
		return (index == this.listHolder.size()) ? 0 : index;
	}

	private int getListSelectionIndex() {
		return this.listHolder.indexOf(this.getListSelection());
	}

	private String getListSelection() {
		return this.listSelectionHolder.getValue();
	}

}

Back to the top