KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > debug > ui > launchConfigurations > JavaConnectTab


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.jdt.debug.ui.launchConfigurations;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.resources.ResourcesPlugin;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.debug.core.ILaunchConfiguration;
22 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
23 import org.eclipse.debug.ui.DebugUITools;
24 import org.eclipse.debug.ui.IDebugUIConstants;
25 import org.eclipse.jdt.core.IJavaElement;
26 import org.eclipse.jdt.core.JavaModelException;
27 import org.eclipse.jdt.internal.debug.ui.IJavaDebugHelpContextIds;
28 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
29 import org.eclipse.jdt.internal.debug.ui.SWTFactory;
30 import org.eclipse.jdt.internal.debug.ui.launcher.AbstractJavaMainTab;
31 import org.eclipse.jdt.internal.debug.ui.launcher.LauncherMessages;
32 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
33 import org.eclipse.jdt.launching.IVMConnector;
34 import org.eclipse.jdt.launching.JavaRuntime;
35 import org.eclipse.jface.preference.BooleanFieldEditor;
36 import org.eclipse.jface.preference.ComboFieldEditor;
37 import org.eclipse.jface.preference.FieldEditor;
38 import org.eclipse.jface.preference.IntegerFieldEditor;
39 import org.eclipse.jface.preference.PreferenceStore;
40 import org.eclipse.jface.preference.StringFieldEditor;
41 import org.eclipse.jface.util.IPropertyChangeListener;
42 import org.eclipse.jface.util.PropertyChangeEvent;
43 import org.eclipse.swt.SWT;
44 import org.eclipse.swt.events.SelectionAdapter;
45 import org.eclipse.swt.events.SelectionEvent;
46 import org.eclipse.swt.graphics.Font;
47 import org.eclipse.swt.graphics.Image;
48 import org.eclipse.swt.layout.GridData;
49 import org.eclipse.swt.layout.GridLayout;
50 import org.eclipse.swt.widgets.Button;
51 import org.eclipse.swt.widgets.Combo;
52 import org.eclipse.swt.widgets.Composite;
53 import org.eclipse.swt.widgets.Control;
54 import org.eclipse.swt.widgets.Group;
55 import org.eclipse.ui.PlatformUI;
56
57 import com.sun.jdi.connect.Connector;
58
59 /**
60  * A launch configuration tab that displays and edits the project associated
61  * with a remote connection and the connector used to connect to a remote
62  * VM.
63  * <p>
64  * This class may be instantiated. This class is not intended to be subclassed.
65  * </p>
66  * @since 2.0
67  */

68 public class JavaConnectTab extends AbstractJavaMainTab implements IPropertyChangeListener {
69     
70     // UI widgets
71
private Button fAllowTerminateButton;
72     private Map JavaDoc fArgumentMap;
73     private Map JavaDoc fFieldEditorMap = new HashMap JavaDoc();
74     private Composite fArgumentComposite;
75     private Combo fConnectorCombo;
76     
77     // the selected connector
78
private IVMConnector fConnector;
79     private IVMConnector[] fConnectors = JavaRuntime.getVMConnectors();
80
81     /* (non-Javadoc)
82      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#createControl(org.eclipse.swt.widgets.Composite)
83      */

84     public void createControl(Composite parent) {
85         Font font = parent.getFont();
86         Composite comp = SWTFactory.createComposite(parent, font, 1, 1, GridData.FILL_BOTH);
87         GridLayout layout = new GridLayout();
88         layout.verticalSpacing = 0;
89         comp.setLayout(layout);
90         createProjectEditor(comp);
91         createVerticalSpacer(comp, 1);
92         
93     //connection type
94
Group group = SWTFactory.createGroup(comp, LauncherMessages.JavaConnectTab_Connect_ion_Type__7, 1, 1, GridData.FILL_HORIZONTAL);
95         String JavaDoc[] names = new String JavaDoc[fConnectors.length];
96         for (int i = 0; i < fConnectors.length; i++) {
97             names[i] = fConnectors[i].getName();
98         }
99         fConnectorCombo = SWTFactory.createCombo(group, SWT.READ_ONLY, 1, GridData.FILL_HORIZONTAL, names);
100         fConnectorCombo.addSelectionListener(new SelectionAdapter() {
101             public void widgetSelected(SelectionEvent e) {
102                 handleConnectorComboModified();
103             }
104         });
105         createVerticalSpacer(comp, 1);
106         
107     //connection properties
108
group = SWTFactory.createGroup(comp, LauncherMessages.JavaConnectTab_Connection_Properties_1, 2, 1, GridData.FILL_HORIZONTAL);
109         Composite cgroup = SWTFactory.createComposite(group, font, 2, 1, GridData.FILL_HORIZONTAL);
110         fArgumentComposite = cgroup;
111         createVerticalSpacer(comp, 2);
112         fAllowTerminateButton = createCheckButton(comp, LauncherMessages.JavaConnectTab__Allow_termination_of_remote_VM_6);
113         fAllowTerminateButton.addSelectionListener(getDefaultListener());
114         
115         setControl(comp);
116         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaDebugHelpContextIds.LAUNCH_CONFIGURATION_DIALOG_CONNECT_TAB);
117     }
118     
119     /**
120      * Update the argument area to show the selected connector's arguments
121      */

