KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > dialogs > SourceActionDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.dialogs;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.List JavaDoc;
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 /**
66  * An advanced version of CheckedTreeSelectionDialog with right-side button layout and
67  * extra buttons and composites.
68  */

69 public class SourceActionDialog extends CheckedTreeSelectionDialog {
70     
71     private List JavaDoc fInsertPositions;
72     private List JavaDoc 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 JavaDoc fCommentString;
82     private boolean fEnableInsertPosition= true;
83         
84     private int fVisibilityModifier;
85     private boolean fFinal;
86     private boolean fSynchronized;
87     
88     private final String JavaDoc SETTINGS_SECTION_METHODS= "SourceActionDialog.methods"; //$NON-NLS-1$
89
private final String JavaDoc SETTINGS_SECTION_CONSTRUCTORS= "SourceActionDialog.constructors"; //$NON-NLS-1$
90

91     private final String JavaDoc SETTINGS_INSERTPOSITION= "InsertPosition"; //$NON-NLS-1$
92
private final String JavaDoc SETTINGS_VISIBILITY_MODIFIER= "VisibilityModifier"; //$NON-NLS-1$
93
private final String JavaDoc SETTINGS_FINAL_MODIFIER= "FinalModifier"; //$NON-NLS-1$
94
private final String JavaDoc SETTINGS_SYNCHRONIZED_MODIFIER= "SynchronizedModifier"; //$NON-NLS-1$
95
private final String JavaDoc SETTINGS_COMMENTS= "Comments"; //$NON-NLS-1$
96
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 JavaDoc 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 JavaDoc();
125         fLabels= new ArrayList JavaDoc();
126         
127         IJavaElement[] members= fType.getChildren();
128         IMethod[] methods= fType.getMethods();
129         
130         fInsertPositions.add(methods.length > 0 ? methods[0]: null); // first
131
fInsertPositions.add(null); // last
132

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             // code is needed to deal with bogus values already present in the dialog store.
141
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 JavaDoc 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 JavaDoc string, boolean defaultValue) {
159         if (string != null) {
160             return StringConverter.asBoolean(string, defaultValue);
161         }
162         return defaultValue;
163     }
164
165     protected int asInt(String JavaDoc 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 JavaDoc 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; // in the middle of a member
199
}
200         }
201         insertPositions.add(null);
202         return true;
203     }
204
205     /* (non-Javadoc)
206      * @see org.eclipse.jface.dialogs.Dialog#close()
207      */

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     /**
221      * Sets the size of the tree in unit of characters.
222      * @param width the width of the tree.
223      * @param height the height of the tree.
224      */

225     public void setSize(int width, int height) {
226         fWidth = width;
227         fHeight = height;
228     }
229
230     
231     /***
232      * Set insert position valid input is 0 for the first position, 1 for the last position, > 1 for all else.
233      */

234     private void setInsertPosition(int insert) {
235         fCurrentPositionIndex= insert;
236     }
237     
238     public void setCommentString(String JavaDoc 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     /**
297      * Returns a composite containing the label created at the top of the dialog. Returns null if there is the
298      * message for the label is null.
299      */

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     /*
311      * @see Dialog#createDialogArea(Composite)
312      */

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; // No link as default
374
}
375     
376     protected void openCodeTempatePage(String JavaDoc id) {
377         HashMap JavaDoc arg= new HashMap JavaDoc();
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         // Add visibility and modifiers buttons: http://bugs.eclipse.org/bugs/show_bug.cgi?id=35870
414
// Add persistence of options: http://bugs.eclipse.org/bugs/show_bug.cgi?id=38400
415
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 JavaDoc convertToIntegerList(int[] array) {
442         List JavaDoc result= new ArrayList JavaDoc(array.length);
443         for (int i= 0; i < array.length; i++) {
444             result.add(new Integer JavaDoc(array[i]));
445         }
446         return result;
447     }
448     
449     /* (non-Javadoc)
450      * @see org.eclipse.ui.dialogs.CheckedTreeSelectionDialog#create()
451      */

452     public void create() {
453         super.create();
454         
455         // select the first checked element, or if none are checked, the first element
456
CheckboxTreeViewer treeViewer= getTreeViewer();
457         TreeItem[] items= treeViewer.getTree().getItems();
458         if (items.length > 0) {
459             Object JavaDoc 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 JavaDoc 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 JavaDoc[] labels= new String JavaDoc[] {
487             ActionMessages.SourceActionDialog_modifier_public,
488             ActionMessages.SourceActionDialog_modifier_protected,
489             ActionMessages.SourceActionDialog_modifier_default,
490             ActionMessages.SourceActionDialog_modifier_private,
491         };
492         Integer JavaDoc[] data= new Integer JavaDoc[] {
493                     new Integer JavaDoc(Modifier.PUBLIC),
494                     new Integer JavaDoc(Modifier.PROTECTED),
495                     new Integer JavaDoc(Modifier.NONE),
496                     new Integer JavaDoc(Modifier.PRIVATE)};
497         Integer JavaDoc initialVisibility= new Integer JavaDoc(correctVisibility);
498         for (int i= 0; i < labels.length; i++) {
499             Button radio= new Button(group, SWT.RADIO);
500             Integer JavaDoc 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 JavaDoc)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 JavaDoc(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 JavaDoc)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 JavaDoc(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 JavaDoc)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                 // Add persistence only if first or last method: http://bugs.eclipse.org/bugs/show_bug.cgi?id=38400
586
setInsertPosition(index);
587             }
588         });
589
590         return buttonComposite;
591     }
592
593     private void fillWithPossibleInsertPositions(Combo combo) {
594         combo.setItems((String JavaDoc[]) fLabels.toArray(new String JavaDoc[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     /*
623      * Determine where in the file to enter the newly created methods.
624      */

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