1 11 package org.eclipse.jdt.internal.ui.dialogs; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.List ; 16 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.events.SelectionAdapter; 19 import org.eclipse.swt.events.SelectionEvent; 20 import org.eclipse.swt.events.SelectionListener; 21 import org.eclipse.swt.layout.GridData; 22 import org.eclipse.swt.layout.GridLayout; 23 import org.eclipse.swt.widgets.Button; 24 import org.eclipse.swt.widgets.Combo; 25 import org.eclipse.swt.widgets.Composite; 26 import org.eclipse.swt.widgets.Control; 27 import org.eclipse.swt.widgets.Group; 28 import org.eclipse.swt.widgets.Label; 29 import org.eclipse.swt.widgets.Shell; 30 import org.eclipse.swt.widgets.TreeItem; 31 32 import org.eclipse.jface.dialogs.IDialogConstants; 33 import org.eclipse.jface.dialogs.IDialogSettings; 34 import org.eclipse.jface.resource.StringConverter; 35 import org.eclipse.jface.viewers.CheckboxTreeViewer; 36 import org.eclipse.jface.viewers.ILabelProvider; 37 import org.eclipse.jface.viewers.ITreeContentProvider; 38 import org.eclipse.jface.viewers.StructuredSelection; 39 40 import org.eclipse.jface.text.ITextSelection; 41 42 import org.eclipse.ui.dialogs.CheckedTreeSelectionDialog; 43 import org.eclipse.ui.dialogs.PreferencesUtil; 44 45 import org.eclipse.jdt.core.IJavaElement; 46 import org.eclipse.jdt.core.IMember; 47 import org.eclipse.jdt.core.IMethod; 48 import org.eclipse.jdt.core.ISourceRange; 49 import org.eclipse.jdt.core.ISourceReference; 50 import org.eclipse.jdt.core.IType; 51 import org.eclipse.jdt.core.JavaModelException; 52 import org.eclipse.jdt.core.dom.Modifier; 53 54 import org.eclipse.jdt.internal.corext.util.Messages; 55 56 import org.eclipse.jdt.ui.JavaElementLabels; 57 58 import org.eclipse.jdt.internal.ui.JavaPlugin; 59 import org.eclipse.jdt.internal.ui.actions.ActionMessages; 60 import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor; 61 import org.eclipse.jdt.internal.ui.preferences.CodeTemplatePreferencePage; 62 import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings; 63 import org.eclipse.jdt.internal.ui.refactoring.IVisibilityChangeListener; 64 65 69 public class SourceActionDialog extends CheckedTreeSelectionDialog { 70 71 private List fInsertPositions; 72 private List fLabels; 73 private int fCurrentPositionIndex; 74 75 private IDialogSettings fSettings; 76 private CompilationUnitEditor fEditor; 77 private ITreeContentProvider fContentProvider; 78 private boolean fGenerateComment; 79 private IType fType; 80 private int fWidth, fHeight; 81 private String fCommentString; 82 private boolean fEnableInsertPosition= true; 83 84 private int fVisibilityModifier; 85 private boolean fFinal; 86 private boolean fSynchronized; 87 88 private final String SETTINGS_SECTION_METHODS= "SourceActionDialog.methods"; private final String SETTINGS_SECTION_CONSTRUCTORS= "SourceActionDialog.constructors"; 91 private final String SETTINGS_INSERTPOSITION= "InsertPosition"; private final String SETTINGS_VISIBILITY_MODIFIER= "VisibilityModifier"; private final String SETTINGS_FINAL_MODIFIER= "FinalModifier"; private final String SETTINGS_SYNCHRONIZED_MODIFIER= "SynchronizedModifier"; private final String SETTINGS_COMMENTS= "Comments"; private Composite fInsertPositionComposite; 97 98 public SourceActionDialog(Shell parent, ILabelProvider labelProvider, ITreeContentProvider contentProvider, CompilationUnitEditor editor, IType type, boolean isConstructor) throws JavaModelException { 99 super(parent, labelProvider, contentProvider); 100 fEditor= editor; 101 fContentProvider= contentProvider; 102 fType= type; 103 fCommentString= ActionMessages.SourceActionDialog_createMethodComment; 104 setEmptyListMessage(ActionMessages.SourceActionDialog_no_entries); 105 106 fWidth= 60; 107 fHeight= 18; 108 109 int insertionDefault= isConstructor ? 0 : 1; 110 boolean generateCommentsDefault= JavaPreferencesSettings.getCodeGenerationSettings(type.getJavaProject()).createComments; 111 112 IDialogSettings dialogSettings= JavaPlugin.getDefault().getDialogSettings(); 113 String sectionId= isConstructor ? SETTINGS_SECTION_CONSTRUCTORS : SETTINGS_SECTION_METHODS; 114 fSettings= dialogSettings.getSection(sectionId); 115 if (fSettings == null) { 116 fSettings= dialogSettings.addNewSection(sectionId); 117 } 118 119 fVisibilityModifier= asInt(fSettings.get(SETTINGS_VISIBILITY_MODIFIER), Modifier.PUBLIC); 120 fFinal= asBoolean(fSettings.get(SETTINGS_FINAL_MODIFIER), false); 121 fSynchronized= asBoolean(fSettings.get(SETTINGS_SYNCHRONIZED_MODIFIER), false); 122 fCurrentPositionIndex= asInt(fSettings.get(SETTINGS_INSERTPOSITION), insertionDefault); 123 fGenerateComment= asBoolean(fSettings.get(SETTINGS_COMMENTS), generateCommentsDefault); 124 fInsertPositions= new ArrayList (); 125 fLabels= new ArrayList (); 126 127 IJavaElement[] members= fType.getChildren(); 128 IMethod[] methods= fType.getMethods(); 129 130 fInsertPositions.add(methods.length > 0 ? methods[0]: null); fInsertPositions.add(null); 133 fLabels.add(ActionMessages.SourceActionDialog_first_method); 134 fLabels.add(ActionMessages.SourceActionDialog_last_method); 135 136 if (hasCursorPositionElement(fEditor, members, fInsertPositions)) { 137 fLabels.add(ActionMessages.SourceActionDialog_cursor); 138 fCurrentPositionIndex= 2; 139 } else { 140 fCurrentPositionIndex= Math.max(fCurrentPositionIndex, 0); 142 fCurrentPositionIndex= Math.min(fCurrentPositionIndex, 1); 143 } 144 145 for (int i = 0; i < methods.length; i++) { 146 IMethod curr= methods[i]; 147 String methodLabel= JavaElementLabels.getElementLabel(curr, JavaElementLabels.M_PARAMETER_TYPES); 148 fLabels.add(Messages.format(ActionMessages.SourceActionDialog_after, methodLabel)); 149 fInsertPositions.add(findSibling(curr, members)); 150 } 151 fInsertPositions.add(null); 152 } 153 154 protected IType getType() { 155 return fType; 156 } 157 158 protected boolean asBoolean(String string, boolean defaultValue) { 159 if (string != null) { 160 return StringConverter.asBoolean(string, defaultValue); 161 } 162 return defaultValue; 163 } 164 165 protected int asInt(String string, int defaultValue) { 166 if (string != null) { 167 return StringConverter.asInt(string, defaultValue); 168 } 169 return defaultValue; 170 } 171 172 private IJavaElement findSibling(IMethod curr, IJavaElement[] members) throws JavaModelException { 173 IJavaElement res= null; 174 int methodStart= curr.getSourceRange().getOffset(); 175 for (int i= members.length-1; i >= 0; i--) { 176 IMember member= (IMember) members[i]; 177 if (methodStart >= member.getSourceRange().getOffset()) { 178 return res; 179 } 180 res= member; 181 } 182 return null; 183 } 184 185 private boolean hasCursorPositionElement(CompilationUnitEditor editor, IJavaElement[] members, List insertPositions) throws JavaModelException { 186 if (editor == null) { 187 return false; 188 } 189 int offset= ((ITextSelection) editor.getSelectionProvider().getSelection()).getOffset(); 190 191 for (int i= 0; i < members.length; i++) { 192 IMember curr= (IMember) members[i]; 193 ISourceRange range= curr.getSourceRange(); 194 if (offset < range.getOffset()) { 195 insertPositions.add(curr); 196 return true; 197 } else if (offset < range.getOffset() + range.getLength()) { 198 return false; } 200 } 201 insertPositions.add(null); 202 return true; 203 } 204 205 208 public boolean close() { 209 fSettings.put(SETTINGS_VISIBILITY_MODIFIER, StringConverter.asString(fVisibilityModifier)); 210 fSettings.put(SETTINGS_FINAL_MODIFIER, StringConverter.asString(fFinal)); 211 fSettings.put(SETTINGS_SYNCHRONIZED_MODIFIER, StringConverter.asString(fSynchronized)); 212 213 if (fCurrentPositionIndex == 0 || fCurrentPositionIndex == 1) { 214 fSettings.put(SETTINGS_INSERTPOSITION, StringConverter.asString(fCurrentPositionIndex)); 215 } 216 fSettings.put(SETTINGS_COMMENTS, fGenerateComment); 217 return super.close(); 218 } 219 220 225 public void setSize(int width, int height) { 226 fWidth = width; 227 fHeight = height; 228 } 229 230 231 234 private void setInsertPosition(int insert) { 235 fCurrentPositionIndex= insert; 236 } 237 238 public void setCommentString(String string) { 239 fCommentString= string; 240 } 241 242 protected ITreeContentProvider getContentProvider() { 243 return fContentProvider; 244 } 245 246 public boolean getGenerateComment() { 247 return fGenerateComment; 248 } 249 250 public int getVisibilityModifier() { 251 return fVisibilityModifier; 252 } 253 254 public void setGenerateComment(boolean comment) { 255 fGenerateComment= comment; 256 } 257 258 protected void setVisibility(int visibility) { 259 fVisibilityModifier= visibility; 260 } 261 262 private void setFinal(boolean value) { 263 fFinal= value; 264 } 265 266 private void setSynchronized(boolean value) { 267 fSynchronized= value; 268 } 269 270 protected Composite createSelectionButtons(Composite composite) { 271 Composite buttonComposite= super.createSelectionButtons(composite); 272 273 GridLayout layout = new GridLayout(); 274 buttonComposite.setLayout(layout); 275 276 layout.marginHeight= 0; 277 layout.marginWidth= 0; 278 layout.numColumns= 1; 279 280 return buttonComposite; 281 } 282 283 protected void buttonPressed(int buttonId) { 284 switch (buttonId) { 285 case IDialogConstants.OK_ID: { 286 okPressed(); 287 break; 288 } 289 case IDialogConstants.CANCEL_ID: { 290 cancelPressed(); 291 break; 292 } 293 } 294 } 295 296 300 protected Label createMessageArea(Composite composite) { 301 if (getMessage() != null) { 302 Label label = new Label(composite,SWT.NONE); 303 label.setText(getMessage()); 304 label.setFont(composite.getFont()); 305 return label; 306 } 307 return null; 308 } 309 310 313 protected Control createDialogArea(Composite parent) { 314 initializeDialogUnits(parent); 315 316 Composite composite= new Composite(parent, SWT.NONE); 317 GridLayout layout= new GridLayout(); 318 GridData gd= null; 319 320 layout.marginHeight= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN); 321 layout.marginWidth= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN); 322 layout.verticalSpacing= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING); 323 layout.horizontalSpacing= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); 324 composite.setLayout(layout); 325 326 Label messageLabel = createMessageArea(composite); 327 if (messageLabel != null) { 328 gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL); 329 gd.horizontalSpan= 2; 330 messageLabel.setLayoutData(gd); 331 } 332 333 Composite inner= new Composite(composite, SWT.NONE); 334 GridLayout innerLayout = new GridLayout(); 335 innerLayout.numColumns= 2; 336 innerLayout.marginHeight= 0; 337 innerLayout.marginWidth= 0; 338 inner.setLayout(innerLayout); 339 inner.setFont(parent.getFont()); 340 341 CheckboxTreeViewer treeViewer= createTreeViewer(inner); 342 gd= new GridData(GridData.FILL_BOTH); 343 gd.widthHint = convertWidthInCharsToPixels(fWidth); 344 gd.heightHint = convertHeightInCharsToPixels(fHeight); 345 treeViewer.getControl().setLayoutData(gd); 346 347 Composite buttonComposite= createSelectionButtons(inner); 348 gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL); 349 buttonComposite.setLayoutData(gd); 350 351 gd= new GridData(GridData.FILL_BOTH); 352 inner.setLayoutData(gd); 353 354 fInsertPositionComposite= createInsertPositionCombo(composite); 355 fInsertPositionComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 356 357 Composite commentComposite= createCommentSelection(composite); 358 commentComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 359 360 Control linkControl= createLinkControl(composite); 361 if (linkControl != null) 362 linkControl.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 363 364 gd= new GridData(GridData.FILL_BOTH); 365 composite.setLayoutData(gd); 366 367 applyDialogFont(composite); 368 369 return composite; 370 } 371 372 protected Control createLinkControl(Composite composite) { 373 return null; } 375 376 protected void openCodeTempatePage(String id) { 377 HashMap arg= new HashMap (); 378 arg.put(CodeTemplatePreferencePage.DATA_SELECT_TEMPLATE, id); 379 PreferencesUtil.createPropertyDialogOn(getShell(), fType.getJavaProject().getProject(), CodeTemplatePreferencePage.PROP_ID, null, arg).open(); 380 } 381 382 383 protected Composite createCommentSelection(Composite composite) { 384 Composite commentComposite = new Composite(composite, SWT.NONE); 385 GridLayout layout = new GridLayout(); 386 layout.marginHeight= 0; 387 layout.marginWidth= 0; 388 commentComposite.setLayout(layout); 389 commentComposite.setFont(composite.getFont()); 390 391 Button commentButton= new Button(commentComposite, SWT.CHECK); 392 commentButton.setText(fCommentString); 393 394 commentButton.addSelectionListener(new SelectionListener() { 395 public void widgetSelected(SelectionEvent e) { 396 boolean isSelected= (((Button) e.widget).getSelection()); 397 setGenerateComment(isSelected); 398 } 399 400 public void widgetDefaultSelected(SelectionEvent e) { 401 widgetSelected(e); 402 } 403 }); 404 commentButton.setSelection(getGenerateComment()); 405 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL); 406 gd.horizontalSpan= 2; 407 commentButton.setLayoutData(gd); 408 409 return commentComposite; 410 } 411 412 protected Composite addVisibilityAndModifiersChoices(Composite buttonComposite) { 413 IVisibilityChangeListener visibilityChangeListener= new IVisibilityChangeListener(){ 416 public void visibilityChanged(int newVisibility) { 417 setVisibility(newVisibility); 418 } 419 public void modifierChanged(int modifier, boolean isChecked) { 420 switch (modifier) { 421 case Modifier.FINAL: { 422 setFinal(isChecked); 423 return; 424 } 425 case Modifier.SYNCHRONIZED: { 426 setSynchronized(isChecked); 427 return; 428 } 429 default: return; 430 } 431 } 432 }; 433 434 int initialVisibility= getVisibilityModifier(); 435 int[] availableVisibilities= new int[]{Modifier.PUBLIC, Modifier.PROTECTED, Modifier.PRIVATE, Modifier.NONE}; 436 437 Composite visibilityComposite= createVisibilityControlAndModifiers(buttonComposite, visibilityChangeListener, availableVisibilities, initialVisibility); 438 return visibilityComposite; 439 } 440 441 private List convertToIntegerList(int[] array) { 442 List result= new ArrayList (array.length); 443 for (int i= 0; i < array.length; i++) { 444 result.add(new Integer (array[i])); 445 } 446 return result; 447 } 448 449 452 public void create() { 453 super.create(); 454 455 CheckboxTreeViewer treeViewer= getTreeViewer(); 457 TreeItem[] items= treeViewer.getTree().getItems(); 458 if (items.length > 0) { 459 Object revealedElement= items[0]; 460 461 for (int i= 0; i < items.length; i++) { 462 if (items[i].getChecked()) { 463 revealedElement= items[i].getData(); 464 break; 465 } 466 } 467 treeViewer.setSelection(new StructuredSelection(revealedElement)); 468 treeViewer.reveal(revealedElement); 469 } 470 } 471 472 protected Composite createVisibilityControl(Composite parent, final IVisibilityChangeListener visibilityChangeListener, int[] availableVisibilities, int correctVisibility) { 473 List allowedVisibilities= convertToIntegerList(availableVisibilities); 474 if (allowedVisibilities.size() == 1) 475 return null; 476 477 Group group= new Group(parent, SWT.NONE); 478 group.setText(ActionMessages.SourceActionDialog_modifier_group); 479 GridData gd= new GridData(GridData.FILL_BOTH); 480 group.setLayoutData(gd); 481 GridLayout layout= new GridLayout(); 482 layout.makeColumnsEqualWidth= true; 483 layout.numColumns= 4; 484 group.setLayout(layout); 485 486 String [] labels= new String [] { 487 ActionMessages.SourceActionDialog_modifier_public, 488 ActionMessages.SourceActionDialog_modifier_protected, 489 ActionMessages.SourceActionDialog_modifier_default, 490 ActionMessages.SourceActionDialog_modifier_private, 491 }; 492 Integer [] data= new Integer [] { 493 new Integer (Modifier.PUBLIC), 494 new Integer (Modifier.PROTECTED), 495 new Integer (Modifier.NONE), 496 new Integer (Modifier.PRIVATE)}; 497 Integer initialVisibility= new Integer (correctVisibility); 498 for (int i= 0; i < labels.length; i++) { 499 Button radio= new Button(group, SWT.RADIO); 500 Integer visibilityCode= data[i]; 501 radio.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); 502 radio.setText(labels[i]); 503 radio.setData(visibilityCode); 504 radio.setSelection(visibilityCode.equals(initialVisibility)); 505 radio.setEnabled(allowedVisibilities.contains(visibilityCode)); 506 radio.addSelectionListener(new SelectionAdapter() { 507 public void widgetSelected(SelectionEvent event) { 508 visibilityChangeListener.visibilityChanged(((Integer )event.widget.getData()).intValue()); 509 } 510 }); 511 } 512 return group; 513 } 514 515 protected Composite createVisibilityControlAndModifiers(Composite parent, final IVisibilityChangeListener visibilityChangeListener, int[] availableVisibilities, int correctVisibility) { 516 Composite visibilityComposite= createVisibilityControl(parent, visibilityChangeListener, availableVisibilities, correctVisibility); 517 518 Button finalCheckboxButton= new Button(visibilityComposite, SWT.CHECK); 519 finalCheckboxButton.setText(ActionMessages.SourceActionDialog_modifier_final); 520 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL); 521 finalCheckboxButton.setLayoutData(gd); 522 finalCheckboxButton.setData(new Integer (Modifier.FINAL)); 523 finalCheckboxButton.setEnabled(true); 524 finalCheckboxButton.setSelection(isFinal()); 525 finalCheckboxButton.addSelectionListener(new SelectionListener() { 526 public void widgetSelected(SelectionEvent event) { 527 visibilityChangeListener.modifierChanged(((Integer )event.widget.getData()).intValue(), ((Button) event.widget).getSelection()); 528 } 529 530 public void widgetDefaultSelected(SelectionEvent event) { 531 widgetSelected(event); 532 } 533 }); 534 535 Button syncCheckboxButton= new Button(visibilityComposite, SWT.CHECK); 536 syncCheckboxButton.setText(ActionMessages.SourceActionDialog_modifier_synchronized); 537 gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL); 538 syncCheckboxButton.setLayoutData(gd); 539 syncCheckboxButton.setData(new Integer (Modifier.SYNCHRONIZED)); 540 syncCheckboxButton.setEnabled(true); 541 syncCheckboxButton.setSelection(isSynchronized()); 542 syncCheckboxButton.addSelectionListener(new SelectionListener() { 543 public void widgetSelected(SelectionEvent event) { 544 visibilityChangeListener.modifierChanged(((Integer )event.widget.getData()).intValue(), ((Button) event.widget).getSelection()); 545 } 546 547 public void widgetDefaultSelected(SelectionEvent event) { 548 widgetSelected(event); 549 } 550 }); 551 return visibilityComposite; 552 } 553 554 protected Composite createInsertPositionCombo(Composite composite) { 555 Composite selectionComposite = new Composite(composite, SWT.NONE); 556 GridLayout layout = new GridLayout(); 557 layout.marginHeight= 0; 558 layout.marginWidth= 0; 559 selectionComposite.setLayout(layout); 560 561 addOrderEntryChoices(selectionComposite); 562 563 return selectionComposite; 564 } 565 566 private Composite addOrderEntryChoices(Composite buttonComposite) { 567 Label enterLabel= new Label(buttonComposite, SWT.NONE); 568 enterLabel.setText(ActionMessages.SourceActionDialog_enterAt_label); 569 if (!fEnableInsertPosition) 570 enterLabel.setEnabled(false); 571 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL); 572 enterLabel.setLayoutData(gd); 573 574 final Combo enterCombo= new Combo(buttonComposite, SWT.READ_ONLY); 575 if (!fEnableInsertPosition) 576 enterCombo.setEnabled(false); 577 fillWithPossibleInsertPositions(enterCombo); 578 579 gd= new GridData(GridData.FILL_BOTH); 580 gd.widthHint= convertWidthInCharsToPixels(fWidth); 581 enterCombo.setLayoutData(gd); 582 enterCombo.addSelectionListener(new SelectionAdapter(){ 583 public void widgetSelected(SelectionEvent e) { 584 int index= enterCombo.getSelectionIndex(); 585 setInsertPosition(index); 587 } 588 }); 589 590 return buttonComposite; 591 } 592 593 private void fillWithPossibleInsertPositions(Combo combo) { 594 combo.setItems((String []) fLabels.toArray(new String [fLabels.size()])); 595 combo.select(fCurrentPositionIndex); 596 } 597 598 public boolean getFinal() { 599 return fFinal; 600 } 601 602 public boolean getSynchronized() { 603 return fSynchronized; 604 } 605 606 public boolean isFinal() { 607 return fFinal; 608 } 609 610 public boolean isSynchronized() { 611 return fSynchronized; 612 } 613 614 public void setElementPositionEnabled(boolean enabled) { 615 fEnableInsertPosition= enabled; 616 } 617 618 public boolean isElementPositionEnabled() { 619 return fEnableInsertPosition; 620 } 621 622 625 public IJavaElement getElementPosition() { 626 return (IJavaElement) fInsertPositions.get(fCurrentPositionIndex); 627 } 628 629 public int getInsertOffset() throws JavaModelException { 630 IJavaElement elementPosition= getElementPosition(); 631 if (elementPosition instanceof ISourceReference) { 632 return ((ISourceReference) elementPosition).getSourceRange().getOffset(); 633 } 634 return -1; 635 } 636 637 protected IDialogSettings getDialogSettings() { 638 return fSettings; 639 } 640 } 641 | Popular Tags |