1 11 package org.eclipse.debug.internal.ui.preferences; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.variables.IStringVariableManager; 19 import org.eclipse.core.variables.IValueVariable; 20 import org.eclipse.core.variables.VariablesPlugin; 21 import org.eclipse.debug.internal.ui.DebugUIPlugin; 22 import org.eclipse.debug.internal.ui.IDebugHelpContextIds; 23 import org.eclipse.debug.internal.ui.MultipleInputDialog; 24 import org.eclipse.debug.internal.ui.SWTFactory; 25 import org.eclipse.jface.dialogs.IDialogConstants; 26 import org.eclipse.jface.dialogs.MessageDialog; 27 import org.eclipse.jface.preference.PreferencePage; 28 import org.eclipse.jface.viewers.ColumnLayoutData; 29 import org.eclipse.jface.viewers.ColumnWeightData; 30 import org.eclipse.jface.viewers.DoubleClickEvent; 31 import org.eclipse.jface.viewers.IColorProvider; 32 import org.eclipse.jface.viewers.IDoubleClickListener; 33 import org.eclipse.jface.viewers.ISelectionChangedListener; 34 import org.eclipse.jface.viewers.IStructuredContentProvider; 35 import org.eclipse.jface.viewers.IStructuredSelection; 36 import org.eclipse.jface.viewers.ITableLabelProvider; 37 import org.eclipse.jface.viewers.LabelProvider; 38 import org.eclipse.jface.viewers.SelectionChangedEvent; 39 import org.eclipse.jface.viewers.TableLayout; 40 import org.eclipse.jface.viewers.TableViewer; 41 import org.eclipse.jface.viewers.Viewer; 42 import org.eclipse.jface.viewers.ViewerComparator; 43 import org.eclipse.jface.viewers.ViewerFilter; 44 import org.eclipse.jface.window.Window; 45 import org.eclipse.swt.SWT; 46 import org.eclipse.swt.events.KeyAdapter; 47 import org.eclipse.swt.events.KeyEvent; 48 import org.eclipse.swt.events.SelectionAdapter; 49 import org.eclipse.swt.events.SelectionEvent; 50 import org.eclipse.swt.graphics.Color; 51 import org.eclipse.swt.graphics.Font; 52 import org.eclipse.swt.graphics.Image; 53 import org.eclipse.swt.layout.GridData; 54 import org.eclipse.swt.layout.GridLayout; 55 import org.eclipse.swt.widgets.Button; 56 import org.eclipse.swt.widgets.Composite; 57 import org.eclipse.swt.widgets.Control; 58 import org.eclipse.swt.widgets.Display; 59 import org.eclipse.swt.widgets.Table; 60 import org.eclipse.swt.widgets.TableColumn; 61 import org.eclipse.ui.IWorkbench; 62 import org.eclipse.ui.IWorkbenchPreferencePage; 63 import org.eclipse.ui.PlatformUI; 64 65 import com.ibm.icu.text.MessageFormat; 66 67 74 public class StringVariablePreferencePage extends PreferencePage implements IWorkbenchPreferencePage { 75 76 private TableViewer variableTable; 77 protected Button envAddButton; 78 protected Button envEditButton; 79 protected Button envRemoveButton; 80 81 protected SimpleVariableContentProvider variableContentProvider= new SimpleVariableContentProvider(); 82 83 protected static final String NAME_LABEL= DebugPreferencesMessages.SimpleVariablePreferencePage_10; 84 protected static final String VALUE_LABEL = DebugPreferencesMessages.SimpleVariablePreferencePage_11; 85 protected static final String DESCRIPTION_LABEL = DebugPreferencesMessages.SimpleVariablePreferencePage_12; 86 87 protected static final String STRING_VARIABLE_PREFERENCE_KEY = "StringVariablePreferencePage"; 89 protected static String [] variableTableColumnProperties= { 90 "variable", "value", "description" }; 94 protected String [] variableTableColumnHeaders= { 95 DebugPreferencesMessages.SimpleVariablePreferencePage_3, 96 DebugPreferencesMessages.SimpleVariablePreferencePage_4, 97 DebugPreferencesMessages.SimpleVariablePreferencePage_5, 98 DebugPreferencesMessages.StringVariablePreferencePage_27 99 }; 100 protected ColumnLayoutData[] variableTableColumnLayouts= { 101 new ColumnWeightData(30), 102 new ColumnWeightData(25), 103 new ColumnWeightData(25), 104 new ColumnWeightData(20) 105 }; 106 107 public StringVariablePreferencePage() { 108 setDescription(DebugPreferencesMessages.SimpleVariablePreferencePage_6); 109 } 110 111 114 public void createControl(Composite parent) { 115 super.createControl(parent); 116 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IDebugHelpContextIds.SIMPLE_VARIABLE_PREFERENCE_PAGE); 117 } 118 119 protected Control createContents(Composite parent) { 120 noDefaultAndApplyButton(); 121 Font font= parent.getFont(); 122 Composite composite = new Composite(parent, SWT.NONE); 124 GridLayout layout = new GridLayout(); 125 layout.marginHeight=0; 126 layout.marginWidth=0; 127 layout.numColumns= 2; 128 composite.setLayout(layout); 129 composite.setFont(font); 130 131 createTable(composite); 132 createButtons(composite); 133 134 return composite; 135 } 136 137 141 private void createTable(Composite parent) { 142 Font font= parent.getFont(); 143 Composite tableComposite = new Composite(parent, SWT.NONE); 145 GridLayout layout = new GridLayout(); 146 layout.marginHeight = 0; 147 layout.marginWidth = 0; 148 layout.numColumns = 1; 149 GridData gridData = new GridData(GridData.FILL_BOTH); 150 gridData.heightHint = 150; 151 gridData.widthHint = 400; 152 tableComposite.setLayout(layout); 153 tableComposite.setLayoutData(gridData); 154 tableComposite.setFont(font); 155 variableTable = new TableViewer(tableComposite, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI | SWT.FULL_SELECTION); 157 Table table = variableTable.getTable(); 158 table.setHeaderVisible(true); 159 table.setLinesVisible(true); 160 table.setFont(font); 161 gridData = new GridData(GridData.FILL_BOTH); 162 variableTable.getControl().setLayoutData(gridData); 163 variableTable.setContentProvider(variableContentProvider); 164 variableTable.setColumnProperties(variableTableColumnProperties); 165 variableTable.addFilter(new VariableFilter()); 166 variableTable.setComparator(new ViewerComparator() { 167 public int compare(Viewer iViewer, Object e1, Object e2) { 168 if (e1 == null) { 169 return -1; 170 } else if (e2 == null) { 171 return 1; 172 } else { 173 return ((VariableWrapper)e1).getName().compareToIgnoreCase(((VariableWrapper)e2).getName()); 174 } 175 } 176 }); 177 178 variableTable.addSelectionChangedListener(new ISelectionChangedListener() { 179 public void selectionChanged(SelectionChangedEvent event) { 180 handleTableSelectionChanged(event); 181 } 182 }); 183 184 variableTable.addDoubleClickListener(new IDoubleClickListener() { 185 public void doubleClick(DoubleClickEvent event) { 186 if (!variableTable.getSelection().isEmpty()) { 187 handleEditButtonPressed(); 188 } 189 } 190 }); 191 variableTable.getTable().addKeyListener(new KeyAdapter() { 192 public void keyPressed(KeyEvent event) { 193 if (event.character == SWT.DEL && event.stateMask == 0) { 194 handleRemoveButtonPressed(); 195 } 196 } 197 }); 198 199 for (int i = 0; i < variableTableColumnHeaders.length; i++) { 200 TableColumn tc = new TableColumn(table, SWT.NONE, i); 201 tc.setResizable(variableTableColumnLayouts[i].resizable); 202 tc.setText(variableTableColumnHeaders[i]); 203 } 204 205 if (!restoreColumnWidths()){ 207 restoreDefaultColumnWidths(); 208 } 209 210 variableTable.setInput(getVariableManager()); 211 variableTable.setLabelProvider(new SimpleVariableLabelProvider()); 212 } 213 214 218 private void createButtons(Composite parent) { 219 Composite buttonComposite = new Composite(parent, SWT.NONE); 221 GridLayout glayout = new GridLayout(); 222 glayout.marginHeight = 0; 223 glayout.marginWidth = 0; 224 glayout.numColumns = 1; 225 GridData gdata = new GridData(GridData.VERTICAL_ALIGN_BEGINNING); 226 buttonComposite.setLayout(glayout); 227 buttonComposite.setLayoutData(gdata); 228 buttonComposite.setFont(parent.getFont()); 229 230 envAddButton = SWTFactory.createPushButton(buttonComposite, DebugPreferencesMessages.SimpleVariablePreferencePage_7, null); 232 envAddButton.addSelectionListener(new SelectionAdapter() 233 { 234 public void widgetSelected(SelectionEvent event) { 235 handleAddButtonPressed(); 236 } 237 }); 238 envEditButton = SWTFactory.createPushButton(buttonComposite, DebugPreferencesMessages.SimpleVariablePreferencePage_8, null); 239 envEditButton.addSelectionListener(new SelectionAdapter() 240 { 241 public void widgetSelected(SelectionEvent event) { 242 handleEditButtonPressed(); 243 } 244 }); 245 envEditButton.setEnabled(false); 246 envRemoveButton = SWTFactory.createPushButton(buttonComposite, DebugPreferencesMessages.SimpleVariablePreferencePage_9, null); 247 envRemoveButton.addSelectionListener(new SelectionAdapter() 248 { 249 public void widgetSelected(SelectionEvent event) { 250 handleRemoveButtonPressed(); 251 } 252 }); 253 envRemoveButton.setEnabled(false); 254 } 255 256 private void handleAddButtonPressed() { 257 boolean done = false; 258 String name = null; 259 String description = null; 260 String value = null; 261 while (!done){ 262 263 MultipleInputDialog dialog= new MultipleInputDialog(getShell(), DebugPreferencesMessages.SimpleVariablePreferencePage_13); 264 dialog.addTextField(NAME_LABEL, name, false); 265 dialog.addBrowseField(VALUE_LABEL, value, true); 266 dialog.addTextField(DESCRIPTION_LABEL, description, true); 267 268 if (dialog.open() != Window.OK) { 269 done = true; 270 } 271 else { 272 name= dialog.getStringValue(NAME_LABEL).trim(); 273 value = dialog.getStringValue(VALUE_LABEL); 274 description= dialog.getStringValue(DESCRIPTION_LABEL); 275 done = addVariable(name, description, value); 276 } 277 } 278 } 279 280 291 private boolean addVariable(String name, String description, String value) { 292 if (name == null || name.length() == 0){ 293 MessageDialog.openError(getShell(),DebugPreferencesMessages.StringVariablePreferencePage_21, DebugPreferencesMessages.StringVariablePreferencePage_20); 294 return false; 295 } 296 List editedVariables= variableContentProvider.getWorkingSetVariables(); 297 Iterator iter= editedVariables.iterator(); 298 while (iter.hasNext()) { 299 VariableWrapper currentVariable = (VariableWrapper) iter.next(); 300 if (!currentVariable.isRemoved()) { 301 String currentName = currentVariable.getName(); 302 if (currentName.equals(name)) { 303 if (currentVariable.isReadOnly()){ 304 MessageDialog.openError(getShell(),DebugPreferencesMessages.StringVariablePreferencePage_23, MessageFormat.format(DebugPreferencesMessages.StringVariablePreferencePage_22, new String [] {name})); 305 return false; 306 } 307 else { 308 MessageDialog dialog = new MessageDialog(getShell(), DebugPreferencesMessages.SimpleVariablePreferencePage_15, null, MessageFormat.format(DebugPreferencesMessages.SimpleVariablePreferencePage_16, new String [] {name}), MessageDialog.QUESTION, new String [] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL }, 0); 309 int overWrite= dialog.open(); 310 if (overWrite == 0) { 311 currentVariable.setValue(value); 312 currentVariable.setDescription(description); 313 variableTable.update(currentVariable, null); 314 return true; 315 } else if(overWrite == 1){ 316 return false; 317 } else { 318 return true; } 320 } 321 } 322 } 323 } 324 VariableWrapper newVariable = new VariableWrapper(name, description, value); 325 variableContentProvider.addVariable(newVariable); 326 variableTable.refresh(); 327 return true; 328 } 329 330 private void handleEditButtonPressed() { 331 IStructuredSelection selection= (IStructuredSelection) variableTable.getSelection(); 332 VariableWrapper variable= (VariableWrapper) selection.getFirstElement(); 333 if (variable == null || variable.isReadOnly()) { 334 return; 335 } 336 String value= variable.getValue(); 337 String description= variable.getDescription(); 338 String name= variable.getName(); 339 MultipleInputDialog dialog= new MultipleInputDialog(getShell(), MessageFormat.format(DebugPreferencesMessages.SimpleVariablePreferencePage_14, new String [] {name})); 340 dialog.addBrowseField(VALUE_LABEL, value, true); 341 dialog.addTextField(DESCRIPTION_LABEL, description, true); 342 343 if (dialog.open() == Window.OK) { 344 value= dialog.getStringValue(VALUE_LABEL); 345 description= dialog.getStringValue(DESCRIPTION_LABEL); 346 if (value != null) { 347 variable.setValue(value); 348 } 349 if (description != null) { 350 variable.setDescription(description); 351 } 352 variableTable.update(variable, null); 353 } 354 } 355 356 359 private void handleRemoveButtonPressed() { 360 IStructuredSelection selection= (IStructuredSelection) variableTable.getSelection(); 361 List variablesToRemove= selection.toList(); 362 StringBuffer contributedVariablesToRemove= new StringBuffer (); 363 Iterator iter= variablesToRemove.iterator(); 364 while (iter.hasNext()) { 365 VariableWrapper variable = (VariableWrapper) iter.next(); 366 if (variable.isContributed()) { 367 contributedVariablesToRemove.append('\t').append(variable.getName()).append('\n'); 368 } 369 } 370 if (contributedVariablesToRemove.length() > 0) { 371 boolean remove= MessageDialog.openQuestion(getShell(), DebugPreferencesMessages.SimpleLaunchVariablePreferencePage_21, MessageFormat.format(DebugPreferencesMessages.SimpleLaunchVariablePreferencePage_22, new String [] {contributedVariablesToRemove.toString()})); if (!remove) { 373 return; 374 } 375 } 376 VariableWrapper[] variables= (VariableWrapper[]) variablesToRemove.toArray(new VariableWrapper[0]); 377 for (int i = 0; i < variables.length; i++) { 378 variables[i].setRemoved(true); 379 } 380 variableTable.refresh(); 381 } 382 383 387 protected void handleTableSelectionChanged(SelectionChangedEvent event) { 388 IStructuredSelection selection = ((IStructuredSelection)event.getSelection()); 389 VariableWrapper variable= (VariableWrapper) selection.getFirstElement(); 390 if (variable == null || variable.isReadOnly()) { 391 envEditButton.setEnabled(false); 392 envRemoveButton.setEnabled(false); 393 } 394 else { 395 envEditButton.setEnabled(selection.size() == 1); 396 envRemoveButton.setEnabled(selection.size() > 0); 397 } 398 } 399 400 public void init(IWorkbench workbench) { 401 } 402 403 406 protected void performDefaults() { 407 variableContentProvider.init(); 408 variableTable.refresh(); 409 super.performDefaults(); 410 } 411 412 415 public boolean performOk() { 416 variableContentProvider.saveChanges(); 417 saveColumnWidths(); 418 return super.performOk(); 419 } 420 421 426 private IStringVariableManager getVariableManager() { 427 return VariablesPlugin.getDefault().getStringVariableManager(); 428 } 429 430 public void saveColumnWidths() { 431 StringBuffer widthPreference = new StringBuffer (); 432 for (int i = 0; i < variableTable.getTable().getColumnCount(); i++) { 433 widthPreference.append(variableTable.getTable().getColumn(i).getWidth()); 434 widthPreference.append(','); 435 } 436 if (widthPreference.length() > 0){ 437 DebugUIPlugin.getDefault().getPreferenceStore().setValue(STRING_VARIABLE_PREFERENCE_KEY, widthPreference.toString()); 438 } 439 } 440 441 private boolean restoreColumnWidths() { 442 String [] columnWidthStrings = DebugUIPlugin.getDefault().getPreferenceStore().getString(STRING_VARIABLE_PREFERENCE_KEY).split(","); int columnCount = variableTable.getTable().getColumnCount(); 444 if (columnWidthStrings.length != columnCount){ 445 return false; } 447 for (int i = 0; i < columnCount; i++) { 448 try{ 449 int columnWidth = Integer.parseInt(columnWidthStrings[i]); 450 variableTable.getTable().getColumn(i).setWidth(columnWidth); 451 } catch (NumberFormatException e){ 452 DebugUIPlugin.log(new Throwable ("Problem loading persisted column sizes for StringVariablePreferencesPage",e)); } 454 } 455 return true; 456 } 457 458 private void restoreDefaultColumnWidths(){ 459 TableLayout layout = new TableLayout(); 460 for (int i = 0; i < variableTableColumnLayouts.length; i++) { 461 layout.addColumnData(variableTableColumnLayouts[i]); 462 } 463 variableTable.getTable().setLayout(layout); 464 } 465 466 private class SimpleVariableContentProvider implements IStructuredContentProvider { 467 470 private List fWorkingSet = new ArrayList (); 471 472 public Object [] getElements(Object inputElement) { 473 return fWorkingSet.toArray(); 474 } 475 476 481 public void addVariable(VariableWrapper variable) { 482 fWorkingSet.add(variable); 483 } 484 485 public void dispose() { 486 } 487 488 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 489 if (newInput == null || !(newInput instanceof IStringVariableManager)){ 490 return; 491 } 492 init(); 493 } 494 495 498 public void saveChanges() { 499 IStringVariableManager manager = getVariableManager(); 500 Iterator iterator = fWorkingSet.iterator(); 501 List remove = new ArrayList (); 502 List add = new ArrayList (); 503 while (iterator.hasNext()) { 504 VariableWrapper variable = (VariableWrapper) iterator.next(); 505 if (!variable.isReadOnly()) { 506 IValueVariable underlyingVariable = variable.getUnderlyingVariable(); 507 if (variable.isRemoved()) { 508 if (underlyingVariable != null) { 509 remove.add(underlyingVariable); 511 } 512 } else if (variable.isAdded()) { 513 IValueVariable vv = manager.newValueVariable(variable.getName(), variable.getDescription()); 514 vv.setValue(variable.getValue()); 515 add.add(vv); 516 } else if (variable.isChanged()) { 517 underlyingVariable.setValue(variable.getValue()); 518 underlyingVariable.setDescription(variable.getDescription()); 519 } 520 } 521 } 522 if (!remove.isEmpty()) { 524 manager.removeVariables((IValueVariable[]) remove.toArray(new IValueVariable[remove.size()])); 525 } 526 if (!add.isEmpty()) { 528 try { 529 manager.addVariables((IValueVariable[]) add.toArray(new IValueVariable[add.size()])); 530 } catch (CoreException e) { 531 DebugUIPlugin.errorDialog(getShell(), DebugPreferencesMessages.StringVariablePreferencePage_24, DebugPreferencesMessages.StringVariablePreferencePage_25, e.getStatus()); } 533 } 534 } 535 536 539 public void init() { 540 fWorkingSet.clear(); 541 IStringVariableManager manager = getVariableManager(); 542 IValueVariable[] variables = manager.getValueVariables(); 543 for (int i = 0; i < variables.length; i++) { 544 fWorkingSet.add(new VariableWrapper(variables[i])); 545 } 546 } 547 548 553 public List getWorkingSetVariables() { 554 return fWorkingSet; 555 } 556 557 } 558 559 class VariableWrapper { 560 561 protected IValueVariable fVariable; 562 protected String fNewName = null; 563 protected String fNewDesc = null; 564 protected String fNewValue = null; 565 boolean fRemoved = false; 566 boolean fAdded = false; 567 568 public VariableWrapper(IValueVariable variable) { 569 fVariable = variable; 570 } 571 572 public VariableWrapper(String name, String desc, String value) { 573 fNewName = name; 574 fNewDesc = desc; 575 fNewValue = value; 576 fAdded = true; 577 } 578 579 public boolean isAdded() { 580 return fAdded; 581 } 582 583 public String getName() { 584 if (fNewName == null) { 585 return fVariable.getName(); 586 } 587 return fNewName; 588 } 589 590 public void setName(String name) { 591 fNewName = name; 592 } 593 594 public String getDescription() { 595 if (fNewDesc == null) { 596 return fVariable.getDescription(); 597 } 598 return fNewDesc; 599 } 600 601 public String getValue() { 602 if (fNewValue == null) { 603 return fVariable.getValue(); 604 } 605 return fNewValue; 606 } 607 608 public void setValue(String value) { 609 fNewValue = value; 610 } 611 612 public void setDescription(String desc) { 613 fNewDesc = desc; 614 } 615 616 public boolean isChanged() { 617 return !fAdded && !fRemoved && (fNewValue != null || fNewDesc != null); 618 } 619 620 public boolean isReadOnly() { 621 if (fVariable == null) { 622 return false; 623 } 624 return fVariable.isReadOnly(); 625 } 626 627 public boolean isContributed() { 628 if (fVariable == null) { 629 return false; 630 } 631 return fVariable.isContributed(); 632 } 633 634 public IValueVariable getUnderlyingVariable() { 635 return fVariable; 636 } 637 638 public boolean isRemoved() { 639 return fRemoved; 640 } 641 642 public void setRemoved(boolean removed) { 643 fRemoved = removed; 644 } 645 } 646 647 private class SimpleVariableLabelProvider extends LabelProvider implements ITableLabelProvider, IColorProvider { 648 public Image getColumnImage(Object element, int columnIndex) { 649 return null; 650 } 651 public String getColumnText(Object element, int columnIndex) { 652 if (element instanceof VariableWrapper) { 653 VariableWrapper variable= (VariableWrapper) element; 654 switch (columnIndex) { 655 case 0 : 656 StringBuffer name = new StringBuffer (); 657 name.append(variable.getName()); 658 if (variable.isReadOnly()){ 659 name.append(DebugPreferencesMessages.StringVariablePreferencePage_26); 660 } 661 return name.toString(); 662 case 1: 663 String value= variable.getValue(); 664 if (value == null) { 665 value= ""; } 667 return value; 668 case 2: 669 String description= variable.getDescription(); 670 if (description == null) { 671 description= ""; } 673 return description; 674 case 3: 675 String contribution = ""; if (variable.isContributed()) { 677 String pluginId = getVariableManager().getContributingPluginId(variable.getUnderlyingVariable()); 678 if (pluginId != null) { 679 contribution = pluginId; 680 } else { 681 contribution = DebugPreferencesMessages.SimpleLaunchVariablePreferencePage_23; 682 } 683 } 684 return contribution; 685 686 } 687 } 688 return null; 689 } 690 public Color getForeground(Object element) { 691 return null; 692 } 693 public Color getBackground(Object element) { 694 if (element instanceof VariableWrapper) { 695 if (((VariableWrapper) element).isReadOnly()) { 696 Display display= Display.getCurrent(); 697 return display.getSystemColor(SWT.COLOR_INFO_BACKGROUND); 698 } 699 } 700 return null; 701 } 702 } 703 704 class VariableFilter extends ViewerFilter { 705 706 709 public boolean select(Viewer viewer, Object parentElement, Object element) { 710 return !((VariableWrapper)element).isRemoved(); 711 } 712 713 } 714 } 715 | Popular Tags |