001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.refactoring;
011:
012: import java.util.Arrays;
013: import java.util.List;
014:
015: import org.eclipse.swt.SWT;
016: import org.eclipse.swt.events.SelectionAdapter;
017: import org.eclipse.swt.events.SelectionEvent;
018: import org.eclipse.swt.layout.GridData;
019: import org.eclipse.swt.layout.GridLayout;
020: import org.eclipse.swt.widgets.Button;
021: import org.eclipse.swt.widgets.Composite;
022: import org.eclipse.swt.widgets.Label;
023: import org.eclipse.swt.widgets.Text;
024:
025: import org.eclipse.jface.dialogs.Dialog;
026: import org.eclipse.jface.dialogs.IDialogSettings;
027: import org.eclipse.jface.viewers.ArrayContentProvider;
028: import org.eclipse.jface.viewers.CheckStateChangedEvent;
029: import org.eclipse.jface.viewers.CheckboxTableViewer;
030: import org.eclipse.jface.viewers.ICheckStateListener;
031: import org.eclipse.jface.viewers.ILabelProvider;
032: import org.eclipse.jface.wizard.IWizardPage;
033:
034: import org.eclipse.ui.PlatformUI;
035:
036: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
037: import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
038:
039: import org.eclipse.jdt.core.IJavaElement;
040: import org.eclipse.jdt.core.IMember;
041: import org.eclipse.jdt.core.JavaModelException;
042:
043: import org.eclipse.jdt.internal.corext.refactoring.structure.ExtractInterfaceProcessor;
044: import org.eclipse.jdt.internal.corext.refactoring.structure.ExtractInterfaceRefactoring;
045: import org.eclipse.jdt.internal.corext.refactoring.structure.constraints.SuperTypeRefactoringProcessor;
046: import org.eclipse.jdt.internal.corext.util.Messages;
047:
048: import org.eclipse.jdt.ui.JavaElementComparator;
049: import org.eclipse.jdt.ui.JavaElementLabels;
050:
051: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
052: import org.eclipse.jdt.internal.ui.JavaPlugin;
053: import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
054: import org.eclipse.jdt.internal.ui.util.SWTUtil;
055: import org.eclipse.jdt.internal.ui.viewsupport.AppearanceAwareLabelProvider;
056: import org.eclipse.jdt.internal.ui.viewsupport.DecoratingJavaLabelProvider;
057:
058: public class ExtractInterfaceWizard extends RefactoringWizard {
059:
060: public ExtractInterfaceWizard(
061: ExtractInterfaceRefactoring refactoring) {
062: super (refactoring, DIALOG_BASED_USER_INTERFACE);
063: setDefaultPageTitle(RefactoringMessages.ExtractInterfaceWizard_Extract_Interface);
064: }
065:
066: /* non java-doc
067: * @see RefactoringWizard#addUserInputPages
068: */
069: protected void addUserInputPages() {
070: addPage(new ExtractInterfaceInputPage());
071: }
072:
073: private static class ExtractInterfaceInputPage extends
074: TextInputWizardPage {
075:
076: private Button fReplaceAllCheckbox;
077: private Button fDeclarePublicCheckbox;
078: private Button fDeclareAbstractCheckbox;
079: private Button fGenerateCommentsCheckbox;
080: private Button fInstanceofCheckbox;
081: private CheckboxTableViewer fTableViewer;
082: private static final String DESCRIPTION = RefactoringMessages.ExtractInterfaceInputPage_description;
083: private static final String SETTING_PUBLIC = "Public";//$NON-NLS-1$
084: private static final String SETTING_ABSTRACT = "Abstract";//$NON-NLS-1$
085: private static final String SETTING_REPLACE = "Replace"; //$NON-NLS-1$
086: private static final String SETTING_COMMENTS = "Comments"; //$NON-NLS-1$
087: private static final String SETTING_INSTANCEOF = "InstanceOf"; //$NON-NLS-1$
088: private Button fSelectAllButton;
089: private Button fDeselectAllButton;
090:
091: public ExtractInterfaceInputPage() {
092: super (DESCRIPTION, true);
093: }
094:
095: public void createControl(Composite parent) {
096: initializeDialogUnits(parent);
097: Composite result = new Composite(parent, SWT.NONE);
098: setControl(result);
099: GridLayout layout = new GridLayout();
100: layout.numColumns = 2;
101: result.setLayout(layout);
102:
103: Label label = new Label(result, SWT.NONE);
104: label
105: .setText(RefactoringMessages.ExtractInterfaceInputPage_Interface_name);
106:
107: Text text = createTextInputField(result);
108: text.selectAll();
109: text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
110:
111: addReplaceAllCheckbox(result);
112: addInstanceofCheckbox(result, layout.marginWidth);
113: fReplaceAllCheckbox
114: .addSelectionListener(new SelectionAdapter() {
115:
116: public void widgetSelected(SelectionEvent e) {
117: fInstanceofCheckbox
118: .setEnabled(fReplaceAllCheckbox
119: .getSelection());
120: }
121: });
122: addDeclareAsPublicCheckbox(result);
123: addDeclareAsAbstractCheckbox(result);
124:
125: Label separator = new Label(result, SWT.NONE);
126: GridData gd = new GridData();
127: gd.horizontalSpan = 2;
128: separator.setLayoutData(gd);
129:
130: Label tableLabel = new Label(result, SWT.NONE);
131: tableLabel
132: .setText(RefactoringMessages.ExtractInterfaceInputPage_Members);
133: tableLabel.setEnabled(anyMembersToExtract());
134: gd = new GridData();
135: gd.horizontalSpan = 2;
136: tableLabel.setLayoutData(gd);
137:
138: addMemberListComposite(result);
139: addGenerateCommentsCheckbox(result);
140: Dialog.applyDialogFont(result);
141: initializeCheckboxes();
142: updateUIElementEnablement();
143: PlatformUI.getWorkbench().getHelpSystem().setHelp(
144: getControl(),
145: IJavaHelpContextIds.EXTRACT_INTERFACE_WIZARD_PAGE);
146: }
147:
148: private void addGenerateCommentsCheckbox(Composite result) {
149: final ExtractInterfaceProcessor processor = getExtractInterfaceRefactoring()
150: .getExtractInterfaceProcessor();
151: String title = RefactoringMessages.ExtractInterfaceWizard_generate_comments;
152: fGenerateCommentsCheckbox = createCheckbox(result, title,
153: false);
154: processor.setComments(fGenerateCommentsCheckbox
155: .getSelection());
156: fGenerateCommentsCheckbox
157: .addSelectionListener(new SelectionAdapter() {
158: public void widgetSelected(SelectionEvent e) {
159: processor
160: .setComments(fGenerateCommentsCheckbox
161: .getSelection());
162: }
163: });
164: }
165:
166: private void addInstanceofCheckbox(Composite result, int margin) {
167: final SuperTypeRefactoringProcessor processor = getExtractInterfaceRefactoring()
168: .getExtractInterfaceProcessor();
169: String title = RefactoringMessages.ExtractInterfaceWizard_use_super type;
170: fInstanceofCheckbox = new Button(result, SWT.CHECK);
171: fInstanceofCheckbox.setSelection(false);
172: GridData gd = new GridData();
173: gd.horizontalIndent = (margin + fInstanceofCheckbox
174: .computeSize(SWT.DEFAULT, SWT.DEFAULT).x);
175: gd.horizontalSpan = 2;
176: fInstanceofCheckbox.setLayoutData(gd);
177: fInstanceofCheckbox.setText(title);
178: processor.setInstanceOf(fInstanceofCheckbox.getSelection());
179: fInstanceofCheckbox
180: .addSelectionListener(new SelectionAdapter() {
181: public void widgetSelected(SelectionEvent e) {
182: processor.setInstanceOf(fInstanceofCheckbox
183: .getSelection());
184: }
185: });
186: }
187:
188: private void addMemberListComposite(Composite result) {
189: Composite composite = new Composite(result, SWT.NONE);
190: GridLayout layout = new GridLayout();
191: layout.numColumns = 2;
192: layout.marginWidth = 0;
193: layout.marginHeight = 0;
194: composite.setLayout(layout);
195: GridData gd = new GridData(GridData.FILL_BOTH);
196: gd.heightHint = convertHeightInCharsToPixels(12);
197: gd.horizontalSpan = 2;
198: composite.setLayoutData(gd);
199:
200: fTableViewer = CheckboxTableViewer.newCheckList(composite,
201: SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
202: fTableViewer.getTable().setLayoutData(
203: new GridData(GridData.FILL_BOTH));
204: fTableViewer.setLabelProvider(createLabelProvider());
205: fTableViewer.setContentProvider(new ArrayContentProvider());
206: try {
207: fTableViewer.setInput(getExtractableMembers());
208: } catch (JavaModelException e) {
209: ExceptionHandler
210: .handle(
211: e,
212: RefactoringMessages.ExtractInterfaceInputPage_Extract_Interface,
213: RefactoringMessages.ExtractInterfaceInputPage_Internal_Error);
214: fTableViewer.setInput(new IMember[0]);
215: }
216: fTableViewer
217: .addCheckStateListener(new ICheckStateListener() {
218: public void checkStateChanged(
219: CheckStateChangedEvent event) {
220: ExtractInterfaceInputPage.this
221: .updateUIElementEnablement();
222: }
223: });
224: fTableViewer.setComparator(new JavaElementComparator());
225: fTableViewer.getControl().setEnabled(anyMembersToExtract());
226:
227: createButtonComposite(composite);
228: }
229:
230: private IMember[] getExtractableMembers()
231: throws JavaModelException {
232: return getExtractInterfaceRefactoring()
233: .getExtractInterfaceProcessor()
234: .getExtractableMembers();
235: }
236:
237: protected void updateUIElementEnablement() {
238: final IMember[] checked = getCheckedMembers();
239: IMember[] extractable;
240: try {
241: extractable = getExtractableMembers();
242: } catch (JavaModelException exception) {
243: extractable = new IMember[0];
244: JavaPlugin.log(exception);
245: }
246: final boolean enabled = containsMethods(checked);
247: fDeclarePublicCheckbox.setEnabled(enabled);
248: fDeclareAbstractCheckbox.setEnabled(enabled);
249: fGenerateCommentsCheckbox.setEnabled(enabled);
250: fInstanceofCheckbox.setEnabled(fReplaceAllCheckbox
251: .getSelection());
252: fSelectAllButton
253: .setEnabled(checked.length < extractable.length);
254: fDeselectAllButton.setEnabled(checked.length > 0);
255: }
256:
257: private static boolean containsMethods(IMember[] members) {
258: for (int i = 0; i < members.length; i++) {
259: if (members[i].getElementType() == IJavaElement.METHOD)
260: return true;
261: }
262: return false;
263: }
264:
265: private ILabelProvider createLabelProvider() {
266: AppearanceAwareLabelProvider lprovider = new AppearanceAwareLabelProvider(
267: AppearanceAwareLabelProvider.DEFAULT_TEXTFLAGS
268: | JavaElementLabels.F_APP_TYPE_SIGNATURE,
269: AppearanceAwareLabelProvider.DEFAULT_IMAGEFLAGS);
270:
271: return new DecoratingJavaLabelProvider(lprovider);
272: }
273:
274: private void createButtonComposite(Composite composite) {
275: GridData gd;
276: Composite buttonComposite = new Composite(composite,
277: SWT.NONE);
278: GridLayout gl = new GridLayout();
279: gl.marginHeight = 0;
280: gl.marginWidth = 0;
281: buttonComposite.setLayout(gl);
282: gd = new GridData(GridData.FILL_VERTICAL);
283: buttonComposite.setLayoutData(gd);
284:
285: fSelectAllButton = new Button(buttonComposite, SWT.PUSH);
286: fSelectAllButton
287: .setText(RefactoringMessages.ExtractInterfaceInputPage_Select_All);
288: fSelectAllButton.setEnabled(anyMembersToExtract());
289: fSelectAllButton.setLayoutData(new GridData());
290: SWTUtil.setButtonDimensionHint(fSelectAllButton);
291: fSelectAllButton
292: .addSelectionListener(new SelectionAdapter() {
293: public void widgetSelected(SelectionEvent e) {
294: fTableViewer.setAllChecked(true);
295: ExtractInterfaceInputPage.this
296: .updateUIElementEnablement();
297: }
298: });
299:
300: fDeselectAllButton = new Button(buttonComposite, SWT.PUSH);
301: fDeselectAllButton
302: .setText(RefactoringMessages.ExtractInterfaceInputPage_Deselect_All);
303: fDeselectAllButton.setEnabled(anyMembersToExtract());
304: fDeselectAllButton.setLayoutData(new GridData());
305: SWTUtil.setButtonDimensionHint(fDeselectAllButton);
306: fDeselectAllButton
307: .addSelectionListener(new SelectionAdapter() {
308: public void widgetSelected(SelectionEvent e) {
309: fTableViewer.setAllChecked(false);
310: ExtractInterfaceInputPage.this
311: .updateUIElementEnablement();
312: }
313: });
314: }
315:
316: private boolean anyMembersToExtract() {
317: try {
318: return getExtractableMembers().length > 0;
319: } catch (JavaModelException e) {
320: return false;
321: }
322: }
323:
324: private void addReplaceAllCheckbox(Composite result) {
325: final ExtractInterfaceProcessor processor = getExtractInterfaceRefactoring()
326: .getExtractInterfaceProcessor();
327: String title = RefactoringMessages.ExtractInterfaceInputPage_change_references;
328: boolean defaultValue = processor.isReplace();
329: fReplaceAllCheckbox = createCheckbox(result, title,
330: defaultValue);
331: processor.setReplace(fReplaceAllCheckbox.getSelection());
332: fReplaceAllCheckbox
333: .addSelectionListener(new SelectionAdapter() {
334: public void widgetSelected(SelectionEvent e) {
335: processor.setReplace(fReplaceAllCheckbox
336: .getSelection());
337: }
338: });
339: }
340:
341: private void addDeclareAsPublicCheckbox(Composite result) {
342: final ExtractInterfaceProcessor processor = getExtractInterfaceRefactoring()
343: .getExtractInterfaceProcessor();
344: String[] keys = { RefactoringMessages.ExtractInterfaceWizard_public_label };
345: String title = Messages
346: .format(
347: RefactoringMessages.ExtractInterfaceWizard_12,
348: keys);
349: boolean defaultValue = processor.getPublic();
350: fDeclarePublicCheckbox = createCheckbox(result, title,
351: defaultValue);
352: processor.setPublic(fDeclarePublicCheckbox.getSelection());
353: fDeclarePublicCheckbox
354: .addSelectionListener(new SelectionAdapter() {
355: public void widgetSelected(SelectionEvent e) {
356: processor.setPublic(fDeclarePublicCheckbox
357: .getSelection());
358: }
359: });
360: }
361:
362: private void addDeclareAsAbstractCheckbox(Composite result) {
363: final ExtractInterfaceProcessor processor = getExtractInterfaceRefactoring()
364: .getExtractInterfaceProcessor();
365: String[] keys = { RefactoringMessages.ExtractInterfaceWizard_abstract_label };
366: String title = Messages
367: .format(
368: RefactoringMessages.ExtractInterfaceWizard_12,
369: keys);
370: boolean defaultValue = processor.getAbstract();
371: fDeclareAbstractCheckbox = createCheckbox(result, title,
372: defaultValue);
373: processor.setAbstract(fDeclareAbstractCheckbox
374: .getSelection());
375: fDeclareAbstractCheckbox
376: .addSelectionListener(new SelectionAdapter() {
377: public void widgetSelected(SelectionEvent e) {
378: processor
379: .setAbstract(fDeclareAbstractCheckbox
380: .getSelection());
381: }
382: });
383: }
384:
385: private static Button createCheckbox(Composite parent,
386: String title, boolean value) {
387: Button checkBox = new Button(parent, SWT.CHECK);
388: checkBox.setText(title);
389: checkBox.setSelection(value);
390: GridData layoutData = new GridData();
391: layoutData.horizontalSpan = 2;
392:
393: checkBox.setLayoutData(layoutData);
394: return checkBox;
395: }
396:
397: /*
398: * @see org.eclipse.jdt.internal.ui.refactoring.TextInputWizardPage#validateTextField(String)
399: */
400: protected RefactoringStatus validateTextField(String text) {
401: final ExtractInterfaceProcessor processor = getExtractInterfaceRefactoring()
402: .getExtractInterfaceProcessor();
403: processor.setTypeName(text);
404: return processor.checkTypeName(text);
405: }
406:
407: private ExtractInterfaceRefactoring getExtractInterfaceRefactoring() {
408: return (ExtractInterfaceRefactoring) getRefactoring();
409: }
410:
411: /*
412: * @see org.eclipse.jface.wizard.IWizardPage#getNextPage()
413: */
414: public IWizardPage getNextPage() {
415: try {
416: initializeRefactoring();
417: storeDialogSettings();
418: return super .getNextPage();
419: } catch (JavaModelException e) {
420: JavaPlugin.log(e);
421: return null;
422: }
423: }
424:
425: /*
426: * @see org.eclipse.jdt.internal.ui.refactoring.RefactoringWizardPage#performFinish()
427: */
428: public boolean performFinish() {
429: try {
430: initializeRefactoring();
431: storeDialogSettings();
432: return super .performFinish();
433: } catch (JavaModelException e) {
434: JavaPlugin.log(e);
435: return false;
436: }
437: }
438:
439: private void initializeRefactoring() throws JavaModelException {
440: final ExtractInterfaceProcessor processor = getExtractInterfaceRefactoring()
441: .getExtractInterfaceProcessor();
442: processor.setTypeName(getText());
443: processor.setReplace(fReplaceAllCheckbox.getSelection());
444: processor.setExtractedMembers(getCheckedMembers());
445: processor.setAbstract(fDeclareAbstractCheckbox
446: .getSelection());
447: processor.setPublic(fDeclarePublicCheckbox.getSelection());
448: processor.setComments(fGenerateCommentsCheckbox
449: .getSelection());
450: processor.setInstanceOf(fInstanceofCheckbox.getSelection());
451: }
452:
453: private IMember[] getCheckedMembers() {
454: List checked = Arrays.asList(fTableViewer
455: .getCheckedElements());
456: return (IMember[]) checked.toArray(new IMember[checked
457: .size()]);
458: }
459:
460: public void dispose() {
461: fInstanceofCheckbox = null;
462: fGenerateCommentsCheckbox = null;
463: fReplaceAllCheckbox = null;
464: fTableViewer = null;
465: super .dispose();
466: }
467:
468: private void initializeCheckboxes() {
469: initializeCheckBox(fDeclarePublicCheckbox, SETTING_PUBLIC,
470: true);
471: initializeCheckBox(fDeclareAbstractCheckbox,
472: SETTING_ABSTRACT, true);
473: initializeCheckBox(fReplaceAllCheckbox, SETTING_REPLACE,
474: true);
475: initializeCheckBox(fGenerateCommentsCheckbox,
476: SETTING_COMMENTS, true);
477: initializeCheckBox(fInstanceofCheckbox, SETTING_INSTANCEOF,
478: false);
479: }
480:
481: private void initializeCheckBox(Button checkbox,
482: String property, boolean def) {
483: String s = JavaPlugin.getDefault().getDialogSettings().get(
484: property);
485: if (s != null)
486: checkbox.setSelection(new Boolean(s).booleanValue());
487: else
488: checkbox.setSelection(def);
489: }
490:
491: private void storeDialogSettings() {
492: final IDialogSettings settings = JavaPlugin.getDefault()
493: .getDialogSettings();
494: settings.put(SETTING_PUBLIC, fDeclarePublicCheckbox
495: .getSelection());
496: settings.put(SETTING_ABSTRACT, fDeclareAbstractCheckbox
497: .getSelection());
498: settings.put(SETTING_REPLACE, fReplaceAllCheckbox
499: .getSelection());
500: settings.put(SETTING_COMMENTS, fGenerateCommentsCheckbox
501: .getSelection());
502: settings.put(SETTING_INSTANCEOF, fInstanceofCheckbox
503: .getSelection());
504: }
505: }
506: }
|