0001: /*******************************************************************************
0002: * Copyright (c) 2006, 2007 IBM Corporation and others.
0003: * All rights reserved. This program and the accompanying materials
0004: * are made available under the terms of the Eclipse Public License v1.0
0005: * which accompanies this distribution, and is available at
0006: * http://www.eclipse.org/legal/epl-v10.html
0007: *
0008: * Contributors:
0009: * IBM Corporation - initial API and implementation
0010: *******************************************************************************/package org.eclipse.jdt.internal.ui.refactoring;
0011:
0012: import java.lang.reflect.InvocationTargetException;
0013: import java.util.ArrayList;
0014: import java.util.Arrays;
0015: import java.util.HashMap;
0016: import java.util.Iterator;
0017: import java.util.List;
0018: import java.util.Map;
0019:
0020: import org.eclipse.core.runtime.Assert;
0021: import org.eclipse.core.runtime.IProgressMonitor;
0022:
0023: import org.eclipse.swt.SWT;
0024: import org.eclipse.swt.events.SelectionAdapter;
0025: import org.eclipse.swt.events.SelectionEvent;
0026: import org.eclipse.swt.graphics.Image;
0027: import org.eclipse.swt.layout.GridData;
0028: import org.eclipse.swt.layout.GridLayout;
0029: import org.eclipse.swt.widgets.Button;
0030: import org.eclipse.swt.widgets.Combo;
0031: import org.eclipse.swt.widgets.Composite;
0032: import org.eclipse.swt.widgets.Item;
0033: import org.eclipse.swt.widgets.Label;
0034: import org.eclipse.swt.widgets.Table;
0035: import org.eclipse.swt.widgets.TableColumn;
0036:
0037: import org.eclipse.jface.dialogs.Dialog;
0038: import org.eclipse.jface.dialogs.IDialogConstants;
0039: import org.eclipse.jface.dialogs.IDialogSettings;
0040: import org.eclipse.jface.operation.IRunnableWithProgress;
0041: import org.eclipse.jface.viewers.ArrayContentProvider;
0042: import org.eclipse.jface.viewers.CellEditor;
0043: import org.eclipse.jface.viewers.CheckStateChangedEvent;
0044: import org.eclipse.jface.viewers.CheckboxTableViewer;
0045: import org.eclipse.jface.viewers.ColumnWeightData;
0046: import org.eclipse.jface.viewers.ComboBoxCellEditor;
0047: import org.eclipse.jface.viewers.DoubleClickEvent;
0048: import org.eclipse.jface.viewers.ICellModifier;
0049: import org.eclipse.jface.viewers.ICheckStateListener;
0050: import org.eclipse.jface.viewers.IDoubleClickListener;
0051: import org.eclipse.jface.viewers.ILabelProvider;
0052: import org.eclipse.jface.viewers.ISelection;
0053: import org.eclipse.jface.viewers.ISelectionChangedListener;
0054: import org.eclipse.jface.viewers.IStructuredSelection;
0055: import org.eclipse.jface.viewers.ITableLabelProvider;
0056: import org.eclipse.jface.viewers.LabelProvider;
0057: import org.eclipse.jface.viewers.SelectionChangedEvent;
0058: import org.eclipse.jface.viewers.StructuredSelection;
0059: import org.eclipse.jface.viewers.TableLayout;
0060: import org.eclipse.jface.window.Window;
0061: import org.eclipse.jface.wizard.IWizardPage;
0062:
0063: import org.eclipse.ui.PlatformUI;
0064:
0065: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
0066: import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
0067:
0068: import org.eclipse.jdt.core.IField;
0069: import org.eclipse.jdt.core.IMember;
0070: import org.eclipse.jdt.core.IMethod;
0071: import org.eclipse.jdt.core.IType;
0072: import org.eclipse.jdt.core.JavaModelException;
0073:
0074: import org.eclipse.jdt.internal.corext.refactoring.structure.HierarchyProcessor;
0075: import org.eclipse.jdt.internal.corext.refactoring.structure.IMemberActionInfo;
0076: import org.eclipse.jdt.internal.corext.refactoring.structure.PullUpRefactoring;
0077: import org.eclipse.jdt.internal.corext.refactoring.structure.PullUpRefactoringProcessor;
0078: import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
0079: import org.eclipse.jdt.internal.corext.util.JdtFlags;
0080: import org.eclipse.jdt.internal.corext.util.Messages;
0081:
0082: import org.eclipse.jdt.ui.JavaElementLabelProvider;
0083:
0084: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
0085: import org.eclipse.jdt.internal.ui.JavaPlugin;
0086: import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
0087: import org.eclipse.jdt.internal.ui.util.PixelConverter;
0088: import org.eclipse.jdt.internal.ui.util.SWTUtil;
0089: import org.eclipse.jdt.internal.ui.util.TableLayoutComposite;
0090:
0091: /**
0092: * Wizard page for pull up refactoring wizards which allows to specify the
0093: * actions on the members to pull up.
0094: *
0095: * @since 3.2
0096: */
0097: public class PullUpMemberPage extends UserInputWizardPage {
0098:
0099: private class MemberActionCellModifier implements ICellModifier {
0100:
0101: public boolean canModify(final Object element,
0102: final String property) {
0103: if (!ACTION_PROPERTY.equals(property))
0104: return false;
0105: return ((MemberActionInfo) element).isEditable();
0106: }
0107:
0108: public Object getValue(final Object element,
0109: final String property) {
0110: if (!ACTION_PROPERTY.equals(property))
0111: return null;
0112: final MemberActionInfo info = (MemberActionInfo) element;
0113: return new Integer(info.getAction());
0114: }
0115:
0116: public void modify(final Object element, final String property,
0117: final Object value) {
0118: if (!ACTION_PROPERTY.equals(property))
0119: return;
0120: final int action = ((Integer) value).intValue();
0121: MemberActionInfo info;
0122: if (element instanceof Item) {
0123: info = (MemberActionInfo) ((Item) element).getData();
0124: } else
0125: info = (MemberActionInfo) element;
0126: if (!canModify(info, property))
0127: return;
0128: Assert.isTrue(info.isMethodInfo());
0129: info.setAction(action);
0130: updateWizardPage(null, true);
0131: }
0132: }
0133:
0134: private class MemberActionInfo implements IMemberActionInfo {
0135:
0136: private static final int NO_ACTION = 2;
0137:
0138: private int fAction;
0139:
0140: private final IMember fMember;
0141:
0142: public MemberActionInfo(final IMember member, final int action) {
0143: Assert.isTrue((member instanceof IMethod)
0144: || (member instanceof IField)
0145: || (member instanceof IType));
0146: assertAction(member, action);
0147: fMember = member;
0148: fAction = action;
0149: }
0150:
0151: private void assertAction(final IMember member, final int action) {
0152: if (member instanceof IMethod) {
0153: try {
0154: Assert.isTrue(action != DECLARE_ABSTRACT_ACTION
0155: || !JdtFlags.isStatic(member));
0156: } catch (JavaModelException e) {
0157: JavaPlugin.log(e);
0158: }
0159: Assert.isTrue(action == NO_ACTION
0160: || action == DECLARE_ABSTRACT_ACTION
0161: || action == PULL_UP_ACTION);
0162: } else {
0163: Assert.isTrue(action == NO_ACTION
0164: || action == PULL_UP_ACTION);
0165: }
0166: }
0167:
0168: public int getAction() {
0169: return fAction;
0170: }
0171:
0172: public String getActionLabel() {
0173: switch (fAction) {
0174: case PULL_UP_ACTION:
0175: return getPullUpActionLabel();
0176: case DECLARE_ABSTRACT_ACTION:
0177: return getDeclareAbstractActionLabel();
0178: case NO_ACTION:
0179: return ""; //$NON-NLS-1$
0180: default:
0181: Assert.isTrue(false);
0182: return null;
0183: }
0184: }
0185:
0186: public String[] getAllowedLabels() {
0187: if (isFieldInfo())
0188: return new String[] { "" }; //$NON-NLS-1$
0189: else if (isMethodInfo())
0190: return METHOD_LABELS;
0191: else if (isTypeInfo())
0192: return TYPE_LABELS;
0193: else {
0194: Assert.isTrue(false);
0195: return null;
0196: }
0197: }
0198:
0199: public IMember getMember() {
0200: return fMember;
0201: }
0202:
0203: public boolean isActive() {
0204: return getAction() != NO_ACTION;
0205: }
0206:
0207: public boolean isEditable() {
0208: if (fAction == NO_ACTION)
0209: return false;
0210: if (!isMethodInfo())
0211: return false;
0212: final IMethod method = (IMethod) fMember;
0213: try {
0214: return !JdtFlags.isStatic(method);
0215: } catch (JavaModelException e) {
0216: JavaPlugin.log(e);
0217: return false;
0218: }
0219: }
0220:
0221: public boolean isFieldInfo() {
0222: return getMember() instanceof IField;
0223: }
0224:
0225: public boolean isMethodInfo() {
0226: return getMember() instanceof IMethod;
0227: }
0228:
0229: public boolean isTypeInfo() {
0230: return getMember() instanceof IType;
0231: }
0232:
0233: public void setAction(final int action) {
0234: assertAction(fMember, action);
0235: fAction = action;
0236: }
0237: }
0238:
0239: private static class MemberActionInfoLabelProvider extends
0240: LabelProvider implements ITableLabelProvider {
0241:
0242: private final ILabelProvider fLabelProvider = new JavaElementLabelProvider(
0243: JavaElementLabelProvider.SHOW_DEFAULT
0244: | JavaElementLabelProvider.SHOW_SMALL_ICONS);
0245:
0246: public void dispose() {
0247: super .dispose();
0248: fLabelProvider.dispose();
0249: }
0250:
0251: public Image getColumnImage(final Object element,
0252: final int columnIndex) {
0253: final MemberActionInfo info = (MemberActionInfo) element;
0254: switch (columnIndex) {
0255: case MEMBER_COLUMN:
0256: return fLabelProvider.getImage(info.getMember());
0257: case ACTION_COLUMN:
0258: return null;
0259: default:
0260: Assert.isTrue(false);
0261: return null;
0262: }
0263: }
0264:
0265: public String getColumnText(final Object element,
0266: final int columnIndex) {
0267: final MemberActionInfo info = (MemberActionInfo) element;
0268: switch (columnIndex) {
0269: case MEMBER_COLUMN:
0270: return fLabelProvider.getText(info.getMember());
0271: case ACTION_COLUMN:
0272: return info.getActionLabel();
0273: default:
0274: Assert.isTrue(false);
0275: return null;
0276: }
0277: }
0278: }
0279:
0280: private static final int ACTION_COLUMN = 1;
0281:
0282: private static final String ACTION_PROPERTY = "action"; //$NON-NLS-1$
0283:
0284: protected static final int DECLARE_ABSTRACT_ACTION = 1;
0285:
0286: private static final int MEMBER_COLUMN = 0;
0287:
0288: private static final String MEMBER_PROPERTY = "member"; //$NON-NLS-1$
0289:
0290: protected static final int PULL_UP_ACTION = 0;
0291:
0292: private static final String SETTING_INSTANCEOF = "InstanceOf"; //$NON-NLS-1$
0293:
0294: private static final String SETTING_REPLACE = "Replace"; //$NON-NLS-1$
0295:
0296: private static int getEditableCount(final MemberActionInfo[] infos) {
0297: int result = 0;
0298: for (int i = 0; i < infos.length; i++) {
0299: final MemberActionInfo info = infos[i];
0300: if (info.isEditable())
0301: result++;
0302: }
0303: return result;
0304: }
0305:
0306: private static void putToStringMapping(final Map result,
0307: final String[] actionLabels, final int actionIndex) {
0308: result.put(actionLabels[actionIndex], new Integer(actionIndex));
0309: }
0310:
0311: private static void setActionForInfos(
0312: final MemberActionInfo[] infos, final int action) {
0313: for (int i = 0; i < infos.length; i++) {
0314: infos[i].setAction(action);
0315: }
0316: }
0317:
0318: private Button fAddButton;
0319:
0320: protected IType[] fCandidateTypes = {};
0321:
0322: private Button fCreateStubsButton;
0323:
0324: private Button fDeselectAllButton;
0325:
0326: private Button fEditButton;
0327:
0328: private Button fInstanceofButton;
0329:
0330: private Label fLabel;
0331:
0332: private Button fReplaceButton;
0333:
0334: private Button fSelectAllButton;
0335:
0336: private Label fStatusLine;
0337:
0338: protected final PullUpMethodPage fSuccessorPage;
0339:
0340: private Combo fSuperTypesCombo;
0341:
0342: private CheckboxTableViewer fTableViewer;
0343:
0344: protected final String[] METHOD_LABELS;
0345:
0346: protected final String[] TYPE_LABELS;
0347:
0348: public PullUpMemberPage(final String name,
0349: final PullUpMethodPage page) {
0350: super (name);
0351: fSuccessorPage = page;
0352: setDescription(RefactoringMessages.PullUpInputPage1_page_message);
0353: METHOD_LABELS = new String[2];
0354: METHOD_LABELS[PULL_UP_ACTION] = RefactoringMessages.PullUpInputPage1_pull_up;
0355: METHOD_LABELS[DECLARE_ABSTRACT_ACTION] = RefactoringMessages.PullUpInputPage1_declare_abstract;
0356:
0357: TYPE_LABELS = new String[1];
0358: TYPE_LABELS[PULL_UP_ACTION] = RefactoringMessages.PullUpInputPage1_pull_up;
0359: }
0360:
0361: private boolean areAllMembersMarkedAsPullUp() {
0362: return getMembersForAction(PULL_UP_ACTION).length == getTableInput().length;
0363: }
0364:
0365: protected boolean areAllMembersMarkedAsWithNoAction() {
0366: return getMembersForAction(MemberActionInfo.NO_ACTION).length == getTableInput().length;
0367: }
0368:
0369: private MemberActionInfo[] asMemberActionInfos() {
0370: final PullUpRefactoringProcessor processor = getPullUpRefactoring()
0371: .getPullUpProcessor();
0372: final List toPullUp = Arrays.asList(processor
0373: .getMembersToMove());
0374: final IMember[] members = processor
0375: .getPullableMembersOfDeclaringType();
0376: final MemberActionInfo[] result = new MemberActionInfo[members.length];
0377: for (int i = 0; i < members.length; i++) {
0378: final IMember member = members[i];
0379: if (toPullUp.contains(member))
0380: result[i] = new MemberActionInfo(member, PULL_UP_ACTION);
0381: else
0382: result[i] = new MemberActionInfo(member,
0383: MemberActionInfo.NO_ACTION);
0384: }
0385: return result;
0386: }
0387:
0388: public boolean canFlipToNextPage() {
0389: return isPageComplete();
0390: }
0391:
0392: private void checkAdditionalRequired() {
0393: try {
0394: PlatformUI.getWorkbench().getActiveWorkbenchWindow().run(
0395: false, false, new IRunnableWithProgress() {
0396:
0397: public void run(final IProgressMonitor pm)
0398: throws InvocationTargetException {
0399: try {
0400: checkPullUp(
0401: getPullUpRefactoring()
0402: .getPullUpProcessor()
0403: .getAdditionalRequiredMembersToPullUp(
0404: pm), true);
0405: } catch (JavaModelException e) {
0406: throw new InvocationTargetException(e);
0407: } finally {
0408: pm.done();
0409: }
0410: }
0411: });
0412: } catch (InvocationTargetException e) {
0413: ExceptionHandler.handle(e, getShell(),
0414: RefactoringMessages.PullUpInputPage_pull_Up,
0415: RefactoringMessages.PullUpInputPage_exception);
0416: } catch (InterruptedException e) {
0417: Assert.isTrue(false);
0418: }
0419: }
0420:
0421: protected void checkPageCompletionStatus(final boolean displayErrors) {
0422: if (areAllMembersMarkedAsWithNoAction()) {
0423: if (displayErrors)
0424: setErrorMessage(getNoMembersMessage());
0425: setPageComplete(false);
0426: } else {
0427: setErrorMessage(null);
0428: setPageComplete(true);
0429: }
0430: fSuccessorPage.fireSettingsChanged();
0431: }
0432:
0433: private void checkPullUp(final IMember[] elements,
0434: final boolean displayErrors) {
0435: setActionForMembers(elements, PULL_UP_ACTION);
0436: updateWizardPage(null, displayErrors);
0437: }
0438:
0439: private void createButtonComposite(final Composite parent) {
0440: final Composite composite = new Composite(parent, SWT.NONE);
0441: composite.setLayoutData(new GridData(GridData.FILL_VERTICAL));
0442: final GridLayout gl = new GridLayout();
0443: gl.marginHeight = 0;
0444: gl.marginWidth = 0;
0445: composite.setLayout(gl);
0446:
0447: fSelectAllButton = new Button(composite, SWT.PUSH);
0448: fSelectAllButton
0449: .setText(RefactoringMessages.PullUpWizard_select_all_label);
0450: fSelectAllButton.setLayoutData(new GridData(
0451: GridData.FILL_HORIZONTAL));
0452: fSelectAllButton.setEnabled(true);
0453: SWTUtil.setButtonDimensionHint(fSelectAllButton);
0454: fSelectAllButton.addSelectionListener(new SelectionAdapter() {
0455:
0456: public void widgetSelected(final SelectionEvent event) {
0457: final IMember[] members = getMembers();
0458: setActionForMembers(members, PULL_UP_ACTION);
0459: updateWizardPage(null, true);
0460: }
0461: });
0462:
0463: fDeselectAllButton = new Button(composite, SWT.PUSH);
0464: fDeselectAllButton
0465: .setText(RefactoringMessages.PullUpWizard_deselect_all_label);
0466: fDeselectAllButton.setLayoutData(new GridData(
0467: GridData.FILL_HORIZONTAL));
0468: fDeselectAllButton.setEnabled(false);
0469: SWTUtil.setButtonDimensionHint(fDeselectAllButton);
0470: fDeselectAllButton.addSelectionListener(new SelectionAdapter() {
0471:
0472: public void widgetSelected(final SelectionEvent event) {
0473: final IMember[] members = getMembers();
0474: setActionForMembers(members, MemberActionInfo.NO_ACTION);
0475: updateWizardPage(null, true);
0476: }
0477: });
0478:
0479: fEditButton = new Button(composite, SWT.PUSH);
0480: fEditButton.setText(RefactoringMessages.PullUpInputPage1_Edit);
0481:
0482: final GridData data = new GridData(GridData.FILL_HORIZONTAL);
0483: data.verticalIndent = new PixelConverter(parent)
0484: .convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
0485: fEditButton.setLayoutData(data);
0486: fEditButton.setEnabled(false);
0487: SWTUtil.setButtonDimensionHint(fEditButton);
0488: fEditButton.addSelectionListener(new SelectionAdapter() {
0489:
0490: public void widgetSelected(final SelectionEvent event) {
0491: editSelectedMembers();
0492: }
0493: });
0494:
0495: fAddButton = new Button(composite, SWT.PUSH);
0496: fAddButton
0497: .setText(RefactoringMessages.PullUpInputPage1_Add_Required);
0498: fAddButton
0499: .setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
0500: SWTUtil.setButtonDimensionHint(fAddButton);
0501: fAddButton.addSelectionListener(new SelectionAdapter() {
0502:
0503: public void widgetSelected(final SelectionEvent event) {
0504: checkAdditionalRequired();
0505: }
0506: });
0507: }
0508:
0509: public void createControl(final Composite parent) {
0510: final Composite composite = new Composite(parent, SWT.NONE);
0511: final GridLayout layout = new GridLayout();
0512: layout.numColumns = 2;
0513: composite.setLayout(layout);
0514:
0515: createSuperTypeControl(composite);
0516: createSpacer(composite);
0517: createSuperTypeCheckbox(composite);
0518: createInstanceOfCheckbox(composite, layout.marginWidth);
0519: createStubCheckbox(composite);
0520: createSpacer(composite);
0521: createMemberTableLabel(composite);
0522: createMemberTableComposite(composite);
0523: createStatusLine(composite);
0524:
0525: setControl(composite);
0526: Dialog.applyDialogFont(composite);
0527: initializeEnablement();
0528: initializeCheckboxes();
0529: PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
0530: IJavaHelpContextIds.PULL_UP_WIZARD_PAGE);
0531: }
0532:
0533: protected void createInstanceOfCheckbox(final Composite result,
0534: final int margin) {
0535: final HierarchyProcessor processor = getPullUpRefactoring()
0536: .getPullUpProcessor();
0537: fInstanceofButton = new Button(result, SWT.CHECK);
0538: fInstanceofButton.setSelection(false);
0539: final GridData gd = new GridData();
0540: gd.horizontalIndent = (margin + fInstanceofButton.computeSize(
0541: SWT.DEFAULT, SWT.DEFAULT).x);
0542: gd.horizontalSpan = 2;
0543: fInstanceofButton.setLayoutData(gd);
0544: fInstanceofButton.setText(getInstanceofButtonLabel());
0545: processor.setInstanceOf(fInstanceofButton.getSelection());
0546: fInstanceofButton.addSelectionListener(new SelectionAdapter() {
0547:
0548: public void widgetSelected(final SelectionEvent e) {
0549: processor.setInstanceOf(fInstanceofButton
0550: .getSelection());
0551: }
0552: });
0553: fReplaceButton.addSelectionListener(new SelectionAdapter() {
0554:
0555: public void widgetSelected(final SelectionEvent e) {
0556: fInstanceofButton.setEnabled(fReplaceButton
0557: .getSelection());
0558: }
0559: });
0560: }
0561:
0562: private void createMemberTable(final Composite parent) {
0563: final TableLayoutComposite layouter = new TableLayoutComposite(
0564: parent, SWT.NONE);
0565: layouter.addColumnData(new ColumnWeightData(60, true));
0566: layouter.addColumnData(new ColumnWeightData(40, true));
0567:
0568: final Table table = new Table(layouter, SWT.H_SCROLL
0569: | SWT.V_SCROLL | SWT.MULTI | SWT.BORDER
0570: | SWT.FULL_SELECTION | SWT.CHECK);
0571: table.setHeaderVisible(true);
0572: table.setLinesVisible(true);
0573:
0574: final GridData gd = new GridData(GridData.FILL_BOTH);
0575: gd.heightHint = SWTUtil.getTableHeightHint(table,
0576: getTableRowCount());
0577: gd.widthHint = convertWidthInCharsToPixels(30);
0578: layouter.setLayoutData(gd);
0579:
0580: final TableLayout tableLayout = new TableLayout();
0581: table.setLayout(tableLayout);
0582:
0583: final TableColumn column0 = new TableColumn(table, SWT.NONE);
0584: column0.setText(RefactoringMessages.PullUpInputPage1_Member);
0585:
0586: final TableColumn column1 = new TableColumn(table, SWT.NONE);
0587: column1.setText(RefactoringMessages.PullUpInputPage1_Action);
0588:
0589: fTableViewer = new PullPushCheckboxTableViewer(table);
0590: fTableViewer.setUseHashlookup(true);
0591: fTableViewer.setContentProvider(new ArrayContentProvider());
0592: fTableViewer
0593: .setLabelProvider(new MemberActionInfoLabelProvider());
0594: fTableViewer
0595: .addSelectionChangedListener(new ISelectionChangedListener() {
0596:
0597: public void selectionChanged(
0598: final SelectionChangedEvent event) {
0599: updateButtonEnablement(event.getSelection());
0600: }
0601: });
0602: fTableViewer.addCheckStateListener(new ICheckStateListener() {
0603:
0604: public void checkStateChanged(
0605: final CheckStateChangedEvent event) {
0606: final boolean checked = event.getChecked();
0607: final MemberActionInfo info = (MemberActionInfo) event
0608: .getElement();
0609: if (checked)
0610: info.setAction(PULL_UP_ACTION);
0611: else
0612: info.setAction(MemberActionInfo.NO_ACTION);
0613: updateWizardPage(null, true);
0614: }
0615: });
0616: fTableViewer.addDoubleClickListener(new IDoubleClickListener() {
0617:
0618: public void doubleClick(final DoubleClickEvent event) {
0619: editSelectedMembers();
0620: }
0621: });
0622:
0623: setTableInput();
0624: checkPullUp(getPullUpRefactoring().getPullUpProcessor()
0625: .getMembersToMove(), false);
0626: setupCellEditors(table);
0627: }
0628:
0629: protected void createMemberTableComposite(final Composite parent) {
0630: final Composite composite = new Composite(parent, SWT.NONE);
0631: final GridData data = new GridData(GridData.FILL_BOTH);
0632: data.horizontalSpan = 2;
0633: composite.setLayoutData(data);
0634: final GridLayout layout = new GridLayout();
0635: layout.numColumns = 2;
0636: layout.marginWidth = 0;
0637: layout.marginHeight = 0;
0638: composite.setLayout(layout);
0639:
0640: createMemberTable(composite);
0641: createButtonComposite(composite);
0642: }
0643:
0644: protected void createMemberTableLabel(final Composite parent) {
0645: fLabel = new Label(parent, SWT.NONE);
0646: fLabel
0647: .setText(RefactoringMessages.PullUpInputPage1_Specify_actions);
0648: final GridData data = new GridData();
0649: data.horizontalSpan = 2;
0650: fLabel.setLayoutData(data);
0651: }
0652:
0653: protected void createSpacer(final Composite parent) {
0654: final Label label = new Label(parent, SWT.NONE);
0655: final GridData data = new GridData();
0656: data.horizontalSpan = 2;
0657: data.heightHint = convertHeightInCharsToPixels(1) / 2;
0658: label.setLayoutData(data);
0659: }
0660:
0661: protected void createStatusLine(final Composite composite) {
0662: fStatusLine = new Label(composite, SWT.NONE);
0663: final GridData data = new GridData();
0664: data.horizontalSpan = 2;
0665: updateStatusLine();
0666: fStatusLine.setLayoutData(data);
0667: }
0668:
0669: // String -> Integer
0670: private Map createStringMappingForSelectedMembers() {
0671: final Map result = new HashMap();
0672: putToStringMapping(result, METHOD_LABELS, PULL_UP_ACTION);
0673: putToStringMapping(result, METHOD_LABELS,
0674: DECLARE_ABSTRACT_ACTION);
0675: return result;
0676: }
0677:
0678: protected void createStubCheckbox(final Composite parent) {
0679: fCreateStubsButton = new Button(parent, SWT.CHECK);
0680: fCreateStubsButton.setText(getCreateStubsButtonLabel());
0681: final GridData data = new GridData();
0682: data.horizontalSpan = 2;
0683: fCreateStubsButton.setLayoutData(data);
0684: fCreateStubsButton.setEnabled(false);
0685: fCreateStubsButton.setSelection(getPullUpRefactoring()
0686: .getPullUpProcessor().getCreateMethodStubs());
0687: }
0688:
0689: protected void createSuperTypeCheckbox(final Composite parent) {
0690: fReplaceButton = new Button(parent, SWT.CHECK);
0691: fReplaceButton.setText(getReplaceButtonLabel());
0692: final GridData data = new GridData();
0693: data.horizontalSpan = 2;
0694: fReplaceButton.setLayoutData(data);
0695: fReplaceButton.setEnabled(true);
0696: fReplaceButton.setSelection(getPullUpRefactoring()
0697: .getPullUpProcessor().isReplace());
0698: }
0699:
0700: private void createSuperTypeCombo(final IProgressMonitor pm,
0701: final Composite parent) throws JavaModelException {
0702: final Label label = new Label(parent, SWT.NONE);
0703: label
0704: .setText(RefactoringMessages.PullUpInputPage1_Select_destination);
0705: label.setLayoutData(new GridData());
0706:
0707: fSuperTypesCombo = new Combo(parent, SWT.READ_ONLY);
0708: fCandidateTypes = getPullUpRefactoring().getPullUpProcessor()
0709: .getCandidateTypes(new RefactoringStatus(), pm);
0710: if (fCandidateTypes.length > 0) {
0711: for (int i = 0; i < fCandidateTypes.length; i++) {
0712: final String comboLabel = JavaModelUtil
0713: .getFullyQualifiedName(fCandidateTypes[i]);
0714: fSuperTypesCombo.add(comboLabel);
0715: }
0716: fSuperTypesCombo.select(fCandidateTypes.length - 1);
0717: fSuperTypesCombo.setLayoutData(new GridData(
0718: GridData.FILL_HORIZONTAL));
0719: }
0720: }
0721:
0722: protected void createSuperTypeControl(final Composite parent) {
0723: try {
0724: PlatformUI.getWorkbench().getActiveWorkbenchWindow().run(
0725: false, false, new IRunnableWithProgress() {
0726:
0727: public void run(final IProgressMonitor monitor)
0728: throws InvocationTargetException {
0729: try {
0730: createSuperTypeCombo(monitor, parent);
0731: } catch (JavaModelException exception) {
0732: throw new InvocationTargetException(
0733: exception);
0734: } finally {
0735: monitor.done();
0736: }
0737: }
0738: });
0739: } catch (InvocationTargetException exception) {
0740: ExceptionHandler.handle(exception, getShell(),
0741: RefactoringMessages.PullUpInputPage_pull_Up,
0742: RefactoringMessages.PullUpInputPage_exception);
0743: } catch (InterruptedException exception) {
0744: Assert.isTrue(false);
0745: }
0746: }
0747:
0748: public void dispose() {
0749: fInstanceofButton = null;
0750: fReplaceButton = null;
0751: fTableViewer = null;
0752: super .dispose();
0753: }
0754:
0755: private void editSelectedMembers() {
0756: if (!fEditButton.isEnabled())
0757: return;
0758:
0759: final ISelection preserved = fTableViewer.getSelection();
0760: try {
0761: final String shellTitle = RefactoringMessages.PullUpInputPage1_Edit_members;
0762: final String labelText = RefactoringMessages.PullUpInputPage1_Mark_selected_members;
0763: final Map stringMapping = createStringMappingForSelectedMembers();
0764: final String[] keys = (String[]) stringMapping.keySet()
0765: .toArray(new String[stringMapping.keySet().size()]);
0766: Arrays.sort(keys);
0767: final int initialSelectionIndex = getInitialSelectionIndexForEditDialog(
0768: stringMapping, keys);
0769: final ComboSelectionDialog dialog = new ComboSelectionDialog(
0770: getShell(), shellTitle, labelText, keys,
0771: initialSelectionIndex);
0772: dialog.setBlockOnOpen(true);
0773: if (dialog.open() == Window.CANCEL)
0774: return;
0775: final int action = ((Integer) stringMapping.get(dialog
0776: .getSelectedString())).intValue();
0777: setActionForInfos(getSelectedMembers(), action);
0778: } finally {
0779: updateWizardPage(preserved, true);
0780: }
0781: }
0782:
0783: private boolean enableEditButton(final IStructuredSelection ss) {
0784: if (ss.isEmpty() || ss.size() == 0)
0785: return false;
0786: return ss.size() == getEditableCount(getSelectedMembers());
0787: }
0788:
0789: private MemberActionInfo[] getActiveInfos() {
0790: final MemberActionInfo[] infos = getTableInput();
0791: final List result = new ArrayList(infos.length);
0792: for (int i = 0; i < infos.length; i++) {
0793: final MemberActionInfo info = infos[i];
0794: if (info.isActive())
0795: result.add(info);
0796: }
0797: return (MemberActionInfo[]) result
0798: .toArray(new MemberActionInfo[result.size()]);
0799: }
0800:
0801: private int getCommonActionCodeForSelectedInfos() {
0802: final MemberActionInfo[] infos = getSelectedMembers();
0803: if (infos.length == 0)
0804: return -1;
0805:
0806: final int code = infos[0].getAction();
0807: for (int i = 0; i < infos.length; i++) {
0808: if (code != infos[i].getAction())
0809: return -1;
0810: }
0811: return code;
0812: }
0813:
0814: protected String getCreateStubsButtonLabel() {
0815: return RefactoringMessages.PullUpInputPage1_Create_stubs;
0816: }
0817:
0818: protected String getDeclareAbstractActionLabel() {
0819: return RefactoringMessages.PullUpInputPage1_declare_abstract;
0820: }
0821:
0822: public IType getDestinationType() {
0823: final int index = fSuperTypesCombo.getSelectionIndex();
0824: if (index >= 0)
0825: return fCandidateTypes[index];
0826: return null;
0827: }
0828:
0829: private int getInitialSelectionIndexForEditDialog(
0830: final Map stringMapping, final String[] keys) {
0831: final int commonActionCode = getCommonActionCodeForSelectedInfos();
0832: if (commonActionCode == -1)
0833: return 0;
0834: for (final Iterator iter = stringMapping.keySet().iterator(); iter
0835: .hasNext();) {
0836: final String key = (String) iter.next();
0837: final int action = ((Integer) stringMapping.get(key))
0838: .intValue();
0839: if (commonActionCode == action) {
0840: for (int i = 0; i < keys.length; i++) {
0841: if (key.equals(keys[i]))
0842: return i;
0843: }
0844: Assert.isTrue(false);
0845: }
0846: }
0847: return 0;
0848: }
0849:
0850: protected String getInstanceofButtonLabel() {
0851: return RefactoringMessages.PullUpInputPage1_label_use_in_instanceof ;
0852: }
0853:
0854: private IMember[] getMembers() {
0855: final MemberActionInfo[] infos = getTableInput();
0856: final List result = new ArrayList(infos.length);
0857: for (int index = 0; index < infos.length; index++) {
0858: result.add(infos[index].getMember());
0859: }
0860: return (IMember[]) result.toArray(new IMember[result.size()]);
0861: }
0862:
0863: private IMember[] getMembersForAction(final int action) {
0864: final MemberActionInfo[] infos = getTableInput();
0865: final List result = new ArrayList(infos.length);
0866: for (int index = 0; index < infos.length; index++) {
0867: if (infos[index].getAction() == action)
0868: result.add(infos[index].getMember());
0869: }
0870: return (IMember[]) result.toArray(new IMember[result.size()]);
0871: }
0872:
0873: private IMethod[] getMethodsForAction(final int action) {
0874: final MemberActionInfo[] infos = getTableInput();
0875: final List list = new ArrayList(infos.length);
0876: for (int index = 0; index < infos.length; index++) {
0877: if (infos[index].isMethodInfo()
0878: && infos[index].getAction() == action) {
0879: list.add(infos[index].getMember());
0880: }
0881: }
0882: return (IMethod[]) list.toArray(new IMethod[list.size()]);
0883: }
0884:
0885: public IWizardPage getNextPage() {
0886: initializeRefactoring();
0887: storeDialogSettings();
0888: if (getMethodsForAction(PULL_UP_ACTION).length == 0)
0889: return computeSuccessorPage();
0890: try {
0891: final IType destination = getDestinationType();
0892: if (destination != null && destination.isInterface())
0893: return computeSuccessorPage();
0894: } catch (JavaModelException exception) {
0895: JavaPlugin.log(exception);
0896: }
0897: return super .getNextPage();
0898: }
0899:
0900: protected String getNoMembersMessage() {
0901: return RefactoringMessages.PullUpInputPage1_Select_members_to_pull_up;
0902: }
0903:
0904: protected String getPullUpActionLabel() {
0905: return RefactoringMessages.PullUpInputPage1_pull_up;
0906: }
0907:
0908: private PullUpRefactoring getPullUpRefactoring() {
0909: return (PullUpRefactoring) getRefactoring();
0910: }
0911:
0912: protected String getReplaceButtonLabel() {
0913: return RefactoringMessages.PullUpInputPage1_label_use_destination;
0914: }
0915:
0916: private MemberActionInfo[] getSelectedMembers() {
0917: Assert
0918: .isTrue(fTableViewer.getSelection() instanceof IStructuredSelection);
0919: final IStructuredSelection structured = (IStructuredSelection) fTableViewer
0920: .getSelection();
0921: final List result = structured.toList();
0922: return (MemberActionInfo[]) result
0923: .toArray(new MemberActionInfo[result.size()]);
0924: }
0925:
0926: private MemberActionInfo[] getTableInput() {
0927: return (MemberActionInfo[]) fTableViewer.getInput();
0928: }
0929:
0930: protected int getTableRowCount() {
0931: return 10;
0932: }
0933:
0934: private void initializeCheckBox(final Button checkbox,
0935: final String property, final boolean def) {
0936: final String s = JavaPlugin.getDefault().getDialogSettings()
0937: .get(property);
0938: if (s != null)
0939: checkbox.setSelection(new Boolean(s).booleanValue());
0940: else
0941: checkbox.setSelection(def);
0942: }
0943:
0944: protected void initializeCheckboxes() {
0945: initializeCheckBox(fReplaceButton, SETTING_REPLACE, true);
0946: initializeCheckBox(fInstanceofButton, SETTING_INSTANCEOF, false);
0947: }
0948:
0949: protected void initializeEnablement() {
0950: MemberActionInfo[] infos = asMemberActionInfos();
0951: final boolean enabled = infos.length > 0;
0952: fTableViewer.getTable().setEnabled(enabled);
0953: fStatusLine.setEnabled(enabled);
0954: fAddButton.setEnabled(enabled);
0955: fLabel.setEnabled(enabled);
0956: }
0957:
0958: private void initializeRefactoring() {
0959: final PullUpRefactoringProcessor processor = getPullUpRefactoring()
0960: .getPullUpProcessor();
0961: processor.setMembersToMove(getMembersForAction(PULL_UP_ACTION));
0962: processor
0963: .setAbstractMethods(getMethodsForAction(DECLARE_ABSTRACT_ACTION));
0964: final IType destination = getDestinationType();
0965: if (destination != null)
0966: processor.setDestinationType(destination);
0967: processor.setCreateMethodStubs(fCreateStubsButton
0968: .getSelection());
0969: processor.setReplace(fReplaceButton.getSelection());
0970: processor.setInstanceOf(fInstanceofButton.getSelection());
0971: processor
0972: .setDeletedMethods(getMethodsForAction(PULL_UP_ACTION));
0973: }
0974:
0975: protected boolean performFinish() {
0976: initializeRefactoring();
0977: storeDialogSettings();
0978: return super .performFinish();
0979: }
0980:
0981: private void setActionForMembers(final IMember[] members,
0982: final int action) {
0983: final MemberActionInfo[] infos = getTableInput();
0984: for (int i = 0; i < members.length; i++) {
0985: for (int j = 0; j < infos.length; j++) {
0986: if (infos[j].getMember().equals(members[i]))
0987: infos[j].setAction(action);
0988: }
0989: }
0990: }
0991:
0992: private void setTableInput() {
0993: fTableViewer.setInput(asMemberActionInfos());
0994: }
0995:
0996: private void setupCellEditors(final Table table) {
0997: final ComboBoxCellEditor editor = new ComboBoxCellEditor();
0998: editor.setStyle(SWT.READ_ONLY);
0999: fTableViewer.setCellEditors(new CellEditor[] { null, editor });
1000: fTableViewer
1001: .addSelectionChangedListener(new ISelectionChangedListener() {
1002:
1003: public void selectionChanged(
1004: final SelectionChangedEvent event) {
1005: if (editor.getControl() == null
1006: & !table.isDisposed())
1007: editor.create(table);
1008: final ISelection sel = event.getSelection();
1009: if (!(sel instanceof IStructuredSelection))
1010: return;
1011: final IStructuredSelection structured = (IStructuredSelection) sel;
1012: if (structured.size() != 1)
1013: return;
1014: final MemberActionInfo info = (MemberActionInfo) structured
1015: .getFirstElement();
1016: editor.setItems(info.getAllowedLabels());
1017: editor.setValue(new Integer(info.getAction()));
1018: }
1019: });
1020:
1021: final ICellModifier cellModifier = new MemberActionCellModifier();
1022: fTableViewer.setCellModifier(cellModifier);
1023: fTableViewer.setColumnProperties(new String[] {
1024: MEMBER_PROPERTY, ACTION_PROPERTY });
1025: }
1026:
1027: public void setVisible(final boolean visible) {
1028: super .setVisible(visible);
1029: if (visible) {
1030: try {
1031: getPullUpRefactoring().getPullUpProcessor()
1032: .resetEnvironment();
1033: } finally {
1034: fTableViewer.setSelection(new StructuredSelection(
1035: getActiveInfos()), true);
1036: fTableViewer.getControl().setFocus();
1037: }
1038: }
1039: }
1040:
1041: private void storeDialogSettings() {
1042: final IDialogSettings settings = JavaPlugin.getDefault()
1043: .getDialogSettings();
1044: settings.put(SETTING_REPLACE, fReplaceButton.getSelection());
1045: settings.put(SETTING_INSTANCEOF, fInstanceofButton
1046: .getSelection());
1047: }
1048:
1049: private void updateButtonEnablement(final ISelection selection) {
1050: if (fEditButton != null)
1051: fEditButton
1052: .setEnabled(enableEditButton((IStructuredSelection) selection));
1053: fCreateStubsButton
1054: .setEnabled(getMethodsForAction(DECLARE_ABSTRACT_ACTION).length != 0);
1055: fInstanceofButton.setEnabled(fReplaceButton.getSelection());
1056: if (fSelectAllButton != null)
1057: fSelectAllButton.setEnabled(!areAllMembersMarkedAsPullUp());
1058: if (fDeselectAllButton != null)
1059: fDeselectAllButton
1060: .setEnabled(!areAllMembersMarkedAsWithNoAction());
1061: }
1062:
1063: private void updateStatusLine() {
1064: if (fStatusLine == null)
1065: return;
1066: final int selected = fTableViewer.getCheckedElements().length;
1067: final String[] keys = { String.valueOf(selected) };
1068: final String msg = Messages.format(
1069: RefactoringMessages.PullUpInputPage1_status_line, keys);
1070: fStatusLine.setText(msg);
1071: }
1072:
1073: private void updateWizardPage(final ISelection selection,
1074: final boolean displayErrors) {
1075: fTableViewer.refresh();
1076: if (selection != null) {
1077: fTableViewer.getControl().setFocus();
1078: fTableViewer.setSelection(selection);
1079: }
1080: checkPageCompletionStatus(displayErrors);
1081: updateButtonEnablement(fTableViewer.getSelection());
1082: updateStatusLine();
1083: }
1084: }
|