Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ca3da3f88cf0f2a86801419b7e6c8c5916b7a831 (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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*******************************************************************************
 * Copyright (c) 2011, 2012 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.tcf.ui.tabbed;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
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.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;
import org.eclipse.tcf.te.core.interfaces.IPropertyChangeProvider;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModel;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModelProvider;
import org.eclipse.tcf.te.ui.swt.SWTControlUtil;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;

/**
 * The base section that displays a title in a title bar.
 */
public abstract class BaseTitledSection extends AbstractPropertySection implements PropertyChangeListener {
	// The section
	protected Section section;

	// The main composite used to create the section content.
	protected Composite composite;

	protected IPropertyChangeProvider viewerInput;

	// The input node.
	protected IPeerModelProvider provider;

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#setInput(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
	 */
	@Override
    public void setInput(IWorkbenchPart part, ISelection selection) {
        super.setInput(part, selection);
		if (this.viewerInput != null) {
			this.viewerInput.removePropertyChangeListener(this);
		}
        Assert.isTrue(selection instanceof IStructuredSelection);
        Object input = ((IStructuredSelection) selection).getFirstElement();

        IPeerModelProvider provider = input instanceof IPeerModelProvider ? (IPeerModelProvider) input : null;
        if (provider == null) provider = input instanceof IAdaptable ? (IPeerModelProvider)((IAdaptable)input).getAdapter(IPeerModelProvider.class) : null;
        if (provider == null) provider = (IPeerModelProvider)Platform.getAdapterManager().getAdapter(input, IPeerModelProvider.class);

		if (provider != null) {
			this.provider = provider;
			IPeerModel peerNode = getPeerModel(provider);
			this.viewerInput = (IPropertyChangeProvider) peerNode.getAdapter(IPropertyChangeProvider.class);
			if (this.viewerInput != null) {
				this.viewerInput.addPropertyChangeListener(this);
			}
		} else {
			this.provider = null;
			this.viewerInput = null;
		}
		updateInput(provider);
    }

	/**
	 * Get the peer model from the provider.
	 * Needs to be overwritten in case of save thread access.
	 * @param provider
	 * @return
	 */
	protected IPeerModel getPeerModel(IPeerModelProvider provider) {
		Assert.isNotNull(provider);
		return provider.getPeerModel();
	}

	/**
	 * Update the input node.
	 *
	 * @param input The input node.
	 */
	protected void updateInput(IPeerModelProvider input) {
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#aboutToBeHidden()
	 */
	@Override
    public void aboutToBeHidden() {
		if(this.viewerInput != null) {
			this.viewerInput.removePropertyChangeListener(this);
		}
    }

	/**
	 * Returns the standard label width of the properties section.
	 *
	 * @return The standard label width.
	 */
	protected int getStandardLabelWidth() {
		return STANDARD_LABEL_WIDTH;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#createControls(org.eclipse.swt.widgets.Composite, org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage)
	 */
	@Override
    public void createControls(Composite parent, TabbedPropertySheetPage aTabbedPropertySheetPage) {
		super.createControls(parent, aTabbedPropertySheetPage);
		parent.setLayout(new FormLayout());

		section = getWidgetFactory().createSection(parent, ExpandableComposite.TITLE_BAR);
		section.setText(getText());
		FormData data = new FormData();
		data.left = new FormAttachment(0, ITabbedPropertyConstants.HMARGIN);
		data.right = new FormAttachment(100, -ITabbedPropertyConstants.HMARGIN);
		data.top = new FormAttachment(0, 2 * ITabbedPropertyConstants.VSPACE);
		section.setLayoutData(data);

		composite = getWidgetFactory().createComposite(parent);
		FormLayout layout = new FormLayout();
		layout.spacing = ITabbedPropertyConstants.HMARGIN;
		composite.setLayout(layout);

		data = new FormData();
		data.left = new FormAttachment(0, 2 * ITabbedPropertyConstants.HMARGIN);
		data.right = new FormAttachment(100, -2 * ITabbedPropertyConstants.HMARGIN);
		data.top = new FormAttachment(section, ITabbedPropertyConstants.VSPACE);
		data.bottom = new FormAttachment(100, 0);
		composite.setLayoutData(data);
	}

	/**
	 * Create a label for the control using the specified text.
	 *
	 * @param control The control for which the label is created.
	 * @param text The label text. Must not be <code>null</code>.
	 */
	protected CLabel createLabel(Control control, String text) {
		Assert.isNotNull(control);
		Assert.isNotNull(text);

		CLabel nameLabel = getWidgetFactory().createCLabel(composite, text);
		FormData data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(control, -ITabbedPropertyConstants.HSPACE);
		data.top = new FormAttachment(control, 0, SWT.CENTER);
		data.width = SWTControlUtil.convertWidthInCharsToPixels(nameLabel, text.length() + 2);
		nameLabel.setLayoutData(data);
		return nameLabel;
	}

	/**
	 * Create a text field and a label with the specified label
	 * relative to the specified control.
	 *
	 * @param control The control relative to.
	 * @param label The text of the label.
	 * @return The new text created.
	 */
	protected Text createTextField(Control control, String label) {
		Text text = createText(control);
		createLabel(text, label);
		return text;
	}

	/**
	 * Create a checkbox with the specified label
	 * relative to the specified control.
	 *
	 * @param control The control relative to.
	 * @param label The text of the label.
	 * @return The new checkbox created.
	 */
	protected Button createCheckbox(Control control, String label) {
		Button check = getWidgetFactory().createButton(composite, label, SWT.CHECK);
		FormData data = new FormData();
		data.left = new FormAttachment(0, getStandardLabelWidth());
		data.right = new FormAttachment(100, 0);
		if (control == null) {
			data.top = new FormAttachment(0, ITabbedPropertyConstants.VSPACE);
		}
		else {
			data.top = new FormAttachment(control, ITabbedPropertyConstants.VSPACE);
		}
		check.setLayoutData(data);
		check.setEnabled(false);
		return check;
	}

	/**
	 * Create a wrap text field and a label with the specified label
	 * relative to the specified control.
	 *
	 * @param control The control relative to.
	 * @param label The text of the label.
	 * @return The new wrap text created.
	 */
	protected Text createWrapTextField(Control control, String label) {
		Text text = createWrapText(control);
		createLabel(text, label);
		return text;
	}

	/**
	 * Create a text field relative to the specified control.
	 *
	 * @param control The control to layout the new text field.
	 * @return The new text field created.
	 */
	protected Text createText(Control control) {
		Text text = getWidgetFactory().createText(composite, "", SWT.SINGLE | SWT.NO_FOCUS); //$NON-NLS-1$
		FormData data = new FormData();
		data.left = new FormAttachment(0, getStandardLabelWidth());
		data.right = new FormAttachment(100, 0);
		if (control == null) {
			data.top = new FormAttachment(0, ITabbedPropertyConstants.VSPACE);
		}
		else {
			data.top = new FormAttachment(control, ITabbedPropertyConstants.VSPACE);
		}
		text.setLayoutData(data);
		text.setEditable(false);
		return text;
	}

	/**
	 * Create a wrap text field relative to the specified control.
	 *
	 * @param control The control to layout the new wrap text field.
	 * @return The new wrap text field created.
	 */
	protected Text createWrapText(Control control) {
		Text text = getWidgetFactory().createText(composite, "", SWT.WRAP | SWT.NO_FOCUS); //$NON-NLS-1$
		FormData data = new FormData();
		data.left = new FormAttachment(0, getStandardLabelWidth());
		data.right = new FormAttachment(100, 0);
		if (control == null) {
			data.top = new FormAttachment(0, ITabbedPropertyConstants.VSPACE);
		}
		else {
			data.top = new FormAttachment(control, ITabbedPropertyConstants.VSPACE);
		}
		data.width = 200;
		text.setLayoutData(data);
		text.setEditable(false);
		return text;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#refresh()
	 */
	@Override
    public void refresh() {
		if (composite != null) {
			composite.layout();
		}
	}

	/*
	 * (non-Javadoc)
	 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
	 */
	@Override
    public void propertyChange(PropertyChangeEvent event) {
		if (event.getSource() == provider) {
			updateInput(provider);
			Display display = getPart().getSite().getShell().getDisplay();
			display.asyncExec(new Runnable() {
				@Override
				public void run() {
					refresh();
				}
			});
		}
    }

	/**
	 * Get the text which is used as the title in the title bar of the section.
	 *
	 * @return A text string representing the section.
	 */
	protected abstract String getText();
}

Back to the top