Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c96ddd178dc1c76210c676029465c13a0462c70c (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
/*******************************************************************************
 * Copyright (c) 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.launch.ui.editor;

import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.tcf.core.TransientPeer;
import org.eclipse.tcf.protocol.IPeer;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.te.launch.core.lm.LaunchManager;
import org.eclipse.tcf.te.launch.core.lm.LaunchSpecification;
import org.eclipse.tcf.te.launch.core.lm.interfaces.ILaunchSpecification;
import org.eclipse.tcf.te.launch.core.persistence.launchcontext.LaunchContextsPersistenceDelegate;
import org.eclipse.tcf.te.launch.ui.editor.AbstractLaunchTabContainerEditorPage;
import org.eclipse.tcf.te.runtime.concurrent.util.ExecutorsUtil;
import org.eclipse.tcf.te.runtime.events.ChangeEvent;
import org.eclipse.tcf.te.runtime.events.EventManager;
import org.eclipse.tcf.te.runtime.interfaces.events.IEventListener;
import org.eclipse.tcf.te.runtime.model.interfaces.IModelNode;
import org.eclipse.tcf.te.runtime.persistence.PersistenceManager;
import org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistenceDelegate;
import org.eclipse.tcf.te.runtime.services.ServiceManager;
import org.eclipse.tcf.te.runtime.services.interfaces.IPropertiesAccessService;
import org.eclipse.tcf.te.tcf.core.peers.Peer;
import org.eclipse.tcf.te.tcf.launch.core.interfaces.ILaunchTypes;
import org.eclipse.tcf.te.tcf.launch.core.interfaces.IPeerModelProperties;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModel;
import org.eclipse.tcf.te.tcf.locator.interfaces.services.ILocatorModelUpdateService;
import org.eclipse.tcf.te.tcf.locator.model.Model;
import org.eclipse.tcf.te.tcf.locator.nodes.PeerRedirector;

/**
 * TCF launch configuration tab container page implementation.
 */
public abstract class AbstractTcfLaunchTabContainerEditorPage extends AbstractLaunchTabContainerEditorPage {

	protected static final String PROP_LAUNCH_CONFIG_WC = "launchConfigWorkingCopy.transient.silent"; //$NON-NLS-1$
	protected static final String PROP_ORIGINAL_LAUNCH_CONFIG_ATTRIBUTES = "launchConfigAttributes.transient.silent"; //$NON-NLS-1$

	private IEventListener eventListener = null;

	/**
	 * Get the peer model from the editor input.
	 * @param input The editor input.
	 * @return The peer model.
	 */
	IPeerModel getPeerModel(Object input) {
		return (IPeerModel)((IAdaptable)input).getAdapter(IPeerModel.class);
	}

	/**
	 * Get the launch configuration from the peer model.
	 * @param peerModel The peer model.
	 * @return The launch configuration.
	 */
	ILaunchConfigurationWorkingCopy getLaunchConfig(final IPeerModel peerModel) {
		ILaunchConfigurationWorkingCopy wc = null;
		if (peerModel != null) {
			IPropertiesAccessService service = ServiceManager.getInstance().getService(peerModel, IPropertiesAccessService.class);
			Assert.isNotNull(service);
			if (service.getProperty(peerModel, PROP_LAUNCH_CONFIG_WC) instanceof ILaunchConfigurationWorkingCopy) {
				wc = (ILaunchConfigurationWorkingCopy)service.getProperty(peerModel, PROP_LAUNCH_CONFIG_WC);
			}
			else {
				String launchConfigAttributes = peerModel.getPeer().getAttributes().get(IPeerModelProperties.PROP_LAUNCH_CONFIG_ATTRIBUTES);
				ILaunchSpecification spec = new LaunchSpecification(ILaunchTypes.ATTACH, ILaunchManager.DEBUG_MODE);
				LaunchContextsPersistenceDelegate.setLaunchContexts(spec, new IModelNode[]{peerModel});
				try {
					wc = LaunchManager.getInstance().getLaunchConfiguration(spec, true).getWorkingCopy();
					LaunchContextsPersistenceDelegate.setLaunchContexts(wc, null);
					IPersistenceDelegate delegate = PersistenceManager.getInstance().getDelegate(wc, String.class, false);
					if (launchConfigAttributes != null && launchConfigAttributes.trim().length() > 0) {
						delegate.read(wc, launchConfigAttributes, null);
					}
					launchConfigAttributes = (String)delegate.write(wc, String.class, null);
					service.setProperty(peerModel, PROP_ORIGINAL_LAUNCH_CONFIG_ATTRIBUTES, launchConfigAttributes);
					service.setProperty(peerModel, PROP_LAUNCH_CONFIG_WC, wc);
				}
				catch (Exception e) {
				}
			}
		}
		return wc;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.launch.ui.editor.AbstractLaunchTabContainerEditorPage#setupData(java.lang.Object)
	 */
	@Override
	public boolean setupData(Object input) {
		ILaunchConfigurationWorkingCopy wc = getLaunchConfig(getPeerModel(input));
		if (wc != null) {
			getLaunchConfigurationTab().initializeFrom(wc);
			checkLaunchConfigDirty();
			return true;
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.launch.ui.editor.AbstractLaunchTabContainerEditorPage#extractData()
	 */
	@Override
	public boolean extractData() {
		ILaunchConfigurationWorkingCopy wc = getLaunchConfig(getPeerModel(getEditorInput()));
		if (wc != null && checkLaunchConfigDirty()) {
			getLaunchConfigurationTab().performApply(wc);
			IPersistenceDelegate delegate = PersistenceManager.getInstance().getDelegate(wc, String.class, false);
			try {
				final String launchConfigAttributes = (String)delegate.write(wc, String.class, null);
				final IPeerModel peerModel = getPeerModel(getEditorInput());
				IPropertiesAccessService service = ServiceManager.getInstance().getService(peerModel, IPropertiesAccessService.class);
				service.setProperty(peerModel, PROP_ORIGINAL_LAUNCH_CONFIG_ATTRIBUTES, launchConfigAttributes);
				if (peerModel != null) {
					Protocol.invokeAndWait(new Runnable() {
						@Override
						public void run() {
							IPeer oldPeer = peerModel.getPeer();
							Map<String, String> attributes = new HashMap<String, String>(peerModel.getPeer().getAttributes());
							attributes.put(IPeerModelProperties.PROP_LAUNCH_CONFIG_ATTRIBUTES, launchConfigAttributes);
							IPeer newPeer = new Peer(attributes);
							if (oldPeer instanceof TransientPeer && !(oldPeer instanceof PeerRedirector || oldPeer instanceof Peer)) {
								peerModel.setProperty(org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModelProperties.PROP_INSTANCE, newPeer);
							} else {
								Model.getModel().getService(ILocatorModelUpdateService.class).mergeUserDefinedAttributes(peerModel, newPeer, false);
							}
							checkLaunchConfigDirty();
						}
					});
					return true;
				}
			}
			catch (Exception e) {
			}
		}
		return false;
	}

	/**
	 * Check if the launch config has changed.
	 * If it has changed, the page is set dirty.
	 * @return <code>true</code> if the launch config has changed since last save.
	 */
	public boolean checkLaunchConfigDirty() {
		boolean dirty = false;
		IPeerModel peerModel = getPeerModel(getEditorInput());
		IPropertiesAccessService service = ServiceManager.getInstance().getService(peerModel, IPropertiesAccessService.class);
		String oldLaunchConfigAttributes = (String)service.getProperty(peerModel, PROP_ORIGINAL_LAUNCH_CONFIG_ATTRIBUTES);
		IPersistenceDelegate delegate = PersistenceManager.getInstance().getDelegate(getLaunchConfig(peerModel), String.class, false);
		String launchConfigAttributes = null;
		try {
			launchConfigAttributes = (String)delegate.write(getLaunchConfig(peerModel), String.class, null);
			dirty = !launchConfigAttributes.equals(oldLaunchConfigAttributes);
		}
		catch (Exception e) {
		}

		setDirty(dirty);
		ExecutorsUtil.executeInUI(new Runnable() {
			@Override
			public void run() {
				getManagedForm().dirtyStateChanged();
			}
		});
		return dirty;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.launch.ui.editor.AbstractLaunchTabContainerEditorPage#setActive(boolean)
	 */
	@Override
	public void setActive(boolean active) {
		super.setActive(active);

		if (eventListener == null) {
			eventListener = new IEventListener() {
				@Override
				public void eventFired(EventObject event) {
					if (event instanceof ChangeEvent && IPeerModelProperties.PROP_LAUNCH_CONFIG_ATTRIBUTES.equals(((ChangeEvent)event).getEventId())) {
						if (event.getSource() instanceof IPeerModel && getPeerModel(getEditorInput()).getUUID().equals(((IPeerModel)event.getSource()).getUUID())) {
							Protocol.invokeAndWait(new Runnable() {
								@Override
								public void run() {
									IPropertiesAccessService service = ServiceManager.getInstance().getService(getPeerModel(getEditorInput()), IPropertiesAccessService.class);
									Assert.isNotNull(service);
									service.setProperty(getPeerModel(getEditorInput()), PROP_LAUNCH_CONFIG_WC, null);
									service.setProperty(getPeerModel(getEditorInput()), PROP_ORIGINAL_LAUNCH_CONFIG_ATTRIBUTES, null);
								}
							});
							ExecutorsUtil.executeInUI(new Runnable() {
								@Override
								public void run() {
									setActive(isActive());
								}
							});
						}
					}
				}
			};
			EventManager.getInstance().addEventListener(eventListener, ChangeEvent.class);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.launch.ui.editor.AbstractLaunchTabContainerEditorPage#dispose()
	 */
	@Override
	public void dispose() {
		super.dispose();
		EventManager.getInstance().removeEventListener(eventListener);
		IPeerModel peerModel = getPeerModel(getEditorInput());
		IPropertiesAccessService service = ServiceManager.getInstance().getService(peerModel, IPropertiesAccessService.class);
		service.setProperty(peerModel, PROP_ORIGINAL_LAUNCH_CONFIG_ATTRIBUTES, null);
		service.setProperty(peerModel, PROP_LAUNCH_CONFIG_WC, null);
	}
}

Back to the top