001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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.core.runtime.Assert;
013:
014: import org.eclipse.swt.SWT;
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.Control;
022: import org.eclipse.swt.widgets.Label;
023:
024: import org.eclipse.jface.dialogs.Dialog;
025: import org.eclipse.jface.preference.IPreferenceStore;
026: import org.eclipse.jface.resource.JFaceResources;
027:
028: import org.eclipse.jface.text.Document;
029:
030: import org.eclipse.ui.PlatformUI;
031:
032: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
033: import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
034: import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
035:
036: import org.eclipse.jdt.core.JavaModelException;
037:
038: import org.eclipse.jdt.internal.corext.refactoring.ParameterInfo;
039: import org.eclipse.jdt.internal.corext.refactoring.code.IntroduceParameterRefactoring;
040:
041: import org.eclipse.jdt.ui.PreferenceConstants;
042: import org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration;
043:
044: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
045: import org.eclipse.jdt.internal.ui.JavaPlugin;
046: import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer;
047: import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
048: import org.eclipse.jdt.internal.ui.util.PixelConverter;
049:
050: public class IntroduceParameterWizard extends RefactoringWizard {
051:
052: public IntroduceParameterWizard(IntroduceParameterRefactoring ref) {
053: super (ref, DIALOG_BASED_USER_INTERFACE
054: | PREVIEW_EXPAND_FIRST_NODE);
055: setDefaultPageTitle(RefactoringMessages.IntroduceParameterWizard_defaultPageTitle);
056: }
057:
058: /* non java-doc
059: * @see RefactoringWizard#addUserInputPages
060: */
061: protected void addUserInputPages() {
062: addPage(new IntroduceParameterInputPage(
063: getIntroduceParameterRefactoring()
064: .guessParameterNames()));
065: }
066:
067: private IntroduceParameterRefactoring getIntroduceParameterRefactoring() {
068: return (IntroduceParameterRefactoring) getRefactoring();
069: }
070:
071: private static class IntroduceParameterInputPage extends
072: UserInputWizardPage {
073:
074: private static final String DESCRIPTION = RefactoringMessages.IntroduceParameterInputPage_description;
075: public static final String PAGE_NAME = "IntroduceParameterInputPage";//$NON-NLS-1$
076: private String[] fParamNameProposals;
077:
078: private JavaSourceViewer fSignaturePreview;
079: private Document fSignaturePreviewDocument;
080: private Button fLeaveDelegateCheckBox;
081: private Button fDeprecateDelegateCheckBox;
082:
083: public IntroduceParameterInputPage(String[] tempNameProposals) {
084: super (PAGE_NAME);
085: setDescription(DESCRIPTION);
086: Assert.isNotNull(tempNameProposals);
087: fParamNameProposals = tempNameProposals;
088: fSignaturePreviewDocument = new Document();
089: }
090:
091: private IntroduceParameterRefactoring getIntroduceParameterRefactoring() {
092: return (IntroduceParameterRefactoring) getRefactoring();
093: }
094:
095: public void createControl(Composite parent) {
096: Composite result = new Composite(parent, SWT.NONE);
097: setControl(result);
098: GridLayout layout = new GridLayout();
099: result.setLayout(layout);
100:
101: createParameterTableControl(result);
102: fLeaveDelegateCheckBox = DelegateUIHelper
103: .generateLeaveDelegateCheckbox(result,
104: getRefactoring(), false);
105: if (fLeaveDelegateCheckBox != null) {
106: fDeprecateDelegateCheckBox = new Button(result,
107: SWT.CHECK);
108: GridData data = new GridData();
109: data.horizontalAlignment = GridData.FILL;
110: data.horizontalIndent = (layout.marginWidth + fDeprecateDelegateCheckBox
111: .computeSize(SWT.DEFAULT, SWT.DEFAULT).x);
112: fDeprecateDelegateCheckBox.setLayoutData(data);
113: fDeprecateDelegateCheckBox.setText(DelegateUIHelper
114: .getDeprecateDelegateCheckBoxTitle());
115: final IntroduceParameterRefactoring refactoring = getIntroduceParameterRefactoring();
116: fDeprecateDelegateCheckBox
117: .setSelection(DelegateUIHelper
118: .loadDeprecateDelegateSetting(refactoring));
119: refactoring
120: .setDeprecateDelegates(fDeprecateDelegateCheckBox
121: .getSelection());
122: fDeprecateDelegateCheckBox
123: .addSelectionListener(new SelectionAdapter() {
124: public void widgetSelected(SelectionEvent e) {
125: refactoring
126: .setDeprecateDelegates(fDeprecateDelegateCheckBox
127: .getSelection());
128: }
129: });
130: fDeprecateDelegateCheckBox
131: .setEnabled(fLeaveDelegateCheckBox
132: .getSelection());
133: fLeaveDelegateCheckBox
134: .addSelectionListener(new SelectionAdapter() {
135: public void widgetSelected(SelectionEvent e) {
136: fDeprecateDelegateCheckBox
137: .setEnabled(fLeaveDelegateCheckBox
138: .getSelection());
139: }
140: });
141: }
142: Label sep = new Label(result, SWT.SEPARATOR
143: | SWT.HORIZONTAL);
144: sep.setLayoutData((new GridData(GridData.FILL_HORIZONTAL)));
145: createSignaturePreview(result);
146:
147: update(false);
148: Dialog.applyDialogFont(result);
149: PlatformUI
150: .getWorkbench()
151: .getHelpSystem()
152: .setHelp(
153: getControl(),
154: IJavaHelpContextIds.INTRODUCE_PARAMETER_WIZARD_PAGE);
155: }
156:
157: private ChangeParametersControl createParameterTableControl(
158: Composite composite) {
159: String labelText = RefactoringMessages.IntroduceParameterWizard_parameters;
160: ChangeParametersControl cp = new ChangeParametersControl(
161: composite, SWT.NONE, labelText,
162: new IParameterListChangeListener() {
163: public void parameterChanged(
164: ParameterInfo parameter) {
165: update(true);
166: }
167:
168: public void parameterListChanged() {
169: update(true);
170: }
171:
172: public void parameterAdded(
173: ParameterInfo parameter) {
174: update(true);
175: }
176: },
177: ChangeParametersControl.Mode.INTRODUCE_PARAMETER,
178: fParamNameProposals);
179: cp.setLayoutData(new GridData(GridData.FILL_BOTH));
180: cp.setInput(getIntroduceParameterRefactoring()
181: .getParameterInfos());
182: cp.editParameter(getIntroduceParameterRefactoring()
183: .getAddedParameterInfo());
184: return cp;
185: }
186:
187: public void dispose() {
188: DelegateUIHelper
189: .saveLeaveDelegateSetting(fLeaveDelegateCheckBox);
190: DelegateUIHelper
191: .saveDeprecateDelegateSetting(fDeprecateDelegateCheckBox);
192: super .dispose();
193: }
194:
195: private void createSignaturePreview(Composite composite) {
196: Label previewLabel = new Label(composite, SWT.NONE);
197: previewLabel
198: .setText(RefactoringMessages.ChangeSignatureInputPage_method_Signature_Preview);
199:
200: IPreferenceStore store = JavaPlugin.getDefault()
201: .getCombinedPreferenceStore();
202: fSignaturePreview = new JavaSourceViewer(composite, null,
203: null, false, SWT.READ_ONLY | SWT.V_SCROLL
204: | SWT.WRAP /*| SWT.BORDER*/, store);
205: fSignaturePreview
206: .configure(new JavaSourceViewerConfiguration(
207: JavaPlugin.getDefault().getJavaTextTools()
208: .getColorManager(), store, null,
209: null));
210: fSignaturePreview
211: .getTextWidget()
212: .setFont(
213: JFaceResources
214: .getFont(PreferenceConstants.EDITOR_TEXT_FONT));
215: fSignaturePreview.getTextWidget().setBackground(
216: composite.getBackground());
217: fSignaturePreview.setDocument(fSignaturePreviewDocument);
218: fSignaturePreview.setEditable(false);
219:
220: //Layouting problems with wrapped text: see https://bugs.eclipse.org/bugs/show_bug.cgi?id=9866
221: Control signaturePreviewControl = fSignaturePreview
222: .getControl();
223: PixelConverter pixelConverter = new PixelConverter(
224: signaturePreviewControl);
225: GridData gdata = new GridData(GridData.FILL_BOTH);
226: gdata.widthHint = pixelConverter
227: .convertWidthInCharsToPixels(50);
228: gdata.heightHint = pixelConverter
229: .convertHeightInCharsToPixels(2);
230: signaturePreviewControl.setLayoutData(gdata);
231: }
232:
233: private void update(boolean displayErrorMessage) {
234: updateStatus(displayErrorMessage);
235: updateSignaturePreview();
236: }
237:
238: private void updateStatus(boolean displayErrorMessage) {
239: RefactoringStatus nameCheck = getIntroduceParameterRefactoring()
240: .validateInput();
241: if (displayErrorMessage) {
242: setPageComplete(nameCheck);
243: } else {
244: setErrorMessage(null);
245: setPageComplete(true);
246: }
247: }
248:
249: private void updateSignaturePreview() {
250: try {
251: int top = fSignaturePreview.getTextWidget()
252: .getTopPixel();
253: fSignaturePreviewDocument
254: .set(getIntroduceParameterRefactoring()
255: .getMethodSignaturePreview());
256: fSignaturePreview.getTextWidget().setTopPixel(top);
257: } catch (JavaModelException e) {
258: ExceptionHandler
259: .handle(
260: e,
261: RefactoringMessages.IntroduceParameterWizard_defaultPageTitle,
262: RefactoringMessages.ChangeSignatureInputPage_exception);
263: }
264: }
265:
266: }
267: }
|