]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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; | |
14 | import java.util.List; | |
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.Refactoring; | |
38 | import org.eclipse.ltk.core.refactoring.RefactoringStatus; | |
39 | import org.eclipse.ltk.ui.refactoring.RefactoringWizard; | |
40 | ||
41 | import org.eclipse.jdt.core.IJavaElement; | |
42 | import org.eclipse.jdt.core.IMember; | |
43 | import org.eclipse.jdt.core.JavaModelException; | |
44 | ||
45 | import org.eclipse.jdt.internal.corext.refactoring.structure.ExtractInterfaceProcessor; | |
46 | import org.eclipse.jdt.internal.corext.util.Messages; | |
47 | ||
48 | import org.eclipse.jdt.ui.JavaElementComparator; | |
49 | import org.eclipse.jdt.ui.JavaElementLabels; | |
50 | ||
51 | import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; | |
52 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
53 | import org.eclipse.jdt.internal.ui.util.ExceptionHandler; | |
54 | import org.eclipse.jdt.internal.ui.util.SWTUtil; | |
55 | import org.eclipse.jdt.internal.ui.viewsupport.AppearanceAwareLabelProvider; | |
56 | import org.eclipse.jdt.internal.ui.viewsupport.DecoratingJavaLabelProvider; | |
57 | ||
58 | public class ExtractInterfaceWizard extends RefactoringWizard { | |
59 | ||
60 | private final ExtractInterfaceProcessor fProcessor; | |
61 | ||
62 | public ExtractInterfaceWizard(ExtractInterfaceProcessor processor, Refactoring refactoring) { | |
63 | super(refactoring, DIALOG_BASED_USER_INTERFACE); | |
64 | fProcessor= processor; | |
65 | setDefaultPageTitle(RefactoringMessages.ExtractInterfaceWizard_Extract_Interface); | |
66 | } | |
67 | ||
68 | /* non java-doc | |
69 | * @see RefactoringWizard#addUserInputPages | |
70 | */ | |
71 | @Override | |
72 | protected void addUserInputPages(){ | |
73 | addPage(new ExtractInterfaceInputPage(fProcessor)); | |
74 | } | |
75 | ||
76 | private static class ExtractInterfaceInputPage extends TextInputWizardPage { | |
77 | ||
78 | private Button fReplaceAllCheckbox; | |
79 | private Button fDeclarePublicCheckbox; | |
80 | private Button fDeclareAbstractCheckbox; | |
81 | private Button fGenerateAnnotationsCheckbox; | |
82 | private Button fGenerateCommentsCheckbox; | |
83 | private Button fInstanceofCheckbox; | |
84 | private CheckboxTableViewer fTableViewer; | |
85 | private static final String DESCRIPTION = RefactoringMessages.ExtractInterfaceInputPage_description; | |
86 | private static final String SETTING_PUBLIC= "Public";//$NON-NLS-1$ | |
87 | private static final String SETTING_ABSTRACT= "Abstract";//$NON-NLS-1$ | |
88 | private static final String SETTING_ANNOTATIONS= "Annotations";//$NON-NLS-1$ | |
89 | private static final String SETTING_REPLACE= "Replace"; //$NON-NLS-1$ | |
90 | private static final String SETTING_COMMENTS= "Comments"; //$NON-NLS-1$ | |
91 | private static final String SETTING_INSTANCEOF= "InstanceOf"; //$NON-NLS-1$ | |
92 | private Button fSelectAllButton; | |
93 | private Button fDeselectAllButton; | |
94 | ||
95 | private final ExtractInterfaceProcessor fProcessor; | |
96 | ||
97 | public ExtractInterfaceInputPage(ExtractInterfaceProcessor processor) { | |
98 | super(DESCRIPTION, true); | |
99 | fProcessor= processor; | |
100 | } | |
101 | ||
102 | public void createControl(Composite parent) { | |
103 | initializeDialogUnits(parent); | |
104 | Composite result= new Composite(parent, SWT.NONE); | |
105 | setControl(result); | |
106 | GridLayout layout= new GridLayout(); | |
107 | layout.numColumns= 2; | |
108 | result.setLayout(layout); | |
109 | ||
110 | Label label= new Label(result, SWT.NONE); | |
111 | label.setText(RefactoringMessages.ExtractInterfaceInputPage_Interface_name); | |
112 | ||
113 | Text text= createTextInputField(result); | |
114 | text.selectAll(); | |
115 | text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); | |
116 | ||
117 | addReplaceAllCheckbox(result); | |
118 | addInstanceofCheckbox(result, layout.marginWidth); | |
119 | fReplaceAllCheckbox.addSelectionListener(new SelectionAdapter() { | |
120 | ||
121 | @Override | |
122 | public void widgetSelected(SelectionEvent e) { | |
123 | fInstanceofCheckbox.setEnabled(fReplaceAllCheckbox.getSelection()); | |
124 | } | |
125 | }); | |
126 | addDeclareAsPublicCheckbox(result); | |
127 | addDeclareAsAbstractCheckbox(result); | |
128 | addGenerateAnnotationsCheckbox(result); | |
129 | ||
130 | Label separator= new Label(result, SWT.NONE); | |
131 | GridData gd= new GridData(); | |
132 | gd.horizontalSpan= 2; | |
133 | separator.setLayoutData(gd); | |
134 | ||
135 | Label tableLabel= new Label(result, SWT.NONE); | |
136 | tableLabel.setText(RefactoringMessages.ExtractInterfaceInputPage_Members); | |
137 | tableLabel.setEnabled(anyMembersToExtract()); | |
138 | gd= new GridData(); | |
139 | gd.horizontalSpan= 2; | |
140 | tableLabel.setLayoutData(gd); | |
141 | ||
142 | addMemberListComposite(result); | |
143 | addGenerateCommentsCheckbox(result); | |
144 | Dialog.applyDialogFont(result); | |
145 | initializeCheckboxes(); | |
146 | updateUIElementEnablement(); | |
147 | PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaHelpContextIds.EXTRACT_INTERFACE_WIZARD_PAGE); | |
148 | } | |
149 | ||
150 | private void addGenerateAnnotationsCheckbox(Composite result) { | |
151 | String title= RefactoringMessages.ExtractInterfaceWizard_generate_annotations; | |
152 | fGenerateAnnotationsCheckbox= createCheckbox(result, title, false); | |
153 | fProcessor.setAnnotations(fGenerateAnnotationsCheckbox.getSelection()); | |
154 | fGenerateAnnotationsCheckbox.addSelectionListener(new SelectionAdapter(){ | |
155 | @Override | |
156 | public void widgetSelected(SelectionEvent e) { | |
157 | fProcessor.setAnnotations(fGenerateAnnotationsCheckbox.getSelection()); | |
158 | } | |
159 | }); | |
160 | } | |
161 | ||
162 | private void addGenerateCommentsCheckbox(Composite result) { | |
163 | String title= RefactoringMessages.ExtractInterfaceWizard_generate_comments; | |
164 | fGenerateCommentsCheckbox= createCheckbox(result, title, false); | |
165 | fProcessor.setComments(fGenerateCommentsCheckbox.getSelection()); | |
166 | fGenerateCommentsCheckbox.addSelectionListener(new SelectionAdapter(){ | |
167 | @Override | |
168 | public void widgetSelected(SelectionEvent e) { | |
169 | fProcessor.setComments(fGenerateCommentsCheckbox.getSelection()); | |
170 | } | |
171 | }); | |
172 | } | |
173 | ||
174 | private void addInstanceofCheckbox(Composite result, int margin) { | |
175 | String title= RefactoringMessages.ExtractInterfaceWizard_use_supertype; | |
176 | fInstanceofCheckbox= new Button(result, SWT.CHECK); | |
177 | fInstanceofCheckbox.setSelection(false); | |
178 | GridData gd= new GridData(); | |
179 | gd.horizontalIndent= (margin + fInstanceofCheckbox.computeSize(SWT.DEFAULT, SWT.DEFAULT).x); | |
180 | gd.horizontalSpan= 2; | |
181 | fInstanceofCheckbox.setLayoutData(gd); | |
182 | fInstanceofCheckbox.setText(title); | |
183 | fProcessor.setInstanceOf(fInstanceofCheckbox.getSelection()); | |
184 | fInstanceofCheckbox.addSelectionListener(new SelectionAdapter(){ | |
185 | @Override | |
186 | public void widgetSelected(SelectionEvent e) { | |
187 | fProcessor.setInstanceOf(fInstanceofCheckbox.getSelection()); | |
188 | } | |
189 | }); | |
190 | } | |
191 | ||
192 | private void addMemberListComposite(Composite result) { | |
193 | Composite composite= new Composite(result, SWT.NONE); | |
194 | GridLayout layout= new GridLayout(); | |
195 | layout.numColumns= 2; | |
196 | layout.marginWidth= 0; | |
197 | layout.marginHeight= 0; | |
198 | composite.setLayout(layout); | |
199 | GridData gd= new GridData(GridData.FILL_BOTH); | |
200 | gd.heightHint= convertHeightInCharsToPixels(12); | |
201 | gd.horizontalSpan= 2; | |
202 | composite.setLayoutData(gd); | |
203 | ||
204 | fTableViewer= CheckboxTableViewer.newCheckList(composite, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); | |
205 | fTableViewer.getTable().setLayoutData(new GridData(GridData.FILL_BOTH)); | |
206 | fTableViewer.setLabelProvider(createLabelProvider()); | |
207 | fTableViewer.setContentProvider(new ArrayContentProvider()); | |
208 | try { | |
209 | fTableViewer.setInput(getExtractableMembers()); | |
210 | } catch (JavaModelException e) { | |
211 | ExceptionHandler.handle(e, RefactoringMessages.ExtractInterfaceInputPage_Extract_Interface, RefactoringMessages.ExtractInterfaceInputPage_Internal_Error); | |
212 | fTableViewer.setInput(new IMember[0]); | |
213 | } | |
214 | fTableViewer.addCheckStateListener(new ICheckStateListener(){ | |
215 | public void checkStateChanged(CheckStateChangedEvent event) { | |
216 | ExtractInterfaceInputPage.this.updateUIElementEnablement(); | |
217 | } | |
218 | }); | |
219 | fTableViewer.setComparator(new JavaElementComparator()); | |
220 | fTableViewer.getControl().setEnabled(anyMembersToExtract()); | |
221 | ||
222 | createButtonComposite(composite); | |
223 | } | |
224 | ||
225 | private IMember[] getExtractableMembers() throws JavaModelException { | |
226 | return fProcessor.getExtractableMembers(); | |
227 | } | |
228 | ||
229 | protected void updateUIElementEnablement() { | |
230 | final IMember[] checked= getCheckedMembers(); | |
231 | IMember[] extractable; | |
232 | try { | |
233 | extractable= getExtractableMembers(); | |
234 | } catch (JavaModelException exception) { | |
235 | extractable= new IMember[0]; | |
236 | JavaPlugin.log(exception); | |
237 | } | |
238 | final boolean enabled= containsMethods(checked); | |
239 | fDeclarePublicCheckbox.setEnabled(enabled); | |
240 | fDeclareAbstractCheckbox.setEnabled(enabled); | |
241 | fGenerateAnnotationsCheckbox.setEnabled(enabled); | |
242 | fGenerateCommentsCheckbox.setEnabled(enabled); | |
243 | fInstanceofCheckbox.setEnabled(fReplaceAllCheckbox.getSelection()); | |
244 | fSelectAllButton.setEnabled(checked.length < extractable.length); | |
245 | fDeselectAllButton.setEnabled(checked.length > 0); | |
246 | } | |
247 | ||
248 | private static boolean containsMethods(IMember[] members) { | |
249 | for (int i= 0; i < members.length; i++) { | |
250 | if (members[i].getElementType() == IJavaElement.METHOD) | |
251 | return true; | |
252 | } | |
253 | return false; | |
254 | } | |
255 | ||
256 | private ILabelProvider createLabelProvider(){ | |
257 | AppearanceAwareLabelProvider lprovider= new AppearanceAwareLabelProvider( | |
258 | AppearanceAwareLabelProvider.DEFAULT_TEXTFLAGS | JavaElementLabels.F_APP_TYPE_SIGNATURE, | |
259 | AppearanceAwareLabelProvider.DEFAULT_IMAGEFLAGS | |
260 | ); | |
261 | ||
262 | return new DecoratingJavaLabelProvider(lprovider); | |
263 | } | |
264 | ||
265 | private void createButtonComposite(Composite composite) { | |
266 | GridData gd; | |
267 | Composite buttonComposite= new Composite(composite, SWT.NONE); | |
268 | GridLayout gl= new GridLayout(); | |
269 | gl.marginHeight= 0; | |
270 | gl.marginWidth= 0; | |
271 | buttonComposite.setLayout(gl); | |
272 | gd= new GridData(GridData.FILL_VERTICAL); | |
273 | buttonComposite.setLayoutData(gd); | |
274 | ||
275 | fSelectAllButton= new Button(buttonComposite, SWT.PUSH); | |
276 | fSelectAllButton.setText(RefactoringMessages.ExtractInterfaceInputPage_Select_All); | |
277 | fSelectAllButton.setEnabled(anyMembersToExtract()); | |
278 | fSelectAllButton.setLayoutData(new GridData()); | |
279 | SWTUtil.setButtonDimensionHint(fSelectAllButton); | |
280 | fSelectAllButton.addSelectionListener(new SelectionAdapter(){ | |
281 | @Override | |
282 | public void widgetSelected(SelectionEvent e) { | |
283 | fTableViewer.setAllChecked(true); | |
284 | ExtractInterfaceInputPage.this.updateUIElementEnablement(); | |
285 | } | |
286 | }); | |
287 | ||
288 | fDeselectAllButton= new Button(buttonComposite, SWT.PUSH); | |
289 | fDeselectAllButton.setText(RefactoringMessages.ExtractInterfaceInputPage_Deselect_All); | |
290 | fDeselectAllButton.setEnabled(anyMembersToExtract()); | |
291 | fDeselectAllButton.setLayoutData(new GridData()); | |
292 | SWTUtil.setButtonDimensionHint(fDeselectAllButton); | |
293 | fDeselectAllButton.addSelectionListener(new SelectionAdapter(){ | |
294 | @Override | |
295 | public void widgetSelected(SelectionEvent e) { | |
296 | fTableViewer.setAllChecked(false); | |
297 | ExtractInterfaceInputPage.this.updateUIElementEnablement(); | |
298 | } | |
299 | }); | |
300 | } | |
301 | ||
302 | private boolean anyMembersToExtract() { | |
303 | try { | |
304 | return getExtractableMembers().length > 0; | |
305 | } catch (JavaModelException e) { | |
306 | return false; | |
307 | } | |
308 | } | |
309 | ||
310 | private void addReplaceAllCheckbox(Composite result) { | |
311 | String title= RefactoringMessages.ExtractInterfaceInputPage_change_references; | |
312 | boolean defaultValue= fProcessor.isReplace(); | |
313 | fReplaceAllCheckbox= createCheckbox(result, title, defaultValue); | |
314 | fProcessor.setReplace(fReplaceAllCheckbox.getSelection()); | |
315 | fReplaceAllCheckbox.addSelectionListener(new SelectionAdapter(){ | |
316 | @Override | |
317 | public void widgetSelected(SelectionEvent e) { | |
318 | fProcessor.setReplace(fReplaceAllCheckbox.getSelection()); | |
319 | } | |
320 | }); | |
321 | } | |
322 | ||
323 | private void addDeclareAsPublicCheckbox(Composite result) { | |
324 | String[] keys= {RefactoringMessages.ExtractInterfaceWizard_public_label}; | |
325 | String title= Messages.format(RefactoringMessages.ExtractInterfaceWizard_12, keys); | |
326 | boolean defaultValue= fProcessor.getPublic(); | |
327 | fDeclarePublicCheckbox= createCheckbox(result, title, defaultValue); | |
328 | fProcessor.setPublic(fDeclarePublicCheckbox.getSelection()); | |
329 | fDeclarePublicCheckbox.addSelectionListener(new SelectionAdapter(){ | |
330 | @Override | |
331 | public void widgetSelected(SelectionEvent e) { | |
332 | fProcessor.setPublic(fDeclarePublicCheckbox.getSelection()); | |
333 | } | |
334 | }); | |
335 | } | |
336 | ||
337 | private void addDeclareAsAbstractCheckbox(Composite result) { | |
338 | final ExtractInterfaceProcessor processor= fProcessor; | |
339 | String[] keys= {RefactoringMessages.ExtractInterfaceWizard_abstract_label}; | |
340 | String title= Messages.format(RefactoringMessages.ExtractInterfaceWizard_12, keys); | |
341 | boolean defaultValue= processor.getAbstract(); | |
342 | fDeclareAbstractCheckbox= createCheckbox(result, title, defaultValue); | |
343 | processor.setAbstract(fDeclareAbstractCheckbox.getSelection()); | |
344 | fDeclareAbstractCheckbox.addSelectionListener(new SelectionAdapter(){ | |
345 | @Override | |
346 | public void widgetSelected(SelectionEvent e) { | |
347 | processor.setAbstract(fDeclareAbstractCheckbox.getSelection()); | |
348 | } | |
349 | }); | |
350 | } | |
351 | ||
352 | private static Button createCheckbox(Composite parent, String title, boolean value){ | |
353 | Button checkBox= new Button(parent, SWT.CHECK); | |
354 | checkBox.setText(title); | |
355 | checkBox.setSelection(value); | |
356 | GridData layoutData= new GridData(); | |
357 | layoutData.horizontalSpan= 2; | |
358 | ||
359 | checkBox.setLayoutData(layoutData); | |
360 | return checkBox; | |
361 | } | |
362 | ||
363 | /* | |
364 | * @see org.eclipse.jdt.internal.ui.refactoring.TextInputWizardPage#validateTextField(String) | |
365 | */ | |
366 | @Override | |
367 | protected RefactoringStatus validateTextField(String text) { | |
368 | final ExtractInterfaceProcessor processor= fProcessor; | |
369 | processor.setTypeName(text); | |
370 | return processor.checkTypeName(text); | |
371 | } | |
372 | ||
373 | /* | |
374 | * @see org.eclipse.jface.wizard.IWizardPage#getNextPage() | |
375 | */ | |
376 | @Override | |
377 | public IWizardPage getNextPage() { | |
378 | try { | |
379 | initializeRefactoring(); | |
380 | storeDialogSettings(); | |
381 | return super.getNextPage(); | |
382 | } catch (JavaModelException e) { | |
383 | JavaPlugin.log(e); | |
384 | return null; | |
385 | } | |
386 | } | |
387 | ||
388 | /* | |
389 | * @see org.eclipse.jdt.internal.ui.refactoring.RefactoringWizardPage#performFinish() | |
390 | */ | |
391 | @Override | |
392 | public boolean performFinish(){ | |
393 | try { | |
394 | initializeRefactoring(); | |
395 | storeDialogSettings(); | |
396 | return super.performFinish(); | |
397 | } catch (JavaModelException e) { | |
398 | JavaPlugin.log(e); | |
399 | return false; | |
400 | } | |
401 | } | |
402 | ||
403 | private void initializeRefactoring() throws JavaModelException { | |
404 | fProcessor.setTypeName(getText()); | |
405 | fProcessor.setReplace(fReplaceAllCheckbox.getSelection()); | |
406 | fProcessor.setExtractedMembers(getCheckedMembers()); | |
407 | fProcessor.setAbstract(fDeclareAbstractCheckbox.getSelection()); | |
408 | fProcessor.setPublic(fDeclarePublicCheckbox.getSelection()); | |
409 | fProcessor.setAnnotations(fGenerateAnnotationsCheckbox.getSelection()); | |
410 | fProcessor.setComments(fGenerateCommentsCheckbox.getSelection()); | |
411 | fProcessor.setInstanceOf(fInstanceofCheckbox.getSelection()); | |
412 | } | |
413 | ||
414 | private IMember[] getCheckedMembers() { | |
415 | List<?> checked= Arrays.asList(fTableViewer.getCheckedElements()); | |
416 | return checked.toArray(new IMember[checked.size()]); | |
417 | } | |
418 | ||
419 | @Override | |
420 | public void dispose() { | |
421 | fInstanceofCheckbox= null; | |
422 | fGenerateCommentsCheckbox= null; | |
423 | fReplaceAllCheckbox= null; | |
424 | fTableViewer= null; | |
425 | super.dispose(); | |
426 | } | |
427 | ||
428 | private void initializeCheckboxes() { | |
429 | initializeCheckBox(fDeclarePublicCheckbox, SETTING_PUBLIC, true); | |
430 | initializeCheckBox(fDeclareAbstractCheckbox, SETTING_ABSTRACT, true); | |
431 | initializeCheckBox(fGenerateAnnotationsCheckbox, SETTING_ANNOTATIONS, true); | |
432 | initializeCheckBox(fReplaceAllCheckbox, SETTING_REPLACE, true); | |
433 | initializeCheckBox(fGenerateCommentsCheckbox, SETTING_COMMENTS, true); | |
434 | initializeCheckBox(fInstanceofCheckbox, SETTING_INSTANCEOF, false); | |
435 | } | |
436 | ||
437 | private void initializeCheckBox(Button checkbox, String property, boolean def) { | |
438 | String s= JavaPlugin.getDefault().getDialogSettings().get(property); | |
439 | if (s != null) | |
440 | checkbox.setSelection(new Boolean(s).booleanValue()); | |
441 | else | |
442 | checkbox.setSelection(def); | |
443 | } | |
444 | ||
445 | private void storeDialogSettings() { | |
446 | final IDialogSettings settings= JavaPlugin.getDefault().getDialogSettings(); | |
447 | settings.put(SETTING_PUBLIC, fDeclarePublicCheckbox.getSelection()); | |
448 | settings.put(SETTING_ABSTRACT, fDeclareAbstractCheckbox.getSelection()); | |
449 | settings.put(SETTING_ANNOTATIONS, fGenerateAnnotationsCheckbox.getSelection()); | |
450 | settings.put(SETTING_REPLACE, fReplaceAllCheckbox.getSelection()); | |
451 | settings.put(SETTING_COMMENTS, fGenerateCommentsCheckbox.getSelection()); | |
452 | settings.put(SETTING_INSTANCEOF, fInstanceofCheckbox.getSelection()); | |
453 | } | |
454 | } | |
455 | } |