001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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;
011:
012: import org.eclipse.swt.SWT;
013: import org.eclipse.swt.events.ModifyEvent;
014: import org.eclipse.swt.events.ModifyListener;
015: import org.eclipse.swt.events.SelectionAdapter;
016: import org.eclipse.swt.events.SelectionEvent;
017: import org.eclipse.swt.layout.GridData;
018: import org.eclipse.swt.layout.GridLayout;
019: import org.eclipse.swt.widgets.Button;
020: import org.eclipse.swt.widgets.Composite;
021: import org.eclipse.swt.widgets.Label;
022: import org.eclipse.swt.widgets.Text;
023:
024: import org.eclipse.jface.window.Window;
025:
026: import org.eclipse.ui.PlatformUI;
027:
028: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
029: import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
030:
031: import org.eclipse.jdt.core.IJavaElement;
032: import org.eclipse.jdt.core.IJavaProject;
033: import org.eclipse.jdt.core.IType;
034: import org.eclipse.jdt.core.search.IJavaSearchConstants;
035: import org.eclipse.jdt.core.search.IJavaSearchScope;
036: import org.eclipse.jdt.core.search.SearchEngine;
037:
038: import org.eclipse.jdt.internal.corext.refactoring.code.IntroduceFactoryRefactoring;
039:
040: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
041: import org.eclipse.jdt.internal.ui.dialogs.FilteredTypesSelectionDialog;
042: import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler;
043: import org.eclipse.jdt.internal.ui.util.SWTUtil;
044:
045: /**
046: * @author rfuhrer
047: */
048: public class IntroduceFactoryInputPage extends UserInputWizardPage {
049: /**
050: * The name of the factory method to be created.
051: */
052: private Text fMethodName;
053:
054: private RefactoringStatus fMethodNameStatus;
055: private RefactoringStatus fDestinationStatus;
056:
057: /**
058: * Constructor for IntroduceFactoryInputPage.
059: * @param name the name of the page
060: */
061: public IntroduceFactoryInputPage(String name) {
062: super (name);
063: }
064:
065: private Text createTextInputField(Composite result) {
066: final Text textField = new Text(result, SWT.SINGLE | SWT.LEFT
067: | SWT.BORDER);
068: textField.selectAll();
069: TextFieldNavigationHandler.install(textField);
070: return textField;
071: }
072:
073: /**
074: * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
075: */
076: public void createControl(Composite parent) {
077: Composite result = new Composite(parent, SWT.NONE);
078:
079: setControl(result);
080:
081: GridLayout layout = new GridLayout();
082: layout.numColumns = 2;
083: result.setLayout(layout);
084:
085: Label methNameLabel = new Label(result, SWT.NONE);
086: methNameLabel
087: .setText(RefactoringMessages.IntroduceFactoryInputPage_method_name);
088:
089: fMethodName = createTextInputField(result);
090: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
091: fMethodName.setLayoutData(gd);
092: fMethodName.setText(getUseFactoryRefactoring()
093: .getNewMethodName());
094:
095: final Label factoryTypeLabel = new Label(result, SWT.NONE);
096: factoryTypeLabel
097: .setText(RefactoringMessages.IntroduceFactoryInputPage_factoryClassLabel);
098:
099: Composite inner = new Composite(result, SWT.NONE);
100: GridLayout innerLayout = new GridLayout();
101: innerLayout.marginHeight = 0;
102: innerLayout.marginWidth = 0;
103: innerLayout.numColumns = 2;
104: inner.setLayout(innerLayout);
105: inner.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
106:
107: final Text factoryTypeName = createTextInputField(inner);
108: factoryTypeName.setLayoutData(new GridData(
109: GridData.FILL_HORIZONTAL));
110:
111: final Button browseTypes = new Button(inner, SWT.PUSH);
112: browseTypes
113: .setText(RefactoringMessages.IntroduceFactoryInputPage_browseLabel);
114: gd = new GridData();
115: gd.horizontalAlignment = GridData.END;
116: gd.widthHint = SWTUtil.getButtonWidthHint(browseTypes);
117: browseTypes.setLayoutData(gd);
118:
119: final Button protectCtorCB = new Button(result, SWT.CHECK);
120: protectCtorCB
121: .setText(RefactoringMessages.IntroduceFactoryInputPage_protectConstructorLabel);
122: gd = new GridData(GridData.FILL_HORIZONTAL);
123: gd.horizontalSpan = 2;
124: protectCtorCB.setLayoutData(gd);
125:
126: fMethodName.addModifyListener(new ModifyListener() {
127: public void modifyText(ModifyEvent e) {
128: fMethodNameStatus = getUseFactoryRefactoring()
129: .setNewMethodName(fMethodName.getText());
130: validateInput(true);
131: /*
132: boolean nameOk= status.isOK();
133:
134: if (status.hasFatalError()) {
135: IntroduceFactoryInputPage.this.setPageComplete(false);
136:
137: }
138: IntroduceFactoryInputPage.this.setPageComplete(!status.hasFatalError());
139: IntroduceFactoryInputPage.this.setErrorMessage(nameOk ?
140: "" : //$NON-NLS-1$
141: status.getMessageMatchingSeverity(RefactoringStatus.ERROR));
142: */
143: }
144: });
145: protectCtorCB.addSelectionListener(new SelectionAdapter() {
146: public void widgetSelected(SelectionEvent e) {
147: boolean isChecked = protectCtorCB.getSelection();
148:
149: getUseFactoryRefactoring().setProtectConstructor(
150: isChecked);
151: }
152: });
153:
154: factoryTypeName.addModifyListener(new ModifyListener() {
155: public void modifyText(ModifyEvent e) {
156: fDestinationStatus = getUseFactoryRefactoring()
157: .setFactoryClass(factoryTypeName.getText());
158: validateInput(false);
159: /*
160: boolean nameOk= status.isOK();
161:
162: IntroduceFactoryInputPage.this.setPageComplete(nameOk);
163: IntroduceFactoryInputPage.this.setErrorMessage(nameOk ? "" : //$NON-NLS-1$
164: status.getMessageMatchingSeverity(RefactoringStatus.ERROR));
165: */
166: }
167: });
168: browseTypes.addSelectionListener(new SelectionAdapter() {
169: public void widgetSelected(SelectionEvent e) {
170: IType factoryType = chooseFactoryClass();
171:
172: if (factoryType == null)
173: return;
174:
175: RefactoringStatus status = getUseFactoryRefactoring()
176: .setFactoryClass(
177: factoryType.getFullyQualifiedName());
178: boolean nameOk = status.isOK();
179:
180: factoryTypeName.setText(factoryType
181: .getFullyQualifiedName());
182: IntroduceFactoryInputPage.this .setPageComplete(nameOk);
183: IntroduceFactoryInputPage.this
184: .setErrorMessage(nameOk ? "" : //$NON-NLS-1$
185: status
186: .getMessageMatchingSeverity(RefactoringStatus.ERROR));
187: }
188: });
189:
190: // Set up the initial state of the various dialog options.
191: if (getUseFactoryRefactoring().canProtectConstructor())
192: protectCtorCB.setSelection(true);
193: else {
194: protectCtorCB.setSelection(false);
195: protectCtorCB.setEnabled(false);
196: getUseFactoryRefactoring().setProtectConstructor(false);
197: }
198: factoryTypeName.setText(getUseFactoryRefactoring()
199: .getFactoryClassName());
200:
201: PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
202: IJavaHelpContextIds.INTRODUCE_FACTORY_WIZARD_PAGE);
203: }
204:
205: private IType chooseFactoryClass() {
206: IJavaProject proj = getUseFactoryRefactoring().getProject();
207:
208: if (proj == null)
209: return null;
210:
211: IJavaElement[] elements = new IJavaElement[] { proj };
212: IJavaSearchScope scope = SearchEngine
213: .createJavaSearchScope(elements);
214:
215: FilteredTypesSelectionDialog dialog = new FilteredTypesSelectionDialog(
216: getShell(), false, getWizard().getContainer(), scope,
217: IJavaSearchConstants.CLASS);
218:
219: dialog
220: .setTitle(RefactoringMessages.IntroduceFactoryInputPage_chooseFactoryClass_title);
221: dialog
222: .setMessage(RefactoringMessages.IntroduceFactoryInputPage_chooseFactoryClass_message);
223:
224: if (dialog.open() == Window.OK) {
225: return (IType) dialog.getFirstResult();
226: }
227: return null;
228: }
229:
230: private IntroduceFactoryRefactoring getUseFactoryRefactoring() {
231: return (IntroduceFactoryRefactoring) getRefactoring();
232: }
233:
234: private void validateInput(boolean methodName) {
235: RefactoringStatus merged = new RefactoringStatus();
236: if (fMethodNameStatus != null
237: && (methodName || fMethodNameStatus.hasError()))
238: merged.merge(fMethodNameStatus);
239: if (fDestinationStatus != null
240: && (!methodName || fDestinationStatus.hasError()))
241: merged.merge(fDestinationStatus);
242:
243: setPageComplete(!merged.hasError());
244: int severity = merged.getSeverity();
245: String message = merged.getMessageMatchingSeverity(severity);
246: if (severity >= RefactoringStatus.INFO) {
247: setMessage(message, severity);
248: } else {
249: setMessage("", NONE); //$NON-NLS-1$
250: }
251: }
252: }
|