]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-before/ui refactoring/org/eclipse/jdt/internal/ui/refactoring/code/ReplaceInvocationsInputPage.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui refactoring / org / eclipse / jdt / internal / ui / refactoring / code / ReplaceInvocationsInputPage.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2000, 2011 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 *******************************************************************************/
11package org.eclipse.jdt.internal.ui.refactoring.code;
12
13import org.eclipse.swt.SWT;
14import org.eclipse.swt.events.SelectionAdapter;
15import org.eclipse.swt.events.SelectionEvent;
16import org.eclipse.swt.layout.GridData;
17import org.eclipse.swt.layout.GridLayout;
18import org.eclipse.swt.widgets.Button;
19import org.eclipse.swt.widgets.Composite;
20import org.eclipse.swt.widgets.Control;
21import org.eclipse.swt.widgets.Label;
22
23import org.eclipse.jface.dialogs.Dialog;
24import org.eclipse.jface.layout.PixelConverter;
25import org.eclipse.jface.preference.IPreferenceStore;
26import org.eclipse.jface.resource.JFaceResources;
27
28import org.eclipse.jface.text.Document;
29import org.eclipse.jface.text.DocumentEvent;
30import org.eclipse.jface.text.IDocumentListener;
31
32import org.eclipse.ltk.core.refactoring.RefactoringStatus;
33import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
34
35import org.eclipse.jdt.core.JavaModelException;
36
37import org.eclipse.jdt.internal.corext.refactoring.code.ReplaceInvocationsRefactoring;
38
39import org.eclipse.jdt.ui.JavaElementLabels;
40import org.eclipse.jdt.ui.PreferenceConstants;
41import org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration;
42
43import org.eclipse.jdt.internal.ui.JavaPlugin;
44import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer;
45import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
46
47
48public class ReplaceInvocationsInputPage extends UserInputWizardPage {
49
50 public static final String PAGE_NAME= "ReplaceInvocationsInputPage";//$NON-NLS-1$
51
52 private ReplaceInvocationsRefactoring fRefactoring;
53
54 private static final long LABEL_FLAGS= JavaElementLabels.M_PRE_TYPE_PARAMETERS | JavaElementLabels.M_PRE_RETURNTYPE | JavaElementLabels.M_PARAMETER_TYPES | JavaElementLabels.M_PARAMETER_NAMES | JavaElementLabels.M_EXCEPTIONS;
55
56 public ReplaceInvocationsInputPage() {
57 super(PAGE_NAME);
58 }
59
60 public void createControl(Composite parent) {
61 initializeDialogUnits(parent);
62 fRefactoring= (ReplaceInvocationsRefactoring) getRefactoring();
63
64 Composite result= new Composite(parent, SWT.NONE);
65 setControl(result);
66 GridLayout layout= new GridLayout();
67 result.setLayout(layout);
68
69 createMethodSignature(result);
70
71 Label separator= new Label(parent, SWT.NONE);
72 GridData gridData= new GridData(SWT.FILL, SWT.FILL, false, false);
73 gridData.heightHint= 5;
74 separator.setLayoutData(gridData);
75
76 Label bodyLabel= new Label(result, SWT.NONE);
77 bodyLabel.setText(RefactoringMessages.ReplaceInvocationsInputPage_replaceInvocationsBy);
78
79 createBody(result);
80
81 Button replaceAll= new Button(result, SWT.CHECK);
82 replaceAll.setText(RefactoringMessages.ReplaceInvocationsInputPage_replaceAll);
83 boolean canSingle= fRefactoring.canReplaceSingle();
84// replaceAll.setEnabled(canSingle);
85 replaceAll.setEnabled(false); // does not work for now...
86 replaceAll.setSelection(! canSingle);
87 replaceAll.addSelectionListener(new SelectionAdapter() {
88 @Override
89 public void widgetSelected(SelectionEvent event) {
90 boolean all= ((Button) event.widget).getSelection();
91 changeMode(all ? ReplaceInvocationsRefactoring.Mode.REPLACE_ALL : ReplaceInvocationsRefactoring.Mode.REPLACE_SINGLE);
92 }
93 });
94
95 Dialog.applyDialogFont(result);
96 }
97
98 private void createMethodSignature(Composite parent) {
99 IPreferenceStore store= JavaPlugin.getDefault().getCombinedPreferenceStore();
100 JavaSourceViewer signatureViewer= new JavaSourceViewer(parent, null, null, false, SWT.READ_ONLY | SWT.WRAP /*| SWT.BORDER*/, store);
101 signatureViewer.configure(new JavaSourceViewerConfiguration(JavaPlugin.getDefault().getJavaTextTools().getColorManager(), store, null, null));
102 signatureViewer.getTextWidget().setFont(JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT));
103 signatureViewer.adaptBackgroundColor(parent);
104 String signatureLabel= JavaElementLabels.getElementLabel(fRefactoring.getMethod(), LABEL_FLAGS);
105 signatureViewer.setDocument(new Document(signatureLabel));
106 signatureViewer.setEditable(false);
107
108 Control signatureControl= signatureViewer.getControl();
109 PixelConverter pixelConverter= new PixelConverter(signatureControl);
110 GridData gdata= new GridData(GridData.FILL_HORIZONTAL);
111 gdata.widthHint= pixelConverter.convertWidthInCharsToPixels(50);
112 signatureControl.setLayoutData(gdata);
113 }
114
115 private void createBody(Composite parent) {
116 IPreferenceStore store= JavaPlugin.getDefault().getCombinedPreferenceStore();
117 JavaSourceViewer bodyEditor= new JavaSourceViewer(parent, null, null, false, SWT.V_SCROLL | SWT.WRAP | SWT.BORDER, store);
118 bodyEditor.configure(new JavaSourceViewerConfiguration(JavaPlugin.getDefault().getJavaTextTools().getColorManager(), store, null, null));
119 bodyEditor.getTextWidget().setFont(JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT));
120 Document bodyDocument= new Document(getInitialBody());
121 bodyEditor.setDocument(bodyDocument);
122 bodyEditor.setEditable(true);
123
124 Control bodyControl= bodyEditor.getControl();
125 PixelConverter pixelConverter= new PixelConverter(bodyControl);
126 GridData gdata= new GridData(GridData.FILL_BOTH);
127 gdata.widthHint= pixelConverter.convertWidthInCharsToPixels(50);
128 gdata.minimumHeight= pixelConverter.convertHeightInCharsToPixels(5);
129 bodyControl.setLayoutData(gdata);
130 bodyControl.setFocus();
131
132 bodyDocument.addDocumentListener(new IDocumentListener() {
133 public void documentAboutToBeChanged(DocumentEvent event) {
134 }
135 public void documentChanged(DocumentEvent event) {
136 try {
137 fRefactoring.setBody(event.getDocument().get(), fRefactoring.getMethod().getParameterNames());
138 } catch (JavaModelException ex) {
139 // TODO Auto-generated catch block
140 JavaPlugin.log(ex);
141 }
142 }
143 });
144 }
145
146 private String getInitialBody() {
147 //TODO
148 return ""; //$NON-NLS-1$
149
150 }
151
152 private void changeMode(ReplaceInvocationsRefactoring.Mode mode) {
153 RefactoringStatus status;
154 try {
155 status= fRefactoring.setCurrentMode(mode);
156 } catch (JavaModelException e) {
157 status= RefactoringStatus.createFatalErrorStatus(e.getMessage());
158 }
159 setPageComplete(status);
160 }
161}