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