001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.refactoring.code;
011:
012: import org.eclipse.swt.SWT;
013: import org.eclipse.swt.events.SelectionAdapter;
014: import org.eclipse.swt.events.SelectionEvent;
015: import org.eclipse.swt.layout.GridData;
016: import org.eclipse.swt.layout.GridLayout;
017: import org.eclipse.swt.widgets.Button;
018: import org.eclipse.swt.widgets.Composite;
019: import org.eclipse.swt.widgets.Control;
020: import org.eclipse.swt.widgets.Label;
021:
022: import org.eclipse.jface.dialogs.Dialog;
023: import org.eclipse.jface.preference.IPreferenceStore;
024: import org.eclipse.jface.resource.JFaceResources;
025:
026: import org.eclipse.jface.text.Document;
027: import org.eclipse.jface.text.DocumentEvent;
028: import org.eclipse.jface.text.IDocumentListener;
029:
030: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
031: import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
032:
033: import org.eclipse.jdt.core.JavaModelException;
034:
035: import org.eclipse.jdt.internal.corext.refactoring.code.ReplaceInvocationsRefactoring;
036:
037: import org.eclipse.jdt.ui.JavaElementLabels;
038: import org.eclipse.jdt.ui.PreferenceConstants;
039: import org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration;
040:
041: import org.eclipse.jdt.internal.ui.JavaPlugin;
042: import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer;
043: import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
044: import org.eclipse.jdt.internal.ui.util.PixelConverter;
045:
046: public class ReplaceInvocationsInputPage extends UserInputWizardPage {
047:
048: public static final String PAGE_NAME = "ReplaceInvocationsInputPage";//$NON-NLS-1$
049:
050: private ReplaceInvocationsRefactoring fRefactoring;
051:
052: private static final long LABEL_FLAGS = JavaElementLabels.M_PRE_TYPE_PARAMETERS
053: | JavaElementLabels.M_PRE_RETURNTYPE
054: | JavaElementLabels.M_PARAMETER_TYPES
055: | JavaElementLabels.M_PARAMETER_NAMES
056: | JavaElementLabels.M_EXCEPTIONS;
057:
058: public ReplaceInvocationsInputPage() {
059: super (PAGE_NAME);
060: }
061:
062: public void createControl(Composite parent) {
063: initializeDialogUnits(parent);
064: fRefactoring = (ReplaceInvocationsRefactoring) getRefactoring();
065:
066: Composite result = new Composite(parent, SWT.NONE);
067: setControl(result);
068: GridLayout layout = new GridLayout();
069: result.setLayout(layout);
070:
071: createMethodSignature(result);
072:
073: Label separator = new Label(parent, SWT.NONE);
074: GridData gridData = new GridData(SWT.FILL, SWT.FILL, false,
075: false);
076: gridData.heightHint = 5;
077: separator.setLayoutData(gridData);
078:
079: Label bodyLabel = new Label(result, SWT.NONE);
080: bodyLabel
081: .setText(RefactoringMessages.ReplaceInvocationsInputPage_replaceInvocationsBy);
082:
083: createBody(result);
084:
085: Button replaceAll = new Button(result, SWT.CHECK);
086: replaceAll
087: .setText(RefactoringMessages.ReplaceInvocationsInputPage_replaceAll);
088: boolean canSingle = fRefactoring.canReplaceSingle();
089: // replaceAll.setEnabled(canSingle);
090: replaceAll.setEnabled(false); // does not work for now...
091: replaceAll.setSelection(!canSingle);
092: replaceAll.addSelectionListener(new SelectionAdapter() {
093: public void widgetSelected(SelectionEvent event) {
094: boolean all = ((Button) event.widget).getSelection();
095: changeMode(all ? ReplaceInvocationsRefactoring.Mode.REPLACE_ALL
096: : ReplaceInvocationsRefactoring.Mode.REPLACE_SINGLE);
097: }
098: });
099:
100: Dialog.applyDialogFont(result);
101: }
102:
103: private void createMethodSignature(Composite parent) {
104: IPreferenceStore store = JavaPlugin.getDefault()
105: .getCombinedPreferenceStore();
106: JavaSourceViewer signatureViewer = new JavaSourceViewer(parent,
107: null, null, false,
108: SWT.READ_ONLY | SWT.WRAP /*| SWT.BORDER*/, store);
109: signatureViewer.configure(new JavaSourceViewerConfiguration(
110: JavaPlugin.getDefault().getJavaTextTools()
111: .getColorManager(), store, null, null));
112: signatureViewer.getTextWidget().setFont(
113: JFaceResources
114: .getFont(PreferenceConstants.EDITOR_TEXT_FONT));
115: signatureViewer.getTextWidget().setBackground(
116: parent.getBackground());
117: String signatureLabel = JavaElementLabels.getElementLabel(
118: fRefactoring.getMethod(), LABEL_FLAGS);
119: signatureViewer.setDocument(new Document(signatureLabel));
120: signatureViewer.setEditable(false);
121:
122: Control signatureControl = signatureViewer.getControl();
123: PixelConverter pixelConverter = new PixelConverter(
124: signatureControl);
125: GridData gdata = new GridData(GridData.FILL_HORIZONTAL);
126: gdata.widthHint = pixelConverter
127: .convertWidthInCharsToPixels(50);
128: signatureControl.setLayoutData(gdata);
129: }
130:
131: private void createBody(Composite parent) {
132: IPreferenceStore store = JavaPlugin.getDefault()
133: .getCombinedPreferenceStore();
134: JavaSourceViewer bodyEditor = new JavaSourceViewer(parent,
135: null, null, false,
136: SWT.V_SCROLL | SWT.WRAP | SWT.BORDER, store);
137: bodyEditor.configure(new JavaSourceViewerConfiguration(
138: JavaPlugin.getDefault().getJavaTextTools()
139: .getColorManager(), store, null, null));
140: bodyEditor.getTextWidget().setFont(
141: JFaceResources
142: .getFont(PreferenceConstants.EDITOR_TEXT_FONT));
143: Document bodyDocument = new Document(getInitialBody());
144: bodyEditor.setDocument(bodyDocument);
145: bodyEditor.setEditable(true);
146:
147: Control bodyControl = bodyEditor.getControl();
148: PixelConverter pixelConverter = new PixelConverter(bodyControl);
149: GridData gdata = new GridData(GridData.FILL_BOTH);
150: gdata.widthHint = pixelConverter
151: .convertWidthInCharsToPixels(50);
152: gdata.minimumHeight = pixelConverter
153: .convertHeightInCharsToPixels(5);
154: bodyControl.setLayoutData(gdata);
155: bodyControl.setFocus();
156:
157: bodyDocument.addDocumentListener(new IDocumentListener() {
158: public void documentAboutToBeChanged(DocumentEvent event) {
159: }
160:
161: public void documentChanged(DocumentEvent event) {
162: try {
163: fRefactoring.setBody(event.getDocument().get(),
164: fRefactoring.getMethod()
165: .getParameterNames());
166: } catch (JavaModelException ex) {
167: // TODO Auto-generated catch block
168: JavaPlugin.log(ex);
169: }
170: }
171: });
172: }
173:
174: private String getInitialBody() {
175: //TODO
176: return ""; //$NON-NLS-1$
177:
178: }
179:
180: private void changeMode(ReplaceInvocationsRefactoring.Mode mode) {
181: RefactoringStatus status;
182: try {
183: status = fRefactoring.setCurrentMode(mode);
184: } catch (JavaModelException e) {
185: status = RefactoringStatus.createFatalErrorStatus(e
186: .getMessage());
187: }
188: setPageComplete(status);
189: }
190: }
|