KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.eclipse.core.runtime.Assert;
22 import org.eclipse.core.runtime.IProgressMonitor;
23
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.SelectionAdapter;
26 import org.eclipse.swt.events.SelectionEvent;
27 import org.eclipse.swt.graphics.Image;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.layout.GridLayout;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.Combo;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Item;
34 import org.eclipse.swt.widgets.Label;
35 import org.eclipse.swt.widgets.Table;
36 import org.eclipse.swt.widgets.TableColumn;
37
38 import org.eclipse.jface.dialogs.Dialog;
39 import org.eclipse.jface.dialogs.IDialogConstants;
40 import org.eclipse.jface.dialogs.IDialogSettings;
41 import org.eclipse.jface.operation.IRunnableWithProgress;
42 import org.eclipse.jface.viewers.ArrayContentProvider;
43 import org.eclipse.jface.viewers.CellEditor;
44 import org.eclipse.jface.viewers.CheckStateChangedEvent;
45 import org.eclipse.jface.viewers.CheckboxTableViewer;
46 import org.eclipse.jface.viewers.ColumnWeightData;
47 import org.eclipse.jface.viewers.ComboBoxCellEditor;
48 import org.eclipse.jface.viewers.DoubleClickEvent;
49 import org.eclipse.jface.viewers.ICellModifier;
50 import org.eclipse.jface.viewers.ICheckStateListener;
51 import org.eclipse.jface.viewers.IDoubleClickListener;
52 import org.eclipse.jface.viewers.ILabelProvider;
53 import org.eclipse.jface.viewers.ISelection;
54 import org.eclipse.jface.viewers.ISelectionChangedListener;
55 import org.eclipse.jface.viewers.IStructuredSelection;
56 import org.eclipse.jface.viewers.ITableLabelProvider;
57 import org.eclipse.jface.viewers.LabelProvider;
58 import org.eclipse.jface.viewers.SelectionChangedEvent;
59 import org.eclipse.jface.viewers.StructuredSelection;
60 import org.eclipse.jface.viewers.TableLayout;
61 import org.eclipse.jface.window.Window;
62 import org.eclipse.jface.wizard.IWizardPage;
63
64 import org.eclipse.ui.PlatformUI;
65
66 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
67 import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
68
69 import org.eclipse.jdt.core.IField;
70 import org.eclipse.jdt.core.IMember;
71 import org.eclipse.jdt.core.IMethod;
72 import org.eclipse.jdt.core.IType;
73 import org.eclipse.jdt.core.JavaModelException;
74
75 import org.eclipse.jdt.internal.corext.refactoring.structure.HierarchyProcessor;
76 import org.eclipse.jdt.internal.corext.refactoring.structure.IMemberActionInfo;
77 import org.eclipse.jdt.internal.corext.refactoring.structure.PullUpRefactoring;
78 import org.eclipse.jdt.internal.corext.refactoring.structure.PullUpRefactoringProcessor;
79 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
80 import org.eclipse.jdt.internal.corext.util.JdtFlags;
81 import org.eclipse.jdt.internal.corext.util.Messages;
82
83 import org.eclipse.jdt.ui.JavaElementLabelProvider;
84
85 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
86 import org.eclipse.jdt.internal.ui.JavaPlugin;
87 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
88 import org.eclipse.jdt.internal.ui.util.PixelConverter;
89 import org.eclipse.jdt.internal.ui.util.SWTUtil;
90 import org.eclipse.jdt.internal.ui.util.TableLayoutComposite;
91
92 /**
93  * Wizard page for pull up refactoring wizards which allows to specify the
94  * actions on the members to pull up.
95  *
96  * @since 3.2
97  */

