Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3f2296a9397897113a29be2f70c36c78da24e2f4 (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
/*******************************************************************************
 * 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.editor.sections;

import java.util.concurrent.atomic.AtomicBoolean;

import org.eclipse.core.runtime.Assert;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Text;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer;
import org.eclipse.tcf.te.runtime.properties.PropertiesContainer;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerNode;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerNodeProperties;
import org.eclipse.tcf.te.tcf.locator.interfaces.services.IPeerModelQueryService;
import org.eclipse.tcf.te.tcf.ui.nls.Messages;
import org.eclipse.tcf.te.ui.forms.parts.AbstractSection;
import org.eclipse.tcf.te.ui.swt.DisplayUtil;
import org.eclipse.tcf.te.ui.swt.SWTControlUtil;
import org.eclipse.tcf.te.ui.views.editor.pages.AbstractEditorPage;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

/**
 * Peer services section implementation.
 */
public class ServicesSection extends AbstractSection {
	// The section sub controls
	/* default */ Text local;
	/* default */ Text remote;

	// Reference to the original data object
	/* default */ IPeerNode od;
	// Reference to a copy of the original data
	/* default */ final IPropertiesContainer odc = new PropertiesContainer();

	/**
	 * Constructor.
	 *
	 * @param form The parent managed form. Must not be <code>null</code>.
	 * @param parent The parent composite. Must not be <code>null</code>.
	 */
	public ServicesSection(IManagedForm form, Composite parent) {
		super(form, parent, Section.DESCRIPTION | ExpandableComposite.TWISTIE);
		createClient(getSection(), form.getToolkit());
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.forms.parts.AbstractSection#createClient(org.eclipse.ui.forms.widgets.Section, org.eclipse.ui.forms.widgets.FormToolkit)
	 */
	@Override
	protected void createClient(Section section, FormToolkit toolkit) {
		Assert.isNotNull(section);
		Assert.isNotNull(toolkit);

		// Configure the section
		section.setText(Messages.ServicesSection_title);
		section.setDescription(Messages.ServicesSection_description);

		if (section.getParent().getLayout() instanceof GridLayout) {
			section.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		}

		// Create the section client
		Composite client = createClientContainer(section, 1, toolkit);
		Assert.isNotNull(client);
		section.setClient(client);

		Group group = new Group(client, SWT.NONE);
		group.setText(Messages.ServicesSection_group_local_title);
		group.setLayout(new GridLayout());
		group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		local = new Text(group, SWT.READ_ONLY | SWT.WRAP | SWT.MULTI);
		GridData layoutData = new GridData(SWT.FILL, SWT.CENTER, true, false);
		layoutData.widthHint = SWTControlUtil.convertWidthInCharsToPixels(local, 20);
		layoutData.heightHint = SWTControlUtil.convertHeightInCharsToPixels(local, 5);
		local.setLayoutData(layoutData);

		group = new Group(client, SWT.NONE);
		group.setText(Messages.ServicesSection_group_remote_title);
		group.setLayout(new GridLayout());
		group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		remote = new Text(group, SWT.READ_ONLY | SWT.WRAP | SWT.MULTI);
		layoutData = new GridData(SWT.FILL, SWT.CENTER, true, false);
		layoutData.widthHint = SWTControlUtil.convertWidthInCharsToPixels(local, 20);
		layoutData.heightHint = SWTControlUtil.convertHeightInCharsToPixels(remote, 5);
		remote.setLayoutData(layoutData);

		// Mark the control update as completed now
		setIsUpdating(false);
	}

	/**
	 * Indicates whether the sections parent page has become the active in the editor.
	 *
	 * @param active <code>True</code> if the parent page should be visible, <code>false</code> otherwise.
	 */
	public void setActive(boolean active) {
		// If the parent page has become the active and it does not contain
		// unsaved data, than fill in the data from the selected node
		if (active) {
			// Leave everything unchanged if the page is in dirty state
			if (getManagedForm().getContainer() instanceof AbstractEditorPage
					&& !((AbstractEditorPage)getManagedForm().getContainer()).isDirty()) {
				Object node = ((AbstractEditorPage)getManagedForm().getContainer()).getEditorInputNode();
				if (node instanceof IPeerNode) {
					setupData((IPeerNode)node);
				}
			}
		}
	}

	// Flag to mark that the services query had been done for the current peer model node
	private boolean servicesQueryTriggered = false;

	/**
	 * Initialize the page widgets based of the data from the given peer node.
	 * <p>
	 * This method may called multiple times during the lifetime of the page and
	 * the given configuration node might be even <code>null</code>.
	 *
	 * @param node The peer node or <code>null</code>.
	 */
	public void setupData(final IPeerNode node) {
		// Reset the services query triggered flag if we setup for a new peer model node
		if (od != node) servicesQueryTriggered = false;

		// Besides the node itself, we need to look at the node data to determine
		// if the widgets needs to be updated. For the comparisation, keep the
		// current properties of the original data copy in a temporary container.
		final IPropertiesContainer previousOdc = new PropertiesContainer();
		previousOdc.setProperties(odc.getProperties());

		// Store a reference to the original data
		od = node;
		// Clean the original data copy
		odc.clearProperties();

		// If no data is available, we are done
		if (node == null) return;

		// Thread access to the model is limited to the executors thread.
		// Copy the data over to the working copy to ease the access.
		Protocol.invokeAndWait(new Runnable() {
			@Override
			public void run() {
				// Copy over the properties
				odc.setProperties(od.getProperties());
			}
		});

		boolean forceQuery = false;

		// If the original data copy does not match the previous original
		// data copy, the services needs to be queried again.
		if (!previousOdc.getProperties().equals(odc.getProperties())) {
			servicesQueryTriggered = false;
			forceQuery = true;
		}

		// Create the UI runnable
		final AtomicBoolean fireRefreshTabs = new AtomicBoolean();
		final Runnable uiRunnable = new Runnable() {

			@Override
			public void run() {
				boolean fireNotification = fireRefreshTabs.get();

				String value = odc.getStringProperty(IPeerNodeProperties.PROP_LOCAL_SERVICES);
				fireNotification |= value != null && !value.equals(SWTControlUtil.getText(local));
				SWTControlUtil.setText(local, value != null ? value : ""); //$NON-NLS-1$
				value = odc.getStringProperty(IPeerNodeProperties.PROP_REMOTE_SERVICES);
				fireNotification |= value != null && !value.equals(SWTControlUtil.getText(remote));
				SWTControlUtil.setText(remote, value != null ? value : ""); //$NON-NLS-1$

				if (fireNotification) {
					// Fire a change event to trigger the editor refresh
					od.fireChangeEvent("editor.refreshTab", Boolean.FALSE, Boolean.TRUE); //$NON-NLS-1$
				}
			}
		};

		// If not yet triggered or if forced, run the service query
		if (!servicesQueryTriggered) {
			// Mark the services query as triggered
			servicesQueryTriggered = true;

			final boolean finForceQuery = forceQuery;

			Runnable runnable = new Runnable() {

				@Override
				public void run() {
					// Check if we have to run the query at all
					boolean doQuery = finForceQuery ||
										(!node.containsKey(IPeerNodeProperties.PROP_REMOTE_SERVICES)
											&& !node.containsKey(IPeerNodeProperties.PROP_LOCAL_SERVICES));

					if (doQuery) {
						IPeerModelQueryService service = node.getModel().getService(IPeerModelQueryService.class);
						if (service != null) {
							service.queryServicesAsync(node, new IPeerModelQueryService.DoneQueryServices() {
								@Override
								public void doneQueryServices(Throwable error) {
									// Copy over the service properties
									odc.setProperty(IPeerNodeProperties.PROP_REMOTE_SERVICES, node.getProperty(IPeerNodeProperties.PROP_REMOTE_SERVICES));
									odc.setProperty(IPeerNodeProperties.PROP_LOCAL_SERVICES, node.getProperty(IPeerNodeProperties.PROP_LOCAL_SERVICES));

									// Setup the data within the UI controls and fire the change notification
									fireRefreshTabs.set(true);
									DisplayUtil.safeAsyncExec(uiRunnable);
								}
							});
						}
					} else {
						// Copy over the properties
						odc.setProperties(node.getProperties());
						// Setup the data within the UI controls
						DisplayUtil.safeAsyncExec(uiRunnable);
					}
				}
			};

			Protocol.invokeLater(runnable);
		} else {
			// Setup the data within the UI controls
			uiRunnable.run();
		}
	}
}

Back to the top