Skip to main content
summaryrefslogtreecommitdiffstats
blob: 62d24923f90c0d2bfe494f61724bb96274d14d43 (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
/*******************************************************************************
 * Copyright (c) 2006, 2008 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsf.common.ui.internal.dialogfield;

import org.eclipse.jst.jsf.common.ui.JSFUICommonPlugin;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;

/**
 * Dialog field containing a label, text control and a button control.
 */
public class StringButtonDialogField extends StringDialogField {
	private Button _browseButton;

	private String _browseButtonLabel;

	private IStringButtonAdapter _stringButtonAdapter;

	private boolean _buttonEnabled;

	/**
	 * @param adapter
	 */
	public StringButtonDialogField(IStringButtonAdapter adapter) {
		_stringButtonAdapter = adapter;
		_browseButtonLabel = JSFUICommonPlugin
				.getResourceString("DialogField.Browse"); //$NON-NLS-1$
		_buttonEnabled = true;
	}

	/**
	 * Sets the label of the button.
	 * @param label 
	 */
	public void setButtonLabel(String label) {
		_browseButtonLabel = label;
	}

	// ------ adapter communication

	/**
	 * Programmatical pressing of the button
	 */
	public void changeControlPressed() {
		_stringButtonAdapter.changeControlPressed(this);
	}

	// ------- layout helpers

	/*
	 * @see DialogField#doFillIntoGrid
	 */
	public Control[] doFillIntoGrid(FormToolkit toolkit, Composite parent,
			int nColumns) {
		assertEnoughColumns(nColumns);

		Control requiredLabel = getRequiredLabelControl(toolkit, parent);
		requiredLabel.setLayoutData(gridDataForLabel(1));

		Control label = getLabelControl(toolkit, parent);
		label.setLayoutData(gridDataForLabel(1));

		Text text = getTextControl(toolkit, parent);
		text.setLayoutData(gridDataForText(nColumns - 3));
		Button button = getChangeControl(toolkit, parent);
		button.setLayoutData(gridDataForButton(toolkit, button, text, 1));

		return new Control[] { requiredLabel, label, text, button };
	}

	/*
	 * @see DialogField#getNumberOfControls
	 */
	public int getNumberOfControls() {
		return 4;
	}

	/**
	 * @param toolkit
	 * @param button
	 * @param text
	 * @param span
	 * @return the grid data for the button
	 */
	protected static GridData gridDataForButton(FormToolkit toolkit,
			Button button, Text text, int span) {
		GridData gd = new GridData();
		gd.horizontalAlignment = GridData.FILL;
		gd.grabExcessHorizontalSpace = false;
		gd.horizontalSpan = span;
		// gd.heightHint = SWTUtil.getButtonHeightHint(button);
		gd.widthHint = LayoutUtil.getButtonWidthHint(button);
		gd.heightHint = LayoutUtil.getButtonHeightHint(toolkit, text);
		return gd;
	}

	// ------- ui creation

	/**
	 * Creates or returns the created buttom widget.
	 * @param toolkit 
	 * 
	 * @param parent
	 *            The parent composite or <code>null</code> if the widget has
	 *            already been created.
	 * @return the button
	 */
	public Button getChangeControl(FormToolkit toolkit, Composite parent) {
		if (_browseButton == null || _browseButton.isDisposed()) {
			assertCompositeNotNull(parent);
			if (toolkit != null) {
				_browseButton = toolkit.createButton(parent,
						_browseButtonLabel, SWT.PUSH);
			} else {
				_browseButton = new Button(parent, SWT.PUSH);
				_browseButton.setText(_browseButtonLabel);
			}
			_browseButton.setEnabled(isEnabled() && _buttonEnabled);
			_browseButton.addSelectionListener(new SelectionListener() {
				public void widgetDefaultSelected(SelectionEvent e) {
					changeControlPressed();
				}

				public void widgetSelected(SelectionEvent e) {
					changeControlPressed();
				}
			});

		}
		return _browseButton;
	}

	// ------ enable / disable management

	/**
	 * Sets the enable state of the button.
	 * @param enable 
	 */
	public void enableButton(boolean enable) {
		if (isOkToUse(_browseButton)) {
			_browseButton.setEnabled(isEnabled() && enable);
		}
		_buttonEnabled = enable;
	}

	/*
	 * @see DialogField#updateEnableState
	 */
	protected void updateEnableState() {
		super.updateEnableState();
		if (isOkToUse(_browseButton)) {
			_browseButton.setEnabled(isEnabled() && _buttonEnabled);
		}
	}

	/**
	 * @return Returns the _stringButtonAdapter.
	 */
	public IStringButtonAdapter getStringButtonAdapter() {
		return _stringButtonAdapter;
	}

	/**
	 * @param buttonAdapter
	 *            The _stringButtonAdapter to set.
	 */
	public void setStringButtonAdapter(IStringButtonAdapter buttonAdapter) {
		_stringButtonAdapter = buttonAdapter;
	}
}

Back to the top