98 public class PullUpMemberPage extends UserInputWizardPage {
99
100     private class MemberActionCellModifier implements ICellModifier {
101
102         public boolean canModify(final Object JavaDoc element, final String JavaDoc property) {
103             if (!ACTION_PROPERTY.equals(property))
104                 return false;
105             return ((MemberActionInfo) element).isEditable();
106         }
107
108         public Object JavaDoc getValue(final Object JavaDoc element, final String JavaDoc property) {
109             if (!ACTION_PROPERTY.equals(property))
110                 return null;
111             final MemberActionInfo info= (MemberActionInfo) element;
112             return new Integer JavaDoc(info.getAction());
113         }
114
115         public void modify(final Object JavaDoc element, final String JavaDoc property, final Object JavaDoc value) {
116             if (!ACTION_PROPERTY.equals(property))
117                 return;
118             final int action= ((Integer JavaDoc) value).intValue();
119             MemberActionInfo info;
120             if (element instanceof Item) {
121                 info= (MemberActionInfo) ((Item) element).getData();
122             } else
123                 info= (MemberActionInfo) element;
124             if (!canModify(info, property))
125                 return;
126             Assert.isTrue(info.isMethodInfo());
127             info.setAction(action);
128             updateWizardPage(null, true);
129         }
130     }
131
132     private class MemberActionInfo implements IMemberActionInfo {
133
134         private static final int NO_ACTION= 2;
135
136         private int fAction;
137
138         private final IMember fMember;
139
140         public MemberActionInfo(final IMember member, final int action) {
141             Assert.isTrue((member instanceof IMethod) || (member instanceof IField) || (member instanceof IType));
142             assertAction(member, action);
143             fMember= member;
144             fAction= action;
145         }
146
147         private void assertAction(final IMember member, final int action) {
148             if (member instanceof IMethod) {
149                 try {
150                     Assert.isTrue(action != DECLARE_ABSTRACT_ACTION || !JdtFlags.isStatic(member));
151                 } catch (JavaModelException e) {
152                     JavaPlugin.log(e);
153                 }
154                 Assert.isTrue(action == NO_ACTION || action == DECLARE_ABSTRACT_ACTION || action == PULL_UP_ACTION);
155             } else {
156                 Assert.isTrue(action == NO_ACTION || action == PULL_UP_ACTION);
157             }
158         }
159
160         public int getAction() {
161             return fAction;
162         }
163
164         public String JavaDoc getActionLabel() {
165             switch (fAction) {
166                 case PULL_UP_ACTION:
167                     return getPullUpActionLabel();
168                 case DECLARE_ABSTRACT_ACTION:
169                     return getDeclareAbstractActionLabel();
170                 case NO_ACTION:
171                     return ""; //$NON-NLS-1$
172
default:
173                     Assert.isTrue(false);
174                     return null;
175             }
176         }
177
178         public String JavaDoc[] getAllowedLabels() {
179             if (isFieldInfo())
180                 return new String JavaDoc[] { ""}; //$NON-NLS-1$
181
else if (isMethodInfo())
182                 return METHOD_LABELS;
183             else if (isTypeInfo())
184                 return TYPE_LABELS;
185             else {
186                 Assert.isTrue(false);
187                 return null;
188             }
189         }
190
191         public IMember getMember() {
192             return fMember;
193         }
194
195         public boolean isActive() {
196             return getAction() != NO_ACTION;
197         }
198
199         public boolean isEditable() {
200             if (fAction == NO_ACTION)
201                 return false;
202             if (!isMethodInfo())
203                 return false;
204             final IMethod method= (IMethod) fMember;
205             try {
206                 return !JdtFlags.isStatic(method);
207             } catch (JavaModelException e) {
208                 JavaPlugin.log(e);
209                 return false;
210             }
211         }
212
213         public boolean isFieldInfo() {
214             return getMember() instanceof IField;
215         }
216
217         public boolean isMethodInfo() {
218             return getMember() instanceof IMethod;
219         }
220
221         public boolean isTypeInfo() {
222             return getMember() instanceof IType;
223         }
224
225         public void setAction(final int action) {
226             assertAction(fMember, action);
227             fAction= action;
228         }
229     }
230
231     private static class MemberActionInfoLabelProvider extends LabelProvider implements ITableLabelProvider {
232
233         private final ILabelProvider fLabelProvider= new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT | JavaElementLabelProvider.SHOW_SMALL_ICONS);
234
235         public void dispose() {
236             super.dispose();
237             fLabelProvider.dispose();
238         }
239
240         public Image getColumnImage(final Object JavaDoc element, final int columnIndex) {
241             final MemberActionInfo info= (MemberActionInfo) element;
242             switch (columnIndex) {
243                 case MEMBER_COLUMN:
244                     return fLabelProvider.getImage(info.getMember());
245                 case ACTION_COLUMN:
246                     return null;
247                 default:
248                     Assert.isTrue(false);
249                     return null;
250             }
251         }
252
253         public String JavaDoc getColumnText(final Object JavaDoc element, final int columnIndex) {
254             final MemberActionInfo info= (MemberActionInfo) element;
255             switch (columnIndex) {
256                 case MEMBER_COLUMN:
257                     return fLabelProvider.getText(info.getMember());
258                 case ACTION_COLUMN:
259                     return info.getActionLabel();
260                 default:
261                     Assert.isTrue(false);
262                     return null;
263             }
264         }
265     }
266
267     private static final int ACTION_COLUMN= 1;
268
269     private static final String JavaDoc ACTION_PROPERTY= "action"; //$NON-NLS-1$
270

271     protected static final int DECLARE_ABSTRACT_ACTION= 1;
272
273     private static final int MEMBER_COLUMN= 0;
274
275     private static final String JavaDoc MEMBER_PROPERTY= "member"; //$NON-NLS-1$
276

277     protected static final int PULL_UP_ACTION= 0;
278
279     private static final String JavaDoc SETTING_INSTANCEOF= "InstanceOf"; //$NON-NLS-1$
280

281     private static final String JavaDoc SETTING_REPLACE= "Replace"; //$NON-NLS-1$
282