122     private void handleConnectorComboModified() {
123         int index = fConnectorCombo.getSelectionIndex();
124         if ( (index < 0) || (index >= fConnectors.length) ) {
125             return;
126         }
127         IVMConnector vm = fConnectors[index];
128         if (vm.equals(fConnector)) {
129             return; // selection did not change
130
}
131         fConnector = vm;
132         try {
133             fArgumentMap = vm.getDefaultArguments();
134         } catch (CoreException e) {
135             JDIDebugUIPlugin.statusDialog(LauncherMessages.JavaConnectTab_Unable_to_display_connection_arguments__2, e.getStatus());
136             return;
137         }
138         
139         // Dispose of any current child widgets in the tab holder area
140
Control[] children = fArgumentComposite.getChildren();
141         for (int i = 0; i < children.length; i++) {
142             children[i].dispose();
143         }
144         fFieldEditorMap.clear();
145         PreferenceStore store = new PreferenceStore();
146         // create editors
147
Iterator JavaDoc keys = vm.getArgumentOrder().iterator();
148         while (keys.hasNext()) {
149             String JavaDoc key = (String JavaDoc)keys.next();
150             Connector.Argument arg = (Connector.Argument)fArgumentMap.get(key);
151             FieldEditor field = null;
152             if (arg instanceof Connector.IntegerArgument) {
153                 store.setDefault(arg.name(), ((Connector.IntegerArgument)arg).intValue());
154                 field = new IntegerFieldEditor(arg.name(), getLabel(arg.label()), fArgumentComposite);
155             } else if (arg instanceof Connector.SelectedArgument) {
156                 List JavaDoc choices = ((Connector.SelectedArgument)arg).choices();
157                 String JavaDoc[][] namesAndValues = new String JavaDoc[choices.size()][2];
158                 Iterator JavaDoc iter = choices.iterator();
159                 int count = 0;
160                 while (iter.hasNext()) {
161                     String JavaDoc choice = (String JavaDoc)iter.next();
162                     namesAndValues[count][0] = choice;
163                     namesAndValues[count][1] = choice;
164                     count++;
165                 }
166                 store.setDefault(arg.name(), arg.value());
167                 field = new ComboFieldEditor(arg.name(), getLabel(arg.label()), namesAndValues, fArgumentComposite);
168             } else if (arg instanceof Connector.StringArgument) {
169                 store.setDefault(arg.name(), arg.value());
170                 field = new StringFieldEditor(arg.name(), getLabel(arg.label()), fArgumentComposite);
171             } else if (arg instanceof Connector.BooleanArgument) {
172                 store.setDefault(arg.name(), ((Connector.BooleanArgument)arg).booleanValue());
173                 field = new BooleanFieldEditor(arg.name(), getLabel(arg.label()), fArgumentComposite);
174             }
175             if(field != null) {
176                 field.setPreferenceStore(store);
177                 field.loadDefault();
178                 field.setPropertyChangeListener(this);
179                 fFieldEditorMap.put(key, field);
180             }
181         }
182         fArgumentComposite.getParent().getParent().layout();
183         fArgumentComposite.layout(true);
184     }
185     
186     /**
187      * Adds a colon to the label if required
188      */

