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.jdt.core.Flags;
013: import org.eclipse.jdt.core.JavaModelException;
014:
015: import org.eclipse.swt.SWT;
016: import org.eclipse.swt.events.SelectionAdapter;
017: import org.eclipse.swt.events.SelectionEvent;
018: import org.eclipse.swt.layout.GridData;
019: import org.eclipse.swt.layout.GridLayout;
020: import org.eclipse.swt.widgets.Button;
021: import org.eclipse.swt.widgets.Composite;
022: import org.eclipse.swt.widgets.Label;
023: import org.eclipse.swt.widgets.Text;
024:
025: import org.eclipse.jface.dialogs.Dialog;
026: import org.eclipse.jface.dialogs.IDialogConstants;
027: import org.eclipse.jface.dialogs.IMessageProvider;
028: import org.eclipse.jface.resource.ImageDescriptor;
029:
030: import org.eclipse.ui.PlatformUI;
031:
032: import org.eclipse.jdt.internal.corext.refactoring.code.ExtractConstantRefactoring;
033: import org.eclipse.jdt.internal.corext.util.JdtFlags;
034:
035: import org.eclipse.jdt.ui.JavaElementImageDescriptor;
036:
037: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
038: import org.eclipse.jdt.internal.ui.JavaPlugin;
039: import org.eclipse.jdt.internal.ui.refactoring.contentassist.ControlContentAssistHelper;
040: import org.eclipse.jdt.internal.ui.refactoring.contentassist.VariableNamesProcessor;
041: import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
042: import org.eclipse.jdt.internal.ui.util.RowLayouter;
043: import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
044:
045: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
046: import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
047:
048: public class ExtractConstantWizard extends RefactoringWizard {
049:
050: private static final String MESSAGE = RefactoringMessages.ExtractConstantInputPage_enter_name;
051:
052: public ExtractConstantWizard(ExtractConstantRefactoring ref) {
053: super (ref, DIALOG_BASED_USER_INTERFACE
054: | PREVIEW_EXPAND_FIRST_NODE);
055: setDefaultPageTitle(RefactoringMessages.ExtractConstantWizard_defaultPageTitle);
056: }
057:
058: /* non java-doc
059: * @see RefactoringWizard#addUserInputPages
060: */
061: protected void addUserInputPages() {
062:
063: String message = null;
064: int messageType = IMessageProvider.NONE;
065: if (!getExtractConstantRefactoring().selectionAllStaticFinal()) {
066: message = RefactoringMessages.ExtractConstantInputPage_selection_refers_to_nonfinal_fields;
067: messageType = IMessageProvider.INFORMATION;
068: } else {
069: message = MESSAGE;
070: messageType = IMessageProvider.NONE;
071: }
072:
073: String[] guessedNames = getExtractConstantRefactoring()
074: .guessConstantNames();
075: String initialValue = guessedNames.length == 0 ? "" : guessedNames[0]; //$NON-NLS-1$
076: addPage(new ExtractConstantInputPage(message, messageType,
077: initialValue, guessedNames));
078: }
079:
080: private ExtractConstantRefactoring getExtractConstantRefactoring() {
081: return (ExtractConstantRefactoring) getRefactoring();
082: }
083:
084: private static class ExtractConstantInputPage extends
085: TextInputWizardPage {
086: private static final String QUALIFY_REFERENCES = "qualifyReferences"; //$NON-NLS-1$
087:
088: private Label fLabel;
089: private final boolean fInitialValid;
090: private final int fOriginalMessageType;
091: private final String fOriginalMessage;
092:
093: private Button fQualifyReferences;
094: private String[] fConstNameProposals;
095:
096: private VariableNamesProcessor fContentAssistProcessor;
097: private String fAccessModifier;
098:
099: public ExtractConstantInputPage(String description,
100: int messageType, String initialValue,
101: String[] guessedNames) {
102: super (description, true, initialValue);
103: fOriginalMessage = description;
104: fOriginalMessageType = messageType;
105: fInitialValid = !("".equals(initialValue)); //$NON-NLS-1$
106: fConstNameProposals = guessedNames;
107: }
108:
109: public void createControl(Composite parent) {
110: Composite result = new Composite(parent, SWT.NONE);
111: setControl(result);
112: GridLayout layout = new GridLayout();
113: layout.numColumns = 2;
114: layout.verticalSpacing = 8;
115: result.setLayout(layout);
116: RowLayouter layouter = new RowLayouter(2);
117:
118: Label label = new Label(result, SWT.NONE);
119: label
120: .setText(RefactoringMessages.ExtractConstantInputPage_constant_name);
121:
122: Text text = createTextInputField(result);
123: text.selectAll();
124: text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
125: if (fConstNameProposals.length > 0) {
126: fContentAssistProcessor = new VariableNamesProcessor(
127: fConstNameProposals);
128: ControlContentAssistHelper.createTextContentAssistant(
129: text, fContentAssistProcessor);
130: }
131:
132: layouter.perform(label, text, 1);
133:
134: addAccessModifierGroup(result, layouter);
135: addReplaceAllCheckbox(result, layouter);
136: addQualifyReferencesCheckbox(result, layouter);
137: addSeparator(result, layouter);
138: addLabel(result, layouter);
139:
140: validateTextField(text.getText());
141:
142: Dialog.applyDialogFont(result);
143: PlatformUI.getWorkbench().getHelpSystem().setHelp(
144: getControl(),
145: IJavaHelpContextIds.EXTRACT_CONSTANT_WIZARD_PAGE);
146: }
147:
148: private void addAccessModifierGroup(Composite result,
149: RowLayouter layouter) {
150: fAccessModifier = getExtractConstantRefactoring()
151: .getVisibility();
152: if (getExtractConstantRefactoring().getTargetIsInterface())
153: return;
154:
155: Label label = new Label(result, SWT.NONE);
156: label
157: .setText(RefactoringMessages.ExtractConstantInputPage_access_modifiers);
158:
159: Composite group = new Composite(result, SWT.NONE);
160: group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
161: GridLayout layout = new GridLayout();
162: layout.numColumns = 4;
163: layout.marginWidth = 0;
164: group.setLayout(layout);
165:
166: String[] labels = new String[] {
167: RefactoringMessages.ExtractMethodInputPage_public,
168: RefactoringMessages.ExtractMethodInputPage_protected,
169: RefactoringMessages.ExtractMethodInputPage_default,
170: RefactoringMessages.ExtractMethodInputPage_private };
171: String[] data = new String[] {
172: JdtFlags.VISIBILITY_STRING_PUBLIC,
173: JdtFlags.VISIBILITY_STRING_PROTECTED,
174: JdtFlags.VISIBILITY_STRING_PACKAGE,
175: JdtFlags.VISIBILITY_STRING_PRIVATE }; //
176:
177: updateContentAssistImage();
178: for (int i = 0; i < labels.length; i++) {
179: Button radio = new Button(group, SWT.RADIO);
180: radio.setText(labels[i]);
181: radio.setData(data[i]);
182: if (data[i] == fAccessModifier)
183: radio.setSelection(true);
184: radio.addSelectionListener(new SelectionAdapter() {
185: public void widgetSelected(SelectionEvent event) {
186: setAccessModifier((String) event.widget
187: .getData());
188: }
189: });
190: }
191: layouter.perform(label, group, 1);
192: }
193:
194: private void updateContentAssistImage() {
195: if (fContentAssistProcessor == null)
196: return;
197:
198: int flags;
199: if (fAccessModifier == JdtFlags.VISIBILITY_STRING_PRIVATE) {
200: flags = Flags.AccPrivate;
201: } else if (fAccessModifier == JdtFlags.VISIBILITY_STRING_PROTECTED) {
202: flags = Flags.AccProtected;
203: } else if (fAccessModifier == JdtFlags.VISIBILITY_STRING_PUBLIC) {
204: flags = Flags.AccPublic;
205: } else {
206: flags = Flags.AccDefault;
207: }
208: ImageDescriptor imageDesc = JavaElementImageProvider
209: .getFieldImageDescriptor(false, flags);
210: imageDesc = new JavaElementImageDescriptor(imageDesc,
211: JavaElementImageDescriptor.STATIC
212: | JavaElementImageDescriptor.FINAL,
213: JavaElementImageProvider.BIG_SIZE);
214: fContentAssistProcessor
215: .setProposalImageDescriptor(imageDesc);
216: }
217:
218: private void addReplaceAllCheckbox(Composite result,
219: RowLayouter layouter) {
220: String title = RefactoringMessages.ExtractConstantInputPage_replace_all;
221: boolean defaultValue = getExtractConstantRefactoring()
222: .replaceAllOccurrences();
223: final Button checkBox = createCheckbox(result, title,
224: defaultValue, layouter);
225: getExtractConstantRefactoring().setReplaceAllOccurrences(
226: checkBox.getSelection());
227: checkBox.addSelectionListener(new SelectionAdapter() {
228: public void widgetSelected(SelectionEvent e) {
229: getExtractConstantRefactoring()
230: .setReplaceAllOccurrences(
231: checkBox.getSelection());
232: }
233: });
234: }
235:
236: private void addQualifyReferencesCheckbox(Composite result,
237: RowLayouter layouter) {
238: String title = RefactoringMessages.ExtractConstantInputPage_qualify_constant_references_with_class_name;
239: boolean defaultValue = getBooleanSetting(
240: QUALIFY_REFERENCES, getExtractConstantRefactoring()
241: .qualifyReferencesWithDeclaringClassName());
242: fQualifyReferences = createCheckbox(result, title,
243: defaultValue, layouter);
244: getExtractConstantRefactoring()
245: .setQualifyReferencesWithDeclaringClassName(
246: fQualifyReferences.getSelection());
247: fQualifyReferences
248: .addSelectionListener(new SelectionAdapter() {
249: public void widgetSelected(SelectionEvent e) {
250: getExtractConstantRefactoring()
251: .setQualifyReferencesWithDeclaringClassName(
252: fQualifyReferences
253: .getSelection());
254: }
255: });
256: }
257:
258: private void addLabel(Composite result, RowLayouter layouter) {
259: fLabel = new Label(result, SWT.WRAP);
260: GridData gd = new GridData(GridData.FILL_BOTH);
261: gd.widthHint = convertWidthInCharsToPixels(50);
262: fLabel.setLayoutData(gd);
263: updatePreviewLabel();
264: layouter.perform(fLabel);
265: }
266:
267: private void addSeparator(Composite result, RowLayouter layouter) {
268: Label separator = new Label(result, SWT.SEPARATOR
269: | SWT.HORIZONTAL);
270: separator.setLayoutData((new GridData(
271: GridData.FILL_HORIZONTAL)));
272: layouter.perform(separator);
273: }
274:
275: private void updatePreviewLabel() {
276: try {
277: if (fLabel != null)
278: fLabel
279: .setText(RefactoringMessages.ExtractConstantInputPage_signature_preview
280: + getExtractConstantRefactoring()
281: .getConstantSignaturePreview());
282: } catch (JavaModelException e) {
283: ExceptionHandler
284: .handle(
285: e,
286: RefactoringMessages.ExtractTempInputPage_extract_local,
287: RefactoringMessages.ExtractConstantInputPage_exception);
288: }
289: }
290:
291: /*
292: * @see org.eclipse.jdt.internal.ui.refactoring.TextInputWizardPage#validateTextField(String)
293: */
294: protected RefactoringStatus validateTextField(String text) {
295: try {
296: getExtractConstantRefactoring().setConstantName(text);
297: updatePreviewLabel();
298: RefactoringStatus result = getExtractConstantRefactoring()
299: .checkConstantNameOnChange();
300: if (fOriginalMessageType == IMessageProvider.INFORMATION
301: && result.getSeverity() == RefactoringStatus.OK)
302: return RefactoringStatus
303: .createInfoStatus(fOriginalMessage);
304: else
305: return result;
306: } catch (JavaModelException e) {
307: JavaPlugin.log(e);
308: return RefactoringStatus
309: .createFatalErrorStatus(RefactoringMessages.ExtractConstantInputPage_Internal_Error);
310: }
311: }
312:
313: private void setAccessModifier(String accessModifier) {
314: getExtractConstantRefactoring().setVisibility(
315: accessModifier);
316: fAccessModifier = accessModifier;
317: updateContentAssistImage();
318: updatePreviewLabel();
319: }
320:
321: private ExtractConstantRefactoring getExtractConstantRefactoring() {
322: return (ExtractConstantRefactoring) getRefactoring();
323: }
324:
325: private static Button createCheckbox(Composite parent,
326: String title, boolean value, RowLayouter layouter) {
327: Button checkBox = new Button(parent, SWT.CHECK);
328: checkBox.setText(title);
329: checkBox.setSelection(value);
330: layouter.perform(checkBox);
331: return checkBox;
332: }
333:
334: /*
335: * @see org.eclipse.jdt.internal.ui.refactoring.TextInputWizardPage#isInitialInputValid()
336: */
337: protected boolean isInitialInputValid() {
338: return fInitialValid;
339: }
340:
341: /*
342: * @see org.eclipse.jdt.internal.ui.refactoring.TextInputWizardPage#restoreMessage()
343: */
344: protected void restoreMessage() {
345: setMessage(fOriginalMessage, fOriginalMessageType);
346: }
347:
348: private boolean getBooleanSetting(String key,
349: boolean defaultValue) {
350: String update = getRefactoringSettings().get(key);
351: if (update != null)
352: return Boolean.valueOf(update).booleanValue();
353: else
354: return defaultValue;
355: }
356:
357: private void saveBooleanSetting(String key, Button checkBox) {
358: if (checkBox != null)
359: getRefactoringSettings().put(key,
360: checkBox.getSelection());
361: }
362:
363: private boolean saveSettings() {
364: if (getContainer() instanceof Dialog)
365: return ((Dialog) getContainer()).getReturnCode() == IDialogConstants.OK_ID;
366: return true;
367: }
368:
369: public void dispose() {
370: if (saveSettings()) {
371: saveBooleanSetting(QUALIFY_REFERENCES,
372: fQualifyReferences);
373: }
374: super.dispose();
375: }
376:
377: }
378: }
|