KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > actions > GenerateConstructorUsingFieldsSelectionDialog


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.actions;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.events.SelectionListener;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Combo;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Control;
26 import org.eclipse.swt.widgets.Label;
27 import org.eclipse.swt.widgets.Link;
28 import org.eclipse.swt.widgets.Shell;
29
30 import org.eclipse.jface.dialogs.IDialogConstants;
31 import org.eclipse.jface.dialogs.IDialogSettings;
32 import org.eclipse.jface.viewers.CheckboxTreeViewer;
33 import org.eclipse.jface.viewers.DoubleClickEvent;
34 import org.eclipse.jface.viewers.IDoubleClickListener;
35 import org.eclipse.jface.viewers.ILabelProvider;
36 import org.eclipse.jface.viewers.ISelectionChangedListener;
37 import org.eclipse.jface.viewers.IStructuredSelection;
38 import org.eclipse.jface.viewers.SelectionChangedEvent;
39
40 import org.eclipse.ui.PlatformUI;
41
42 import org.eclipse.jdt.core.IType;
43 import org.eclipse.jdt.core.JavaModelException;
44 import org.eclipse.jdt.core.dom.IMethodBinding;
45 import org.eclipse.jdt.core.dom.Modifier;
46
47 import org.eclipse.jdt.internal.corext.template.java.CodeTemplateContextType;
48
49 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
50 import org.eclipse.jdt.internal.ui.JavaPlugin;
51 import org.eclipse.jdt.internal.ui.dialogs.SourceActionDialog;
52 import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor;
53 import org.eclipse.jdt.internal.ui.refactoring.IVisibilityChangeListener;
54 import org.eclipse.jdt.internal.ui.viewsupport.BindingLabelProvider;
55
56 public class GenerateConstructorUsingFieldsSelectionDialog extends SourceActionDialog {
57
58     class GenerateConstructorUsingFieldsTreeViewerAdapter implements ISelectionChangedListener, IDoubleClickListener {
59
60         public void doubleClick(DoubleClickEvent event) {
61             // Do nothing
62
}
63
64         public void selectionChanged(SelectionChangedEvent event) {
65             IStructuredSelection selection= (IStructuredSelection) getTreeViewer().getSelection();
66
67             List JavaDoc selectedList= selection.toList();
68             GenerateConstructorUsingFieldsContentProvider cp= (GenerateConstructorUsingFieldsContentProvider) getContentProvider();
69
70             fButtonControls[GenerateConstructorUsingFieldsSelectionDialog.UP_INDEX].setEnabled(cp.canMoveUp(selectedList));
71             fButtonControls[GenerateConstructorUsingFieldsSelectionDialog.DOWN_INDEX].setEnabled(cp.canMoveDown(selectedList));
72         }
73     }
74
75     private static final int DOWN_BUTTON= IDialogConstants.CLIENT_ID + 2;
76
77     private static final int UP_BUTTON= IDialogConstants.CLIENT_ID + 1;
78
79     protected Button[] fButtonControls;
80
81     boolean[] fButtonsEnabled;
82
83     IDialogSettings fGenConstructorSettings;
84
85     int fHeight= 18;
86
87     boolean fOmitSuper;
88
89     Button fOmitSuperButton;
90
91     IMethodBinding[] fSuperConstructors;
92
93     int fSuperIndex;
94
95     GenerateConstructorUsingFieldsTreeViewerAdapter fTreeViewerAdapter;
96
97     int fWidth= 60;
98
99     final String JavaDoc OMIT_SUPER= "OmitCallToSuper"; //$NON-NLS-1$
100

101     final String JavaDoc SETTINGS_SECTION= "GenerateConstructorUsingFieldsSelectionDialog"; //$NON-NLS-1$
102

103     private static final int DOWN_INDEX= 1;
104
105     private static final int UP_INDEX= 0;
106
107     public GenerateConstructorUsingFieldsSelectionDialog(Shell parent, ILabelProvider labelProvider, GenerateConstructorUsingFieldsContentProvider contentProvider, CompilationUnitEditor editor, IType type, IMethodBinding[] superConstructors) throws JavaModelException {
108         super(parent, labelProvider, contentProvider, editor, type, true);
109         fTreeViewerAdapter= new GenerateConstructorUsingFieldsTreeViewerAdapter();
110
111         fSuperConstructors= superConstructors;
112
113         IDialogSettings dialogSettings= JavaPlugin.getDefault().getDialogSettings();
114         fGenConstructorSettings= dialogSettings.getSection(SETTINGS_SECTION);
115         if (fGenConstructorSettings == null) {
116             fGenConstructorSettings= dialogSettings.addNewSection(SETTINGS_SECTION);
117             fGenConstructorSettings.put(OMIT_SUPER, false);
118         }
119
120         final boolean isEnum= type.isEnum();
121         fOmitSuper= fGenConstructorSettings.getBoolean(OMIT_SUPER) || isEnum;
122         if (isEnum)
123             setVisibility(Modifier.PRIVATE);
124     }
125
126     Composite addSuperClassConstructorChoices(Composite composite) {
127         Label label= new Label(composite, SWT.NONE);
128         label.setText(ActionMessages.GenerateConstructorUsingFieldsSelectionDialog_sort_constructor_choices_label);
129         GridData gd= new GridData(GridData.FILL_HORIZONTAL);
130         label.setLayoutData(gd);
131
132         BindingLabelProvider provider= new BindingLabelProvider();
133         final Combo combo= new Combo(composite, SWT.READ_ONLY);
134         for (int i= 0; i < fSuperConstructors.length; i++) {
135             combo.add(provider.getText(fSuperConstructors[i]));
136         }
137
138         // TODO: Can we be a little more intelligent about guessing the super() ?
139
combo.setText(combo.getItem(0));
140         combo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
141         combo.addSelectionListener(new SelectionAdapter() {
142
143             public void widgetSelected(SelectionEvent e) {
144                 fSuperIndex= combo.getSelectionIndex();
145                 // Disable omit super checkbox unless default constructor
146
fOmitSuperButton.setEnabled(getSuperConstructorChoice().getParameterTypes().length == 0);
147                 updateOKStatus();
148             }
149         });
150
151         return composite;
152     }
153
154     protected void buttonPressed(int buttonId) {
155         super.buttonPressed(buttonId);
156         switch (buttonId) {
157             case UP_BUTTON: {
158                 GenerateConstructorUsingFieldsContentProvider contentProvider= (GenerateConstructorUsingFieldsContentProvider) getTreeViewer().getContentProvider();
159                 contentProvider.up(getElementList(), getTreeViewer());
160                 updateOKStatus();
161                 break;
162             }
163             case DOWN_BUTTON: {
164                 GenerateConstructorUsingFieldsContentProvider contentProvider= (GenerateConstructorUsingFieldsContentProvider) getTreeViewer().getContentProvider();
165                 contentProvider.down(getElementList(), getTreeViewer());
166                 updateOKStatus();
167                 break;
168             }
169         }
170     }
171
172     protected void configureShell(Shell shell) {
173         super.configureShell(shell);
174         PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, IJavaHelpContextIds.GENERATE_CONSTRUCTOR_USING_FIELDS_SELECTION_DIALOG);
175     }
176     
177     protected Control createDialogArea(Composite parent) {
178         initializeDialogUnits(parent);
179
180         Composite composite= new Composite(parent, SWT.NONE);
181         GridLayout layout= new GridLayout();
182         GridData gd= null;
183
184         layout.marginHeight= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
185         layout.marginWidth= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
186         layout.verticalSpacing= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
187         layout.horizontalSpacing= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
188         composite.setLayout(layout);
189
190         Composite classConstructorComposite= addSuperClassConstructorChoices(composite);
191         gd= new GridData(GridData.FILL_BOTH);
192         classConstructorComposite.setLayoutData(gd);
193
194         Composite inner= new Composite(composite, SWT.NONE);
195         GridLayout innerLayout= new GridLayout();
196         innerLayout.numColumns= 2;
197         innerLayout.marginHeight= 0;
198         innerLayout.marginWidth= 0;
199         inner.setLayout(innerLayout);
200
201         Label messageLabel= createMessageArea(inner);
202         if (messageLabel != null) {
203             gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
204             gd.horizontalSpan= 2;
205             messageLabel.setLayoutData(gd);
206         }
207
208         CheckboxTreeViewer treeViewer= createTreeViewer(inner);
209         gd= new GridData(GridData.FILL_BOTH);
210         gd.widthHint= convertWidthInCharsToPixels(fWidth);
211         gd.heightHint= convertHeightInCharsToPixels(fHeight);
212         treeViewer.getControl().setLayoutData(gd);
213         treeViewer.addSelectionChangedListener(fTreeViewerAdapter);
214
215         Composite buttonComposite= createSelectionButtons(inner);
216         gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL);
217         buttonComposite.setLayoutData(gd);
218
219         gd= new GridData(GridData.FILL_BOTH);
220         inner.setLayoutData(gd);
221
222         Composite entryComposite= createInsertPositionCombo(composite);
223         entryComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
224
225         Composite commentComposite= createCommentSelection(composite);
226         commentComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
227
228         Composite overrideSuperComposite= createOmitSuper(composite);
229         overrideSuperComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
230
231         Control linkControl= createLinkControl(composite);
232         if (linkControl != null)
233             linkControl.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
234
235         gd= new GridData(GridData.FILL_BOTH);
236         composite.setLayoutData(gd);
237
238         applyDialogFont(composite);
239
240         return composite;
241     }
242
243     protected Composite createInsertPositionCombo(Composite composite) {
244         Composite entryComposite= super.createInsertPositionCombo(composite);
245         addVisibilityAndModifiersChoices(entryComposite);
246         return entryComposite;
247     }
248
249     /*
250      * @see org.eclipse.jdt.internal.ui.dialogs.SourceActionDialog#createLinkControl(org.eclipse.swt.widgets.Composite)
251      */

