KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > IntroduceFactoryInputPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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 org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.ModifyEvent;
15 import org.eclipse.swt.events.ModifyListener;
16 import org.eclipse.swt.events.SelectionAdapter;
17 import org.eclipse.swt.events.SelectionEvent;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Button;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Label;
23 import org.eclipse.swt.widgets.Text;
24
25 import org.eclipse.jface.window.Window;
26
27 import org.eclipse.ui.PlatformUI;
28
29 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
30 import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
31
32 import org.eclipse.jdt.core.IJavaElement;
33 import org.eclipse.jdt.core.IJavaProject;
34 import org.eclipse.jdt.core.IType;
35 import org.eclipse.jdt.core.search.IJavaSearchConstants;
36 import org.eclipse.jdt.core.search.IJavaSearchScope;
37 import org.eclipse.jdt.core.search.SearchEngine;
38
39 import org.eclipse.jdt.internal.corext.refactoring.code.IntroduceFactoryRefactoring;
40
41 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
42 import org.eclipse.jdt.internal.ui.dialogs.FilteredTypesSelectionDialog;
43 import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler;
44 import org.eclipse.jdt.internal.ui.util.SWTUtil;
45
46 /**
47  * @author rfuhrer
48  */

49 public class IntroduceFactoryInputPage extends UserInputWizardPage {
50     /**
51      * The name of the factory method to be created.
52      */

53     private Text fMethodName;
54     
55     private RefactoringStatus fMethodNameStatus;
56     private RefactoringStatus fDestinationStatus;
57
58     /**
59      * Constructor for IntroduceFactoryInputPage.
60      * @param name the name of the page
61      */

62     public IntroduceFactoryInputPage(String JavaDoc name) {
63         super(name);
64     }
65
66     private Text createTextInputField(Composite result) {
67         final Text textField = new Text(result, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
68         textField.selectAll();
69         TextFieldNavigationHandler.install(textField);
70         return textField;
71     }
72
73     /**
74      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
75      */

76     public void createControl(Composite parent) {
77         Composite result = new Composite(parent, SWT.NONE);
78
79         setControl(result);
80
81         GridLayout layout= new GridLayout();
82         layout.numColumns= 2;
83         result.setLayout(layout);
84
85         Label methNameLabel= new Label(result, SWT.NONE);
86         methNameLabel.setText(RefactoringMessages.IntroduceFactoryInputPage_method_name);
87         
88         fMethodName= createTextInputField(result);
89         GridData gd= new GridData(GridData.FILL_HORIZONTAL);
90         fMethodName.setLayoutData(gd);
91         fMethodName.setText(getUseFactoryRefactoring().getNewMethodName());
92
93         final Label factoryTypeLabel= new Label(result, SWT.NONE);
94         factoryTypeLabel.setText(RefactoringMessages.IntroduceFactoryInputPage_factoryClassLabel);
95         
96         Composite inner= new Composite(result, SWT.NONE);
97         GridLayout innerLayout= new GridLayout();
98         innerLayout.marginHeight= 0; innerLayout.marginWidth= 0;
99         innerLayout.numColumns= 2;
100         inner.setLayout(innerLayout);
101         inner.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
102         
103         final Text factoryTypeName= createTextInputField(inner);
104         factoryTypeName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
105         
106         final Button browseTypes= new Button(inner, SWT.PUSH);
107         browseTypes.setText(RefactoringMessages.IntroduceFactoryInputPage_browseLabel);
108         gd= new GridData();
109         gd.horizontalAlignment= GridData.END;
110         gd.widthHint = SWTUtil.getButtonWidthHint(browseTypes);
111         browseTypes.setLayoutData(gd);
112
113         final Button protectCtorCB= new Button(result, SWT.CHECK);
114         protectCtorCB.setText(RefactoringMessages.IntroduceFactoryInputPage_protectConstructorLabel);
115         gd= new GridData(GridData.FILL_HORIZONTAL);
116         gd.horizontalSpan= 2;
117         protectCtorCB.setLayoutData(gd);
118
119         fMethodName.addModifyListener(new ModifyListener() {
120             public void modifyText(ModifyEvent e) {
121                 fMethodNameStatus = getUseFactoryRefactoring().setNewMethodName(fMethodName.getText());
122                 validateInput(true);
123                 /*
124                 boolean nameOk= status.isOK();
125
126                 if (status.hasFatalError()) {
127                     IntroduceFactoryInputPage.this.setPageComplete(false);
128                     
129                 }
130                 IntroduceFactoryInputPage.this.setPageComplete(!status.hasFatalError());
131                 IntroduceFactoryInputPage.this.setErrorMessage(nameOk ?
132                     "" : //$NON-NLS-1$
133                     status.getMessageMatchingSeverity(RefactoringStatus.ERROR));
134                     */

135             }
136         });
137         protectCtorCB.addSelectionListener(new SelectionAdapter() {
138             public void widgetSelected(SelectionEvent e) {
139                 boolean isChecked = protectCtorCB.getSelection();
140
141                 getUseFactoryRefactoring().setProtectConstructor(isChecked);
142             }
143         });
144
145         factoryTypeName.addModifyListener(new ModifyListener() {
146             public void modifyText(ModifyEvent e) {
147                 fDestinationStatus= getUseFactoryRefactoring().setFactoryClass(factoryTypeName.getText());
148                 validateInput(false);
149                 /*
150                 boolean nameOk= status.isOK();
151
152                 IntroduceFactoryInputPage.this.setPageComplete(nameOk);
153                 IntroduceFactoryInputPage.this.setErrorMessage(nameOk ? "" : //$NON-NLS-1$
154                                                                status.getMessageMatchingSeverity(RefactoringStatus.ERROR));
155                                                                */

156             }
157         });
158         browseTypes.addSelectionListener(new SelectionAdapter() {
159             public void widgetSelected(SelectionEvent e) {
160                 IType factoryType= chooseFactoryClass();
161
162                 if (factoryType == null)
163                     return;
164
165                 RefactoringStatus status= getUseFactoryRefactoring().setFactoryClass(factoryType.getFullyQualifiedName());
166                 boolean nameOk= status.isOK();
167
168                 factoryTypeName.setText(factoryType.getFullyQualifiedName());
169                 IntroduceFactoryInputPage.this.setPageComplete(nameOk);
170                 IntroduceFactoryInputPage.this.setErrorMessage(nameOk ? "" : //$NON-NLS-1$
171
status.getMessageMatchingSeverity(RefactoringStatus.ERROR));
172             }
173         });
174
175         // Set up the initial state of the various dialog options.
176
if (getUseFactoryRefactoring().canProtectConstructor())
177             protectCtorCB.setSelection(true);
178         else {
179             protectCtorCB.setSelection(false);
180             protectCtorCB.setEnabled(false);
181             getUseFactoryRefactoring().setProtectConstructor(false);
182         }
183         factoryTypeName.setText(getUseFactoryRefactoring().getFactoryClassName());
184
185         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaHelpContextIds.INTRODUCE_FACTORY_WIZARD_PAGE);
186     }
187
188     private IType chooseFactoryClass() {
189         IJavaProject proj= getUseFactoryRefactoring().getProject();
190
191         if (proj == null)
192             return null;
193
194         IJavaElement[] elements= new IJavaElement[] { proj };
195         IJavaSearchScope scope= SearchEngine.createJavaSearchScope(elements);
196
197         FilteredTypesSelectionDialog dialog= new FilteredTypesSelectionDialog(
198             getShell(), false, getWizard().getContainer(), scope, IJavaSearchConstants.CLASS);
199
200         dialog.setTitle(RefactoringMessages.IntroduceFactoryInputPage_chooseFactoryClass_title);
201         dialog.setMessage(RefactoringMessages.IntroduceFactoryInputPage_chooseFactoryClass_message);
202
203         if (dialog.open() == Window.OK) {
204             return (IType) dialog.getFirstResult();
205         }
206         return null;
207     }
208
209     private IntroduceFactoryRefactoring getUseFactoryRefactoring() {
210         return (IntroduceFactoryRefactoring) getRefactoring();
211     }
212     
213     private void validateInput(boolean methodName) {
214         RefactoringStatus merged= new RefactoringStatus();
215         if (fMethodNameStatus != null && (methodName || fMethodNameStatus.hasError()))
216             merged.merge(fMethodNameStatus);
217         if (fDestinationStatus != null && (!methodName || fDestinationStatus.hasError()))
218             merged.merge(fDestinationStatus);
219         
220         setPageComplete(!merged.hasError());
221         int severity= merged.getSeverity();
222         String JavaDoc message= merged.getMessageMatchingSeverity(severity);
223         if (severity >= RefactoringStatus.INFO) {
224             setMessage(message, severity);
225         } else {
226             setMessage("", NONE); //$NON-NLS-1$
227
}
228     }
229  }
230
Popular Tags