1 11 package org.eclipse.jdt.debug.ui.launchConfigurations; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import java.util.Map ; 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 68 public class JavaConnectTab extends AbstractJavaMainTab implements IPropertyChangeListener { 69 70 private Button fAllowTerminateButton; 72 private Map fArgumentMap; 73 private Map fFieldEditorMap = new HashMap (); 74 private Composite fArgumentComposite; 75 private Combo fConnectorCombo; 76 77 private IVMConnector fConnector; 79 private IVMConnector[] fConnectors = JavaRuntime.getVMConnectors(); 80 81 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 Group group = SWTFactory.createGroup(comp, LauncherMessages.JavaConnectTab_Connect_ion_Type__7, 1, 1, GridData.FILL_HORIZONTAL); 95 String [] names = new String [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 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 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; } 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 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 Iterator keys = vm.getArgumentOrder().iterator(); 148 while (keys.hasNext()) { 149 String key = (String )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 choices = ((Connector.SelectedArgument)arg).choices(); 157 String [][] namesAndValues = new String [choices.size()][2]; 158 Iterator iter = choices.iterator(); 159 int count = 0; 160 while (iter.hasNext()) { 161 String choice = (String )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 189 private String getLabel(String label) { 190 if (!label.endsWith(":")) { return label+":"; } 193 return label; 194 } 195 196 199 public void initializeFrom(ILaunchConfiguration config) { 200 super.initializeFrom(config); 201 updateAllowTerminateFromConfig(config); 202 updateConnectionFromConfig(config); 203 } 204 205 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 222 private void updateConnectionFromConfig(ILaunchConfiguration config) { 223 String 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 attrMap = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_CONNECT_MAP, (Map )null); 230 if (attrMap == null) { 231 return; 232 } 233 Iterator keys = attrMap.keySet().iterator(); 234 while (keys.hasNext()) { 235 String key = (String )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 value = (String )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 (value).intValue()); 248 } 249 editor.load(); 250 } 251 } 252 } 253 catch (CoreException ce) {JDIDebugUIPlugin.log(ce);} 254 } 255 256 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 attrMap = new HashMap (fFieldEditorMap.size()); 265 Iterator keys = fFieldEditorMap.keySet().iterator(); 266 while (keys.hasNext()) { 267 String key = (String )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 (editor.getPreferenceStore().getInt(key)).toString()); 282 } 283 } 284 config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CONNECT_MAP, attrMap); 285 } 286 287 290 private void initializeDefaults(IJavaElement javaElement, ILaunchConfigurationWorkingCopy config) { 291 initializeJavaProject(javaElement, config); 292 initializeName(javaElement, config); 293 initializeHardCodedDefaults(config); 294 } 295 296 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 313 private void initializeName(IJavaElement javaElement, ILaunchConfigurationWorkingCopy config) { 314 String 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 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 344 public boolean isValid(ILaunchConfiguration config) { 345 setErrorMessage(null); 346 setMessage(null); 347 String 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 keys = fFieldEditorMap.keySet().iterator(); 355 while (keys.hasNext()) { 356 String key = (String )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 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 373 public String getName() { 374 return LauncherMessages.JavaConnectTab_Conn_ect_20; 375 } 376 377 380 public Image getImage() { 381 return DebugUITools.getImage(IDebugUIConstants.IMG_LCL_DISCONNECT); 382 } 383 384 389 public String getId() { 390 return "org.eclipse.jdt.debug.ui.javaConnectTab"; } 392 393 396 private IVMConnector getSelectedConnector() { 397 return fConnector; 398 } 399 400 403 public void propertyChange(PropertyChangeEvent event) { 404 updateLaunchConfigurationDialog(); 405 } 406 } 407 | Popular Tags |