Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c9a83b018597612820443f33e022945f7e355bbc (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
/*******************************************************************************
 * Copyright (c) 2012, 2014 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 2.0 which accompanies this distribution, and is
 * available at https://www.eclipse.org/legal/epl-2.0/
 *
 * Contributors:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/

package org.eclipse.tcf.te.launch.ui.model;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.core.runtime.Assert;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.tcf.te.launch.core.lm.LaunchConfigHelper;
import org.eclipse.tcf.te.launch.core.lm.LaunchManager;
import org.eclipse.tcf.te.launch.core.lm.interfaces.ILaunchManagerDelegate;
import org.eclipse.tcf.te.runtime.model.ContainerModelNode;
import org.eclipse.tcf.te.runtime.model.interfaces.IModelNode;

/**
 * LaunchNode
 */
public class LaunchNode extends ContainerModelNode {

	public static final String TYPE_ROOT = "root"; //$NON-NLS-1$
	public static final String TYPE_LAUNCH_CONFIG_TYPE = "launchConfigType"; //$NON-NLS-1$
	public static final String TYPE_LAUNCH_CONFIG = "launchConfig"; //$NON-NLS-1$

	protected static final String PROPERTY_LAUNCH_CONFIG_TYPE = TYPE_LAUNCH_CONFIG_TYPE;
	protected static final String PROPERTY_LAUNCH_CONFIG = TYPE_LAUNCH_CONFIG;
	protected static final String PROPERTY_MODEL = "model"; //$NON-NLS-1$

	private LaunchNode(String type) {
		super();
		setProperty(IModelNode.PROPERTY_TYPE, type);
	}

	public LaunchNode(LaunchModel model) {
		this(TYPE_ROOT);
		setProperty(PROPERTY_MODEL, model);
	}

	public LaunchNode(ILaunchConfiguration config) {
		this(TYPE_LAUNCH_CONFIG);
		setProperty(PROPERTY_LAUNCH_CONFIG, config);
	}

	public LaunchNode(ILaunchConfigurationType configType) {
		this(TYPE_LAUNCH_CONFIG_TYPE);
		setProperty(PROPERTY_LAUNCH_CONFIG_TYPE, configType);
	}

	public boolean isType(String type) {
		return type.equals(getStringProperty(IModelNode.PROPERTY_TYPE));
	}

	/**
	 * Return the model for this node. Must not be <code>null</code>.
	 */
	public LaunchModel getModel() {
		LaunchModel model = (LaunchModel)getProperty(PROPERTY_MODEL);
		IModelNode parent = getParent();

		while (model == null && parent != null) {
			model = (LaunchModel)parent.getProperty(PROPERTY_MODEL);
			parent = parent.getParent();
		}

		Assert.isNotNull(model);
		return model;
	}

	/**
	 * Return the launch confuration for this node or <code>null</code>.
	 */
	public ILaunchConfiguration getLaunchConfiguration() {
		return (ILaunchConfiguration)getProperty(PROPERTY_LAUNCH_CONFIG);
	}

	/**
	 * Return the launch configuration type for this launch node or <code>null</code>.
	 */
	public ILaunchConfigurationType getLaunchConfigurationType() {
		if (getLaunchConfiguration() != null) {
			try {
				return getLaunchConfiguration().getType();
			}
			catch (Exception e) {
			}
		}
		else if (isType(TYPE_LAUNCH_CONFIG)) {
			return ((LaunchNode)getParent()).getLaunchConfigurationType();
		}
		return (ILaunchConfigurationType)getProperty(PROPERTY_LAUNCH_CONFIG_TYPE);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.model.ModelNode#getName()
	 */
	@Override
	public String getName() {
		if (isType(TYPE_ROOT)) {
			return "Launches"; //$NON-NLS-1$
		}
		else if (isType(TYPE_LAUNCH_CONFIG_TYPE)) {
			return getLaunchConfigurationType().getName();
		}
		else if (isType(TYPE_LAUNCH_CONFIG)) {
			return getLaunchConfiguration().getName();
		}
		return super.getName();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.properties.PropertiesContainer#hashCode()
	 */
	@Override
	public int hashCode() {
		if (isType(TYPE_LAUNCH_CONFIG_TYPE) && getLaunchConfigurationType() != null) {
			return getLaunchConfigurationType().hashCode();
		}
		else if (isType(TYPE_LAUNCH_CONFIG) && getLaunchConfiguration() != null) {
			return getLaunchConfiguration().hashCode();
		}
	    return super.hashCode();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.properties.PropertiesContainer#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof LaunchNode && isType(((LaunchNode)obj).getStringProperty(PROPERTY_TYPE))) {
			if (getModel().getModelRoot().equals(((LaunchNode)obj).getModel().getModelRoot())) {
				if (isType(TYPE_LAUNCH_CONFIG_TYPE)) {
					return getLaunchConfigurationType().equals(((LaunchNode)obj).getLaunchConfigurationType());
				}
				else if (isType(TYPE_LAUNCH_CONFIG)) {
					return getLaunchConfiguration().equals(((LaunchNode)obj).getLaunchConfiguration());
				}
			}
		}
		return super.equals(obj);
	}

	/**
	 * Check if the launch config node is valid for the given launch mode.
	 * @param mode The launch mode or <code>null</code> to check for all supported modes;
	 * @return <code>true</code> if the node is valid for the the given launch mode (or all supported modes if mode is <code>null</code>).
	 */
	public boolean isValidFor(String mode) {
		if (isType(TYPE_LAUNCH_CONFIG)) {
			if (getLaunchConfigurationType() == null) {
				return false;
			}
			List<String> modes;
			if (mode != null && mode.trim().length() > 0) {
				modes = new ArrayList<String>();
				modes.add(mode);
			}
			else {
				modes = Arrays.asList(LaunchConfigHelper.getLaunchConfigTypeModes(getLaunchConfigurationType(), false));
			}
			for (String m : modes) {
				if (!getLaunchConfigurationType().supportsMode(m)) {
					return false;
				}
				ILaunchManagerDelegate delegate = LaunchManager.getInstance().getLaunchManagerDelegate(getLaunchConfigurationType(), m);
				if (delegate != null) {
					try {
						delegate.validate(mode, getLaunchConfiguration());
					}
					catch (Exception e) {
						return false;
					}
				}
			}
		}
		return true;
	}
}

Back to the top