Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ada7fd55fadf82fd7a79d0bf5e7992bbfead9723 (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
/*******************************************************************************
 * 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 org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationListener;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.tcf.te.launch.ui.editor.AbstractLaunchTabContainerEditorPage;
import org.eclipse.tcf.te.runtime.concurrent.util.ExecutorsUtil;
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.locator.interfaces.nodes.IPeerModel;

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

	protected ILaunchConfigurationListener launchConfigListener = null;

	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$

	/**
	 * Get the peer model from the editor input.
	 * @param input The editor input.
	 * @return The peer model.
	 */
	public 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.
	 */
	public 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 {
				wc = (ILaunchConfigurationWorkingCopy)Platform.getAdapterManager().getAdapter(peerModel, ILaunchConfigurationWorkingCopy.class);
				if (wc == null) {
					wc = (ILaunchConfigurationWorkingCopy)Platform.getAdapterManager().loadAdapter(peerModel, "org.eclipse.debug.core.ILaunchConfigurationWorkingCopy"); //$NON-NLS-1$
				}
				service.setProperty(peerModel, PROP_LAUNCH_CONFIG_WC, wc);
				IPersistenceDelegate delegate = PersistenceManager.getInstance().getDelegate(wc, String.class);
				String launchConfigAttributes = null;
				try {
					launchConfigAttributes = (String)delegate.write(wc, String.class);
				}
				catch (Exception e) {
				}
				service.setProperty(peerModel, PROP_ORIGINAL_LAUNCH_CONFIG_ATTRIBUTES, launchConfigAttributes);
			}
		}
		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);
			try {
				wc.doSave();
				IPeerModel peerModel = getPeerModel(getEditorInput());
				IPropertiesAccessService service = ServiceManager.getInstance().getService(peerModel, IPropertiesAccessService.class);
				Assert.isNotNull(service);
				service.setProperty(peerModel, PROP_LAUNCH_CONFIG_WC, null);
				checkLaunchConfigDirty();
				return true;
			}
			catch (Exception e) {
			}
		}
		return false;
	}

	/**
	 * Check if the launch configuration has changed.
	 * If it has changed, the page is set dirty.
	 * @return <code>true</code> if the launch configuration 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);
		String launchConfigAttributes = null;
		try {
			launchConfigAttributes = (String)delegate.write(getLaunchConfig(peerModel), String.class);
			dirty = !launchConfigAttributes.equals(oldLaunchConfigAttributes);
		}
		catch (Exception e) {
		}
		setDirty(dirty);
		return dirty;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.launch.ui.editor.AbstractLaunchTabContainerEditorPage#setDirty(boolean)
	 */
	@Override
	public void setDirty(boolean dirty) {
		super.setDirty(dirty);
		ExecutorsUtil.executeInUI(new Runnable() {
			@Override
			public void run() {
				getManagedForm().dirtyStateChanged();
			}
		});
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.launch.ui.editor.AbstractLaunchTabContainerEditorPage#setActive(boolean)
	 */
	@Override
	public void setActive(boolean active) {
		super.setActive(active);
		if (active && launchConfigListener == null) {
			launchConfigListener = this;
			DebugPlugin.getDefault().getLaunchManager().addLaunchConfigurationListener(this);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.launch.ui.editor.AbstractLaunchTabContainerEditorPage#dispose()
	 */
	@Override
	public void dispose() {
		super.dispose();
		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);
		DebugPlugin.getDefault().getLaunchManager().removeLaunchConfigurationListener(this);
		launchConfigListener = null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.ILaunchConfigurationListener#launchConfigurationAdded(org.eclipse.debug.core.ILaunchConfiguration)
	 */
	@Override
	public void launchConfigurationAdded(ILaunchConfiguration configuration) {
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.ILaunchConfigurationListener#launchConfigurationRemoved(org.eclipse.debug.core.ILaunchConfiguration)
	 */
	@Override
	public void launchConfigurationRemoved(ILaunchConfiguration configuration) {
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.ILaunchConfigurationListener#launchConfigurationChanged(org.eclipse.debug.core.ILaunchConfiguration)
	 */
	@Override
	public void launchConfigurationChanged(ILaunchConfiguration configuration) {
		if (!(configuration instanceof ILaunchConfigurationWorkingCopy)) {
			IPeerModel peerModel = getPeerModel(getEditorInput());
			IPropertiesAccessService service = ServiceManager.getInstance().getService(peerModel, IPropertiesAccessService.class);
			ILaunchConfigurationWorkingCopy wc = (ILaunchConfigurationWorkingCopy)service.getProperty(peerModel, PROP_LAUNCH_CONFIG_WC);
			if (wc != null && configuration.getName().equals(wc.getName())) {
				service.setProperty(peerModel, PROP_ORIGINAL_LAUNCH_CONFIG_ATTRIBUTES, null);
				service.setProperty(peerModel, PROP_LAUNCH_CONFIG_WC, null);
				ExecutorsUtil.executeInUI(new Runnable() {
					@Override
					public void run() {
						setActive(isActive());
					}
				});
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.views.editor.pages.AbstractEditorPage#doValidate()
	 */
	@Override
	protected ValidationResult doValidate() {
	    return null;
	}
}

Back to the top