KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > core > LaunchDelegate


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.core;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IConfigurationElement;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.debug.core.DebugPlugin;
24 import org.eclipse.debug.core.ILaunchDelegate;
25 import org.eclipse.debug.core.model.ILaunchConfigurationDelegate;
26
27 import com.ibm.icu.text.MessageFormat;
28
29 /**
30  * Proxy to a launch delegate extension
31  * Clients can contribute launch delegates through the <code>launchDelegates</code> extension point
32  *
33  * Example contribution of the local java launch delegate
34  * <pre>
35  * <extension point="org.eclipse.debug.core.launchDelegates">
36       <launchDelegate
37             delegate="org.eclipse.jdt.launching.JavaLaunchDelegate"
38             id="org.eclipse.jdt.launching.localJavaApplicationDelegate"
39             modes="run, debug"
40             name="%localJavaApplication"
41             type="org.eclipse.jdt.launching.localJavaApplication">
42           <modeCombination
43             modes="run, profile">
44             perspective="com.example.Perspective">
45           </modeCombination>
46       </launchDelegate>
47  * </pre>
48  *
49  * Clients are NOT intended to subclass this class
50  *
51  * @since 3.3
52  */

53 public final class LaunchDelegate implements ILaunchDelegate {
54     
55     /**
56      * The configuration element for this delegate
57      */

58     private IConfigurationElement fElement = null;
59     
60     /**
61      * The cached delegate. Remains null until asked for, then persisted
62      */

63     private ILaunchConfigurationDelegate fDelegate = null;
64     
65     //a listing of sets of
66
private List JavaDoc fLaunchModes = null;
67     private String JavaDoc fType = null;
68     private HashMap JavaDoc fPerspectiveIds = null;
69     
70     /**
71      * Constructor
72      * @param element the configuration element to associate with this launch delegate
73      */

74     public LaunchDelegate(IConfigurationElement element) {
75         fElement = element;
76     }
77     
78     /* (non-Javadoc)
79      * @see org.eclipse.debug.core.ILaunchDelegateProxy#getDelegate()
80      */

81     public ILaunchConfigurationDelegate getDelegate() throws CoreException {
82         if(fDelegate == null) {
83             Object JavaDoc obj = fElement.createExecutableExtension(IConfigurationElementConstants.DELEGATE);
84             if(obj instanceof ILaunchConfigurationDelegate) {
85                 fDelegate = (ILaunchConfigurationDelegate)obj;
86             } else {
87                 throw new CoreException(new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), DebugPlugin.INTERNAL_ERROR, MessageFormat.format(DebugCoreMessages.LaunchConfigurationType_Launch_delegate_for__0__does_not_implement_required_interface_ILaunchConfigurationDelegate__1, new String JavaDoc[]{getId()}), null));
88             }
89         }
90         return fDelegate;
91     }
92
93     /* (non-Javadoc)
94      * @see org.eclipse.debug.core.ILaunchDelegateProxy#getId()
95      */

96     public String JavaDoc getId() {
97         return fElement.getAttribute(IConfigurationElementConstants.ID);
98     }
99
100     /**
101      * Returns the id of the associated <code>ILaunchConfigurationType</code> or <code>null</code> if none provided
102      * @return the id of the <code>ILaunchConfigurationType</code> associated with this delegate
103      */

104     public String JavaDoc getLaunchConfigurationTypeId() {
105         if(fType == null) {
106             //fall back to single association if no appliesTo
107
fType = fElement.getAttribute(IConfigurationElementConstants.TYPE);
108             if(fType == null) {
109                 //the case when we have passed a launch configuration type to the launch delegate
110
fType = fElement.getAttribute(IConfigurationElementConstants.ID);
111             }
112         }
113         return fType;
114     }
115     
116     /**
117      * Simple method to parse mode strings (seperated by commmas)
118      * @param element the config element to read the mode string from
119      * @return a set of the parsed strings or an empty collection
120      * @since 3.3
121      */

122     private Set JavaDoc parseModes(IConfigurationElement element) {
123         HashSet JavaDoc set = new HashSet JavaDoc();
124         String JavaDoc modes = element.getAttribute(IConfigurationElementConstants.MODES);
125         if (modes != null) {
126             String JavaDoc[] strings = modes.split(","); //$NON-NLS-1$
127
for (int i = 0; i < strings.length; i++) {
128                 set.add(strings[i].trim());
129             }
130         }
131         return set;
132     }
133     
134     /* (non-Javadoc)
135      * @see org.eclipse.debug.core.ILaunchDelegateProxy#getModes()
136      */