252     protected Control createLinkControl(Composite composite) {
253         Link link= new Link(composite, SWT.WRAP);
254         link.setText(ActionMessages.GenerateConstructorUsingFieldsSelectionDialog_template_link_message);
255         link.addSelectionListener(new SelectionAdapter() {
256             public void widgetSelected(SelectionEvent e) {
257                 openCodeTempatePage(CodeTemplateContextType.CONSTRUCTORCOMMENT_ID);
258             }
259         });
260         link.setToolTipText(ActionMessages.GenerateConstructorUsingFieldsSelectionDialog_template_link_tooltip);
261         
262         GridData gridData= new GridData(SWT.FILL, SWT.BEGINNING, true, false);
263         gridData.widthHint= convertWidthInCharsToPixels(40); // only expand further if anyone else requires it
264
link.setLayoutData(gridData);
265         return link;
266     }
267
268     protected Composite createOmitSuper(Composite composite) {
269         Composite omitSuperComposite= new Composite(composite, SWT.NONE);
270         GridLayout layout= new GridLayout();
271         layout.marginHeight= 0;
272         layout.marginWidth= 0;
273         omitSuperComposite.setLayout(layout);
274
275         fOmitSuperButton= new Button(omitSuperComposite, SWT.CHECK);
276         fOmitSuperButton.setText(ActionMessages.GenerateConstructorUsingFieldsSelectionDialog_omit_super);
277         fOmitSuperButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
278
279         fOmitSuperButton.addSelectionListener(new SelectionListener() {
280
281             public void widgetDefaultSelected(SelectionEvent e) {
282                 widgetSelected(e);
283             }
284
285             public void widgetSelected(SelectionEvent e) {
286                 boolean isSelected= (((Button) e.widget).getSelection());
287                 setOmitSuper(isSelected);
288             }
289         });
290         fOmitSuperButton.setSelection(isOmitSuper());
291         try {
292             // Disable omit super checkbox unless default constructor and enum
293
final boolean hasContructor= getSuperConstructorChoice().getParameterTypes().length == 0;
294             fOmitSuperButton.setEnabled(hasContructor && !getType().isEnum());
295         } catch (JavaModelException exception) {
296             JavaPlugin.log(exception);
297         }
298         GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
299         gd.horizontalSpan= 2;
300         fOmitSuperButton.setLayoutData(gd);
301
302         return omitSuperComposite;
303     }
304
305     protected Composite createSelectionButtons(Composite composite) {
306         Composite buttonComposite= super.createSelectionButtons(composite);
307
308         GridLayout layout= new GridLayout();
309         buttonComposite.setLayout(layout);
310
311         createUpDownButtons(buttonComposite);
312
313         layout.marginHeight= 0;
314         layout.marginWidth= 0;
315         layout.numColumns= 1;
316
317         return buttonComposite;
318     }
319
320     void createUpDownButtons(Composite buttonComposite) {
321         int numButtons= 2; // up, down
322
fButtonControls= new Button[numButtons];
323         fButtonsEnabled= new boolean[numButtons];
324         fButtonControls[GenerateConstructorUsingFieldsSelectionDialog.UP_INDEX]= createButton(buttonComposite, UP_BUTTON, ActionMessages.GenerateConstructorUsingFieldsSelectionDialog_up_button, false);
325         fButtonControls[GenerateConstructorUsingFieldsSelectionDialog.DOWN_INDEX]= createButton(buttonComposite, DOWN_BUTTON, ActionMessages.GenerateConstructorUsingFieldsSelectionDialog_down_button, false);
326         boolean defaultState= false;
327         fButtonControls[GenerateConstructorUsingFieldsSelectionDialog.UP_INDEX].setEnabled(defaultState);
328         fButtonControls[GenerateConstructorUsingFieldsSelectionDialog.DOWN_INDEX].setEnabled(defaultState);
329         fButtonsEnabled[GenerateConstructorUsingFieldsSelectionDialog.UP_INDEX]= defaultState;
330         fButtonsEnabled[GenerateConstructorUsingFieldsSelectionDialog.DOWN_INDEX]= defaultState;
331     }
332
333     protected Composite createVisibilityControlAndModifiers(Composite parent, final IVisibilityChangeListener visibilityChangeListener, int[] availableVisibilities, int correctVisibility) {
334         int[] visibilities= availableVisibilities;
335         try {
336             if (getType().isEnum())
337                 visibilities= new int[] { };
338         } catch (JavaModelException exception) {
339             JavaPlugin.log(exception);
340         }
341         return createVisibilityControl(parent, visibilityChangeListener, visibilities, correctVisibility);
342     }
343
344     List JavaDoc getElementList() {
345         IStructuredSelection selection= (IStructuredSelection) getTreeViewer().getSelection();
346         List JavaDoc elements= selection.toList();
347         ArrayList JavaDoc elementList= new ArrayList JavaDoc();
348
349         for (int i= 0; i < elements.size(); i++) {
350             elementList.add(elements.get(i));
351         }
352         return elementList;
353     }
354
355     public IMethodBinding getSuperConstructorChoice() {
356         return fSuperConstructors[fSuperIndex];
357     }
358
359     public boolean isOmitSuper() {
360         return fOmitSuper;
361     }
362
363     public void setOmitSuper(boolean omitSuper) {
364         if (fOmitSuper != omitSuper) {
365             fOmitSuper= omitSuper;
366             fGenConstructorSettings.put(OMIT_SUPER, omitSuper);
367         }
368     }
369 }
370
Popular Tags