Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8f1463a4eedb84eeb7c9abfdd4d480d707b027f7 (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
/*******************************************************************************
 * Copyright (c) 2011 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.forms.parts;

import java.util.Arrays;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.layout.PixelConverter;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.widgets.FormToolkit;

/**
 * Abstract part with buttons implementation.
 */
public abstract class AbstractPartWithButtons extends AbstractPart {
	// The button labels
	private final String[] labels;
	// The buttons list
	private Button[] buttons = null;

	/**
	 * Constructor.
	 *
	 * @param labels The list of label to apply to the created buttons in the given order. Must not be <code>null</code>.
	 */
	public AbstractPartWithButtons(String[] labels) {
		super();
		Assert.isNotNull(labels);
		this.labels = labels;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.forms.parts.AbstractPart#createControl(org.eclipse.swt.widgets.Composite, int, int, org.eclipse.ui.forms.widgets.FormToolkit)
	 */
	@Override
	public void createControl(Composite parent, int style, int span, FormToolkit toolkit) {
		Assert.isNotNull(parent);
		createMainLabel(parent, span, toolkit);
		createMainControl(parent, style, span - 1, toolkit);
		createButtonsPanel(parent, toolkit);
	}

	/**
	 * Creates the part main control(s).
	 *
	 * @param parent The parent composite. Must not be <code>null</code>.
	 * @param style The control style if applicable.
	 * @param span The horizontal span if applicable.
	 * @param toolkit The form toolkit or <code>null</code>.
	 */
	protected abstract void createMainControl(Composite parent, int style, int span, FormToolkit toolkit);

	/**
	 * Create the part main label control.
	 *
	 * @param parent The parent composite. Must not be <code>null</code>.
	 * @param span The horizontal span if applicable.
	 * @param toolkit The form toolkit or <code>null</code>.
	 */
	protected void createMainLabel(Composite parent, int span, FormToolkit toolkit) {
		Assert.isNotNull(parent);
	}

	/**
	 * Create the buttons panel.
	 *
	 * @param parent The parent composite. Must not be <code>null</code>.
	 * @param toolkit The form toolkit or <code>null</code>.
	 *
	 * @return The buttons panel composite or <code>null</code>.
	 */
	protected Composite createButtonsPanel(Composite parent, FormToolkit toolkit) {
		if (labels.length == 0) {
			return null;
		}

		buttons = new Button[labels.length];

		Composite panel = createComposite(parent, toolkit);
		GridLayout layout = new GridLayout();
		layout.marginHeight = 0; layout.marginWidth = 0;
		panel.setLayout(layout);
		GridData layoutData = new GridData(SWT.BEGINNING, SWT.FILL, false, true);
		panel.setLayoutData(layoutData);
		panel.setBackground(parent.getBackground());

		SelectionListener listener = new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				AbstractPartWithButtons.this.onButtonSelected((Button) e.widget);
			}
		};

		for (int i = 0; i < labels.length; i++) {
			if (labels[i] != null) {
				Button button = toolkit != null ? toolkit.createButton(panel, null, SWT.PUSH) : new Button(panel, SWT.PUSH);
				Assert.isNotNull(button);

				button.setFont(JFaceResources.getDialogFont());
				button.setText(labels[i]);
				button.setData(Integer.valueOf(i));
				button.setBackground(panel.getBackground());
				button.addSelectionListener(listener);

				onButtonCreated(button);

				layoutData = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
				layoutData.widthHint = Math.max(new PixelConverter(button).convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH), button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
				button.setLayoutData(layoutData);

				buttons[i] = button;
			} else {
				createEmptySpace(panel, 1, toolkit);
			}
		}

		return panel;
	}

	/**
	 * Called from {@link #createButtonsPanel(Composite, FormToolkit)} for each created button.
	 * <p>
	 * <b>Note:</b> The button layout data is set by {@link #createButtonsPanel(Composite, FormToolkit)}
	 *              after this method has been returned.
	 *
	 * @param button The created button. Must not be <code>null</code>.
	 */
	protected void onButtonCreated(Button button) {
		Assert.isNotNull(button);
	}

	/**
	 * Called from the buttons selection listener to signal when
	 * the user clicked on the button.
	 *
	 * @param button The button selected. Must not be <code>null</code>
	 */
	protected void onButtonSelected(Button button) {
		Assert.isNotNull(button);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.forms.parts.AbstractPart#onEnabledStateChanged()
	 */
	@Override
	protected void onEnabledStateChanged() {
		for (Button button : buttons) {
			if (button != null && !button.isDisposed()) {
				button.setEnabled(isEnabled());
			}
		}
	}

	/**
	 * Returns the button with the given label.
	 *
	 * @param label The button label.
	 * @return The button.
	 *
	 * @throws ArrayIndexOutOfBoundsException if the label is invalid.
	 */
	public Button getButton(String label) {
		return getButton(Arrays.asList(labels).indexOf(label));
	}

	/**
	 * Returns the button at the given index.
	 *
	 * @param index The index.
	 * @return The button.
	 *
	 * @throws ArrayIndexOutOfBoundsException if the index is out of bounds.
	 */
	public Button getButton(int index) {
		return buttons[index];
	}
}

Back to the top