283     private static int getEditableCount(final MemberActionInfo[] infos) {
284         int result= 0;
285         for (int i= 0; i < infos.length; i++) {
286             final MemberActionInfo info= infos[i];
287             if (info.isEditable())
288                 result++;
289         }
290         return result;
291     }
292
293     private static void putToStringMapping(final Map JavaDoc result, final String JavaDoc[] actionLabels, final int actionIndex) {
294         result.put(actionLabels[actionIndex], new Integer JavaDoc(actionIndex));
295     }
296
297     private static void setActionForInfos(final MemberActionInfo[] infos, final int action) {
298         for (int i= 0; i < infos.length; i++) {
299             infos[i].setAction(action);
300         }
301     }
302
303     private Button fAddButton;
304
305     protected IType[] fCandidateTypes= {};
306
307     private Button fCreateStubsButton;
308
309     private Button fDeselectAllButton;
310
311     private Button fEditButton;
312
313     private Button fInstanceofButton;
314
315     private Label fLabel;
316
317     private Button fReplaceButton;
318
319     private Button fSelectAllButton;
320
321     private Label fStatusLine;
322
323     protected final PullUpMethodPage fSuccessorPage;
324
325     private Combo fSuperTypesCombo;
326
327     private CheckboxTableViewer fTableViewer;
328
329     protected final String JavaDoc[] METHOD_LABELS;
330
331     protected final String JavaDoc[] TYPE_LABELS;
332
333     public PullUpMemberPage(final String JavaDoc name, final PullUpMethodPage page) {
334         super(name);
335         fSuccessorPage= page;
336         setDescription(RefactoringMessages.PullUpInputPage1_page_message);
337         METHOD_LABELS= new String JavaDoc[2];
338         METHOD_LABELS[PULL_UP_ACTION]= RefactoringMessages.PullUpInputPage1_pull_up;
339         METHOD_LABELS[DECLARE_ABSTRACT_ACTION]= RefactoringMessages.PullUpInputPage1_declare_abstract;
340
341         TYPE_LABELS= new String JavaDoc[1];
342         TYPE_LABELS[PULL_UP_ACTION]= RefactoringMessages.PullUpInputPage1_pull_up;
343     }
344
345     private boolean areAllMembersMarkedAsPullUp() {
346         return getMembersForAction(PULL_UP_ACTION).length == getTableInput().length;
347     }
348
349     protected boolean areAllMembersMarkedAsWithNoAction() {
350         return getMembersForAction(MemberActionInfo.NO_ACTION).length == getTableInput().length;
351     }
352
353     private MemberActionInfo[] asMemberActionInfos() {
354         final PullUpRefactoringProcessor processor= getPullUpRefactoring().getPullUpProcessor();
355         final List JavaDoc toPullUp= Arrays.asList(processor.getMembersToMove());
356         final IMember[] members= processor.getPullableMembersOfDeclaringType();
357         final MemberActionInfo[] result= new MemberActionInfo[members.length];
358         for (int i= 0; i < members.length; i++) {
359             final IMember member= members[i];
360             if (toPullUp.contains(member))
361                 result[i]= new MemberActionInfo(member, PULL_UP_ACTION);
362             else
363                 result[i]= new MemberActionInfo(member, MemberActionInfo.NO_ACTION);
364         }
365         return result;
366     }
367
368     public boolean canFlipToNextPage() {
369         return isPageComplete();
370     }
371
372     private void checkAdditionalRequired() {
373         try {
374             PlatformUI.getWorkbench().getActiveWorkbenchWindow().run(false, false, new IRunnableWithProgress() {
375
376                 public void run(final IProgressMonitor pm) throws InvocationTargetException JavaDoc {
377                     try {
378                         checkPullUp(getPullUpRefactoring().getPullUpProcessor().getAdditionalRequiredMembersToPullUp(pm), true);
379                     } catch (JavaModelException e) {
380                         throw new InvocationTargetException JavaDoc(e);
381                     } finally {
382                         pm.done();
383                     }
384                 }
385             });
386         } catch (InvocationTargetException JavaDoc e) {
387             ExceptionHandler.handle(e, getShell(), RefactoringMessages.PullUpInputPage_pull_Up, RefactoringMessages.PullUpInputPage_exception);
388         } catch (InterruptedException JavaDoc e) {
389             Assert.isTrue(false);
390         }
391     }
392
393     protected void checkPageCompletionStatus(final boolean displayErrors) {
394         if (areAllMembersMarkedAsWithNoAction()) {
395             if (displayErrors)
396                 setErrorMessage(getNoMembersMessage());
397             setPageComplete(false);
398         } else {
399             setErrorMessage(null);
400             setPageComplete(true);
401         }
402         fSuccessorPage.fireSettingsChanged();
403     }
404
405     private void checkPullUp(final IMember[] elements, final boolean displayErrors) {
406         setActionForMembers(elements, PULL_UP_ACTION);
407         updateWizardPage(null, displayErrors);
408     }
409
410     private void createButtonComposite(final Composite parent) {
411         final Composite composite= new Composite(parent, SWT.NONE);
412         composite.setLayoutData(new GridData(GridData.FILL_VERTICAL));
413         final GridLayout gl= new GridLayout();
414         gl.marginHeight= 0;
415         gl.marginWidth= 0;
416         composite.setLayout(gl);
417
418         fSelectAllButton= new Button(composite, SWT.PUSH);
419         fSelectAllButton.setText(RefactoringMessages.PullUpWizard_select_all_label);
420         fSelectAllButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
421         fSelectAllButton.setEnabled(true);
422         SWTUtil.setButtonDimensionHint(fSelectAllButton);
423         fSelectAllButton.addSelectionListener(new SelectionAdapter() {
424
425             public void widgetSelected(final SelectionEvent event) {
426                 final IMember[] members= getMembers();
427                 setActionForMembers(members, PULL_UP_ACTION);
428                 updateWizardPage(null, true);
429             }
430         });
431
432         fDeselectAllButton= new Button(composite, SWT.PUSH);
433         fDeselectAllButton.setText(RefactoringMessages.PullUpWizard_deselect_all_label);
434         fDeselectAllButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
435         fDeselectAllButton.setEnabled(false);
436         SWTUtil.setButtonDimensionHint(fDeselectAllButton);
437         fDeselectAllButton.addSelectionListener(new SelectionAdapter() {
438
439             public void widgetSelected(final SelectionEvent event) {
440                 final IMember[] members= getMembers();
441                 setActionForMembers(members, MemberActionInfo.NO_ACTION);
442                 updateWizardPage(null, true);
443             }
444         });
445
446         fEditButton= new Button(composite, SWT.PUSH);
447         fEditButton.setText(RefactoringMessages.PullUpInputPage1_Edit);
448
449         final GridData data= new GridData(GridData.FILL_HORIZONTAL);
450         data.verticalIndent= new PixelConverter(parent).convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
451         fEditButton.setLayoutData(data);
452         fEditButton.setEnabled(false);
453         SWTUtil.setButtonDimensionHint(fEditButton);
454         fEditButton.addSelectionListener(new SelectionAdapter() {
455
456             public void widgetSelected(final SelectionEvent event) {
457                 editSelectedMembers();
458             }
459         });
460
461         fAddButton= new Button(composite, SWT.PUSH);
462         fAddButton.setText(RefactoringMessages.PullUpInputPage1_Add_Required);
463         fAddButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
464         SWTUtil.setButtonDimensionHint(fAddButton);
465         fAddButton.addSelectionListener(new SelectionAdapter() {
466
467             public void widgetSelected(final SelectionEvent event) {
468                 checkAdditionalRequired();
469             }
470         });
471     }
472
473     public void createControl(final Composite parent) {
474         final Composite composite= new Composite(parent, SWT.NONE);
475         final GridLayout layout= new GridLayout();
476         layout.numColumns= 2;
477         composite.setLayout(layout);
478
479         createSuperTypeControl(composite);
480         createSpacer(composite);
481         createSuperTypeCheckbox(composite);
482         createInstanceOfCheckbox(composite, layout.marginWidth);
483         createStubCheckbox(composite);
484         createSpacer(composite);
485         createMemberTableLabel(composite);
486         createMemberTableComposite(composite);
487         createStatusLine(composite);
488
489         setControl(composite);
490         Dialog.applyDialogFont(composite);
491         initializeEnablement();
492         initializeCheckboxes();
493         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaHelpContextIds.PULL_UP_WIZARD_PAGE);
494     }
495
496     protected void createInstanceOfCheckbox(final Composite result, final int margin) {
497         final HierarchyProcessor processor= getPullUpRefactoring().getPullUpProcessor();
498         fInstanceofButton= new Button(result, SWT.CHECK);
499         fInstanceofButton.setSelection(false);
500         final GridData gd= new GridData();
501         gd.horizontalIndent= (margin + fInstanceofButton.computeSize(SWT.DEFAULT, SWT.DEFAULT).x);
502         gd.horizontalSpan= 2;
503         fInstanceofButton.setLayoutData(gd);
504         fInstanceofButton.setText(getInstanceofButtonLabel());
505         processor.setInstanceOf(fInstanceofButton.getSelection());
506         fInstanceofButton.addSelectionListener(new SelectionAdapter() {
507
508             public void widgetSelected(final SelectionEvent e) {
509                 processor.setInstanceOf(fInstanceofButton.getSelection());
510             }
511         });
512         fReplaceButton.addSelectionListener(new SelectionAdapter() {
513
514             public void widgetSelected(final SelectionEvent e) {
515                 fInstanceofButton.setEnabled(fReplaceButton.getSelection());
516             }
517         });
518     }
519
520     private void createMemberTable(final Composite parent) {
521         final TableLayoutComposite layouter= new TableLayoutComposite(parent, SWT.NONE);
522         layouter.addColumnData(new ColumnWeightData(60, true));
523         layouter.addColumnData(new ColumnWeightData(40, true));
524
525         final Table table= new Table(layouter, SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION | SWT.CHECK);
526         table.setHeaderVisible(true);
527         table.setLinesVisible(true);
528
529         final GridData gd= new GridData(GridData.FILL_BOTH);
530         gd.heightHint= SWTUtil.getTableHeightHint(table, getTableRowCount());
531         gd.widthHint= convertWidthInCharsToPixels(30);
532         layouter.setLayoutData(gd);
533
534         final TableLayout tableLayout= new TableLayout();
535         table.setLayout(tableLayout);
536
537         final TableColumn column0= new TableColumn(table, SWT.NONE);
538         column0.setText(RefactoringMessages.PullUpInputPage1_Member);
539
540         final TableColumn column1= new TableColumn(table, SWT.NONE);
541         column1.setText(RefactoringMessages.PullUpInputPage1_Action);
542
543         fTableViewer= new PullPushCheckboxTableViewer(table);
544         fTableViewer.setUseHashlookup(true);
545         fTableViewer.setContentProvider(new ArrayContentProvider());
546         fTableViewer.setLabelProvider(new MemberActionInfoLabelProvider());
547         fTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
548
549             public void selectionChanged(final SelectionChangedEvent event) {
550                 updateButtonEnablement(event.getSelection());
551             }
552         });
553         fTableViewer.addCheckStateListener(new ICheckStateListener() {
554
555             public void checkStateChanged(final CheckStateChangedEvent event) {
556                 final boolean checked= event.getChecked();
557                 final MemberActionInfo info= (MemberActionInfo) event.getElement();
558                 if (checked)
559                     info.setAction(PULL_UP_ACTION);
560                 else
561                     info.setAction(MemberActionInfo.NO_ACTION);
562                 updateWizardPage(null, true);
563             }
564         });
565         fTableViewer.addDoubleClickListener(new IDoubleClickListener() {
566
567             public void doubleClick(final DoubleClickEvent event) {
568                 editSelectedMembers();
569             }
570         });
571
572         setTableInput();
573         checkPullUp(getPullUpRefactoring().getPullUpProcessor().getMembersToMove(), false);
574         setupCellEditors(table);
575     }
576
577     protected void createMemberTableComposite(final Composite parent) {
578         final Composite composite= new Composite(parent, SWT.NONE);
579         final GridData data= new GridData(GridData.FILL_BOTH);
580         data.horizontalSpan= 2;
581         composite.setLayoutData(data);
582         final GridLayout layout= new GridLayout();
583         layout.numColumns= 2;
584         layout.marginWidth= 0;
585         layout.marginHeight= 0;
586         composite.setLayout(layout);
587
588         createMemberTable(composite);
589         createButtonComposite(composite);
590     }
591
592     protected void createMemberTableLabel(final Composite parent) {
593         fLabel= new Label(parent, SWT.NONE);
594         fLabel.setText(RefactoringMessages.PullUpInputPage1_Specify_actions);
595         final GridData data= new GridData();
596         data.horizontalSpan= 2;
597         fLabel.setLayoutData(data);
598     }
599
600     protected void createSpacer(final Composite parent) {
601         final Label label= new Label(parent, SWT.NONE);
602         final GridData data= new GridData();
603         data.horizontalSpan= 2;
604         data.heightHint= convertHeightInCharsToPixels(1) / 2;
605         label.setLayoutData(data);
606     }
607
608     protected void createStatusLine(final Composite composite) {
609         fStatusLine= new Label(composite, SWT.NONE);
610         final GridData data= new GridData();
611         data.horizontalSpan= 2;
612         updateStatusLine();
613         fStatusLine.setLayoutData(data);
614     }
615
616     // String -> Integer
617
private Map JavaDoc createStringMappingForSelectedMembers() {
618         final Map JavaDoc result= new HashMap JavaDoc();
619         putToStringMapping(result, METHOD_LABELS, PULL_UP_ACTION);
620         putToStringMapping(result, METHOD_LABELS, DECLARE_ABSTRACT_ACTION);
621         return result;
622     }
623
624     protected void createStubCheckbox(final Composite parent) {
625         fCreateStubsButton= new Button(parent, SWT.CHECK);
626         fCreateStubsButton.setText(getCreateStubsButtonLabel());
627         final GridData data= new GridData();
628         data.horizontalSpan= 2;
629         fCreateStubsButton.setLayoutData(data);
630         fCreateStubsButton.setEnabled(false);
631         fCreateStubsButton.setSelection(getPullUpRefactoring().getPullUpProcessor().getCreateMethodStubs());
632     }
633
634     protected void createSuperTypeCheckbox(final Composite parent) {
635         fReplaceButton= new Button(parent, SWT.CHECK);
636         fReplaceButton.setText(getReplaceButtonLabel());
637         final GridData data= new GridData();
638         data.horizontalSpan= 2;
639         fReplaceButton.setLayoutData(data);
640         fReplaceButton.setEnabled(true);
641         fReplaceButton.setSelection(getPullUpRefactoring().getPullUpProcessor().isReplace());
642     }
643
644     private void createSuperTypeCombo(final IProgressMonitor pm, final Composite parent) throws JavaModelException {
645         final Label label= new Label(parent, SWT.NONE);
646         label.setText(RefactoringMessages.PullUpInputPage1_Select_destination);
647         label.setLayoutData(new GridData());
648
649         fSuperTypesCombo= new Combo(parent, SWT.READ_ONLY);
650         fCandidateTypes= getPullUpRefactoring().getPullUpProcessor().getCandidateTypes(new RefactoringStatus(), pm);
651         if (fCandidateTypes.length > 0) {
652             for (int i= 0; i < fCandidateTypes.length; i++) {
653                 final String JavaDoc comboLabel= JavaModelUtil.getFullyQualifiedName(fCandidateTypes[i]);
654                 fSuperTypesCombo.add(comboLabel);
655             }
656             fSuperTypesCombo.select(fCandidateTypes.length - 1);
657             fSuperTypesCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
658         }
659     }
660
661     protected void createSuperTypeControl(final Composite parent) {
662         try {
663             PlatformUI.getWorkbench().getActiveWorkbenchWindow().run(false, false, new IRunnableWithProgress() {
664
665                 public void run(final IProgressMonitor monitor) throws InvocationTargetException JavaDoc {
666                     try {
667                         createSuperTypeCombo(monitor, parent);
668                     } catch (JavaModelException exception) {
669                         throw new InvocationTargetException JavaDoc(exception);
670                     } finally {
671                         monitor.done();
672                     }
673                 }
674             });
675         } catch (InvocationTargetException JavaDoc exception) {
676             ExceptionHandler.handle(exception, getShell(), RefactoringMessages.PullUpInputPage_pull_Up, RefactoringMessages.PullUpInputPage_exception);
677         } catch (InterruptedException JavaDoc exception) {
678             Assert.isTrue(false);
679         }
680     }
681
682     public void dispose() {
683         fInstanceofButton= null;
684         fReplaceButton= null;
685         fTableViewer= null;
686         super.dispose();
687     }
688
689     private void editSelectedMembers() {
690         if (!fEditButton.isEnabled())
691             return;
692
693         final ISelection preserved= fTableViewer.getSelection();
694         try {
695             final String JavaDoc shellTitle= RefactoringMessages.PullUpInputPage1_Edit_members;
696             final String JavaDoc labelText= RefactoringMessages.PullUpInputPage1_Mark_selected_members;
697             final Map JavaDoc stringMapping= createStringMappingForSelectedMembers();
698             final String JavaDoc[] keys= (String JavaDoc[]) stringMapping.keySet().toArray(new String JavaDoc[stringMapping.keySet().size()]);
699             Arrays.sort(keys);
700             final int initialSelectionIndex= getInitialSelectionIndexForEditDialog(stringMapping, keys);
701             final ComboSelectionDialog dialog= new ComboSelectionDialog(getShell(), shellTitle, labelText, keys, initialSelectionIndex);
702             dialog.setBlockOnOpen(true);
703             if (dialog.open() == Window.CANCEL)
704                 return;
705             final int action= ((Integer JavaDoc) stringMapping.get(dialog.getSelectedString())).intValue();
706             setActionForInfos(getSelectedMembers(), action);
707         } finally {
708             updateWizardPage(preserved, true);
709         }
710     }
711
712     private boolean enableEditButton(final IStructuredSelection ss) {
713         if (ss.isEmpty() || ss.size() == 0)
714             return false;
715         return ss.size() == getEditableCount(getSelectedMembers());
716     }
717
718     private MemberActionInfo[] getActiveInfos() {
719         final MemberActionInfo[] infos= getTableInput();
720         final List JavaDoc result= new ArrayList JavaDoc(infos.length);
721         for (int i= 0; i < infos.length; i++) {
722             final MemberActionInfo info= infos[i];
723             if (info.isActive())
724                 result.add(info);
725         }
726         return (MemberActionInfo[]) result.toArray(new MemberActionInfo[result.size()]);
727     }
728
729     private int getCommonActionCodeForSelectedInfos() {
730         final MemberActionInfo[] infos= getSelectedMembers();
731         if (infos.length == 0)
732             return -1;
733
734         final int code= infos[0].getAction();
735         for (int i= 0; i < infos.length; i++) {
736             if (code != infos[i].getAction())
737                 return -1;
738         }
739         return code;
740     }
741
742     protected String JavaDoc getCreateStubsButtonLabel() {
743         return RefactoringMessages.PullUpInputPage1_Create_stubs;
744     }
745
746     protected String JavaDoc getDeclareAbstractActionLabel() {
747         return RefactoringMessages.PullUpInputPage1_declare_abstract;
748     }
749
750     public IType getDestinationType() {
751         final int index= fSuperTypesCombo.getSelectionIndex();
752         if (index >= 0)
753             return fCandidateTypes[index];
754         return null;
755     }
756
757     private int getInitialSelectionIndexForEditDialog(final Map JavaDoc stringMapping, final String JavaDoc[] keys) {
758         final int commonActionCode= getCommonActionCodeForSelectedInfos();
759         if (commonActionCode == -1)
760             return 0;
761         for (final Iterator JavaDoc iter= stringMapping.keySet().iterator(); iter.hasNext();) {
762             final String JavaDoc key= (String JavaDoc) iter.next();
763             final int action= ((Integer JavaDoc) stringMapping.get(key)).intValue();
764             if (commonActionCode == action) {
765                 for (int i= 0; i < keys.length; i++) {
766                     if (key.equals(keys[i]))
767                         return i;
768                 }
769                 Assert.isTrue(false);
770             }
771         }
772         return 0;
773     }
774
775     protected String JavaDoc getInstanceofButtonLabel() {
776         return RefactoringMessages.PullUpInputPage1_label_use_in_instanceof;
777     }
778
779     private IMember[] getMembers() {
780         final MemberActionInfo[] infos= getTableInput();
781         final List JavaDoc result= new ArrayList JavaDoc(infos.length);
782         for (int index= 0; index < infos.length; index++) {
783             result.add(infos[index].getMember());
784         }
785         return (IMember[]) result.toArray(new IMember[result.size()]);
786     }
787
788     private IMember[] getMembersForAction(final int action) {
789         final MemberActionInfo[] infos= getTableInput();
790         final List JavaDoc result= new ArrayList JavaDoc(infos.length);
791         for (int index= 0; index < infos.length; index++) {
792             if (infos[index].getAction() == action)
793                 result.add(infos[index].getMember());
794         }
795         return (IMember[]) result.toArray(new IMember[result.size()]);
796     }
797
798     private IMethod[] getMethodsForAction(final int action) {
799         final MemberActionInfo[] infos= getTableInput();
800         final List JavaDoc list= new ArrayList JavaDoc(infos.length);
801         for (int index= 0; index < infos.length; index++) {
802             if (infos[index].isMethodInfo() && infos[index].getAction() == action) {
803                 list.add(infos[index].getMember());
804             }
805         }
806         return (IMethod[]) list.toArray(new IMethod[list.size()]);
807     }
808
809     public IWizardPage getNextPage() {
810         initializeRefactoring();
811         storeDialogSettings();
812         if (getMethodsForAction(PULL_UP_ACTION).length == 0)
813             return computeSuccessorPage();
814         try {
815             final IType destination= getDestinationType();
816             if (destination != null && destination.isInterface())
817                 return computeSuccessorPage();
818         } catch (JavaModelException exception) {
819             JavaPlugin.log(exception);
820         }
821         return super.getNextPage();
822     }
823
824     protected String JavaDoc getNoMembersMessage() {
825         return RefactoringMessages.PullUpInputPage1_Select_members_to_pull_up;
826     }
827
828     protected String JavaDoc getPullUpActionLabel() {
829         return RefactoringMessages.PullUpInputPage1_pull_up;
830     }
831
832     private PullUpRefactoring getPullUpRefactoring() {
833         return (PullUpRefactoring) getRefactoring();
834     }
835
836     protected String JavaDoc getReplaceButtonLabel() {
837         return RefactoringMessages.PullUpInputPage1_label_use_destination;
838     }
839
840     private MemberActionInfo[] getSelectedMembers() {
841         Assert.isTrue(fTableViewer.getSelection() instanceof IStructuredSelection);
842         final IStructuredSelection structured= (IStructuredSelection) fTableViewer.getSelection();
843         final List JavaDoc result= structured.toList();
844         return (MemberActionInfo[]) result.toArray(new MemberActionInfo[result.size()]);
845     }
846
847     private MemberActionInfo[] getTableInput() {
848         return (MemberActionInfo[]) fTableViewer.getInput();
849     }
850
851     protected int getTableRowCount() {
852         return 10;
853     }
854
855     private void initializeCheckBox(final Button checkbox, final String JavaDoc property, final boolean def) {
856         final String JavaDoc s= JavaPlugin.getDefault().getDialogSettings().get(property);
857         if (s != null)
858             checkbox.setSelection(new Boolean JavaDoc(s).booleanValue());
859         else
860             checkbox.setSelection(def);
861     }
862
863     protected void initializeCheckboxes() {
864         initializeCheckBox(fReplaceButton, SETTING_REPLACE, true);
865         initializeCheckBox(fInstanceofButton, SETTING_INSTANCEOF, false);
866     }
867
868     protected void initializeEnablement() {
869         MemberActionInfo[] infos= asMemberActionInfos();
870         final boolean enabled= infos.length > 0;
871         fTableViewer.getTable().setEnabled(enabled);
872         fStatusLine.setEnabled(enabled);
873         fAddButton.setEnabled(enabled);
874         fLabel.setEnabled(enabled);
875     }
876
877     private void initializeRefactoring() {
878         final PullUpRefactoringProcessor processor= getPullUpRefactoring().getPullUpProcessor();
879         processor.setMembersToMove(getMembersForAction(PULL_UP_ACTION));
880         processor.setAbstractMethods(getMethodsForAction(DECLARE_ABSTRACT_ACTION));
881         final IType destination= getDestinationType();
882         if (destination != null)
883             processor.setDestinationType(destination);
884         processor.setCreateMethodStubs(fCreateStubsButton.getSelection());
885         processor.setReplace(fReplaceButton.getSelection());
886         processor.setInstanceOf(fInstanceofButton.getSelection());
887         processor.setDeletedMethods(getMethodsForAction(PULL_UP_ACTION));
888     }
889
890     protected boolean performFinish() {
891         initializeRefactoring();
892         storeDialogSettings();
893         return super.performFinish();
894     }
895
896     private void setActionForMembers(final IMember[] members, final int action) {
897         final MemberActionInfo[] infos= getTableInput();
898         for (int i= 0; i < members.length; i++) {
899             for (int j= 0; j < infos.length; j++) {
900                 if (infos[j].getMember().equals(members[i]))
901                     infos[j].setAction(action);
902             }
903         }
904     }
905
906     private void setTableInput() {
907         fTableViewer.setInput(asMemberActionInfos());
908     }
909
910     private void setupCellEditors(final Table table) {
911         final ComboBoxCellEditor editor= new ComboBoxCellEditor();
912         editor.setStyle(SWT.READ_ONLY);
913         fTableViewer.setCellEditors(new CellEditor[] { null, editor});
914         fTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
915
916             public void selectionChanged(final SelectionChangedEvent event) {
917                 if (editor.getControl() == null & !table.isDisposed())
918                     editor.create(table);
919                 final ISelection sel= event.getSelection();
920                 if (!(sel instanceof IStructuredSelection))
921                     return;
922                 final IStructuredSelection structured= (IStructuredSelection) sel;
923                 if (structured.size() != 1)
924                     return;
925                 final MemberActionInfo info= (MemberActionInfo) structured.getFirstElement();
926                 editor.setItems(info.getAllowedLabels());
927                 editor.setValue(new Integer JavaDoc(info.getAction()));
928             }
929         });
930
931         final ICellModifier cellModifier= new MemberActionCellModifier();
932         fTableViewer.setCellModifier(cellModifier);
933         fTableViewer.setColumnProperties(new String JavaDoc[] { MEMBER_PROPERTY, ACTION_PROPERTY});
934     }
935
936     public void setVisible(final boolean visible) {
937         super.setVisible(visible);
938         if (visible) {
939             try {
940                 getPullUpRefactoring().getPullUpProcessor().resetEnvironment();
941             } finally {
942                 fTableViewer.setSelection(new StructuredSelection(getActiveInfos()), true);
943                 fTableViewer.getControl().setFocus();
944             }
945         }
946     }
947
948     private void storeDialogSettings() {
949         final IDialogSettings settings= JavaPlugin.getDefault().getDialogSettings();
950         settings.put(SETTING_REPLACE, fReplaceButton.getSelection());
951         settings.put(SETTING_INSTANCEOF, fInstanceofButton.getSelection());
952     }
953
954     private void updateButtonEnablement(final ISelection selection) {
955         if (fEditButton != null)
956             fEditButton.setEnabled(enableEditButton((IStructuredSelection) selection));
957         fCreateStubsButton.setEnabled(getMethodsForAction(DECLARE_ABSTRACT_ACTION).length != 0);
958         fInstanceofButton.setEnabled(fReplaceButton.getSelection());
959         if (fSelectAllButton != null)
960             fSelectAllButton.setEnabled(!areAllMembersMarkedAsPullUp());
961         if (fDeselectAllButton != null)
962             fDeselectAllButton.setEnabled(!areAllMembersMarkedAsWithNoAction());
963     }
964
965     private void updateStatusLine() {
966         if (fStatusLine == null)
967             return;
968         final int selected= fTableViewer.getCheckedElements().length;
969         final String JavaDoc[] keys= { String.valueOf(selected)};
970         final String JavaDoc msg= Messages.format(RefactoringMessages.PullUpInputPage1_status_line, keys);
971         fStatusLine.setText(msg);
972     }
973
974     private void updateWizardPage(final ISelection selection, final boolean displayErrors) {
975         fTableViewer.refresh();
976         if (selection != null) {
977             fTableViewer.getControl().setFocus();
978             fTableViewer.setSelection(selection);
979         }
980         checkPageCompletionStatus(displayErrors);
981         updateButtonEnablement(fTableViewer.getSelection());
982         updateStatusLine();
983     }
984 }
985
Popular Tags