1 12 package org.eclipse.debug.ui; 13 14 import java.util.Comparator ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.Map ; 18 import java.util.TreeMap ; 19 20 import org.eclipse.core.runtime.CoreException; 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.ILaunchConfiguration; 25 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 26 import org.eclipse.debug.core.ILaunchManager; 27 import org.eclipse.debug.internal.ui.DebugPluginImages; 28 import org.eclipse.debug.internal.ui.DebugUIPlugin; 29 import org.eclipse.debug.internal.ui.IDebugHelpContextIds; 30 import org.eclipse.debug.internal.ui.MultipleInputDialog; 31 import org.eclipse.debug.internal.ui.launchConfigurations.EnvironmentVariable; 32 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationsMessages; 33 import org.eclipse.jface.dialogs.Dialog; 34 import org.eclipse.jface.dialogs.IDialogSettings; 35 import org.eclipse.jface.dialogs.MessageDialog; 36 import org.eclipse.jface.viewers.ColumnLayoutData; 37 import org.eclipse.jface.viewers.ColumnWeightData; 38 import org.eclipse.jface.viewers.DoubleClickEvent; 39 import org.eclipse.jface.viewers.IDoubleClickListener; 40 import org.eclipse.jface.viewers.ILabelProvider; 41 import org.eclipse.jface.viewers.ILabelProviderListener; 42 import org.eclipse.jface.viewers.ISelectionChangedListener; 43 import org.eclipse.jface.viewers.IStructuredContentProvider; 44 import org.eclipse.jface.viewers.IStructuredSelection; 45 import org.eclipse.jface.viewers.ITableLabelProvider; 46 import org.eclipse.jface.viewers.LabelProvider; 47 import org.eclipse.jface.viewers.SelectionChangedEvent; 48 import org.eclipse.jface.viewers.TableLayout; 49 import org.eclipse.jface.viewers.TableViewer; 50 import org.eclipse.jface.viewers.Viewer; 51 import org.eclipse.jface.viewers.ViewerComparator; 52 import org.eclipse.jface.window.Window; 53 import org.eclipse.swt.SWT; 54 import org.eclipse.swt.events.SelectionAdapter; 55 import org.eclipse.swt.events.SelectionEvent; 56 import org.eclipse.swt.graphics.Font; 57 import org.eclipse.swt.graphics.Image; 58 import org.eclipse.swt.layout.GridData; 59 import org.eclipse.swt.layout.GridLayout; 60 import org.eclipse.swt.widgets.Button; 61 import org.eclipse.swt.widgets.Composite; 62 import org.eclipse.swt.widgets.Label; 63 import org.eclipse.swt.widgets.Shell; 64 import org.eclipse.swt.widgets.Table; 65 import org.eclipse.swt.widgets.TableColumn; 66 import org.eclipse.swt.widgets.TableItem; 67 import org.eclipse.ui.PlatformUI; 68 import org.eclipse.ui.dialogs.ListSelectionDialog; 69 70 import com.ibm.icu.text.MessageFormat; 71 72 81 public class EnvironmentTab extends AbstractLaunchConfigurationTab { 82 83 protected TableViewer environmentTable; 84 protected String [] envTableColumnHeaders = 85 { 86 LaunchConfigurationsMessages.EnvironmentTab_Variable_1, 87 LaunchConfigurationsMessages.EnvironmentTab_Value_2, 88 }; 89 protected ColumnLayoutData[] envTableColumnLayouts = 90 { 91 new ColumnWeightData(50), 92 new ColumnWeightData(50) 93 }; 94 private static final String NAME_LABEL= LaunchConfigurationsMessages.EnvironmentTab_8; 95 private static final String VALUE_LABEL= LaunchConfigurationsMessages.EnvironmentTab_9; 96 protected static final String P_VARIABLE = "variable"; protected static final String P_VALUE = "value"; protected static String [] envTableColumnProperties = 99 { 100 P_VARIABLE, 101 P_VALUE 102 }; 103 protected Button envAddButton; 104 protected Button envEditButton; 105 protected Button envRemoveButton; 106 protected Button appendEnvironment; 107 protected Button replaceEnvironment; 108 protected Button envSelectButton; 109 110 113 protected class EnvironmentVariableContentProvider implements IStructuredContentProvider { 114 public Object [] getElements(Object inputElement) { 115 EnvironmentVariable[] elements = new EnvironmentVariable[0]; 116 ILaunchConfiguration config = (ILaunchConfiguration) inputElement; 117 Map m; 118 try { 119 m = config.getAttribute(ILaunchManager.ATTR_ENVIRONMENT_VARIABLES, (Map ) null); 120 } catch (CoreException e) { 121 DebugUIPlugin.log(new Status(IStatus.ERROR, DebugUIPlugin.getUniqueIdentifier(), IStatus.ERROR, "Error reading configuration", e)); return elements; 123 } 124 if (m != null && !m.isEmpty()) { 125 elements = new EnvironmentVariable[m.size()]; 126 String [] varNames = new String [m.size()]; 127 m.keySet().toArray(varNames); 128 for (int i = 0; i < m.size(); i++) { 129 elements[i] = new EnvironmentVariable(varNames[i], (String ) m.get(varNames[i])); 130 } 131 } 132 return elements; 133 } 134 public void dispose() { 135 } 136 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 137 if (newInput == null){ 138 return; 139 } 140 if (viewer instanceof TableViewer){ 141 TableViewer tableViewer= (TableViewer) viewer; 142 if (tableViewer.getTable().isDisposed()) { 143 return; 144 } 145 tableViewer.setComparator(new ViewerComparator() { 146 public int compare(Viewer iviewer, Object e1, Object e2) { 147 if (e1 == null) { 148 return -1; 149 } else if (e2 == null) { 150 return 1; 151 } else { 152 return ((EnvironmentVariable)e1).getName().compareToIgnoreCase(((EnvironmentVariable)e2).getName()); 153 } 154 } 155 }); 156 } 157 } 158 } 159 160 163 public class EnvironmentVariableLabelProvider extends LabelProvider implements ITableLabelProvider { 164 public String getColumnText(Object element, int columnIndex) { 165 String result = null; 166 if (element != null) { 167 EnvironmentVariable var = (EnvironmentVariable) element; 168 switch (columnIndex) { 169 case 0: result = var.getName(); 171 break; 172 case 1: result = var.getValue(); 174 break; 175 } 176 } 177 return result; 178 } 179 public Image getColumnImage(Object element, int columnIndex) { 180 if (columnIndex == 0) { 181 return DebugPluginImages.getImage(IDebugUIConstants.IMG_OBJS_ENV_VAR); 182 } 183 return null; 184 } 185 } 186 187 190 public void createControl(Composite parent) { 191 Composite mainComposite = new Composite(parent, SWT.NONE); 193 setControl(mainComposite); 194 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IDebugHelpContextIds.LAUNCH_CONFIGURATION_DIALOG_ENVIRONMENT_TAB); 195 GridLayout layout = new GridLayout(); 196 layout.numColumns = 2; 197 GridData gridData = new GridData(GridData.FILL_HORIZONTAL); 198 mainComposite.setLayout(layout); 199 mainComposite.setLayoutData(gridData); 200 mainComposite.setFont(parent.getFont()); 201 202 createEnvironmentTable(mainComposite); 203 createTableButtons(mainComposite); 204 createAppendReplace(mainComposite); 205 206 Dialog.applyDialogFont(mainComposite); 207 } 208 209 215 protected void createAppendReplace(Composite parent) { 216 Composite appendReplaceComposite= new Composite(parent, SWT.NONE); 217 GridData gridData= new GridData(); 218 gridData.horizontalSpan= 2; 219 GridLayout layout= new GridLayout(); 220 appendReplaceComposite.setLayoutData(gridData); 221 appendReplaceComposite.setLayout(layout); 222 appendReplaceComposite.setFont(parent.getFont()); 223 224 appendEnvironment= createRadioButton(appendReplaceComposite, LaunchConfigurationsMessages.EnvironmentTab_16); 225 appendEnvironment.addSelectionListener(new SelectionAdapter() { 226 public void widgetSelected(SelectionEvent e) { 227 updateLaunchConfigurationDialog(); 228 } 229 }); 230 replaceEnvironment= createRadioButton(appendReplaceComposite, LaunchConfigurationsMessages.EnvironmentTab_17); 231 } 232 233 237 protected void updateAppendReplace() { 238 boolean enable= environmentTable.getTable().getItemCount() > 0; 239 appendEnvironment.setEnabled(enable); 240 replaceEnvironment.setEnabled(enable); 241 } 242 243 248 protected void createEnvironmentTable(Composite parent) { 249 Font font= parent.getFont(); 250 Composite tableComposite = new Composite(parent, SWT.NONE); 252 GridLayout layout = new GridLayout(); 253 layout.marginHeight = 0; 254 layout.marginWidth = 0; 255 layout.numColumns = 1; 256 GridData gridData = new GridData(GridData.FILL_BOTH); 257 gridData.heightHint = 150; 258 tableComposite.setLayout(layout); 259 tableComposite.setLayoutData(gridData); 260 tableComposite.setFont(font); 261 Label label = new Label(tableComposite, SWT.NONE); 263 label.setFont(font); 264 label.setText(LaunchConfigurationsMessages.EnvironmentTab_Environment_variables_to_set__3); 265 environmentTable = new TableViewer(tableComposite, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI | SWT.FULL_SELECTION); 267 Table table = environmentTable.getTable(); 268 TableLayout tableLayout = new TableLayout(); 269 table.setLayout(tableLayout); 270 table.setHeaderVisible(true); 271 table.setFont(font); 272 gridData = new GridData(GridData.FILL_BOTH); 273 environmentTable.getControl().setLayoutData(gridData); 274 environmentTable.setContentProvider(new EnvironmentVariableContentProvider()); 275 environmentTable.setLabelProvider(new EnvironmentVariableLabelProvider()); 276 environmentTable.setColumnProperties(envTableColumnProperties); 277 environmentTable.addSelectionChangedListener(new ISelectionChangedListener() { 278 public void selectionChanged(SelectionChangedEvent event) { 279 handleTableSelectionChanged(event); 280 } 281 }); 282 environmentTable.addDoubleClickListener(new IDoubleClickListener() { 283 public void doubleClick(DoubleClickEvent event) { 284 if (!environmentTable.getSelection().isEmpty()) { 285 handleEnvEditButtonSelected(); 286 } 287 } 288 }); 289 for (int i = 0; i < envTableColumnHeaders.length; i++) { 291 tableLayout.addColumnData(envTableColumnLayouts[i]); 292 TableColumn tc = new TableColumn(table, SWT.NONE, i); 293 tc.setResizable(envTableColumnLayouts[i].resizable); 294 tc.setText(envTableColumnHeaders[i]); 295 } 296 } 297 298 302 protected void handleTableSelectionChanged(SelectionChangedEvent event) { 303 int size = ((IStructuredSelection)event.getSelection()).size(); 304 envEditButton.setEnabled(size == 1); 305 envRemoveButton.setEnabled(size > 0); 306 } 307 308 312 protected void createTableButtons(Composite parent) { 313 Composite buttonComposite = new Composite(parent, SWT.NONE); 315 GridLayout glayout = new GridLayout(); 316 glayout.marginHeight = 0; 317 glayout.marginWidth = 0; 318 glayout.numColumns = 1; 319 GridData gdata = new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.HORIZONTAL_ALIGN_END); 320 buttonComposite.setLayout(glayout); 321 buttonComposite.setLayoutData(gdata); 322 buttonComposite.setFont(parent.getFont()); 323 324 createVerticalSpacer(buttonComposite, 1); 325 envAddButton = createPushButton(buttonComposite, LaunchConfigurationsMessages.EnvironmentTab_New_4, null); 327 envAddButton.addSelectionListener(new SelectionAdapter() 328 { 329 public void widgetSelected(SelectionEvent event) { 330 handleEnvAddButtonSelected(); 331 } 332 }); 333 envSelectButton = createPushButton(buttonComposite, LaunchConfigurationsMessages.EnvironmentTab_18, null); 334 envSelectButton.addSelectionListener(new SelectionAdapter() { 335 public void widgetSelected(SelectionEvent event) { 336 handleEnvSelectButtonSelected(); 337 } 338 }); 339 envEditButton = createPushButton(buttonComposite, LaunchConfigurationsMessages.EnvironmentTab_Edit_5, null); 340 envEditButton.addSelectionListener(new SelectionAdapter() 341 { 342 public void widgetSelected(SelectionEvent event) { 343 handleEnvEditButtonSelected(); 344 } 345 }); 346 envEditButton.setEnabled(false); 347 envRemoveButton = createPushButton(buttonComposite, LaunchConfigurationsMessages.EnvironmentTab_Remove_6, null); 348 envRemoveButton.addSelectionListener(new SelectionAdapter() 349 { 350 public void widgetSelected(SelectionEvent event) { 351 handleEnvRemoveButtonSelected(); 352 } 353 }); 354 envRemoveButton.setEnabled(false); 355 } 356 357 360 protected void handleEnvAddButtonSelected() { 361 MultipleInputDialog dialog = new MultipleInputDialog(getShell(), LaunchConfigurationsMessages.EnvironmentTab_22); 362 dialog.addTextField(NAME_LABEL, null, false); 363 dialog.addVariablesField(VALUE_LABEL, null, true); 364 365 if (dialog.open() != Window.OK) { 366 return; 367 } 368 369 String name = dialog.getStringValue(NAME_LABEL); 370 String value = dialog.getStringValue(VALUE_LABEL); 371 372 if (name != null && value != null && name.length() > 0 && value.length() >0) { 373 addVariable(new EnvironmentVariable(name.trim(), value.trim())); 374 updateAppendReplace(); 375 } 376 } 377 378 385 protected boolean addVariable(EnvironmentVariable variable) { 386 String name= variable.getName(); 387 TableItem[] items = environmentTable.getTable().getItems(); 388 for (int i = 0; i < items.length; i++) { 389 EnvironmentVariable existingVariable = (EnvironmentVariable) items[i].getData(); 390 if (existingVariable.getName().equals(name)) { 391 boolean overWrite= MessageDialog.openQuestion(getShell(), LaunchConfigurationsMessages.EnvironmentTab_12, MessageFormat.format(LaunchConfigurationsMessages.EnvironmentTab_13, new String [] {name})); if (!overWrite) { 393 return false; 394 } 395 environmentTable.remove(existingVariable); 396 break; 397 } 398 } 399 environmentTable.add(variable); 400 updateLaunchConfigurationDialog(); 401 return true; 402 } 403 404 408 private void handleEnvSelectButtonSelected() { 409 Map envVariables = getNativeEnvironment(); 411 412 TableItem[] items = environmentTable.getTable().getItems(); 414 for (int i = 0; i < items.length; i++) { 415 EnvironmentVariable var = (EnvironmentVariable) items[i].getData(); 416 envVariables.remove(var.getName()); 417 } 418 419 ListSelectionDialog dialog = new NativeEnvironmentDialog(getShell(), envVariables, createSelectionDialogContentProvider(), createSelectionDialogLabelProvider(), LaunchConfigurationsMessages.EnvironmentTab_19); 420 dialog.setTitle(LaunchConfigurationsMessages.EnvironmentTab_20); 421 422 int button = dialog.open(); 423 if (button == Window.OK) { 424 Object [] selected = dialog.getResult(); 425 for (int i = 0; i < selected.length; i++) { 426 environmentTable.add(selected[i]); 427 } 428 } 429 430 updateAppendReplace(); 431 updateLaunchConfigurationDialog(); 432 } 433 434 438 private ILabelProvider createSelectionDialogLabelProvider() { 439 return new ILabelProvider() { 440 public Image getImage(Object element) { 441 return DebugPluginImages.getImage(IDebugUIConstants.IMG_OBJS_ENVIRONMENT); 442 } 443 public String getText(Object element) { 444 EnvironmentVariable var = (EnvironmentVariable) element; 445 return MessageFormat.format(LaunchConfigurationsMessages.EnvironmentTab_7, new String [] {var.getName(), var.getValue()}); 446 } 447 public void addListener(ILabelProviderListener listener) { 448 } 449 public void dispose() { 450 } 451 public boolean isLabelProperty(Object element, String property) { 452 return false; 453 } 454 public void removeListener(ILabelProviderListener listener) { 455 } 456 }; 457 } 458 459 463 private IStructuredContentProvider createSelectionDialogContentProvider() { 464 return new IStructuredContentProvider() { 465 public Object [] getElements(Object inputElement) { 466 EnvironmentVariable[] elements = null; 467 if (inputElement instanceof HashMap ) { 468 Comparator comparator = new Comparator () { 469 public int compare(Object o1, Object o2) { 470 String s1 = (String )o1; 471 String s2 = (String )o2; 472 return s1.compareTo(s2); 473 } 474 }; 475 TreeMap envVars = new TreeMap (comparator); 476 envVars.putAll((Map )inputElement); 477 elements = new EnvironmentVariable[envVars.size()]; 478 int index = 0; 479 for (Iterator iterator = envVars.keySet().iterator(); iterator.hasNext(); index++) { 480 Object key = iterator.next(); 481 elements[index] = (EnvironmentVariable) envVars.get(key); 482 } 483 } 484 return elements; 485 } 486 public void dispose() { 487 } 488 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 489 } 490 }; 491 } 492 493 497 private Map getNativeEnvironment() { 498 Map stringVars = DebugPlugin.getDefault().getLaunchManager().getNativeEnvironmentCasePreserved(); 499 HashMap vars = new HashMap (); 500 for (Iterator i = stringVars.keySet().iterator(); i.hasNext(); ) { 501 String key = (String ) i.next(); 502 String value = (String ) stringVars.get(key); 503 vars.put(key, new EnvironmentVariable(key, value)); 504 } 505 return vars; 506 } 507 508 511 private void handleEnvEditButtonSelected() { 512 IStructuredSelection sel= (IStructuredSelection) environmentTable.getSelection(); 513 EnvironmentVariable var= (EnvironmentVariable) sel.getFirstElement(); 514 if (var == null) { 515 return; 516 } 517 String originalName= var.getName(); 518 String value= var.getValue(); 519 MultipleInputDialog dialog= new MultipleInputDialog(getShell(), LaunchConfigurationsMessages.EnvironmentTab_11); 520 dialog.addTextField(NAME_LABEL, originalName, false); 521 dialog.addVariablesField(VALUE_LABEL, value, true); 522 523 if (dialog.open() != Window.OK) { 524 return; 525 } 526 String name= dialog.getStringValue(NAME_LABEL); 527 value= dialog.getStringValue(VALUE_LABEL); 528 if (!originalName.equals(name)) { 529 if (addVariable(new EnvironmentVariable(name, value))) { 530 environmentTable.remove(var); 531 } 532 } else { 533 var.setValue(value); 534 environmentTable.update(var, null); 535 updateLaunchConfigurationDialog(); 536 } 537 } 538 539 542 private void handleEnvRemoveButtonSelected() { 543 IStructuredSelection sel = (IStructuredSelection) environmentTable.getSelection(); 544 environmentTable.getControl().setRedraw(false); 545 for (Iterator i = sel.iterator(); i.hasNext(); ) { 546 EnvironmentVariable var = (EnvironmentVariable) i.next(); 547 environmentTable.remove(var); 548 } 549 environmentTable.getControl().setRedraw(true); 550 updateAppendReplace(); 551 updateLaunchConfigurationDialog(); 552 } 553 554 558 protected void updateEnvironment(ILaunchConfiguration configuration) { 559 environmentTable.setInput(configuration); 560 } 561 562 565 public void setDefaults(ILaunchConfigurationWorkingCopy configuration) { 566 configuration.setAttribute(ILaunchManager.ATTR_APPEND_ENVIRONMENT_VARIABLES, true); 567 } 568 569 572 public void initializeFrom(ILaunchConfiguration configuration) { 573 boolean append= true; 574 try { 575 append = configuration.getAttribute(ILaunchManager.ATTR_APPEND_ENVIRONMENT_VARIABLES, true); 576 } catch (CoreException e) { 577 DebugUIPlugin.log(e.getStatus()); 578 } 579 if (append) { 580 appendEnvironment.setSelection(true); 581 replaceEnvironment.setSelection(false); 582 } else { 583 replaceEnvironment.setSelection(true); 584 appendEnvironment.setSelection(false); 585 } 586 updateEnvironment(configuration); 587 updateAppendReplace(); 588 } 589 590 594 public void performApply(ILaunchConfigurationWorkingCopy configuration) { 595 TableItem[] items = environmentTable.getTable().getItems(); 598 Map map = new HashMap (items.length); 599 for (int i = 0; i < items.length; i++) 600 { 601 EnvironmentVariable var = (EnvironmentVariable) items[i].getData(); 602 map.put(var.getName(), var.getValue()); 603 } 604 if (map.size() == 0) { 605 configuration.setAttribute(ILaunchManager.ATTR_ENVIRONMENT_VARIABLES, (Map ) null); 606 } else { 607 configuration.setAttribute(ILaunchManager.ATTR_ENVIRONMENT_VARIABLES, map); 608 } 609 configuration.setAttribute(ILaunchManager.ATTR_APPEND_ENVIRONMENT_VARIABLES, appendEnvironment.getSelection()); 610 } 611 612 615 public String getName() { 616 return LaunchConfigurationsMessages.EnvironmentTab_Environment_7; 617 } 618 619 624 public String getId() { 625 return "org.eclipse.debug.ui.environmentTab"; } 627 628 631 public Image getImage() { 632 return DebugPluginImages.getImage(IDebugUIConstants.IMG_OBJS_ENVIRONMENT); 633 } 634 635 638 public void activated(ILaunchConfigurationWorkingCopy workingCopy) { 639 } 641 642 645 public void deactivated(ILaunchConfigurationWorkingCopy workingCopy) { 646 } 648 649 private class NativeEnvironmentDialog extends ListSelectionDialog { 650 public NativeEnvironmentDialog(Shell parentShell, Object input, IStructuredContentProvider contentProvider, ILabelProvider labelProvider, String message) { 651 super(parentShell, input, contentProvider, labelProvider, message); 652 setShellStyle(getShellStyle() | SWT.RESIZE); 653 } 654 655 660 protected String getDialogSettingsSectionName() { 661 return IDebugUIConstants.PLUGIN_ID + ".ENVIRONMENT_TAB.NATIVE_ENVIROMENT_DIALOG"; } 663 664 667 protected IDialogSettings getDialogBoundsSettings() { 668 IDialogSettings settings = DebugUIPlugin.getDefault().getDialogSettings(); 669 IDialogSettings section = settings.getSection(getDialogSettingsSectionName()); 670 if (section == null) { 671 section = settings.addNewSection(getDialogSettingsSectionName()); 672 } 673 return section; 674 } 675 } 676 } 677 | Popular Tags |