189     private String JavaDoc getLabel(String JavaDoc label) {
190         if (!label.endsWith(":")) { //$NON-NLS-1$
191
return label+":"; //$NON-NLS-1$
192
}
193         return label;
194     }
195
196      /* (non-Javadoc)
197      * @see org.eclipse.jdt.internal.debug.ui.launcher.AbstractJavaMainTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration)
198      */

199     public void initializeFrom(ILaunchConfiguration config) {
200         super.initializeFrom(config);
201         updateAllowTerminateFromConfig(config);
202         updateConnectionFromConfig(config);
203     }
204     
205     /**
206      * Updates the state of the allow terminate check button from the specified configuration
207      * @param config the config to load from
208      */

209     private void updateAllowTerminateFromConfig(ILaunchConfiguration config) {
210         boolean allowTerminate = false;
211         try {
212             allowTerminate = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_ALLOW_TERMINATE, false);
213         }
214         catch (CoreException ce) {JDIDebugUIPlugin.log(ce);}
215         fAllowTerminateButton.setSelection(allowTerminate);
216     }
217
218     /**
219      * Updates the connection argument field editors from the specified configuration
220      * @param config the config to load from
221      */

222     private void updateConnectionFromConfig(ILaunchConfiguration config) {
223         String JavaDoc id = null;
224         try {
225             id = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_CONNECTOR, JavaRuntime.getDefaultVMConnector().getIdentifier());
226             fConnectorCombo.setText(JavaRuntime.getVMConnector(id).getName());
227             handleConnectorComboModified();
228             
229             Map JavaDoc attrMap = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_CONNECT_MAP, (Map JavaDoc)null);
230             if (attrMap == null) {
231                 return;
232             }
233             Iterator JavaDoc keys = attrMap.keySet().iterator();
234             while (keys.hasNext()) {
235                 String JavaDoc key = (String JavaDoc)keys.next();
236                 Connector.Argument arg = (Connector.Argument)fArgumentMap.get(key);
237                 FieldEditor editor = (FieldEditor)fFieldEditorMap.get(key);
238                 if (arg != null && editor != null) {
239                     String JavaDoc value = (String JavaDoc)attrMap.get(key);
240                     if (arg instanceof Connector.StringArgument || arg instanceof Connector.SelectedArgument) {
241                         editor.getPreferenceStore().setValue(key, value);
242                     }
243                     else if (arg instanceof Connector.BooleanArgument) {
244                         editor.getPreferenceStore().setValue(key, Boolean.valueOf(value).booleanValue());
245                     }
246                     else if (arg instanceof Connector.IntegerArgument) {
247                         editor.getPreferenceStore().setValue(key, new Integer JavaDoc(value).intValue());
248                     }
249                     editor.load();
250                 }
251             }
252         }
253         catch (CoreException ce) {JDIDebugUIPlugin.log(ce);}
254     }
255
256     /* (non-Javadoc)
257      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#performApply(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
258      */

259     public void performApply(ILaunchConfigurationWorkingCopy config) {
260         config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, fProjText.getText().trim());
261         config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_ALLOW_TERMINATE, fAllowTerminateButton.getSelection());
262         config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_CONNECTOR, getSelectedConnector().getIdentifier());
263         mapResources(config);
264         Map JavaDoc attrMap = new HashMap JavaDoc(fFieldEditorMap.size());
265         Iterator JavaDoc keys = fFieldEditorMap.keySet().iterator();
266         while (keys.hasNext()) {
267             String JavaDoc key = (String JavaDoc)keys.next();
268             FieldEditor editor = (FieldEditor)fFieldEditorMap.get(key);
269             if (!editor.isValid()) {
270                 return;
271             }
272             Connector.Argument arg = (Connector.Argument)fArgumentMap.get(key);
273             editor.store();
274             if (arg instanceof Connector.StringArgument || arg instanceof Connector.SelectedArgument) {
275                 attrMap.put(key, editor.getPreferenceStore().getString(key));
276             }
277             else if (arg instanceof Connector.BooleanArgument) {
278                 attrMap.put(key, Boolean.valueOf(editor.getPreferenceStore().getBoolean(key)).toString());
279             }
280             else if (arg instanceof Connector.IntegerArgument) {
281                 attrMap.put(key, new Integer JavaDoc(editor.getPreferenceStore().getInt(key)).toString());
282             }
283         }
284         config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CONNECT_MAP, attrMap);
285     }
286     
287     /**
288      * Initialize default settings for the given Java element
289      */

