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 java.util.ArrayList;
013: import java.util.List;
014:
015: import org.eclipse.swt.SWT;
016: import org.eclipse.swt.events.ModifyEvent;
017: import org.eclipse.swt.events.ModifyListener;
018: import org.eclipse.swt.events.SelectionAdapter;
019: import org.eclipse.swt.events.SelectionEvent;
020: import org.eclipse.swt.layout.GridData;
021: import org.eclipse.swt.layout.GridLayout;
022: import org.eclipse.swt.widgets.Button;
023: import org.eclipse.swt.widgets.Combo;
024: import org.eclipse.swt.widgets.Composite;
025: import org.eclipse.swt.widgets.Label;
026: import org.eclipse.swt.widgets.Text;
027:
028: import org.eclipse.jface.window.Window;
029: import org.eclipse.jface.wizard.IWizardPage;
030:
031: import org.eclipse.ui.PlatformUI;
032:
033: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
034: import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
035:
036: import org.eclipse.jdt.core.IJavaElement;
037: import org.eclipse.jdt.core.IJavaProject;
038: import org.eclipse.jdt.core.IType;
039: import org.eclipse.jdt.core.search.IJavaSearchConstants;
040: import org.eclipse.jdt.core.search.IJavaSearchScope;
041: import org.eclipse.jdt.core.search.SearchEngine;
042:
043: import org.eclipse.jdt.internal.corext.refactoring.code.IntroduceIndirectionRefactoring;
044:
045: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
046: import org.eclipse.jdt.internal.ui.dialogs.FilteredTypesSelectionDialog;
047: import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler;
048: import org.eclipse.jdt.internal.ui.refactoring.contentassist.ControlContentAssistHelper;
049: import org.eclipse.jdt.internal.ui.refactoring.contentassist.JavaTypeCompletionProcessor;
050: import org.eclipse.jdt.internal.ui.util.SWTUtil;
051:
052: /**
053: * @since 3.2
054: */
055: public class IntroduceIndirectionInputPage extends UserInputWizardPage {
056:
057: /**
058: * The name of the intermediary method to be created.
059: */
060: private Text fIntermediaryMethodName;
061:
062: private Combo fIntermediaryTypeName;
063: private static final int INTERMEDIARY_TYPE_COUNT = 10;
064: private static List fgIntermediaryTypes = new ArrayList(
065: INTERMEDIARY_TYPE_COUNT);
066:
067: /**
068: * Constructor for IntroduceIndirectionInputPage.
069: * @param name the name of the page
070: */
071: public IntroduceIndirectionInputPage(String name) {
072: super (name);
073: }
074:
075: private Text createIntermediaryNameCombo(Composite result) {
076: final Text textField = new Text(result, SWT.SINGLE | SWT.LEFT
077: | SWT.BORDER);
078: textField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
079: TextFieldNavigationHandler.install(textField);
080: return textField;
081: }
082:
083: private Combo createIntermediaryTypeCombo(Composite composite) {
084: final Combo textCombo = new Combo(composite, SWT.SINGLE
085: | SWT.BORDER);
086: textCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
087: textCombo.setItems((String[]) fgIntermediaryTypes
088: .toArray(new String[fgIntermediaryTypes.size()]));
089: textCombo.setVisibleItemCount(INTERMEDIARY_TYPE_COUNT);
090:
091: JavaTypeCompletionProcessor processor = new JavaTypeCompletionProcessor(
092: false, false, true);
093: processor
094: .setPackageFragment(getIntroduceIndirectionRefactoring()
095: .getInvocationPackage());
096: ControlContentAssistHelper.createComboContentAssistant(
097: textCombo, processor);
098: TextFieldNavigationHandler.install(textCombo);
099: return textCombo;
100: }
101:
102: /**
103: * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
104: */
105: public void createControl(Composite parent) {
106: Composite result = new Composite(parent, SWT.NONE);
107:
108: setControl(result);
109:
110: GridLayout layout = new GridLayout();
111: layout.numColumns = 2;
112: result.setLayout(layout);
113:
114: Label methNameLabel = new Label(result, SWT.NONE);
115: methNameLabel
116: .setText(RefactoringMessages.IntroduceIndirectionInputPage_new_method_name);
117:
118: fIntermediaryMethodName = createIntermediaryNameCombo(result);
119:
120: final Label intermediaryTypeLabel = new Label(result, SWT.NONE);
121: intermediaryTypeLabel
122: .setText(RefactoringMessages.IntroduceIndirectionInputPage_declaring_class);
123:
124: Composite inner = new Composite(result, SWT.NONE);
125: GridLayout innerLayout = new GridLayout();
126: innerLayout.marginHeight = 0;
127: innerLayout.marginWidth = 0;
128: innerLayout.numColumns = 2;
129: inner.setLayout(innerLayout);
130: inner.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
131:
132: fIntermediaryTypeName = createIntermediaryTypeCombo(inner);
133: fIntermediaryTypeName.setLayoutData(new GridData(
134: GridData.FILL_HORIZONTAL));
135:
136: final Button browseTypes = new Button(inner, SWT.PUSH);
137: browseTypes
138: .setText(RefactoringMessages.IntroduceIndirectionInputPage_browse);
139: GridData gd = new GridData();
140: gd.horizontalAlignment = GridData.END;
141: gd.widthHint = SWTUtil.getButtonWidthHint(browseTypes);
142: browseTypes.setLayoutData(gd);
143:
144: final Button enableReferencesCheckBox = new Button(result,
145: SWT.CHECK);
146: enableReferencesCheckBox
147: .setText(RefactoringMessages.IntroduceIndirectionInputPage_update_references);
148: gd = new GridData(GridData.FILL_HORIZONTAL);
149: gd.horizontalSpan = 2;
150: gd.verticalIndent = 2;
151: enableReferencesCheckBox.setLayoutData(gd);
152:
153: fIntermediaryMethodName
154: .setText(getIntroduceIndirectionRefactoring()
155: .getIntermediaryMethodName());
156: fIntermediaryTypeName
157: .setText(getIntroduceIndirectionRefactoring()
158: .getIntermediaryClassName());
159:
160: fIntermediaryMethodName.addModifyListener(new ModifyListener() {
161: public void modifyText(ModifyEvent e) {
162: validateInput();
163: }
164: });
165:
166: enableReferencesCheckBox
167: .addSelectionListener(new SelectionAdapter() {
168: public void widgetSelected(SelectionEvent e) {
169: getIntroduceIndirectionRefactoring()
170: .setEnableUpdateReferences(
171: enableReferencesCheckBox
172: .getSelection());
173: }
174: });
175:
176: fIntermediaryTypeName.addModifyListener(new ModifyListener() {
177: public void modifyText(ModifyEvent e) {
178: validateInput();
179: }
180: });
181:
182: browseTypes.addSelectionListener(new SelectionAdapter() {
183: public void widgetSelected(SelectionEvent e) {
184: IType intermediaryType = chooseIntermediaryClass();
185:
186: if (intermediaryType == null)
187: return;
188:
189: fIntermediaryTypeName.setText(intermediaryType
190: .getFullyQualifiedName());
191: }
192: });
193:
194: if (getIntroduceIndirectionRefactoring()
195: .canEnableUpdateReferences())
196: enableReferencesCheckBox.setSelection(true);
197: else {
198: enableReferencesCheckBox.setSelection(false);
199: enableReferencesCheckBox.setEnabled(false);
200: getIntroduceIndirectionRefactoring()
201: .setEnableUpdateReferences(false);
202: }
203:
204: fIntermediaryMethodName.setFocus();
205: fIntermediaryMethodName.selectAll();
206: validateInput();
207:
208: PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(),
209: IJavaHelpContextIds.INTRODUCE_INDIRECTION_WIZARD_PAGE);
210: }
211:
212: private IType chooseIntermediaryClass() {
213: IJavaProject proj = getIntroduceIndirectionRefactoring()
214: .getProject();
215:
216: if (proj == null)
217: return null;
218:
219: IJavaElement[] elements = new IJavaElement[] { proj };
220: IJavaSearchScope scope = SearchEngine
221: .createJavaSearchScope(elements);
222:
223: FilteredTypesSelectionDialog dialog = new FilteredTypesSelectionDialog(
224: getShell(), false, getWizard().getContainer(), scope,
225: IJavaSearchConstants.CLASS);
226:
227: dialog
228: .setTitle(RefactoringMessages.IntroduceIndirectionInputPage_dialog_choose_declaring_class);
229: dialog
230: .setMessage(RefactoringMessages.IntroduceIndirectionInputPage_dialog_choose_declaring_class_long);
231:
232: if (dialog.open() == Window.OK) {
233: return (IType) dialog.getFirstResult();
234: }
235: return null;
236: }
237:
238: private IntroduceIndirectionRefactoring getIntroduceIndirectionRefactoring() {
239: return (IntroduceIndirectionRefactoring) getRefactoring();
240: }
241:
242: private void validateInput() {
243: RefactoringStatus merged = new RefactoringStatus();
244: merged.merge(getIntroduceIndirectionRefactoring()
245: .setIntermediaryClassName(
246: fIntermediaryTypeName.getText()));
247: merged.merge(getIntroduceIndirectionRefactoring()
248: .setIntermediaryMethodName(
249: fIntermediaryMethodName.getText()));
250:
251: setPageComplete(!merged.hasError());
252: int severity = merged.getSeverity();
253: String message = merged.getMessageMatchingSeverity(severity);
254: if (severity >= RefactoringStatus.INFO) {
255: setMessage(message, severity);
256: } else {
257: setMessage("", NONE); //$NON-NLS-1$
258: }
259: }
260:
261: protected boolean performFinish() {
262: storeIntermediaryTypeName();
263: return super .performFinish();
264: }
265:
266: public IWizardPage getNextPage() {
267: storeIntermediaryTypeName();
268: return super .getNextPage();
269: }
270:
271: private void storeIntermediaryTypeName() {
272: String destination = fIntermediaryTypeName.getText();
273: if (!fgIntermediaryTypes.remove(destination)
274: && fgIntermediaryTypes.size() >= INTERMEDIARY_TYPE_COUNT)
275: fgIntermediaryTypes.remove(fgIntermediaryTypes.size() - 1);
276: fgIntermediaryTypes.add(0, destination);
277: }
278: }
|