1 11 package org.eclipse.jdt.internal.ui.preferences; 12 13 import java.util.ArrayList ; 14 import java.util.Collections ; 15 import java.util.Comparator ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 21 import org.eclipse.core.commands.Command; 22 import org.eclipse.core.commands.CommandManager; 23 import org.eclipse.core.commands.IParameter; 24 import org.eclipse.core.commands.Parameterization; 25 import org.eclipse.core.commands.ParameterizedCommand; 26 import org.eclipse.core.commands.common.NotDefinedException; 27 import org.eclipse.core.commands.contexts.ContextManager; 28 29 import org.eclipse.core.runtime.Assert; 30 31 import org.eclipse.swt.SWT; 32 import org.eclipse.swt.events.SelectionAdapter; 33 import org.eclipse.swt.events.SelectionEvent; 34 import org.eclipse.swt.graphics.GC; 35 import org.eclipse.swt.graphics.Image; 36 import org.eclipse.swt.layout.GridData; 37 import org.eclipse.swt.layout.GridLayout; 38 import org.eclipse.swt.widgets.Button; 39 import org.eclipse.swt.widgets.Composite; 40 import org.eclipse.swt.widgets.Control; 41 import org.eclipse.swt.widgets.Label; 42 import org.eclipse.swt.widgets.Link; 43 import org.eclipse.swt.widgets.Table; 44 import org.eclipse.swt.widgets.TableColumn; 45 46 import org.eclipse.jface.bindings.BindingManager; 47 import org.eclipse.jface.bindings.Scheme; 48 import org.eclipse.jface.bindings.TriggerSequence; 49 import org.eclipse.jface.resource.ImageDescriptor; 50 import org.eclipse.jface.resource.JFaceResources; 51 import org.eclipse.jface.viewers.ArrayContentProvider; 52 import org.eclipse.jface.viewers.CheckStateChangedEvent; 53 import org.eclipse.jface.viewers.CheckboxTableViewer; 54 import org.eclipse.jface.viewers.ICheckStateListener; 55 import org.eclipse.jface.viewers.IStructuredSelection; 56 import org.eclipse.jface.viewers.ITableLabelProvider; 57 import org.eclipse.jface.viewers.LabelProvider; 58 import org.eclipse.jface.viewers.ViewerComparator; 59 60 import org.eclipse.ui.PlatformUI; 61 import org.eclipse.ui.commands.ICommandService; 62 import org.eclipse.ui.dialogs.PreferencesUtil; 63 import org.eclipse.ui.keys.IBindingService; 64 import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer; 65 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds; 66 67 import org.eclipse.jdt.core.JavaCore; 68 69 import org.eclipse.jdt.internal.corext.util.Messages; 70 71 import org.eclipse.jdt.ui.PreferenceConstants; 72 73 import org.eclipse.jdt.internal.ui.JavaPlugin; 74 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; 75 import org.eclipse.jdt.internal.ui.text.java.CompletionProposalCategory; 76 import org.eclipse.jdt.internal.ui.text.java.CompletionProposalComputerRegistry; 77 import org.eclipse.jdt.internal.ui.util.PixelConverter; 78 import org.eclipse.jdt.internal.ui.util.SWTUtil; 79 import org.eclipse.jdt.internal.ui.wizards.IStatusChangeListener; 80 81 85 final class CodeAssistAdvancedConfigurationBlock extends OptionsConfigurationBlock { 86 87 private static final Key PREF_EXCLUDED_CATEGORIES= getJDTUIKey(PreferenceConstants.CODEASSIST_EXCLUDED_CATEGORIES); 88 private static final Key PREF_CATEGORY_ORDER= getJDTUIKey(PreferenceConstants.CODEASSIST_CATEGORY_ORDER); 89 private static final Key PREF_CODEASSIST_TIMEOUT_FOR_PARAMETER_NAME_FROM_ATTACHED_JAVADOC= getJDTCoreKey(JavaCore.TIMEOUT_FOR_PARAMETER_NAME_FROM_ATTACHED_JAVADOC); 90 91 private static Key[] getAllKeys() { 92 return new Key[] { 93 PREF_EXCLUDED_CATEGORIES, 94 PREF_CATEGORY_ORDER, 95 PREF_CODEASSIST_TIMEOUT_FOR_PARAMETER_NAME_FROM_ATTACHED_JAVADOC 96 }; 97 } 98 99 private final class DefaultTableLabelProvider extends LabelProvider implements ITableLabelProvider { 100 101 104 public Image getColumnImage(Object element, int columnIndex) { 105 if (columnIndex == 0) 106 return ((ModelElement) element).getImage(); 107 return null; 108 } 109 110 113 public String getColumnText(Object element, int columnIndex) { 114 switch (columnIndex) { 115 case 0: 116 return ((ModelElement) element).getName(); 117 case 1: 118 return ((ModelElement) element).getKeybindingAsString(); 119 default: 120 Assert.isTrue(false); 121 return null; 122 } 123 } 124 125 128 public String getText(Object element) { 129 return getColumnText(element, 0); } 131 } 132 133 private final class SeparateTableLabelProvider extends LabelProvider implements ITableLabelProvider { 134 135 138 public Image getColumnImage(Object element, int columnIndex) { 139 if (columnIndex == 0) 140 return ((ModelElement) element).getImage(); 141 return null; 142 } 143 144 147 public String getColumnText(Object element, int columnIndex) { 148 switch (columnIndex) { 149 case 0: 150 return ((ModelElement) element).getName(); 151 default: 152 Assert.isTrue(false); 153 return null; 154 } 155 } 156 } 157 158 private final Comparator fCategoryComparator= new Comparator () { 159 private int getRank(Object o) { 160 return ((ModelElement) o).getRank(); 161 } 162 163 public int compare(Object o1, Object o2) { 164 return getRank(o1) - getRank(o2); 165 } 166 }; 167 168 private final class PreferenceModel { 169 private static final int LIMIT= 0xffff; 170 private static final String COLON= ":"; private static final String SEPARATOR= "\0"; 173 private final List fElements; 174 177 final List elements; 178 179 public PreferenceModel(CompletionProposalComputerRegistry registry) { 180 List categories= registry.getProposalCategories(); 181 fElements= new ArrayList (); 182 for (Iterator it= categories.iterator(); it.hasNext();) { 183 CompletionProposalCategory category= (CompletionProposalCategory) it.next(); 184 if (category.hasComputers()) { 185 fElements.add(new ModelElement(category, this)); 186 } 187 } 188 Collections.sort(fElements, fCategoryComparator); 189 elements= Collections.unmodifiableList(fElements); 190 } 191 192 public void moveUp(ModelElement category) { 193 int index= fElements.indexOf(category); 194 if (index > 0) { 195 Object item= fElements.remove(index); 196 fElements.add(index - 1, item); 197 writeOrderPreference(null, false); 198 } 199 } 200 201 public void moveDown(ModelElement category) { 202 int index= fElements.indexOf(category); 203 if (index < fElements.size() - 1) { 204 Object item= fElements.remove(index); 205 fElements.add(index + 1, item); 206 writeOrderPreference(null, false); 207 } 208 } 209 210 private void writeInclusionPreference(ModelElement changed, boolean isInDefaultCategory) { 211 StringBuffer buf= new StringBuffer (); 212 for (Iterator it= fElements.iterator(); it.hasNext();) { 213 ModelElement item= (ModelElement) it.next(); 214 boolean included= changed == item ? isInDefaultCategory : item.isInDefaultCategory(); 215 if (!included) 216 buf.append(item.getId() + SEPARATOR); 217 } 218 219 String newValue= buf.toString(); 220 String oldValue= setValue(PREF_EXCLUDED_CATEGORIES, newValue); 221 validateSettings(PREF_EXCLUDED_CATEGORIES, oldValue, newValue); 222 } 223 224 private void writeOrderPreference(ModelElement changed, boolean isSeparate) { 225 StringBuffer buf= new StringBuffer (); 226 int i= 0; 227 for (Iterator it= fElements.iterator(); it.hasNext(); i++) { 228 ModelElement item= (ModelElement) it.next(); 229 boolean separate= changed == item ? isSeparate : item.isSeparateCommand(); 230 int rank= separate ? i : i + LIMIT; 231 buf.append(item.getId() + COLON + rank + SEPARATOR); 232 } 233 234 String newValue= buf.toString(); 235 String oldValue= setValue(PREF_CATEGORY_ORDER, newValue); 236 validateSettings(PREF_CATEGORY_ORDER, oldValue, newValue); 237 } 238 239 240 private boolean readInclusionPreference(CompletionProposalCategory cat) { 241 String [] ids= getTokens(getValue(PREF_EXCLUDED_CATEGORIES), SEPARATOR); 242 for (int i= 0; i < ids.length; i++) { 243 if (ids[i].equals(cat.getId())) 244 return false; 245 } 246 return true; 247 } 248 249 private int readOrderPreference(CompletionProposalCategory cat) { 250 String [] sortOrderIds= getTokens(getValue(PREF_CATEGORY_ORDER), SEPARATOR); 251 for (int i= 0; i < sortOrderIds.length; i++) { 252 String [] idAndRank= getTokens(sortOrderIds[i], COLON); 253 if (idAndRank[0].equals(cat.getId())) 254 return Integer.parseInt(idAndRank[1]); 255 } 256 return LIMIT + 1; 257 } 258 259 public void update() { 260 Collections.sort(fElements, fCategoryComparator); 261 } 262 } 263 264 private final class ModelElement { 265 private final CompletionProposalCategory fCategory; 266 private final Command fCommand; 267 private final IParameter fParam; 268 private final PreferenceModel fPreferenceModel; 269 270 ModelElement(CompletionProposalCategory category, PreferenceModel model) { 271 fCategory= category; 272 ICommandService commandSvc= (ICommandService) PlatformUI.getWorkbench().getAdapter(ICommandService.class); 273 fCommand= commandSvc.getCommand("org.eclipse.jdt.ui.specific_content_assist.command"); IParameter type; 275 try { 276 type= fCommand.getParameters()[0]; 277 } catch (NotDefinedException x) { 278 Assert.isTrue(false); 279 type= null; 280 } 281 fParam= type; 282 fPreferenceModel= model; 283 } 284 Image getImage() { 285 return CodeAssistAdvancedConfigurationBlock.this.getImage(fCategory.getImageDescriptor()); 286 } 287 String getName() { 288 return fCategory.getDisplayName(); 289 } 290 String getKeybindingAsString() { 291 final Parameterization[] params= { new Parameterization(fParam, fCategory.getId()) }; 292 final ParameterizedCommand pCmd= new ParameterizedCommand(fCommand, params); 293 String key= getKeyboardShortcut(pCmd); 294 return key; 295 } 296 boolean isInDefaultCategory() { 297 return fPreferenceModel.readInclusionPreference(fCategory); 298 } 299 void setInDefaultCategory(boolean included) { 300 if (included != isInDefaultCategory()) 301 fPreferenceModel.writeInclusionPreference(this, included); 302 } 303 String getId() { 304 return fCategory.getId(); 305 } 306 int getRank() { 307 int rank= getInternalRank(); 308 if (rank > PreferenceModel.LIMIT) 309 return rank - PreferenceModel.LIMIT; 310 return rank; 311 } 312 void moveUp() { 313 fPreferenceModel.moveUp(this); 314 } 315 void moveDown() { 316 fPreferenceModel.moveDown(this); 317 } 318 private int getInternalRank() { 319 return fPreferenceModel.readOrderPreference(fCategory); 320 } 321 boolean isSeparateCommand() { 322 return getInternalRank() < PreferenceModel.LIMIT; 323 } 324 325 void setSeparateCommand(boolean separate) { 326 if (separate != isSeparateCommand()) 327 fPreferenceModel.writeOrderPreference(this, separate); 328 } 329 330 void update() { 331 fCategory.setIncluded(isInDefaultCategory()); 332 int rank= getInternalRank(); 333 fCategory.setSortOrder(rank); 334 fCategory.setSeparateCommand(rank < PreferenceModel.LIMIT); 335 } 336 } 337 338 339 private final PreferenceModel fModel; 340 private final Map fImages= new HashMap (); 341 342 private CheckboxTableViewer fDefaultViewer; 343 private CheckboxTableViewer fSeparateViewer; 344 private Button fUpButton; 345 private Button fDownButton; 346 347 CodeAssistAdvancedConfigurationBlock(IStatusChangeListener statusListener, IWorkbenchPreferenceContainer container) { 348 super(statusListener, null, getAllKeys(), container); 349 fModel= new PreferenceModel(CompletionProposalComputerRegistry.getDefault()); 350 } 351 352 355 protected Control createContents(Composite parent) { 356 357 ScrolledPageContent scrolled= new ScrolledPageContent(parent, SWT.H_SCROLL | SWT.V_SCROLL); 358 359 scrolled.setExpandHorizontal(true); 360 scrolled.setExpandVertical(true); 361 362 Composite composite= new Composite(scrolled, SWT.NONE); 363 int columns= 2; 364 GridLayout layout= new GridLayout(columns, false); 365 layout.marginWidth= 0; 366 layout.marginHeight= 0; 367 composite.setLayout(layout); 368 369 370 createDefaultLabel(composite, columns); 371 createDefaultViewer(composite, columns); 372 createKeysLink(composite, columns); 373 374 createFiller(composite, columns); 375 376 createSeparateLabel(composite, columns); 377 createSeparateSection(composite); 378 379 createFiller(composite, columns); 380 381 createParameterTimeoutControl(composite, columns); 382 383 updateControls(); 384 if (fModel.elements.size() > 0) { 385 fDefaultViewer.getTable().select(0); 386 fSeparateViewer.getTable().select(0); 387 handleTableSelection(); 388 } 389 390 scrolled.setContent(composite); 391 scrolled.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT)); 392 return scrolled; 393 } 394 395 private void createDefaultLabel(Composite composite, int h_span) { 396 final ICommandService commandSvc= (ICommandService) PlatformUI.getWorkbench().getAdapter(ICommandService.class); 397 final Command command= commandSvc.getCommand(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS); 398 ParameterizedCommand pCmd= new ParameterizedCommand(command, null); 399 String key= getKeyboardShortcut(pCmd); 400 if (key == null) 401 key= PreferencesMessages.CodeAssistAdvancedConfigurationBlock_no_shortcut; 402 403 PixelConverter pixelConverter= new PixelConverter(composite); 404 int width= pixelConverter.convertWidthInCharsToPixels(40); 405 406 Label label= new Label(composite, SWT.NONE | SWT.WRAP); 407 label.setText(Messages.format(PreferencesMessages.CodeAssistAdvancedConfigurationBlock_page_description, new Object [] { key })); 408 GridData gd= new GridData(GridData.FILL, GridData.FILL, true, false, h_span, 1); 409 gd.widthHint= width; 410 label.setLayoutData(gd); 411 412 createFiller(composite, h_span); 413 414 label= new Label(composite, SWT.NONE | SWT.WRAP); 415 label.setText(PreferencesMessages.CodeAssistAdvancedConfigurationBlock_default_table_description); 416 gd= new GridData(GridData.FILL, GridData.FILL, true, false, h_span, 1); 417 gd.widthHint= width; 418 label.setLayoutData(gd); 419 } 420 421 private void createDefaultViewer(Composite composite, int h_span) { 422 fDefaultViewer= CheckboxTableViewer.newCheckList(composite, SWT.SINGLE | SWT.BORDER); 423 Table table= fDefaultViewer.getTable(); 424 table.setHeaderVisible(true); 425 table.setLinesVisible(false); 426 table.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, false, false, h_span, 1)); 427 428 TableColumn nameColumn= new TableColumn(table, SWT.NONE); 429 nameColumn.setText(PreferencesMessages.CodeAssistAdvancedConfigurationBlock_default_table_category_column_title); 430 nameColumn.setResizable(false); 431 TableColumn keyColumn= new TableColumn(table, SWT.NONE); 432 keyColumn.setText(PreferencesMessages.CodeAssistAdvancedConfigurationBlock_default_table_keybinding_column_title); 433 keyColumn.setResizable(false); 434 435 fDefaultViewer.addCheckStateListener(new ICheckStateListener() { 436 public void checkStateChanged(CheckStateChangedEvent event) { 437 boolean checked= event.getChecked(); 438 ModelElement element= (ModelElement) event.getElement(); 439 element.setInDefaultCategory(checked); 440 } 441 }); 442 443 fDefaultViewer.setContentProvider(new ArrayContentProvider()); 444 445 DefaultTableLabelProvider labelProvider= new DefaultTableLabelProvider(); 446 fDefaultViewer.setLabelProvider(labelProvider); 447 fDefaultViewer.setInput(fModel.elements); 448 fDefaultViewer.setComparator(new ViewerComparator()); 450 final int ICON_AND_CHECKBOX_WITH= 50; 451 final int HEADER_MARGIN= 20; 452 int minNameWidth= computeWidth(table, nameColumn.getText()) + HEADER_MARGIN; 453 int minKeyWidth= computeWidth(table, keyColumn.getText()) + HEADER_MARGIN; 454 for (int i= 0; i < fModel.elements.size(); i++) { 455 minNameWidth= Math.max(minNameWidth, computeWidth(table, labelProvider.getColumnText(fModel.elements.get(i), 0)) + ICON_AND_CHECKBOX_WITH); 456 minKeyWidth= Math.max(minKeyWidth, computeWidth(table, labelProvider.getColumnText(fModel.elements.get(i), 1))); 457 } 458 459 nameColumn.setWidth(minNameWidth); 460 keyColumn.setWidth(minKeyWidth); 461 } 462 463 private void createKeysLink(Composite composite, int h_span) { 464 Link link= new Link(composite, SWT.NONE | SWT.WRAP); 465 link.setText(PreferencesMessages.CodeAssistAdvancedConfigurationBlock_key_binding_hint); 466 link.addSelectionListener(new SelectionAdapter() { 467 public void widgetSelected(SelectionEvent e) { 468 PreferencesUtil.createPreferenceDialogOn(getShell(), e.text, null, null); 469 } 470 }); 471 472 PixelConverter pixelConverter= new PixelConverter(composite); 473 int width= pixelConverter.convertWidthInCharsToPixels(40); 474 475 GridData gd= new GridData(GridData.FILL, GridData.FILL, false, false, h_span, 1); 477 gd.widthHint= width; 478 link.setLayoutData(gd); 479 } 480 481 private void createFiller(Composite composite, int h_span) { 482 Label filler= new Label(composite, SWT.NONE); 483 filler.setVisible(false); 484 filler.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, h_span, 1)); 485 } 486 487 private void createSeparateLabel(Composite composite, int h_span) { 488 PixelConverter pixelConverter= new PixelConverter(composite); 489 int width= pixelConverter.convertWidthInCharsToPixels(40); 490 491 Label label= new Label(composite, SWT.NONE | SWT.WRAP); 492 label.setText(PreferencesMessages.CodeAssistAdvancedConfigurationBlock_separate_table_description); 493 GridData gd= new GridData(GridData.FILL, GridData.FILL, false, false, h_span, 1); 494 gd.widthHint= width; 495 label.setLayoutData(gd); 496 } 497 498 private void createSeparateSection(Composite composite) { 499 createSeparateViewer(composite); 500 createButtonList(composite); 501 } 502 503 private void createSeparateViewer(Composite composite) { 504 fSeparateViewer= CheckboxTableViewer.newCheckList(composite, SWT.SINGLE | SWT.BORDER); 505 Table table= fSeparateViewer.getTable(); 506 table.setHeaderVisible(false); 507 table.setLinesVisible(false); 508 table.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, false, 1, 1)); 509 510 TableColumn nameColumn= new TableColumn(table, SWT.NONE); 511 nameColumn.setText(PreferencesMessages.CodeAssistAdvancedConfigurationBlock_separate_table_category_column_title); 512 nameColumn.setResizable(false); 513 514 fSeparateViewer.setContentProvider(new ArrayContentProvider()); 515 516 ITableLabelProvider labelProvider= new SeparateTableLabelProvider(); 517 fSeparateViewer.setLabelProvider(labelProvider); 518 fSeparateViewer.setInput(fModel.elements); 519 520 final int ICON_AND_CHECKBOX_WITH= 50; 521 final int HEADER_MARGIN= 20; 522 int minNameWidth= computeWidth(table, nameColumn.getText()) + HEADER_MARGIN; 523 for (int i= 0; i < fModel.elements.size(); i++) { 524 minNameWidth= Math.max(minNameWidth, computeWidth(table, labelProvider.getColumnText(fModel.elements.get(i), 0)) + ICON_AND_CHECKBOX_WITH); 525 } 526 527 nameColumn.setWidth(minNameWidth); 528 529 fSeparateViewer.addCheckStateListener(new ICheckStateListener() { 530 public void checkStateChanged(CheckStateChangedEvent event) { 531 boolean checked= event.getChecked(); 532 ModelElement element= (ModelElement) event.getElement(); 533 element.setSeparateCommand(checked); 534 } 535 }); 536 537 table.addSelectionListener(new SelectionAdapter() { 538 public void widgetSelected(SelectionEvent e) { 539 handleTableSelection(); 540 } 541 }); 542 543 } 544 545 private void createButtonList(Composite parent) { 546 Composite composite= new Composite(parent, SWT.NONE); 547 composite.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false)); 548 549 GridLayout layout= new GridLayout(); 550 layout.marginWidth= 0; 551 layout.marginHeight= 0; 552 composite.setLayout(layout); 553 554 fUpButton= new Button(composite, SWT.PUSH | SWT.CENTER); 555 fUpButton.setText(PreferencesMessages.CodeAssistAdvancedConfigurationBlock_Up); 556 fUpButton.addSelectionListener(new SelectionAdapter() { 557 public void widgetSelected(SelectionEvent e) { 558 int index= getSelectionIndex(); 559 if (index != -1) { 560 ((ModelElement) fModel.elements.get(index)).moveUp(); 561 fSeparateViewer.refresh(); 562 handleTableSelection(); 563 } 564 } 565 }); 566 fUpButton.setLayoutData(new GridData()); 567 SWTUtil.setButtonDimensionHint(fUpButton); 568 569 fDownButton= new Button(composite, SWT.PUSH | SWT.CENTER); 570 fDownButton.setText(PreferencesMessages.CodeAssistAdvancedConfigurationBlock_Down); 571 fDownButton.addSelectionListener(new SelectionAdapter() { 572 public void widgetSelected(SelectionEvent e) { 573 int index= getSelectionIndex(); 574 if (index != -1) { 575 ((ModelElement) fModel.elements.get(index)).moveDown(); 576 fSeparateViewer.refresh(); 577 handleTableSelection(); 578 } 579 } 580 }); 581 fDownButton.setLayoutData(new GridData()); 582 SWTUtil.setButtonDimensionHint(fDownButton); 583 } 584 585 private void createParameterTimeoutControl(Composite composite, int h_span) { 586 Composite timeoutComposite= new Composite(composite, SWT.NONE); 587 GridLayout layout= new GridLayout(4, false); 588 layout.marginWidth= 0; 589 layout.marginHeight= 0; 590 timeoutComposite.setLayout(layout); 591 GridData gd= new GridData(GridData.FILL, GridData.FILL, true, false, h_span, 1); 592 timeoutComposite.setLayoutData(gd); 593 594 PixelConverter pixelConverter= new PixelConverter(composite); 595 String str= PreferencesMessages.CodeAssistAdvancedConfigurationBlock_parameterNameFromAttachedJavadoc_timeout; 596 addTextField(timeoutComposite, str, PREF_CODEASSIST_TIMEOUT_FOR_PARAMETER_NAME_FROM_ATTACHED_JAVADOC, 0, pixelConverter.convertWidthInCharsToPixels(7)); 597 598 Label ms= new Label(timeoutComposite, SWT.NONE); 599 gd= new GridData(); 600 ms.setLayoutData(gd); 601 ms.setText(PreferencesMessages.CodeAssistAdvancedConfigurationBlock_parameterNameFromAttachedJavadoc_timeout_ms); 602 603 } 604 605 private void handleTableSelection() { 606 ModelElement item= getSelectedItem(); 607 if (item != null) { 608 int index= getSelectionIndex(); 609 fUpButton.setEnabled(index > 0); 610 fDownButton.setEnabled(index < fModel.elements.size() - 1); 611 } else { 612 fUpButton.setEnabled(false); 613 fDownButton.setEnabled(false); 614 } 615 } 616 617 private ModelElement getSelectedItem() { 618 return (ModelElement) ((IStructuredSelection) fSeparateViewer.getSelection()).getFirstElement(); 619 } 620 621 private int getSelectionIndex() { 622 return fSeparateViewer.getTable().getSelectionIndex(); 623 } 624 625 628 protected void updateControls() { 629 super.updateControls(); 630 631 fModel.update(); 632 updateCheckedState(); 633 fDefaultViewer.refresh(); 634 fSeparateViewer.refresh(); 635 handleTableSelection(); 636 } 637 638 private void updateCheckedState() { 639 final int size= fModel.elements.size(); 640 List defaultChecked= new ArrayList (size); 641 List separateChecked= new ArrayList (size); 642 643 for (Iterator it= fModel.elements.iterator(); it.hasNext();) { 644 ModelElement element= (ModelElement) it.next(); 645 if (element.isInDefaultCategory()) 646 defaultChecked.add(element); 647 if (element.isSeparateCommand()) 648 separateChecked.add(element); 649 } 650 651 fDefaultViewer.setCheckedElements(defaultChecked.toArray(new Object [defaultChecked.size()])); 652 fSeparateViewer.setCheckedElements(separateChecked.toArray(new Object [separateChecked.size()])); 653 } 654 655 658 protected boolean processChanges(IWorkbenchPreferenceContainer container) { 659 for (Iterator it= fModel.elements.iterator(); it.hasNext();) { 660 ModelElement item= (ModelElement) it.next(); 661 item.update(); 662 } 663 664 return super.processChanges(container); 665 } 666 667 670 protected void validateSettings(Key changedKey, String oldValue, String newValue) { 671 if (changedKey == PREF_CODEASSIST_TIMEOUT_FOR_PARAMETER_NAME_FROM_ATTACHED_JAVADOC) { 672 final StatusInfo status= new StatusInfo(); 673 if (newValue.length() == 0) 674 status.setError(PreferencesMessages.CodeAssistAdvancedConfigurationBlock_parameterNameFromAttachedJavadoc_timeout_emptyInput); 675 else { 676 try { 677 int number= Integer.parseInt(newValue); 678 int min= 0; 679 int max= 5000; 680 if (number < min || number > max) { 681 String msgFormat= PreferencesMessages.CodeAssistAdvancedConfigurationBlock_parameterNameFromAttachedJavadoc_timeout_invalidRange; 682 String msg= Messages.format(msgFormat, new Object [] {new Integer (min), new Integer (max)}); 683 status.setError(msg); 684 } 685 } catch (NumberFormatException ex) { 686 String msgFormat= PreferencesMessages.CodeAssistAdvancedConfigurationBlock_parameterNameFromAttachedJavadoc_timeout_invalidInput; 687 String msg= Messages.format(msgFormat, newValue); 688 status.setError(msg); 689 } 690 } 691 fContext.statusChanged(status); 692 } 693 } 694 695 698 protected String [] getFullBuildDialogStrings(boolean workspaceSettings) { 699 return null; 701 } 702 703 706 public void dispose() { 707 for (Iterator it= fImages.values().iterator(); it.hasNext();) { 708 Image image= (Image) it.next(); 709 image.dispose(); 710 } 711 712 super.dispose(); 713 } 714 715 private int computeWidth(Control control, String name) { 716 if (name == null) 717 return 0; 718 GC gc= new GC(control); 719 try { 720 gc.setFont(JFaceResources.getDialogFont()); 721 return gc.stringExtent(name).x + 10; 722 } finally { 723 gc.dispose(); 724 } 725 } 726 727 private static BindingManager fgLocalBindingManager; 728 static { 729 fgLocalBindingManager= new BindingManager(new ContextManager(), new CommandManager()); 730 final IBindingService bindingService= (IBindingService)PlatformUI.getWorkbench().getService(IBindingService.class); 731 final Scheme[] definedSchemes= bindingService.getDefinedSchemes(); 732 if (definedSchemes != null) { 733 try { 734 for (int i = 0; i < definedSchemes.length; i++) { 735 final Scheme scheme= definedSchemes[i]; 736 final Scheme copy= fgLocalBindingManager.getScheme(scheme.getId()); 737 copy.define(scheme.getName(), scheme.getDescription(), scheme.getParentId()); 738 } 739 } catch (final NotDefinedException e) { 740 JavaPlugin.log(e); 741 } 742 } 743 fgLocalBindingManager.setLocale(bindingService.getLocale()); 744 fgLocalBindingManager.setPlatform(bindingService.getPlatform()); 745 } 746 747 private static String getKeyboardShortcut(ParameterizedCommand command) { 748 IBindingService bindingService= (IBindingService) PlatformUI.getWorkbench().getAdapter(IBindingService.class); 749 fgLocalBindingManager.setBindings(bindingService.getBindings()); 750 try { 751 Scheme activeScheme= bindingService.getActiveScheme(); 752 if (activeScheme != null) 753 fgLocalBindingManager.setActiveScheme(activeScheme); 754 } catch (NotDefinedException e) { 755 JavaPlugin.log(e); 756 } 757 758 TriggerSequence[] bindings= fgLocalBindingManager.getActiveBindingsDisregardingContextFor(command); 759 if (bindings.length > 0) 760 return bindings[0].format(); 761 return null; 762 } 763 764 private Image getImage(ImageDescriptor imgDesc) { 765 if (imgDesc == null) 766 return null; 767 768 Image img= (Image) fImages.get(imgDesc); 769 if (img == null) { 770 img= imgDesc.createImage(false); 771 fImages.put(imgDesc, img); 772 } 773 return img; 774 } 775 776 } 777 | Popular Tags |