KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > ExtractInterfaceWizard


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.refactoring;
12
13 import java.util.Arrays 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.layout.GridData;
20 import org.eclipse.swt.layout.GridLayout;
21 import org.eclipse.swt.widgets.Button;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Label;
24 import org.eclipse.swt.widgets.Text;
25
26 import org.eclipse.jface.dialogs.Dialog;
27 import org.eclipse.jface.dialogs.IDialogSettings;
28 import org.eclipse.jface.viewers.ArrayContentProvider;
29 import org.eclipse.jface.viewers.CheckStateChangedEvent;
30 import org.eclipse.jface.viewers.CheckboxTableViewer;
31 import org.eclipse.jface.viewers.ICheckStateListener;
32 import org.eclipse.jface.viewers.ILabelProvider;
33 import org.eclipse.jface.wizard.IWizardPage;
34
35 import org.eclipse.ui.PlatformUI;
36
37 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
38 import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
39
40 import org.eclipse.jdt.core.IJavaElement;
41 import org.eclipse.jdt.core.IMember;
42 import org.eclipse.jdt.core.JavaModelException;
43
44 import org.eclipse.jdt.internal.corext.refactoring.structure.ExtractInterfaceProcessor;
45 import org.eclipse.jdt.internal.corext.refactoring.structure.ExtractInterfaceRefactoring;
46 import org.eclipse.jdt.internal.corext.refactoring.structure.constraints.SuperTypeRefactoringProcessor;
47 import org.eclipse.jdt.internal.corext.util.Messages;
48
49 import org.eclipse.jdt.ui.JavaElementComparator;
50 import org.eclipse.jdt.ui.JavaElementLabels;
51
52 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
53 import org.eclipse.jdt.internal.ui.JavaPlugin;
54 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
55 import org.eclipse.jdt.internal.ui.util.SWTUtil;
56 import org.eclipse.jdt.internal.ui.viewsupport.AppearanceAwareLabelProvider;
57 import org.eclipse.jdt.internal.ui.viewsupport.DecoratingJavaLabelProvider;
58
59 public class ExtractInterfaceWizard extends RefactoringWizard {
60     
61     public ExtractInterfaceWizard(ExtractInterfaceRefactoring refactoring) {
62         super(refactoring, DIALOG_BASED_USER_INTERFACE);
63         setDefaultPageTitle(RefactoringMessages.ExtractInterfaceWizard_Extract_Interface);
64     }
65
66     /* non java-doc
67      * @see RefactoringWizard#addUserInputPages
68      */

69     protected void addUserInputPages(){
70         addPage(new ExtractInterfaceInputPage());
71     }
72     
73     private static class ExtractInterfaceInputPage extends TextInputWizardPage {
74
75         private Button fReplaceAllCheckbox;
76         private Button fDeclarePublicCheckbox;
77         private Button fDeclareAbstractCheckbox;
78         private Button fGenerateCommentsCheckbox;
79         private Button fInstanceofCheckbox;
80         private CheckboxTableViewer fTableViewer;
81         private static final String JavaDoc DESCRIPTION = RefactoringMessages.ExtractInterfaceInputPage_description;
82         private static final String JavaDoc SETTING_PUBLIC= "Public";//$NON-NLS-1$
83
private static final String JavaDoc SETTING_ABSTRACT= "Abstract";//$NON-NLS-1$
84
private static final String JavaDoc SETTING_REPLACE= "Replace"; //$NON-NLS-1$
85
private static final String JavaDoc SETTING_COMMENTS= "Comments"; //$NON-NLS-1$
86
private static final String JavaDoc SETTING_INSTANCEOF= "InstanceOf"; //$NON-NLS-1$
87
private Button fSelectAllButton;
88         private Button fDeselectAllButton;
89
90         public ExtractInterfaceInputPage() {
91             super(DESCRIPTION, true);
92         }
93
94         public void createControl(Composite parent) {
95             initializeDialogUnits(parent);
96             Composite result= new Composite(parent, SWT.NONE);
97             setControl(result);
98             GridLayout layout= new GridLayout();
99             layout.numColumns= 2;
100             result.setLayout(layout);
101         
102             Label label= new Label(result, SWT.NONE);
103             label.setText(RefactoringMessages.ExtractInterfaceInputPage_Interface_name);
104         
105             Text text= createTextInputField(result);
106             text.selectAll();
107             text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
108                 
109             addReplaceAllCheckbox(result);
110             addInstanceofCheckbox(result, layout.marginWidth);
111             fReplaceAllCheckbox.addSelectionListener(new SelectionAdapter() {
112                 
113                 public void widgetSelected(SelectionEvent e) {
114                     fInstanceofCheckbox.setEnabled(fReplaceAllCheckbox.getSelection());
115                 }
116             });
117             addDeclareAsPublicCheckbox(result);
118             addDeclareAsAbstractCheckbox(result);
119
120             Label separator= new Label(result, SWT.NONE);
121             GridData gd= new GridData();
122             gd.horizontalSpan= 2;
123             separator.setLayoutData(gd);
124
125             Label tableLabel= new Label(result, SWT.NONE);
126             tableLabel.setText(RefactoringMessages.ExtractInterfaceInputPage_Members);
127             tableLabel.setEnabled(anyMembersToExtract());
128             gd= new GridData();
129             gd.horizontalSpan= 2;
130             tableLabel.setLayoutData(gd);
131         
132             addMemberListComposite(result);
133             addGenerateCommentsCheckbox(result);
134             Dialog.applyDialogFont(result);
135             initializeCheckboxes();
136             updateUIElementEnablement();
137             PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaHelpContextIds.EXTRACT_INTERFACE_WIZARD_PAGE);
138         }
139
140         private void addGenerateCommentsCheckbox(Composite result) {
141             final ExtractInterfaceProcessor processor= getExtractInterfaceRefactoring().getExtractInterfaceProcessor();
142             String JavaDoc title= RefactoringMessages.ExtractInterfaceWizard_generate_comments;
143             fGenerateCommentsCheckbox= createCheckbox(result, title, false);
144             processor.setComments(fGenerateCommentsCheckbox.getSelection());
145             fGenerateCommentsCheckbox.addSelectionListener(new SelectionAdapter(){
146                 public void widgetSelected(SelectionEvent e) {
147                     processor.setComments(fGenerateCommentsCheckbox.getSelection());
148                 }
149             });
150         }
151
152         private void addInstanceofCheckbox(Composite result, int margin) {
153             final SuperTypeRefactoringProcessor processor= getExtractInterfaceRefactoring().getExtractInterfaceProcessor();
154             String JavaDoc title= RefactoringMessages.ExtractInterfaceWizard_use_supertype;
155             fInstanceofCheckbox= new Button(result, SWT.CHECK);
156             fInstanceofCheckbox.setSelection(false);
157             GridData gd= new GridData();
158             gd.horizontalIndent= (margin + fInstanceofCheckbox.computeSize(SWT.DEFAULT, SWT.DEFAULT).x);
159             gd.horizontalSpan= 2;
160             fInstanceofCheckbox.setLayoutData(gd);
161             fInstanceofCheckbox.setText(title);
162             processor.setInstanceOf(fInstanceofCheckbox.getSelection());
163             fInstanceofCheckbox.addSelectionListener(new SelectionAdapter(){
164                 public void widgetSelected(SelectionEvent e) {
165                     processor.setInstanceOf(fInstanceofCheckbox.getSelection());
166                 }
167             });
168         }
169         
170         private void addMemberListComposite(Composite result) {
171             Composite composite= new Composite(result, SWT.NONE);
172             GridLayout layout= new GridLayout();
173             layout.numColumns= 2;
174             layout.marginWidth= 0;
175             layout.marginHeight= 0;
176             composite.setLayout(layout);
177             GridData gd= new GridData(GridData.FILL_BOTH);
178             gd.heightHint= convertHeightInCharsToPixels(12);
179             gd.horizontalSpan= 2;
180             composite.setLayoutData(gd);
181         
182             fTableViewer= CheckboxTableViewer.newCheckList(composite, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
183             fTableViewer.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
184             fTableViewer.setLabelProvider(createLabelProvider());
185             fTableViewer.setContentProvider(new ArrayContentProvider());
186             try {
187                 fTableViewer.setInput(getExtractableMembers());
188             } catch (JavaModelException e) {
189                 ExceptionHandler.handle(e, RefactoringMessages.ExtractInterfaceInputPage_Extract_Interface, RefactoringMessages.ExtractInterfaceInputPage_Internal_Error);
190                 fTableViewer.setInput(new IMember[0]);
191             }
192             fTableViewer.addCheckStateListener(new ICheckStateListener(){
193                 public void checkStateChanged(CheckStateChangedEvent event) {
194                     ExtractInterfaceInputPage.this.updateUIElementEnablement();
195                 }
196             });
197             fTableViewer.setComparator(new JavaElementComparator());
198             fTableViewer.getControl().setEnabled(anyMembersToExtract());
199
200             createButtonComposite(composite);
201         }
202
203         private IMember[] getExtractableMembers() throws JavaModelException {
204             return getExtractInterfaceRefactoring().getExtractInterfaceProcessor().getExtractableMembers();
205         }
206
207         protected void updateUIElementEnablement() {
208             final IMember[] checked= getCheckedMembers();
209             IMember[] extractable;
210             try {
211                 extractable= getExtractableMembers();
212             } catch (JavaModelException exception) {
213                 extractable= new IMember[0];
214                 JavaPlugin.log(exception);
215             }
216             final boolean enabled= containsMethods(checked);
217             fDeclarePublicCheckbox.setEnabled(enabled);
218             fDeclareAbstractCheckbox.setEnabled(enabled);
219             fGenerateCommentsCheckbox.setEnabled(enabled);
220             fInstanceofCheckbox.setEnabled(fReplaceAllCheckbox.getSelection());
221             fSelectAllButton.setEnabled(checked.length < extractable.length);
222             fDeselectAllButton.setEnabled(checked.length > 0);
223         }
224
225         private static boolean containsMethods(IMember[] members) {
226             for (int i= 0; i < members.length; i++) {
227                 if (members[i].getElementType() == IJavaElement.METHOD)
228                     return true;
229             }
230             return false;
231         }
232
233         private ILabelProvider createLabelProvider(){
234             AppearanceAwareLabelProvider lprovider= new AppearanceAwareLabelProvider(
235                 AppearanceAwareLabelProvider.DEFAULT_TEXTFLAGS | JavaElementLabels.F_APP_TYPE_SIGNATURE,
236                 AppearanceAwareLabelProvider.DEFAULT_IMAGEFLAGS
237             );
238         
239             return new DecoratingJavaLabelProvider(lprovider);
240         }
241
242         private void createButtonComposite(Composite composite) {
243             GridData gd;
244             Composite buttonComposite= new Composite(composite, SWT.NONE);
245             GridLayout gl= new GridLayout();
246             gl.marginHeight= 0;
247             gl.marginWidth= 0;
248             buttonComposite.setLayout(gl);
249             gd= new GridData(GridData.FILL_VERTICAL);
250             buttonComposite.setLayoutData(gd);
251         
252             fSelectAllButton= new Button(buttonComposite, SWT.PUSH);
253             fSelectAllButton.setText(RefactoringMessages.ExtractInterfaceInputPage_Select_All);
254             fSelectAllButton.setEnabled(anyMembersToExtract());
255             fSelectAllButton.setLayoutData(new GridData());
256             SWTUtil.setButtonDimensionHint(fSelectAllButton);
257             fSelectAllButton.addSelectionListener(new SelectionAdapter(){
258                 public void widgetSelected(SelectionEvent e) {
259                     fTableViewer.setAllChecked(true);
260                     ExtractInterfaceInputPage.this.updateUIElementEnablement();
261                 }
262             });
263         
264             fDeselectAllButton= new Button(buttonComposite, SWT.PUSH);
265             fDeselectAllButton.setText(RefactoringMessages.ExtractInterfaceInputPage_Deselect_All);
266             fDeselectAllButton.setEnabled(anyMembersToExtract());
267             fDeselectAllButton.setLayoutData(new GridData());
268             SWTUtil.setButtonDimensionHint(fDeselectAllButton);
269             fDeselectAllButton.addSelectionListener(new SelectionAdapter(){
270                 public void widgetSelected(SelectionEvent e) {
271                     fTableViewer.setAllChecked(false);
272                     ExtractInterfaceInputPage.this.updateUIElementEnablement();
273                 }
274             });
275         }
276
277         private boolean anyMembersToExtract() {
278             try {
279                 return getExtractableMembers().length > 0;
280             } catch (JavaModelException e) {
281                 return false;
282             }
283         }
284
285         private void addReplaceAllCheckbox(Composite result) {
286             final ExtractInterfaceProcessor processor= getExtractInterfaceRefactoring().getExtractInterfaceProcessor();
287             String JavaDoc title= RefactoringMessages.ExtractInterfaceInputPage_change_references;
288             boolean defaultValue= processor.isReplace();
289             fReplaceAllCheckbox= createCheckbox(result, title, defaultValue);
290             processor.setReplace(fReplaceAllCheckbox.getSelection());
291             fReplaceAllCheckbox.addSelectionListener(new SelectionAdapter(){
292                 public void widgetSelected(SelectionEvent e) {
293                     processor.setReplace(fReplaceAllCheckbox.getSelection());
294                 }
295             });
296         }
297         
298         private void addDeclareAsPublicCheckbox(Composite result) {
299             final ExtractInterfaceProcessor processor= getExtractInterfaceRefactoring().getExtractInterfaceProcessor();
300             String JavaDoc[] keys= {RefactoringMessages.ExtractInterfaceWizard_public_label};
301             String JavaDoc title= Messages.format(RefactoringMessages.ExtractInterfaceWizard_12, keys);
302             boolean defaultValue= processor.getPublic();
303             fDeclarePublicCheckbox= createCheckbox(result, title, defaultValue);
304             processor.setPublic(fDeclarePublicCheckbox.getSelection());
305             fDeclarePublicCheckbox.addSelectionListener(new SelectionAdapter(){
306                 public void widgetSelected(SelectionEvent e) {
307                     processor.setPublic(fDeclarePublicCheckbox.getSelection());
308                 }
309             });
310         }
311
312         private void addDeclareAsAbstractCheckbox(Composite result) {
313             final ExtractInterfaceProcessor processor= getExtractInterfaceRefactoring().getExtractInterfaceProcessor();
314             String JavaDoc[] keys= {RefactoringMessages.ExtractInterfaceWizard_abstract_label};
315             String JavaDoc title= Messages.format(RefactoringMessages.ExtractInterfaceWizard_12, keys);
316             boolean defaultValue= processor.getAbstract();
317             fDeclareAbstractCheckbox= createCheckbox(result, title, defaultValue);
318             processor.setAbstract(fDeclareAbstractCheckbox.getSelection());
319             fDeclareAbstractCheckbox.addSelectionListener(new SelectionAdapter(){
320                 public void widgetSelected(SelectionEvent e) {
321                     processor.setAbstract(fDeclareAbstractCheckbox.getSelection());
322                 }
323             });
324         }
325         
326         private static Button createCheckbox(Composite parent, String JavaDoc title, boolean value){
327             Button checkBox= new Button(parent, SWT.CHECK);
328             checkBox.setText(title);
329             checkBox.setSelection(value);
330             GridData layoutData= new GridData();
331             layoutData.horizontalSpan= 2;
332
333             checkBox.setLayoutData(layoutData);
334             return checkBox;
335         }
336
337         /*
338          * @see org.eclipse.jdt.internal.ui.refactoring.TextInputWizardPage#validateTextField(String)
339          */

340         protected RefactoringStatus validateTextField(String JavaDoc text) {
341             final ExtractInterfaceProcessor processor= getExtractInterfaceRefactoring().getExtractInterfaceProcessor();
342             processor.setTypeName(text);
343             return processor.checkTypeName(text);
344         }
345
346         private ExtractInterfaceRefactoring getExtractInterfaceRefactoring() {
347             return (ExtractInterfaceRefactoring) getRefactoring();
348         }
349     
350         /*
351          * @see org.eclipse.jface.wizard.IWizardPage#getNextPage()
352          */

353         public IWizardPage getNextPage() {
354             try {
355                 initializeRefactoring();
356                 storeDialogSettings();
357                 return super.getNextPage();
358             } catch (JavaModelException e) {
359                 JavaPlugin.log(e);
360                 return null;
361             }
362         }
363
364         /*
365          * @see org.eclipse.jdt.internal.ui.refactoring.RefactoringWizardPage#performFinish()
366          */

367         public boolean performFinish(){
368             try {
369                 initializeRefactoring();
370                 storeDialogSettings();
371                 return super.performFinish();
372             } catch (JavaModelException e) {
373                 JavaPlugin.log(e);
374                 return false;
375             }
376         }
377
378         private void initializeRefactoring() throws JavaModelException {
379             final ExtractInterfaceProcessor processor= getExtractInterfaceRefactoring().getExtractInterfaceProcessor();
380             processor.setTypeName(getText());
381             processor.setReplace(fReplaceAllCheckbox.getSelection());
382             processor.setExtractedMembers(getCheckedMembers());
383             processor.setAbstract(fDeclareAbstractCheckbox.getSelection());
384             processor.setPublic(fDeclarePublicCheckbox.getSelection());
385             processor.setComments(fGenerateCommentsCheckbox.getSelection());
386             processor.setInstanceOf(fInstanceofCheckbox.getSelection());
387         }
388         
389         private IMember[] getCheckedMembers() {
390             List JavaDoc checked= Arrays.asList(fTableViewer.getCheckedElements());
391             return (IMember[]) checked.toArray(new IMember[checked.size()]);
392         }
393
394         public void dispose() {
395             fInstanceofCheckbox= null;
396             fGenerateCommentsCheckbox= null;
397             fReplaceAllCheckbox= null;
398             fTableViewer= null;
399             super.dispose();
400         }
401
402         private void initializeCheckboxes() {
403             initializeCheckBox(fDeclarePublicCheckbox, SETTING_PUBLIC, true);
404             initializeCheckBox(fDeclareAbstractCheckbox, SETTING_ABSTRACT, true);
405             initializeCheckBox(fReplaceAllCheckbox, SETTING_REPLACE, true);
406             initializeCheckBox(fGenerateCommentsCheckbox, SETTING_COMMENTS, true);
407             initializeCheckBox(fInstanceofCheckbox, SETTING_INSTANCEOF, false);
408         }
409
410         private void initializeCheckBox(Button checkbox, String JavaDoc property, boolean def) {
411             String JavaDoc s= JavaPlugin.getDefault().getDialogSettings().get(property);
412             if (s != null)
413                 checkbox.setSelection(new Boolean JavaDoc(s).booleanValue());
414             else
415                 checkbox.setSelection(def);
416         }
417
418         private void storeDialogSettings() {
419             final IDialogSettings settings= JavaPlugin.getDefault().getDialogSettings();
420             settings.put(SETTING_PUBLIC, fDeclarePublicCheckbox.getSelection());
421             settings.put(SETTING_ABSTRACT, fDeclareAbstractCheckbox.getSelection());
422             settings.put(SETTING_REPLACE, fReplaceAllCheckbox.getSelection());
423             settings.put(SETTING_COMMENTS, fGenerateCommentsCheckbox.getSelection());
424             settings.put(SETTING_INSTANCEOF, fInstanceofCheckbox.getSelection());
425         }
426     }
427 }
428
Popular Tags