290     private void initializeDefaults(IJavaElement javaElement, ILaunchConfigurationWorkingCopy config) {
291         initializeJavaProject(javaElement, config);
292         initializeName(javaElement, config);
293         initializeHardCodedDefaults(config);
294     }
295
296     /* (non-Javadoc)
297      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#setDefaults(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
298      */

299     public void setDefaults(ILaunchConfigurationWorkingCopy config) {
300         IJavaElement javaElement = getContext();
301         if (javaElement == null) {
302             initializeHardCodedDefaults(config);
303         }
304         else {
305             initializeDefaults(javaElement, config);
306         }
307     }
308
309     /**
310      * Find the first instance of a type, compilation unit, class file or project in the
311      * specified element's parental hierarchy, and use this as the default name.
312      */

313     private void initializeName(IJavaElement javaElement, ILaunchConfigurationWorkingCopy config) {
314         String JavaDoc name = EMPTY_STRING;
315         try {
316             IResource resource = javaElement.getUnderlyingResource();
317             if (resource != null) {
318                 name = resource.getName();
319                 int index = name.lastIndexOf('.');
320                 if (index > 0) {
321                     name = name.substring(0, index);
322                 }
323             }
324             else {
325                 name= javaElement.getElementName();
326             }
327             name = getLaunchConfigurationDialog().generateName(name);
328         }
329         catch (JavaModelException jme) {JDIDebugUIPlugin.log(jme);}
330         config.rename(name);
331     }
332
333     /**
334      * Initialize those attributes whose default values are independent of any context.
335      */

336     private void initializeHardCodedDefaults(ILaunchConfigurationWorkingCopy config) {
337         config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_ALLOW_TERMINATE, false);
338         config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_VM_CONNECTOR, JavaRuntime.getDefaultVMConnector().getIdentifier());
339     }
340
341      /* (non-Javadoc)
342      * @see org.eclipse.debug.ui.AbstractLaunchConfigurationTab#isValid(org.eclipse.debug.core.ILaunchConfiguration)
343      */

344     public boolean isValid(ILaunchConfiguration config) {
345         setErrorMessage(null);
346         setMessage(null);
347         String JavaDoc name = fProjText.getText().trim();
348         if (name.length() > 0) {
349             if (!ResourcesPlugin.getWorkspace().getRoot().getProject(name).exists()) {
350                 setErrorMessage(LauncherMessages.JavaConnectTab_Project_does_not_exist_14);
351                 return false;
352             }
353         }
354         Iterator JavaDoc keys = fFieldEditorMap.keySet().iterator();
355         while (keys.hasNext()) {
356             String JavaDoc key = (String JavaDoc)keys.next();
357             Connector.Argument arg = (Connector.Argument)fArgumentMap.get(key);
358             FieldEditor editor = (FieldEditor)fFieldEditorMap.get(key);
359             if (editor instanceof StringFieldEditor) {
360                 String JavaDoc value = ((StringFieldEditor)editor).getStringValue();
361                 if (!arg.isValid(value)) {
362                     setErrorMessage(arg.label() + LauncherMessages.JavaConnectTab__is_invalid__5);
363                     return false;
364                 }
365             }
366         }
367         return true;
368     }
369
370     /* (non-Javadoc)
371      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getName()
372      */

373     public String JavaDoc getName() {
374         return LauncherMessages.JavaConnectTab_Conn_ect_20;
375     }
376
377     /* (non-Javadoc)
378      * @see org.eclipse.debug.ui.AbstractLaunchConfigurationTab#getImage()
379      */

380     public Image getImage() {
381         return DebugUITools.getImage(IDebugUIConstants.IMG_LCL_DISCONNECT);
382     }
383         
384     /**
385      * @see org.eclipse.debug.ui.AbstractLaunchConfigurationTab#getId()
386      *
387      * @since 3.3
388      */

389     public String JavaDoc getId() {
390         return "org.eclipse.jdt.debug.ui.javaConnectTab"; //$NON-NLS-1$
391
}
392     
393     /**
394      * Returns the selected connector
395      */

396     private IVMConnector getSelectedConnector() {
397         return fConnector;
398     }
399
400     /* (non-Javadoc)
401      * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
402      */

403     public void propertyChange(PropertyChangeEvent event) {
404         updateLaunchConfigurationDialog();
405     }
406 }
407
Popular Tags