/******************************************************************************* * Copyright (c) 2008, 2009 Mateusz Matela and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Mateusz Matela - [code manipulation] [dcr] toString() builder wizard - https://bugs.eclipse.org/bugs/show_bug.cgi?id=26070 * Mateusz Matela - [toString] finish toString() builder wizard - https://bugs.eclipse.org/bugs/show_bug.cgi?id=267710 *******************************************************************************/ package org.eclipse.jdt.internal.corext.codemanipulation.tostringgeneration; import java.text.MessageFormat; import org.eclipse.swt.SWT; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Spinner; import org.eclipse.core.runtime.IStatus; import org.eclipse.jface.dialogs.IDialogSettings; import org.eclipse.jface.resource.StringConverter; import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.core.dom.ITypeBinding; import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings; import org.eclipse.jdt.internal.corext.codemanipulation.tostringgeneration.ToStringGenerationSettings.CustomBuilderSettings; import org.eclipse.jdt.internal.corext.util.JavaModelUtil; import org.eclipse.jdt.ui.actions.GenerateToStringAction; import org.eclipse.jdt.internal.ui.JavaUIMessages; import org.eclipse.jdt.internal.ui.dialogs.GenerateToStringDialog; import org.eclipse.jdt.internal.ui.dialogs.GenerateToStringDialog.CustomBuilderConfigurationDialog; import org.eclipse.jdt.internal.ui.dialogs.GenerateToStringDialog.CustomBuilderValidator; import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; public class ToStringGenerationSettings extends CodeGenerationSettings { public static final String SETTINGS_SELECTED_TEMPLATE= "ToStringTemplateSelected"; //$NON-NLS-1$ public static final String SETTINGS_STRINGSTYLE= "StringStyle"; //$NON-NLS-1$ public static final String SETTINGS_SKIPNULLS= "SkipNull"; //$NON-NLS-1$ public static final String SETTINGS_IGNOREDEFAULT= "IgnoreDefault"; //$NON-NLS-1$ public static final String SETTINGS_LIMITELEMENTS= "LimitElements"; //$NON-NLS-1$ public static final String SETTINGS_LIMITVALUE= "LimitValue"; //$NON-NLS-1$ public static final String SETTINGS_TEMPLATE_NAMES= "ToStringTemplateNames"; //$NON-NLS-1$ public static final String SETTINGS_TEMPLATES= "ToStringTemplates"; //$NON-NLS-1$ public static final String SETTINGS_CUSTOMBUILDER_CLASS= "CustomBuilderClass"; //$NON-NLS-1$ public static final String SETTINGS_CUSTOMBUILDER_LABEL= "CustomBuilderLabel"; //$NON-NLS-1$ public static final String SETTINGS_CUSTOMBUILDER_APPENDMETHOD= "CustomBuilderAppendMethod"; //$NON-NLS-1$ public static final String SETTINGS_CUSTOMBUILDER_RESULTMETHOD= "CustomBuilderResultMethod"; //$NON-NLS-1$ public static final String SETTINGS_CUSTOMBUILDER_CHAINCALLS= "CustomBuilderChainCalls"; //$NON-NLS-1$ /** * Container for settings specific for custom toString() generator code style */ public static class CustomBuilderSettings { /** * what class should be used as a custom toString() builder (this is a fully qualified and * Parameterized name) **/ public String className; /** identifier for the local variable that holds the custom toString() builder in generated code **/ public String variableName; /** name of a custom toString() builder's methods that should be called to append content **/ public String appendMethod; /** name of a custom toString() builder method that should be called to retrieve result **/ public String resultMethod; /** should custom toString() builder method calls be joined into chains? **/ public boolean chainCalls; public IStatus generated_6801785400558061357(CustomBuilderValidator custombuildervalidator) { try { if (className.length() == 0) { return new StatusInfo(IStatus.ERROR, JavaUIMessages.GenerateToStringDialog_customBuilderConfig_noBuilderClassError); } IType type= custombuildervalidator.findType(className); if (type == null || !type.exists()) { return new StatusInfo(IStatus.ERROR, MessageFormat.format(JavaUIMessages.GenerateToStringDialog_customBuilderConfig_invalidClassError, new Object[] { className })); } IStatus typeValidation= custombuildervalidator.validateBuilderType(type); if (!typeValidation.isOK()) return typeValidation; if (!custombuildervalidator.getAppendMethodSuggestions(type).contains(appendMethod)) return new StatusInfo(IStatus.ERROR, MessageFormat.format(JavaUIMessages.GenerateToStringDialog_customBuilderConfig_invalidAppendMethodError, new Object[] { appendMethod })); if (!custombuildervalidator.getResultMethodSuggestions(type).contains(resultMethod)) return new StatusInfo(IStatus.ERROR, MessageFormat.format(JavaUIMessages.GenerateToStringDialog_customBuilderConfig_invalidResultMethodError, new Object[] { resultMethod })); if (!custombuildervalidator.isValidJavaIdentifier(variableName)) return new StatusInfo(IStatus.ERROR, MessageFormat.format(JavaUIMessages.GenerateToStringDialog_customBuilderConfig_invalidVariableNameError, new Object[] { variableName })); } catch (JavaModelException e) { return new StatusInfo(IStatus.WARNING, JavaUIMessages.GenerateToStringDialog_customBuilderConfig_dataValidationError); } return new StatusInfo(); } public void generated_1197153510219597381(final CustomBuilderConfigurationDialog custombuilderconfigurationdialog, Composite composite, ModifyListener comboListener) { custombuilderconfigurationdialog.fResultMethodName.addModifyListener(comboListener); if (!custombuilderconfigurationdialog.select(custombuilderconfigurationdialog.fAppendMethodName, appendMethod)) { custombuilderconfigurationdialog.fAppendMethodName.select(0); } if (!custombuilderconfigurationdialog.select(custombuilderconfigurationdialog.fResultMethodName, resultMethod)) { custombuilderconfigurationdialog.fResultMethodName.select(0); } custombuilderconfigurationdialog.fChainInvocations= new Button(composite, SWT.CHECK); custombuilderconfigurationdialog.fChainInvocations.setText(JavaUIMessages.GenerateToStringDialog_customBuilderConfig_chainedCallsCheckbox); custombuilderconfigurationdialog.fChainInvocations.setSelection(chainCalls); custombuilderconfigurationdialog.fChainInvocations.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1)); custombuilderconfigurationdialog.fChainInvocations.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { chainCalls= custombuilderconfigurationdialog.fChainInvocations.getSelection(); custombuilderconfigurationdialog.enableApplyButton(); } }); } public void generated_4380449884425403228(CustomBuilderConfigurationDialog custombuilderconfigurationdialog, final String[] empty, IType type) throws JavaModelException { custombuilderconfigurationdialog.select(custombuilderconfigurationdialog.fAppendMethodName, appendMethod); custombuilderconfigurationdialog.fResultMethodName.setItems(custombuilderconfigurationdialog.fValidator.getResultMethodSuggestions(type).toArray(empty)); custombuilderconfigurationdialog.select(custombuilderconfigurationdialog.fResultMethodName, resultMethod); } public void generated_1036961313981835720(CustomBuilderConfigurationDialog custombuilderconfigurationdialog) { appendMethod= custombuilderconfigurationdialog.fAppendMethodName.getText(); resultMethod= custombuilderconfigurationdialog.fResultMethodName.getText(); } public String generated_9161054712216784088(ToStringGenerationContext tostringgenerationcontext) { return className; } public String generated_3844037368995630247(ToStringGenerationContext tostringgenerationcontext) { return variableName; } public String generated_3511844344444339683(ToStringGenerationContext tostringgenerationcontext) { return appendMethod; } public String generated_1592728035830864358(ToStringGenerationContext tostringgenerationcontext) { return resultMethod; } public boolean generated_1251988224814424550(ToStringGenerationContext tostringgenerationcontext) { return chainCalls; } public void generated_2329411435482929497(ToStringGenerationSettings tostringgenerationsettings, IDialogSettings dialogSettings) { className= ToStringGenerationSettings.asString(dialogSettings.get(ToStringGenerationSettings.SETTINGS_CUSTOMBUILDER_CLASS), ""); //$NON-NLS-1$ variableName= ToStringGenerationSettings.asString(dialogSettings.get(ToStringGenerationSettings.SETTINGS_CUSTOMBUILDER_LABEL), "builder"); //$NON-NLS-1$ appendMethod= ToStringGenerationSettings.asString(dialogSettings.get(ToStringGenerationSettings.SETTINGS_CUSTOMBUILDER_APPENDMETHOD), "append"); //$NON-NLS-1$ resultMethod= ToStringGenerationSettings.asString(dialogSettings.get(ToStringGenerationSettings.SETTINGS_CUSTOMBUILDER_RESULTMETHOD), "toString"); //$NON-NLS-1$ chainCalls= tostringgenerationsettings.asBoolean(dialogSettings.get(ToStringGenerationSettings.SETTINGS_CUSTOMBUILDER_CHAINCALLS), false); } public CustomBuilderSettings generated_3104832852564215374(ToStringGenerationSettings tostringgenerationsettings) { CustomBuilderSettings result= new CustomBuilderSettings(); result.className= className; result.variableName= variableName; result.appendMethod= appendMethod; result.resultMethod= resultMethod; result.chainCalls= chainCalls; return result; } public void generated_7480550366222733311(ToStringGenerationSettings tostringgenerationsettings) { tostringgenerationsettings.dialogSettings.put(ToStringGenerationSettings.SETTINGS_CUSTOMBUILDER_CLASS, className); tostringgenerationsettings.dialogSettings.put(ToStringGenerationSettings.SETTINGS_CUSTOMBUILDER_LABEL, variableName); tostringgenerationsettings.dialogSettings.put(ToStringGenerationSettings.SETTINGS_CUSTOMBUILDER_APPENDMETHOD, appendMethod); tostringgenerationsettings.dialogSettings.put(ToStringGenerationSettings.SETTINGS_CUSTOMBUILDER_RESULTMETHOD, resultMethod); tostringgenerationsettings.dialogSettings.put(ToStringGenerationSettings.SETTINGS_CUSTOMBUILDER_CHAINCALLS, chainCalls); tostringgenerationsettings.customBuilderSettings= this; } } /** which template should be used to format the output of the toString() method? */ public int stringFormatTemplateNumber; /** * what is the template (redundancy - this field can be determined basing on * GenerateToStringDialog.getTemplates() and * stringFormatTemplateNumber, but this way it's more convenient) */ public String stringFormatTemplate; /** what style of code should the toString() method have? */ public int toStringStyle; /** should the toString() method skip null values? */ public boolean skipNulls; /** should the toString() method use its own way to show elements of an array? */ public boolean customArrayToString; /** * should the toString() limit maximum number of elements of arrays/Collections to be * listed? */ public boolean limitElements; /** what is the maximum number of elements in array/Collection to show? */ public int limitValue; /** should blocks be forced in if/for/while statements? */ public boolean useBlocks; /** can generated code use jdk 1.5 API? **/ public boolean is50orHigher; /** can generated code use jdk 1.6 API? **/ public boolean is60orHigher; /** settings specific for custom builder code style **/ public CustomBuilderSettings customBuilderSettings; private IDialogSettings dialogSettings; public ToStringGenerationSettings(IDialogSettings dialogSettings) { this.dialogSettings= dialogSettings; limitElements= asBoolean(dialogSettings.get(SETTINGS_LIMITELEMENTS), false); customArrayToString= asBoolean(dialogSettings.get(SETTINGS_IGNOREDEFAULT), true); toStringStyle= asInt(dialogSettings.get(SETTINGS_STRINGSTYLE), 0); limitValue= asInt(dialogSettings.get(SETTINGS_LIMITVALUE), 10); skipNulls= asBoolean(dialogSettings.get(SETTINGS_SKIPNULLS), false); stringFormatTemplateNumber= asInt(dialogSettings.get(SETTINGS_SELECTED_TEMPLATE), 0); customBuilderSettings= new CustomBuilderSettings(); customBuilderSettings.generated_2329411435482929497(this, dialogSettings); } public ToStringGenerationSettings() { } public void writeDialogSettings() { dialogSettings.put(SETTINGS_LIMITELEMENTS, limitElements); dialogSettings.put(SETTINGS_IGNOREDEFAULT, customArrayToString); dialogSettings.put(SETTINGS_STRINGSTYLE, toStringStyle); dialogSettings.put(SETTINGS_LIMITVALUE, limitValue); dialogSettings.put(SETTINGS_SKIPNULLS, skipNulls); dialogSettings.put(SETTINGS_SELECTED_TEMPLATE, stringFormatTemplateNumber); } /** * Returns a copy of customBuilderSettings. Changes made in the returned object will not affect * this settings object. To save changes made in returned object, use * {@link #writeCustomBuilderSettings(ToStringGenerationSettings.CustomBuilderSettings)}. * * @return copy of custom builder settings object */ public CustomBuilderSettings getCustomBuilderSettings() { return customBuilderSettings.generated_3104832852564215374(this); } /** * Writes given custom builder settings object to the underlying dialog settings. * * @param customBuilderSettings1 settings to save */ public void writeCustomBuilderSettings(CustomBuilderSettings customBuilderSettings1) { customBuilderSettings1.generated_7480550366222733311(this); } private boolean asBoolean(String string, boolean defaultValue) { if (string != null) { return StringConverter.asBoolean(string, defaultValue); } return defaultValue; } public void generated_5905533701111269973(GenerateToStringDialog generatetostringdialog) { createComments= generatetostringdialog.getGenerateComment(); } public void generated_8279305899038655747(final GenerateToStringDialog generatetostringdialog, Group group, final Combo styleCombo) { generatetostringdialog.styleButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { generatetostringdialog.configureStyleButtonSelected(); } }); generatetostringdialog.skipNullsButton= new Button(group, SWT.CHECK); generatetostringdialog.skipNullsButton.setText(JavaUIMessages.GenerateToStringDialog_skip_null_button); generatetostringdialog.skipNullsButton.setSelection(skipNulls); generatetostringdialog.skipNullsButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent event) { skipNulls= ((Button)event.widget).getSelection(); } }); final Button arrayButton= new Button(group, SWT.CHECK); arrayButton.setText(JavaUIMessages.GenerateToStringDialog_ignore_default_button); arrayButton.setSelection(customArrayToString); arrayButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { customArrayToString= ((Button)e.widget).getSelection(); } }); Composite limitRow= new Composite(group, SWT.NONE); RowLayout rowLayout= new RowLayout(); rowLayout.center= true; rowLayout.marginLeft= rowLayout.marginRight= rowLayout.marginTop= rowLayout.marginBottom= 0; limitRow.setLayout(rowLayout); final Button limitButton= new Button(limitRow, SWT.CHECK); limitButton.setText(JavaUIMessages.GenerateToStringDialog_limit_elements_button); limitButton.setSelection(limitElements); limitButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { limitElements= ((Button)e.widget).getSelection(); } }); final Spinner limitSpinner= new Spinner(limitRow, SWT.BORDER); limitSpinner.setMinimum(0); limitSpinner.setSelection(limitValue); limitSpinner.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { limitValue= ((Spinner)e.widget).getSelection(); } }); //invoked to change initial enable state of controls generatetostringdialog.changeToStringStyle(styleCombo.getSelectionIndex()); } public void generated_4135918593992476495(GenerateToStringDialog generatetostringdialog) { generatetostringdialog.formatCombo.select(Math.min(stringFormatTemplateNumber, generatetostringdialog.formatCombo.getItemCount() - 1)); } public boolean generated_7416749355982389519(GenerateToStringDialog generatetostringdialog, int style) { toStringStyle= style; generatetostringdialog.skipNullsButton.setEnabled(style != GenerateToStringOperation.STRING_FORMAT); boolean enableFormat= (style != GenerateToStringOperation.CUSTOM_BUILDER); return enableFormat; } public CodeGenerationSettings generated_8579084277108818275(GenerateToStringAction generatetostringaction, String version) { is50orHigher= !JavaModelUtil.isVersionLessThan(version, JavaCore.VERSION_1_5); is60orHigher= !JavaModelUtil.isVersionLessThan(version, JavaCore.VERSION_1_6); return this; } public void generated_5495634267311453766(Object[] selectedMembers, ToStringGenerationContext tostringgenerationcontext, ITypeBinding type) { tostringgenerationcontext.fSelectedMembers= selectedMembers; tostringgenerationcontext.fSettings= this; tostringgenerationcontext.fCustomBuilderSettings= getCustomBuilderSettings(); tostringgenerationcontext.fType= type; } public int generated_6203512945634259293(ToStringGenerationContext tostringgenerationcontext) { return limitValue; } public boolean generated_4720457951684026118(ToStringGenerationContext tostringgenerationcontext) { return is50orHigher; } public boolean generated_4408472360174240783(ToStringGenerationContext tostringgenerationcontext) { return is60orHigher; } public boolean generated_3890522325313653657(ToStringGenerationContext tostringgenerationcontext) { return createComments; } public boolean generated_8719427988826962334(ToStringGenerationContext tostringgenerationcontext) { return customArrayToString; } public boolean generated_9053475721656625563(ToStringGenerationContext tostringgenerationcontext) { return useBlocks; } public boolean generated_1061606803051821626(ToStringGenerationContext tostringgenerationcontext) { return useKeywordThis; } public boolean generated_1165715848829217649(ToStringGenerationContext tostringgenerationcontext) { return limitElements; } public boolean generated_5588797316267707432(ToStringGenerationContext tostringgenerationcontext) { return overrideAnnotation; } public boolean generated_3397837620992553666(ToStringGenerationContext tostringgenerationcontext) { return skipNulls; } private static int asInt(String string, int defaultValue) { if (string != null) { return StringConverter.asInt(string, defaultValue); } return defaultValue; } private static String asString(String string, String defaultValue) { if (string != null) { return string; } return defaultValue; } }