001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.core.runtime.Assert;
013:
014: import org.eclipse.swt.SWT;
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.dialogs.Dialog;
025: import org.eclipse.jface.dialogs.IDialogSettings;
026:
027: import org.eclipse.ui.PlatformUI;
028:
029: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
030: import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
031:
032: import org.eclipse.jdt.internal.corext.refactoring.code.ExtractTempRefactoring;
033:
034: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
035: import org.eclipse.jdt.internal.ui.refactoring.contentassist.ControlContentAssistHelper;
036: import org.eclipse.jdt.internal.ui.refactoring.contentassist.VariableNamesProcessor;
037: import org.eclipse.jdt.internal.ui.util.RowLayouter;
038:
039: public class ExtractTempWizard extends RefactoringWizard {
040:
041: /* package */static final String DIALOG_SETTING_SECTION = "ExtractTempWizard"; //$NON-NLS-1$
042:
043: public ExtractTempWizard(ExtractTempRefactoring ref) {
044: super (ref, DIALOG_BASED_USER_INTERFACE
045: | PREVIEW_EXPAND_FIRST_NODE);
046: setDefaultPageTitle(RefactoringMessages.ExtractTempWizard_defaultPageTitle);
047: }
048:
049: /* non java-doc
050: * @see RefactoringWizard#addUserInputPages
051: */
052: protected void addUserInputPages() {
053: addPage(new ExtractTempInputPage(getExtractTempRefactoring()
054: .guessTempNames()));
055: }
056:
057: private ExtractTempRefactoring getExtractTempRefactoring() {
058: return (ExtractTempRefactoring) getRefactoring();
059: }
060:
061: private static class ExtractTempInputPage extends
062: TextInputWizardPage {
063:
064: private static final String DECLARE_FINAL = "declareFinal"; //$NON-NLS-1$
065: private static final String REPLACE_ALL = "replaceOccurrences"; //$NON-NLS-1$
066:
067: private final boolean fInitialValid;
068: private static final String DESCRIPTION = RefactoringMessages.ExtractTempInputPage_enter_name;
069: private String[] fTempNameProposals;
070: private IDialogSettings fSettings;
071:
072: public ExtractTempInputPage(String[] tempNameProposals) {
073: super (
074: DESCRIPTION,
075: true,
076: tempNameProposals.length == 0 ? "" : tempNameProposals[0]); //$NON-NLS-1$
077: Assert.isNotNull(tempNameProposals);
078: fTempNameProposals = tempNameProposals;
079: fInitialValid = tempNameProposals.length > 0;
080: }
081:
082: public void createControl(Composite parent) {
083: loadSettings();
084: Composite result = new Composite(parent, SWT.NONE);
085: setControl(result);
086: GridLayout layout = new GridLayout();
087: layout.numColumns = 2;
088: layout.verticalSpacing = 8;
089: result.setLayout(layout);
090: RowLayouter layouter = new RowLayouter(2);
091:
092: Label label = new Label(result, SWT.NONE);
093: label
094: .setText(RefactoringMessages.ExtractTempInputPage_variable_name);
095:
096: Text text = createTextInputField(result);
097: text.selectAll();
098: text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
099: ControlContentAssistHelper.createTextContentAssistant(text,
100: new VariableNamesProcessor(fTempNameProposals));
101:
102: layouter.perform(label, text, 1);
103:
104: addReplaceAllCheckbox(result, layouter);
105: addDeclareFinalCheckbox(result, layouter);
106:
107: validateTextField(text.getText());
108:
109: Dialog.applyDialogFont(result);
110: PlatformUI.getWorkbench().getHelpSystem().setHelp(
111: getControl(),
112: IJavaHelpContextIds.EXTRACT_TEMP_WIZARD_PAGE);
113: }
114:
115: private void loadSettings() {
116: fSettings = getDialogSettings().getSection(
117: ExtractTempWizard.DIALOG_SETTING_SECTION);
118: if (fSettings == null) {
119: fSettings = getDialogSettings().addNewSection(
120: ExtractTempWizard.DIALOG_SETTING_SECTION);
121: fSettings.put(DECLARE_FINAL, false);
122: fSettings.put(REPLACE_ALL, true);
123: }
124: getExtractTempRefactoring().setDeclareFinal(
125: fSettings.getBoolean(DECLARE_FINAL));
126: getExtractTempRefactoring().setReplaceAllOccurrences(
127: fSettings.getBoolean(REPLACE_ALL));
128: }
129:
130: private void addReplaceAllCheckbox(Composite result,
131: RowLayouter layouter) {
132: String title = RefactoringMessages.ExtractTempInputPage_replace_all;
133: boolean defaultValue = getExtractTempRefactoring()
134: .replaceAllOccurrences();
135: final Button checkBox = createCheckbox(result, title,
136: defaultValue, layouter);
137: getExtractTempRefactoring().setReplaceAllOccurrences(
138: checkBox.getSelection());
139: checkBox.addSelectionListener(new SelectionAdapter() {
140: public void widgetSelected(SelectionEvent e) {
141: fSettings.put(REPLACE_ALL, checkBox.getSelection());
142: getExtractTempRefactoring()
143: .setReplaceAllOccurrences(
144: checkBox.getSelection());
145: }
146: });
147: }
148:
149: private void addDeclareFinalCheckbox(Composite result,
150: RowLayouter layouter) {
151: String title = RefactoringMessages.ExtractTempInputPage_declare_final;
152: boolean defaultValue = getExtractTempRefactoring()
153: .declareFinal();
154: final Button checkBox = createCheckbox(result, title,
155: defaultValue, layouter);
156: getExtractTempRefactoring().setDeclareFinal(
157: checkBox.getSelection());
158: checkBox.addSelectionListener(new SelectionAdapter() {
159: public void widgetSelected(SelectionEvent e) {
160: fSettings.put(DECLARE_FINAL, checkBox
161: .getSelection());
162: getExtractTempRefactoring().setDeclareFinal(
163: checkBox.getSelection());
164: }
165: });
166: }
167:
168: /*
169: * @see org.eclipse.jdt.internal.ui.refactoring.TextInputWizardPage#textModified(java.lang.String)
170: */
171: protected void textModified(String text) {
172: getExtractTempRefactoring().setTempName(text);
173: super .textModified(text);
174: }
175:
176: /*
177: * @see org.eclipse.jdt.internal.ui.refactoring.TextInputWizardPage#validateTextField(String)
178: */
179: protected RefactoringStatus validateTextField(String text) {
180: return getExtractTempRefactoring().checkTempName(text);
181: }
182:
183: private ExtractTempRefactoring getExtractTempRefactoring() {
184: return (ExtractTempRefactoring) getRefactoring();
185: }
186:
187: private static Button createCheckbox(Composite parent,
188: String title, boolean value, RowLayouter layouter) {
189: Button checkBox = new Button(parent, SWT.CHECK);
190: checkBox.setText(title);
191: checkBox.setSelection(value);
192: layouter.perform(checkBox);
193: return checkBox;
194: }
195:
196: /*
197: * @see org.eclipse.jdt.internal.ui.refactoring.TextInputWizardPage#isInitialInputValid()
198: */
199: protected boolean isInitialInputValid() {
200: return fInitialValid;
201: }
202: }
203: }
|