137     public List JavaDoc getModes() {
138         if(fLaunchModes == null) {
139             fLaunchModes = new ArrayList JavaDoc();
140             fPerspectiveIds = new HashMap JavaDoc();
141             IConfigurationElement[] children = fElement.getChildren(IConfigurationElementConstants.MODE_COMBINATION);
142             Set JavaDoc modeset = null;
143             for (int i = 0; i < children.length; i++) {
144                 modeset = parseModes(children[i]);
145                 fLaunchModes.add(modeset);
146                 fPerspectiveIds.put(modeset, children[i].getAttribute(IConfigurationElementConstants.PERSPECTIVE));
147             }
148             //try to get the modes from the old definition and make each one
149
//a seperate set of one element
150
modeset = null;
151             String JavaDoc modes = fElement.getAttribute(IConfigurationElementConstants.MODES);
152             if (modes != null) {
153                 String JavaDoc[] strings = modes.split(","); //$NON-NLS-1$
154
for (int i = 0; i < strings.length; i++) {
155                     modeset = new HashSet JavaDoc();
156                     modeset.add(strings[i].trim());
157                     fLaunchModes.add(modeset);
158                 }
159             }
160         }
161         return fLaunchModes;
162     }
163     
164     /**
165      * Returns the human readable name for this launch delegate
166      * @return the human readable name for this launch delegate, or <code>null</code> if none
167      */

168     public String JavaDoc getName() {
169         //try a delegateName attribute first, in the event this delegate was made from an ILaunchConfigurationType
170
String JavaDoc name = fElement.getAttribute(IConfigurationElementConstants.DELEGATE_NAME);
171         if(name == null) {
172             name = fElement.getAttribute(IConfigurationElementConstants.NAME);
173             if (name == null) {
174                 name = getContributorName();
175             }
176             name = name.trim();
177             if (Character.isUpperCase(name.charAt(0))) {
178                 name = MessageFormat.format(DebugCoreMessages.LaunchDelegate_1, new String JavaDoc[]{name});
179             } else {
180                 name = MessageFormat.format(DebugCoreMessages.LaunchDelegate_2, new String JavaDoc[]{name});
181             }
182         }
183         return name;
184     }
185     
186     /**
187      * Returns the contributor name of this delegate (plug-in name).
188      *
189      * @return contributor name
190      */

191     public String JavaDoc getContributorName() {
192         return fElement.getContributor().getName();
193     }
194     
195     /**
196      * Returns the associated source locator id or <code>null</code>
197      * @return the associated source locator id or <code>null</code> if not provided
198      */

199     public String JavaDoc getSourceLocatorId() {
200         return fElement.getAttribute(IConfigurationElementConstants.SOURCE_LOCATOR);
201     }
202
203     /**
204      * Returns the associated source path computer id or <code>null</code>
205      * @return the associated source path computer id or <code>null</code> if not provided
206      */

207     public String JavaDoc getSourcePathComputerId() {
208         return fElement.getAttribute(IConfigurationElementConstants.SOURCE_PATH_COMPUTER);
209     }
210     
211     /**
212      * @see org.eclipse.debug.core.ILaunchDelegate#getDescription()
213      */

214     public String JavaDoc getDescription() {
215         String JavaDoc desc = fElement.getAttribute(IConfigurationElementConstants.DELEGATE_DESCRIPTION);
216         if(desc == null) {
217             return DebugCoreMessages.LaunchDelegate_0;
218         }
219         return desc;
220     }
221     
222     /**
223      * @see org.eclipse.debug.core.ILaunchDelegate#getPluginIdentifier()
224      */

225     public String JavaDoc getPluginIdentifier() {
226         return fElement.getContributor().getName();
227     }
228
229     /**
230      * @see java.lang.Object#equals(java.lang.Object)
231      */

232     public boolean equals(Object JavaDoc obj) {
233         if(obj == null) {
234             return false;
235         }
236         return obj instanceof ILaunchDelegate && getId() != null && getId().equals(((ILaunchDelegate)obj).getId());
237     }
238
239     /**
240      * @see org.eclipse.debug.core.ILaunchDelegate#getPerspectiveId(java.util.Set)
241      */

242     public String JavaDoc getPerspectiveId(Set JavaDoc modes) {
243         if(fPerspectiveIds == null) {
244             getModes();
245         }
246         return (String JavaDoc) fPerspectiveIds.get(modes);
247     }
248 }
249
Popular Tags