]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui refactoring/org/eclipse/jdt/internal/ui/refactoring/IntroduceIndirectionInputPage.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui refactoring / org / eclipse / jdt / internal / ui / refactoring / IntroduceIndirectionInputPage.java
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  *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.refactoring;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.ModifyEvent;
18 import org.eclipse.swt.events.ModifyListener;
19 import org.eclipse.swt.events.SelectionAdapter;
20 import org.eclipse.swt.events.SelectionEvent;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Combo;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Label;
27 import org.eclipse.swt.widgets.Text;
28
29 import org.eclipse.jface.wizard.IWizardPage;
30
31 import org.eclipse.ui.PlatformUI;
32
33 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
34 import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
35
36 import org.eclipse.jdt.core.IJavaElement;
37 import org.eclipse.jdt.core.IJavaProject;
38 import org.eclipse.jdt.core.IType;
39 import org.eclipse.jdt.core.search.IJavaSearchConstants;
40 import org.eclipse.jdt.core.search.IJavaSearchScope;
41 import org.eclipse.jdt.core.search.SearchEngine;
42
43 import org.eclipse.jdt.internal.corext.refactoring.code.IntroduceIndirectionRefactoring;
44
45 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
46 import org.eclipse.jdt.internal.ui.dialogs.FilteredTypesSelectionDialog;
47 import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler;
48 import org.eclipse.jdt.internal.ui.refactoring.contentassist.JavaTypeCompletionProcessor;
49 import org.eclipse.jdt.internal.ui.util.SWTUtil;
50
51 /**
52  * @since 3.2
53  */
54 public class IntroduceIndirectionInputPage extends UserInputWizardPage {
55
56         /**
57          * The name of the intermediary method to be created.
58          */
59         private Text fIntermediaryMethodName;
60
61         private Combo fIntermediaryTypeName;
62         private static final int INTERMEDIARY_TYPE_COUNT= 10;
63         private static List<String> fgIntermediaryTypes= new ArrayList<String>(INTERMEDIARY_TYPE_COUNT);
64
65         /**
66          * Constructor for IntroduceIndirectionInputPage.
67          * @param name the name of the page
68          */
69         public IntroduceIndirectionInputPage(String name) {
70                 super(name);
71         }
72
73         private Text createIntermediaryNameCombo(Composite result) {
74                 final Text textField= new Text(result, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
75                 textField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
76                 TextFieldNavigationHandler.install(textField);
77                 return textField;
78         }
79
80         private Combo createIntermediaryTypeCombo(Composite composite) {
81                 final Combo textCombo= new Combo(composite, SWT.SINGLE | SWT.BORDER);
82                 textCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
83                 textCombo.setItems(fgIntermediaryTypes.toArray(new String[fgIntermediaryTypes.size()]));
84                 textCombo.setVisibleItemCount(INTERMEDIARY_TYPE_COUNT);
85
86                 JavaTypeCompletionProcessor processor= new JavaTypeCompletionProcessor(false, false, true);
87                 processor.generated_4678007740975691192(textCombo, this);
88                 return textCombo;
89         }
90
91         /**
92          * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
93          */
94         public void createControl(Composite parent) {
95                 Composite result= new Composite(parent, SWT.NONE);
96
97                 setControl(result);
98
99                 GridLayout layout= new GridLayout();
100                 layout.numColumns= 2;
101                 result.setLayout(layout);
102
103                 Label methNameLabel= new Label(result, SWT.NONE);
104                 methNameLabel.setText(RefactoringMessages.IntroduceIndirectionInputPage_new_method_name);
105
106                 fIntermediaryMethodName= createIntermediaryNameCombo(result);
107
108                 final Label intermediaryTypeLabel= new Label(result, SWT.NONE);
109                 intermediaryTypeLabel.setText(RefactoringMessages.IntroduceIndirectionInputPage_declaring_class);
110
111                 Composite inner= new Composite(result, SWT.NONE);
112                 GridLayout innerLayout= new GridLayout();
113                 innerLayout.marginHeight= 0;
114                 innerLayout.marginWidth= 0;
115                 innerLayout.numColumns= 2;
116                 inner.setLayout(innerLayout);
117                 inner.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
118
119                 fIntermediaryTypeName= createIntermediaryTypeCombo(inner);
120                 fIntermediaryTypeName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
121
122                 final Button browseTypes= new Button(inner, SWT.PUSH);
123                 browseTypes.setText(RefactoringMessages.IntroduceIndirectionInputPage_browse);
124                 GridData gd= new GridData();
125                 gd.horizontalAlignment= GridData.END;
126                 gd.widthHint= SWTUtil.getButtonWidthHint(browseTypes);
127                 browseTypes.setLayoutData(gd);
128
129                 final Button enableReferencesCheckBox= new Button(result, SWT.CHECK);
130                 enableReferencesCheckBox.setText(RefactoringMessages.IntroduceIndirectionInputPage_update_references);
131                 gd= new GridData(GridData.FILL_HORIZONTAL);
132                 gd.horizontalSpan= 2;
133                 gd.verticalIndent= 2;
134                 enableReferencesCheckBox.setLayoutData(gd);
135
136                 fIntermediaryMethodName.setText(getIntroduceIndirectionRefactoring().getIntermediaryMethodName());
137                 fIntermediaryTypeName.setText(getIntroduceIndirectionRefactoring().getIntermediaryClassName());
138
139                 fIntermediaryMethodName.addModifyListener(new ModifyListener() {
140                         public void modifyText(ModifyEvent e) {
141                                 validateInput();
142                         }
143                 });
144
145                 enableReferencesCheckBox.addSelectionListener(new SelectionAdapter() {
146                         @Override
147                         public void widgetSelected(SelectionEvent e) {
148                                 getIntroduceIndirectionRefactoring().setEnableUpdateReferences(enableReferencesCheckBox.getSelection());
149                         }
150                 });
151
152                 fIntermediaryTypeName.addModifyListener(new ModifyListener() {
153                         public void modifyText(ModifyEvent e) {
154                                 validateInput();
155                         }
156                 });
157
158                 browseTypes.addSelectionListener(new SelectionAdapter() {
159                         @Override
160                         public void widgetSelected(SelectionEvent e) {
161                                 IType intermediaryType= chooseIntermediaryClass();
162
163                                 if (intermediaryType == null)
164                                         return;
165
166                                 fIntermediaryTypeName.setText(intermediaryType.getFullyQualifiedName('.'));
167                         }
168                 });
169
170                 if (getIntroduceIndirectionRefactoring().canEnableUpdateReferences())
171                         enableReferencesCheckBox.setSelection(true);
172                 else {
173                         enableReferencesCheckBox.setSelection(false);
174                         enableReferencesCheckBox.setEnabled(false);
175                         getIntroduceIndirectionRefactoring().setEnableUpdateReferences(false);
176                 }
177
178                 fIntermediaryMethodName.setFocus();
179                 fIntermediaryMethodName.selectAll();
180                 validateInput();
181
182                 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaHelpContextIds.INTRODUCE_INDIRECTION_WIZARD_PAGE);
183         }
184
185         private IType chooseIntermediaryClass() {
186                 IJavaProject proj= getIntroduceIndirectionRefactoring().getProject();
187
188                 if (proj == null)
189                         return null;
190
191                 IJavaElement[] elements= new IJavaElement[] { proj };
192                 IJavaSearchScope scope= SearchEngine.createJavaSearchScope(elements);
193
194                 FilteredTypesSelectionDialog dialog= new FilteredTypesSelectionDialog(getShell(), false, getWizard().getContainer(), scope, IJavaSearchConstants.CLASS);
195
196                 return dialog.generated_1810288139041559918();
197         }
198
199         public IntroduceIndirectionRefactoring getIntroduceIndirectionRefactoring() {
200                 return (IntroduceIndirectionRefactoring) getRefactoring();
201         }
202
203         private void validateInput() {
204                 RefactoringStatus merged= new RefactoringStatus();
205                 merged.merge(getIntroduceIndirectionRefactoring().setIntermediaryClassName(fIntermediaryTypeName.getText()));
206                 merged.merge(getIntroduceIndirectionRefactoring().setIntermediaryMethodName(fIntermediaryMethodName.getText()));
207
208                 setPageComplete(!merged.hasError());
209                 int severity= merged.getSeverity();
210                 String message= merged.getMessageMatchingSeverity(severity);
211                 if (severity >= RefactoringStatus.INFO) {
212                         setMessage(message, severity);
213                 } else {
214                         setMessage("", NONE); //$NON-NLS-1$
215                 }
216         }
217
218         @Override
219         protected boolean performFinish() {
220                 storeIntermediaryTypeName();
221                 return super.performFinish();
222         }
223
224         @Override
225         public IWizardPage getNextPage() {
226                 storeIntermediaryTypeName();
227                 return super.getNextPage();
228         }
229
230         private void storeIntermediaryTypeName() {
231                 String destination= fIntermediaryTypeName.getText();
232                 if (!fgIntermediaryTypes.remove(destination) && fgIntermediaryTypes.size() >= INTERMEDIARY_TYPE_COUNT)
233                         fgIntermediaryTypes.remove(fgIntermediaryTypes.size() - 1);
234                 fgIntermediaryTypes.add(0, destination);
235         }
236 }