001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 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 org.eclipse.swt.SWT;
013: import org.eclipse.swt.events.ModifyEvent;
014: import org.eclipse.swt.events.ModifyListener;
015: import org.eclipse.swt.events.SelectionAdapter;
016: import org.eclipse.swt.events.SelectionEvent;
017: import org.eclipse.swt.layout.GridData;
018: import org.eclipse.swt.layout.GridLayout;
019: import org.eclipse.swt.widgets.Button;
020: import org.eclipse.swt.widgets.Composite;
021: import org.eclipse.swt.widgets.Label;
022: import org.eclipse.swt.widgets.Text;
023:
024: import org.eclipse.jface.dialogs.Dialog;
025:
026: import org.eclipse.ui.PlatformUI;
027:
028: import org.eclipse.ltk.core.refactoring.Refactoring;
029: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
030: import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
031:
032: import org.eclipse.jdt.internal.corext.refactoring.structure.MoveInnerToTopRefactoring;
033:
034: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
035:
036: public class MoveInnerToTopWizard extends RefactoringWizard {
037:
038: private class MoveInnerToToplnputPage extends TextInputWizardPage {
039:
040: private Text fFieldNameEntryText;
041:
042: private Label fFieldNameLabel;
043:
044: private Button fFinalCheckBox;
045:
046: private final boolean fInitialInputValid;
047:
048: public MoveInnerToToplnputPage(String initialValue) {
049: super (
050: RefactoringMessages.MoveInnerToToplnputPage_description,
051: true, initialValue);
052: final MoveInnerToTopRefactoring refactoring = getMoveRefactoring();
053: final boolean mandatory = refactoring
054: .isCreatingInstanceFieldMandatory();
055: fInitialInputValid = (!initialValue.equals("")) || !mandatory; //$NON-NLS-1$
056: if (!mandatory)
057: refactoring.setCreateInstanceField(false);
058: }
059:
060: private void addFieldNameEntry(Composite newControl) {
061: fFieldNameLabel = new Label(newControl, SWT.NONE);
062: if (getMoveRefactoring().isCreatingInstanceFieldMandatory())
063: fFieldNameLabel
064: .setText(RefactoringMessages.MoveInnerToToplnputPage_enter_name_mandatory);
065: else
066: fFieldNameLabel
067: .setText(RefactoringMessages.MoveInnerToToplnputPage_enter_name);
068: fFieldNameLabel.setLayoutData(new GridData());
069:
070: fFieldNameEntryText = createTextInputField(newControl);
071: fFieldNameEntryText.selectAll();
072: fFieldNameEntryText.setLayoutData(new GridData(
073: GridData.FILL_HORIZONTAL));
074: }
075:
076: private void addFinalCheckBox(Composite newControl) {
077: fFinalCheckBox = new Button(newControl, SWT.CHECK);
078: fFinalCheckBox
079: .setText(RefactoringMessages.MoveInnerToToplnputPage_instance_final);
080: GridData data = new GridData(GridData.FILL_HORIZONTAL);
081: data.horizontalSpan = 2;
082: fFinalCheckBox.setLayoutData(data);
083: fFinalCheckBox.addSelectionListener(new SelectionAdapter() {
084:
085: public void widgetSelected(SelectionEvent event) {
086: getMoveRefactoring().setMarkInstanceFieldAsFinal(
087: fFinalCheckBox.getSelection());
088: }
089: });
090: fFieldNameEntryText.addModifyListener(new ModifyListener() {
091:
092: public final void modifyText(ModifyEvent event) {
093: final String text = fFieldNameEntryText.getText();
094: final MoveInnerToTopRefactoring refactoring = getMoveRefactoring();
095: if (refactoring.isCreatingInstanceFieldMandatory())
096: setPageComplete(validateTextField(text));
097: final boolean empty = text.length() == 0;
098: if (refactoring.isCreatingInstanceFieldMandatory()) {
099: // Do nothing
100: } else if (refactoring
101: .isCreatingInstanceFieldPossible()) {
102: fFinalCheckBox.setEnabled(!empty);
103: }
104: if (!refactoring.isCreatingInstanceFieldMandatory())
105: refactoring.setCreateInstanceField(!empty);
106: }
107: });
108: }
109:
110: public void createControl(Composite parent) {
111: initializeDialogUnits(parent);
112: Composite newControl = new Composite(parent, SWT.NONE);
113: setControl(newControl);
114: PlatformUI.getWorkbench().getHelpSystem().setHelp(
115: newControl,
116: IJavaHelpContextIds.MOVE_INNER_TO_TOP_WIZARD_PAGE);
117: newControl.setLayout(new GridLayout());
118: Dialog.applyDialogFont(newControl);
119:
120: GridLayout layout = new GridLayout();
121: layout.numColumns = 2;
122: layout.verticalSpacing = 8;
123: newControl.setLayout(layout);
124:
125: addFieldNameEntry(newControl);
126: addFinalCheckBox(newControl);
127:
128: if (getMoveRefactoring().isCreatingInstanceFieldPossible()) {
129: fFinalCheckBox.setSelection(getMoveRefactoring()
130: .isInstanceFieldMarkedFinal());
131: fFinalCheckBox.setEnabled(true);
132: } else {
133: fFinalCheckBox.setSelection(false);
134: fFinalCheckBox.setEnabled(false);
135: }
136: }
137:
138: /*
139: * @see org.eclipse.jdt.internal.ui.refactoring.TextInputWizardPage#isEmptyInputValid()
140: */
141: protected boolean isEmptyInputValid() {
142: return !getMoveRefactoring()
143: .isCreatingInstanceFieldMandatory();
144: }
145:
146: /*
147: * @see org.eclipse.jdt.internal.ui.refactoring.TextInputWizardPage#isInitialInputValid()
148: */
149: protected boolean isInitialInputValid() {
150: return fInitialInputValid;
151: }
152:
153: /*
154: * @see org.eclipse.jface.dialogs.IDialogPage#setVisible(boolean)
155: */
156: public void setVisible(boolean visible) {
157: super .setVisible(visible);
158: if (visible) {
159: String message = getMoveRefactoring()
160: .isCreatingInstanceFieldMandatory() ? RefactoringMessages.MoveInnerToToplnputPage_mandatory_info
161: : RefactoringMessages.MoveInnerToToplnputPage_optional_info;
162: setPageComplete(RefactoringStatus
163: .createInfoStatus(message));
164: } else {
165: setPageComplete(new RefactoringStatus());
166: getContainer().updateMessage();
167: }
168: }
169:
170: /*
171: * @see org.eclipse.jdt.internal.ui.refactoring.TextInputWizardPage#validateTextField(String)
172: */
173: protected RefactoringStatus validateTextField(String text) {
174: final MoveInnerToTopRefactoring refactoring = getMoveRefactoring();
175: refactoring.setEnclosingInstanceName(text);
176: if (refactoring.isCreatingInstanceFieldMandatory())
177: return refactoring.checkEnclosingInstanceName(text);
178: else if (!text.equals("")) //$NON-NLS-1$
179: return refactoring.checkEnclosingInstanceName(text);
180: else
181: return new RefactoringStatus();
182: }
183: }
184:
185: public MoveInnerToTopWizard(Refactoring refactoring) {
186: super (refactoring, DIALOG_BASED_USER_INTERFACE);
187: setDefaultPageTitle(RefactoringMessages.MoveInnerToTopWizard_Move_Inner);
188: }
189:
190: /*
191: * @see RefactoringWizard#addUserInputPages
192: */
193: protected void addUserInputPages() {
194: final MoveInnerToTopRefactoring refactoring = getMoveRefactoring();
195: if (refactoring.isCreatingInstanceFieldPossible())
196: addPage(new MoveInnerToToplnputPage(refactoring
197: .isCreatingInstanceFieldMandatory() ? refactoring
198: .getEnclosingInstanceName() : "")); //$NON-NLS-1$
199: else
200: setChangeCreationCancelable(false);
201: }
202:
203: private MoveInnerToTopRefactoring getMoveRefactoring() {
204: return (MoveInnerToTopRefactoring) getRefactoring();
205: }
206: }
|