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.ModifyEvent;
016: import org.eclipse.swt.events.ModifyListener;
017: import org.eclipse.swt.events.SelectionAdapter;
018: import org.eclipse.swt.events.SelectionEvent;
019: import org.eclipse.swt.layout.GridData;
020: import org.eclipse.swt.layout.GridLayout;
021: import org.eclipse.swt.widgets.Button;
022: import org.eclipse.swt.widgets.Composite;
023: import org.eclipse.swt.widgets.Group;
024: import org.eclipse.swt.widgets.Label;
025: import org.eclipse.swt.widgets.Text;
026:
027: import org.eclipse.jface.dialogs.Dialog;
028: import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
029:
030: import org.eclipse.ui.PlatformUI;
031:
032: import org.eclipse.jdt.internal.corext.refactoring.code.PromoteTempToFieldRefactoring;
033:
034: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
035: import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler;
036: import org.eclipse.jdt.internal.ui.refactoring.contentassist.ControlContentAssistHelper;
037: import org.eclipse.jdt.internal.ui.refactoring.contentassist.FieldNameProcessor;
038:
039: import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
040: import org.eclipse.ltk.ui.refactoring.UserInputWizardPage;
041:
042: public class PromoteTempWizard extends RefactoringWizard {
043:
044: public PromoteTempWizard(PromoteTempToFieldRefactoring ref) {
045: super (ref, DIALOG_BASED_USER_INTERFACE
046: | PREVIEW_EXPAND_FIRST_NODE);
047: setDefaultPageTitle(RefactoringMessages.ConvertLocalToField_title);
048: }
049:
050: /* non java-doc
051: * @see RefactoringWizard#addUserInputPages
052: */
053: protected void addUserInputPages() {
054: addPage(new PromoteTempInputPage());
055: }
056:
057: private static class PromoteTempInputPage extends
058: UserInputWizardPage {
059:
060: private static final String DESCRIPTION = RefactoringMessages.PromoteTempInputPage_description;
061: public static final String PAGE_NAME = "PromoteTempInputPage";//$NON-NLS-1$
062: private static final String[] RADIO_BUTTON_LABELS = {
063: RefactoringMessages.PromoteTempInputPage_Field_declaration,
064: RefactoringMessages.PromoteTempInputPage_Current_method,
065: RefactoringMessages.PromoteTempInputPage_constructors };
066: private static final Integer[] RADIO_BUTTON_DATA = {
067: new Integer(
068: PromoteTempToFieldRefactoring.INITIALIZE_IN_FIELD),
069: new Integer(
070: PromoteTempToFieldRefactoring.INITIALIZE_IN_METHOD),
071: new Integer(
072: PromoteTempToFieldRefactoring.INITIALIZE_IN_CONSTRUCTOR) };
073: private Button fDeclareStaticCheckbox;
074: private Button fDeclareFinalCheckbox;
075: private Button[] fInitializeInRadioButtons;
076: private Text fNameField;
077:
078: public PromoteTempInputPage() {
079: super (PAGE_NAME);
080: setDescription(DESCRIPTION);
081: }
082:
083: public void createControl(Composite parent) {
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:
091: addFieldNameField(result);
092: addVisibilityControl(result);
093: addInitizeInRadioButtonGroup(result);
094: addDeclareStaticCheckbox(result);
095: addDeclareFinalCheckbox(result);
096:
097: Dialog.applyDialogFont(result);
098: PlatformUI
099: .getWorkbench()
100: .getHelpSystem()
101: .setHelp(
102: getControl(),
103: IJavaHelpContextIds.PROMOTE_TEMP_TO_FIELD_WIZARD_PAGE);
104: }
105:
106: private void addFieldNameField(Composite result) {
107: Label nameLabel = new Label(result, SWT.NONE);
108: nameLabel
109: .setText(RefactoringMessages.PromoteTempInputPage_Field_name);
110: nameLabel.setLayoutData(new GridData());
111:
112: String[] guessedFieldNames = getPromoteTempRefactoring()
113: .guessFieldNames();
114:
115: fNameField = new Text(result, SWT.BORDER | SWT.SINGLE);
116: fNameField.setText(guessedFieldNames[0]);
117: fNameField.selectAll();
118: fNameField.setLayoutData(new GridData(
119: GridData.FILL_HORIZONTAL));
120: fNameField.addModifyListener(new ModifyListener() {
121: public void modifyText(ModifyEvent e) {
122: PromoteTempInputPage.this
123: .getPromoteTempRefactoring().setFieldName(
124: fNameField.getText());
125: PromoteTempInputPage.this .updateStatus();
126: }
127: });
128: IContentAssistProcessor processor = new FieldNameProcessor(
129: guessedFieldNames, getPromoteTempRefactoring());
130: ControlContentAssistHelper.createTextContentAssistant(
131: fNameField, processor);
132: TextFieldNavigationHandler.install(fNameField);
133: }
134:
135: private void updateStatus() {
136: setPageComplete(getPromoteTempRefactoring().validateInput());
137: }
138:
139: private void addInitizeInRadioButtonGroup(Composite result) {
140: GridData gd;
141: Group initializeIn = new Group(result, SWT.NONE);
142: initializeIn
143: .setText(RefactoringMessages.PromoteTempInputPage_Initialize);
144: initializeIn.setLayout(new GridLayout());
145: gd = new GridData(GridData.FILL_HORIZONTAL);
146: gd.horizontalSpan = 2;
147: initializeIn.setLayoutData(gd);
148:
149: Assert
150: .isTrue(RADIO_BUTTON_LABELS.length == RADIO_BUTTON_DATA.length);
151: fInitializeInRadioButtons = new Button[RADIO_BUTTON_LABELS.length];
152: for (int i = 0; i < RADIO_BUTTON_LABELS.length; i++) {
153: Integer dataItem = RADIO_BUTTON_DATA[i];
154: fInitializeInRadioButtons[i] = new Button(initializeIn,
155: SWT.RADIO);
156: fInitializeInRadioButtons[i]
157: .setEnabled(canEnable(dataItem.intValue()));
158: fInitializeInRadioButtons[i]
159: .setText(RADIO_BUTTON_LABELS[i]);
160: fInitializeInRadioButtons[i].setSelection(dataItem
161: .intValue() == getPromoteTempRefactoring()
162: .getInitializeIn());
163: fInitializeInRadioButtons[i]
164: .setLayoutData(new GridData());
165: fInitializeInRadioButtons[i].setData(dataItem);
166: final int j = i;
167: fInitializeInRadioButtons[i]
168: .addSelectionListener(new SelectionAdapter() {
169: public void widgetSelected(SelectionEvent e) {
170: getPromoteTempRefactoring()
171: .setInitializeIn(
172: getDataAsInt(fInitializeInRadioButtons[j]));
173: updateButtonsEnablement();
174: }
175: });
176: }
177: }
178:
179: private void updateButtonsEnablement() {
180: fDeclareFinalCheckbox
181: .setEnabled(getPromoteTempRefactoring()
182: .canEnableSettingFinal());
183: fDeclareStaticCheckbox
184: .setEnabled(getPromoteTempRefactoring()
185: .canEnableSettingStatic());
186: for (int i = 0; i < fInitializeInRadioButtons.length; i++) {
187: fInitializeInRadioButtons[i]
188: .setEnabled(canEnable(getDataAsInt(fInitializeInRadioButtons[i])));
189: }
190: }
191:
192: private static int getDataAsInt(Button button) {
193: return ((Integer) button.getData()).intValue();
194: }
195:
196: private boolean canEnable(int initializeIn) {
197: switch (initializeIn) {
198: case PromoteTempToFieldRefactoring.INITIALIZE_IN_CONSTRUCTOR:
199: return getPromoteTempRefactoring()
200: .canEnableSettingDeclareInConstructors();
201: case PromoteTempToFieldRefactoring.INITIALIZE_IN_FIELD:
202: return getPromoteTempRefactoring()
203: .canEnableSettingDeclareInFieldDeclaration();
204: case PromoteTempToFieldRefactoring.INITIALIZE_IN_METHOD:
205: return getPromoteTempRefactoring()
206: .canEnableSettingDeclareInMethod();
207: default:
208: Assert.isTrue(false);
209: return false;
210: }
211: }
212:
213: public void addDeclareStaticCheckbox(Composite result) {
214: GridData gd;
215: fDeclareStaticCheckbox = new Button(result, SWT.CHECK);
216: fDeclareStaticCheckbox
217: .setEnabled(getPromoteTempRefactoring()
218: .canEnableSettingStatic());
219: fDeclareStaticCheckbox
220: .setSelection(getPromoteTempRefactoring()
221: .getDeclareStatic());
222: fDeclareStaticCheckbox
223: .setText(RefactoringMessages.PromoteTempInputPage_declare_static);
224: gd = new GridData(GridData.FILL_HORIZONTAL);
225: gd.horizontalSpan = 2;
226: fDeclareStaticCheckbox.setLayoutData(gd);
227: fDeclareStaticCheckbox
228: .addSelectionListener(new SelectionAdapter() {
229: public void widgetSelected(SelectionEvent e) {
230: getPromoteTempRefactoring()
231: .setDeclareStatic(
232: fDeclareStaticCheckbox
233: .getSelection());
234: updateButtonsEnablement();
235: }
236: });
237: }
238:
239: private void addDeclareFinalCheckbox(Composite result) {
240: GridData gd;
241: fDeclareFinalCheckbox = new Button(result, SWT.CHECK);
242: fDeclareFinalCheckbox
243: .setEnabled(getPromoteTempRefactoring()
244: .canEnableSettingFinal());
245: fDeclareFinalCheckbox
246: .setSelection(getPromoteTempRefactoring()
247: .getDeclareFinal());
248: fDeclareFinalCheckbox
249: .setText(RefactoringMessages.PromoteTempInputPage_declare_final);
250: gd = new GridData(GridData.FILL_HORIZONTAL);
251: gd.horizontalSpan = 2;
252: fDeclareFinalCheckbox.setLayoutData(gd);
253: fDeclareFinalCheckbox
254: .addSelectionListener(new SelectionAdapter() {
255: public void widgetSelected(SelectionEvent e) {
256: getPromoteTempRefactoring()
257: .setDeclareFinal(
258: fDeclareFinalCheckbox
259: .getSelection());
260: updateButtonsEnablement();
261: }
262: });
263: }
264:
265: private void addVisibilityControl(Composite result) {
266: int[] availableVisibilities = getPromoteTempRefactoring()
267: .getAvailableVisibilities();
268: int currectVisibility = getPromoteTempRefactoring()
269: .getVisibility();
270: IVisibilityChangeListener visibilityChangeListener = new IVisibilityChangeListener() {
271: public void visibilityChanged(int newVisibility) {
272: getPromoteTempRefactoring().setVisibility(
273: newVisibility);
274: }
275:
276: public void modifierChanged(int modifier,
277: boolean isChecked) {
278: }
279: };
280: Composite visibilityComposite = VisibilityControlUtil
281: .createVisibilityControl(result,
282: visibilityChangeListener,
283: availableVisibilities, currectVisibility);
284: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
285: gd.horizontalSpan = 2;
286: visibilityComposite.setLayoutData(gd);
287: }
288:
289: private PromoteTempToFieldRefactoring getPromoteTempRefactoring() {
290: return (PromoteTempToFieldRefactoring) getRefactoring();
291: }
292:
293: /* (non-Javadoc)
294: * @see org.eclipse.jface.dialogs.IDialogPage#setVisible(boolean)
295: */
296: public void setVisible(boolean visible) {
297: super.setVisible(visible);
298: if (visible && fNameField != null)
299: fNameField.setFocus();
300: }
301:
302: }
303: }
|