]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-after/core extension/org/eclipse/jdt/internal/corext/codemanipulation/tostringgeneration/ToStringGenerationSettings.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / core extension / org / eclipse / jdt / internal / corext / codemanipulation / tostringgeneration / ToStringGenerationSettings.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2008, 2009 Mateusz Matela and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Mateusz Matela <mateusz.matela@gmail.com> - [code manipulation] [dcr] toString() builder wizard - https://bugs.eclipse.org/bugs/show_bug.cgi?id=26070
10 * Mateusz Matela <mateusz.matela@gmail.com> - [toString] finish toString() builder wizard - https://bugs.eclipse.org/bugs/show_bug.cgi?id=267710
11 *******************************************************************************/
12package org.eclipse.jdt.internal.corext.codemanipulation.tostringgeneration;
13
14import java.text.MessageFormat;
15
16import org.eclipse.swt.SWT;
17import org.eclipse.swt.events.ModifyListener;
18import org.eclipse.swt.events.SelectionAdapter;
19import org.eclipse.swt.events.SelectionEvent;
20import org.eclipse.swt.layout.GridData;
21import org.eclipse.swt.layout.RowLayout;
22import org.eclipse.swt.widgets.Button;
23import org.eclipse.swt.widgets.Combo;
24import org.eclipse.swt.widgets.Composite;
25import org.eclipse.swt.widgets.Group;
26import org.eclipse.swt.widgets.Spinner;
27
28import org.eclipse.core.runtime.IStatus;
29
30import org.eclipse.jface.dialogs.IDialogSettings;
31import org.eclipse.jface.resource.StringConverter;
32
33import org.eclipse.jdt.core.IType;
34import org.eclipse.jdt.core.JavaCore;
35import org.eclipse.jdt.core.JavaModelException;
36import org.eclipse.jdt.core.dom.ITypeBinding;
37
38import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings;
39import org.eclipse.jdt.internal.corext.codemanipulation.tostringgeneration.ToStringGenerationSettings.CustomBuilderSettings;
40import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
41
42import org.eclipse.jdt.ui.actions.GenerateToStringAction;
43
44import org.eclipse.jdt.internal.ui.JavaUIMessages;
45import org.eclipse.jdt.internal.ui.dialogs.GenerateToStringDialog;
46import org.eclipse.jdt.internal.ui.dialogs.GenerateToStringDialog.CustomBuilderConfigurationDialog;
47import org.eclipse.jdt.internal.ui.dialogs.GenerateToStringDialog.CustomBuilderValidator;
48import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
49
50public class ToStringGenerationSettings extends CodeGenerationSettings {
51
52 public static final String SETTINGS_SELECTED_TEMPLATE= "ToStringTemplateSelected"; //$NON-NLS-1$
53
54 public static final String SETTINGS_STRINGSTYLE= "StringStyle"; //$NON-NLS-1$
55
56 public static final String SETTINGS_SKIPNULLS= "SkipNull"; //$NON-NLS-1$
57
58 public static final String SETTINGS_IGNOREDEFAULT= "IgnoreDefault"; //$NON-NLS-1$
59
60 public static final String SETTINGS_LIMITELEMENTS= "LimitElements"; //$NON-NLS-1$
61
62 public static final String SETTINGS_LIMITVALUE= "LimitValue"; //$NON-NLS-1$
63
64 public static final String SETTINGS_TEMPLATE_NAMES= "ToStringTemplateNames"; //$NON-NLS-1$
65
66 public static final String SETTINGS_TEMPLATES= "ToStringTemplates"; //$NON-NLS-1$
67
68 public static final String SETTINGS_CUSTOMBUILDER_CLASS= "CustomBuilderClass"; //$NON-NLS-1$
69
70 public static final String SETTINGS_CUSTOMBUILDER_LABEL= "CustomBuilderLabel"; //$NON-NLS-1$
71
72 public static final String SETTINGS_CUSTOMBUILDER_APPENDMETHOD= "CustomBuilderAppendMethod"; //$NON-NLS-1$
73
74 public static final String SETTINGS_CUSTOMBUILDER_RESULTMETHOD= "CustomBuilderResultMethod"; //$NON-NLS-1$
75
76 public static final String SETTINGS_CUSTOMBUILDER_CHAINCALLS= "CustomBuilderChainCalls"; //$NON-NLS-1$
77
78 /**
79 * Container for settings specific for custom toString() generator code style
80 */
81 public static class CustomBuilderSettings {
82 /**
83 * what class should be used as a custom toString() builder (this is a fully qualified and
84 * Parameterized name)
85 **/
86 public String className;
87
88 /** identifier for the local variable that holds the custom toString() builder in generated code **/
89 public String variableName;
90
91 /** name of a custom toString() builder's methods that should be called to append content **/
92 public String appendMethod;
93
94 /** name of a custom toString() builder method that should be called to retrieve result **/
95 public String resultMethod;
96
97 /** should custom toString() builder method calls be joined into chains? **/
98 public boolean chainCalls;
99
100 public IStatus generated_6801785400558061357(CustomBuilderValidator custombuildervalidator) {
101 try {
102 if (className.length() == 0) {
103 return new StatusInfo(IStatus.ERROR, JavaUIMessages.GenerateToStringDialog_customBuilderConfig_noBuilderClassError);
104 }
105
106 IType type= custombuildervalidator.findType(className);
107
108 if (type == null || !type.exists()) {
109 return new StatusInfo(IStatus.ERROR, MessageFormat.format(JavaUIMessages.GenerateToStringDialog_customBuilderConfig_invalidClassError,
110 new Object[] { className }));
111 }
112
113 IStatus typeValidation= custombuildervalidator.validateBuilderType(type);
114 if (!typeValidation.isOK())
115 return typeValidation;
116
117 if (!custombuildervalidator.getAppendMethodSuggestions(type).contains(appendMethod))
118 return new StatusInfo(IStatus.ERROR, MessageFormat.format(JavaUIMessages.GenerateToStringDialog_customBuilderConfig_invalidAppendMethodError,
119 new Object[] { appendMethod }));
120
121 if (!custombuildervalidator.getResultMethodSuggestions(type).contains(resultMethod))
122 return new StatusInfo(IStatus.ERROR, MessageFormat.format(JavaUIMessages.GenerateToStringDialog_customBuilderConfig_invalidResultMethodError,
123 new Object[] { resultMethod }));
124
125 if (!custombuildervalidator.isValidJavaIdentifier(variableName))
126 return new StatusInfo(IStatus.ERROR, MessageFormat.format(JavaUIMessages.GenerateToStringDialog_customBuilderConfig_invalidVariableNameError,
127 new Object[] { variableName }));
128
129 } catch (JavaModelException e) {
130 return new StatusInfo(IStatus.WARNING, JavaUIMessages.GenerateToStringDialog_customBuilderConfig_dataValidationError);
131 }
132 return new StatusInfo();
133 }
134
135 public void generated_1197153510219597381(final CustomBuilderConfigurationDialog custombuilderconfigurationdialog, Composite composite, ModifyListener comboListener) {
136 custombuilderconfigurationdialog.fResultMethodName.addModifyListener(comboListener);
137 if (!custombuilderconfigurationdialog.select(custombuilderconfigurationdialog.fAppendMethodName, appendMethod)) {
138 custombuilderconfigurationdialog.fAppendMethodName.select(0);
139 }
140 if (!custombuilderconfigurationdialog.select(custombuilderconfigurationdialog.fResultMethodName, resultMethod)) {
141 custombuilderconfigurationdialog.fResultMethodName.select(0);
142 }
143
144 custombuilderconfigurationdialog.fChainInvocations= new Button(composite, SWT.CHECK);
145 custombuilderconfigurationdialog.fChainInvocations.setText(JavaUIMessages.GenerateToStringDialog_customBuilderConfig_chainedCallsCheckbox);
146 custombuilderconfigurationdialog.fChainInvocations.setSelection(chainCalls);
147 custombuilderconfigurationdialog.fChainInvocations.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
148 custombuilderconfigurationdialog.fChainInvocations.addSelectionListener(new SelectionAdapter() {
149 @Override
150 public void widgetSelected(SelectionEvent e) {
151 chainCalls= custombuilderconfigurationdialog.fChainInvocations.getSelection();
152 custombuilderconfigurationdialog.enableApplyButton();
153 }
154 });
155 }
156
157 public void generated_4380449884425403228(CustomBuilderConfigurationDialog custombuilderconfigurationdialog, final String[] empty, IType type) throws JavaModelException {
158 custombuilderconfigurationdialog.select(custombuilderconfigurationdialog.fAppendMethodName, appendMethod);
159 custombuilderconfigurationdialog.fResultMethodName.setItems(custombuilderconfigurationdialog.fValidator.getResultMethodSuggestions(type).toArray(empty));
160 custombuilderconfigurationdialog.select(custombuilderconfigurationdialog.fResultMethodName, resultMethod);
161 }
162
163 public void generated_1036961313981835720(CustomBuilderConfigurationDialog custombuilderconfigurationdialog) {
164 appendMethod= custombuilderconfigurationdialog.fAppendMethodName.getText();
165 resultMethod= custombuilderconfigurationdialog.fResultMethodName.getText();
166 }
167
168 public String generated_9161054712216784088(ToStringGenerationContext tostringgenerationcontext) {
169 return className;
170 }
171
172 public String generated_3844037368995630247(ToStringGenerationContext tostringgenerationcontext) {
173 return variableName;
174 }
175
176 public String generated_3511844344444339683(ToStringGenerationContext tostringgenerationcontext) {
177 return appendMethod;
178 }
179
180 public String generated_1592728035830864358(ToStringGenerationContext tostringgenerationcontext) {
181 return resultMethod;
182 }
183
184 public boolean generated_1251988224814424550(ToStringGenerationContext tostringgenerationcontext) {
185 return chainCalls;
186 }
187
188 public void generated_2329411435482929497(ToStringGenerationSettings tostringgenerationsettings, IDialogSettings dialogSettings) {
189 className= ToStringGenerationSettings.asString(dialogSettings.get(ToStringGenerationSettings.SETTINGS_CUSTOMBUILDER_CLASS), ""); //$NON-NLS-1$
190 variableName= ToStringGenerationSettings.asString(dialogSettings.get(ToStringGenerationSettings.SETTINGS_CUSTOMBUILDER_LABEL), "builder"); //$NON-NLS-1$
191 appendMethod= ToStringGenerationSettings.asString(dialogSettings.get(ToStringGenerationSettings.SETTINGS_CUSTOMBUILDER_APPENDMETHOD), "append"); //$NON-NLS-1$
192 resultMethod= ToStringGenerationSettings.asString(dialogSettings.get(ToStringGenerationSettings.SETTINGS_CUSTOMBUILDER_RESULTMETHOD), "toString"); //$NON-NLS-1$
193 chainCalls= tostringgenerationsettings.asBoolean(dialogSettings.get(ToStringGenerationSettings.SETTINGS_CUSTOMBUILDER_CHAINCALLS), false);
194 }
195
196 public CustomBuilderSettings generated_3104832852564215374(ToStringGenerationSettings tostringgenerationsettings) {
197 CustomBuilderSettings result= new CustomBuilderSettings();
198 result.className= className;
199 result.variableName= variableName;
200 result.appendMethod= appendMethod;
201 result.resultMethod= resultMethod;
202 result.chainCalls= chainCalls;
203 return result;
204 }
205
206 public void generated_7480550366222733311(ToStringGenerationSettings tostringgenerationsettings) {
207 tostringgenerationsettings.dialogSettings.put(ToStringGenerationSettings.SETTINGS_CUSTOMBUILDER_CLASS, className);
208 tostringgenerationsettings.dialogSettings.put(ToStringGenerationSettings.SETTINGS_CUSTOMBUILDER_LABEL, variableName);
209 tostringgenerationsettings.dialogSettings.put(ToStringGenerationSettings.SETTINGS_CUSTOMBUILDER_APPENDMETHOD, appendMethod);
210 tostringgenerationsettings.dialogSettings.put(ToStringGenerationSettings.SETTINGS_CUSTOMBUILDER_RESULTMETHOD, resultMethod);
211 tostringgenerationsettings.dialogSettings.put(ToStringGenerationSettings.SETTINGS_CUSTOMBUILDER_CHAINCALLS, chainCalls);
212 tostringgenerationsettings.customBuilderSettings= this;
213 }
214 }
215
216 /** which template should be used to format the output of the toString() method? */
217 public int stringFormatTemplateNumber;
218
219 /**
220 * what is the template (redundancy - this field can be determined basing on
221 * <code>GenerateToStringDialog.getTemplates()</code> and
222 * <code>stringFormatTemplateNumber</code>, but this way it's more convenient)
223 */
224 public String stringFormatTemplate;
225
226 /** what style of code should the toString() method have? */
227 public int toStringStyle;
228
229 /** should the toString() method skip null values? */
230 public boolean skipNulls;
231
232 /** should the toString() method use its own way to show elements of an array? */
233 public boolean customArrayToString;
234
235 /**
236 * should the toString() limit maximum number of elements of arrays/Collections to be
237 * listed?
238 */
239 public boolean limitElements;
240
241 /** what is the maximum number of elements in array/Collection to show? */
242 public int limitValue;
243
244 /** should blocks be forced in if/for/while statements? */
245 public boolean useBlocks;
246
247 /** can generated code use jdk 1.5 API? **/
248 public boolean is50orHigher;
249
250 /** can generated code use jdk 1.6 API? **/
251 public boolean is60orHigher;
252
253 /** settings specific for custom builder code style **/
254 public CustomBuilderSettings customBuilderSettings;
255
256 private IDialogSettings dialogSettings;
257
258 public ToStringGenerationSettings(IDialogSettings dialogSettings) {
259 this.dialogSettings= dialogSettings;
260 limitElements= asBoolean(dialogSettings.get(SETTINGS_LIMITELEMENTS), false);
261 customArrayToString= asBoolean(dialogSettings.get(SETTINGS_IGNOREDEFAULT), true);
262 toStringStyle= asInt(dialogSettings.get(SETTINGS_STRINGSTYLE), 0);
263 limitValue= asInt(dialogSettings.get(SETTINGS_LIMITVALUE), 10);
264 skipNulls= asBoolean(dialogSettings.get(SETTINGS_SKIPNULLS), false);
265 stringFormatTemplateNumber= asInt(dialogSettings.get(SETTINGS_SELECTED_TEMPLATE), 0);
266 customBuilderSettings= new CustomBuilderSettings();
267 customBuilderSettings.generated_2329411435482929497(this, dialogSettings);
268 }
269
270 public ToStringGenerationSettings() {
271
272 }
273
274 public void writeDialogSettings() {
275 dialogSettings.put(SETTINGS_LIMITELEMENTS, limitElements);
276 dialogSettings.put(SETTINGS_IGNOREDEFAULT, customArrayToString);
277 dialogSettings.put(SETTINGS_STRINGSTYLE, toStringStyle);
278 dialogSettings.put(SETTINGS_LIMITVALUE, limitValue);
279 dialogSettings.put(SETTINGS_SKIPNULLS, skipNulls);
280 dialogSettings.put(SETTINGS_SELECTED_TEMPLATE, stringFormatTemplateNumber);
281 }
282
283 /**
284 * Returns a copy of customBuilderSettings. Changes made in the returned object will not affect
285 * this settings object. To save changes made in returned object, use
286 * {@link #writeCustomBuilderSettings(ToStringGenerationSettings.CustomBuilderSettings)}.
287 *
288 * @return copy of custom builder settings object
289 */
290 public CustomBuilderSettings getCustomBuilderSettings() {
291 return customBuilderSettings.generated_3104832852564215374(this);
292 }
293
294 /**
295 * Writes given custom builder settings object to the underlying dialog settings.
296 *
297 * @param customBuilderSettings1 settings to save
298 */
299 public void writeCustomBuilderSettings(CustomBuilderSettings customBuilderSettings1) {
300 customBuilderSettings1.generated_7480550366222733311(this);
301 }
302
303 private boolean asBoolean(String string, boolean defaultValue) {
304 if (string != null) {
305 return StringConverter.asBoolean(string, defaultValue);
306 }
307 return defaultValue;
308 }
309
310 public void generated_5905533701111269973(GenerateToStringDialog generatetostringdialog) {
311 createComments= generatetostringdialog.getGenerateComment();
312 }
313
314 public void generated_8279305899038655747(final GenerateToStringDialog generatetostringdialog, Group group, final Combo styleCombo) {
315 generatetostringdialog.styleButton.addSelectionListener(new SelectionAdapter() {
316 @Override
317 public void widgetSelected(SelectionEvent e) {
318 generatetostringdialog.configureStyleButtonSelected();
319 }
320 });
321
322 generatetostringdialog.skipNullsButton= new Button(group, SWT.CHECK);
323 generatetostringdialog.skipNullsButton.setText(JavaUIMessages.GenerateToStringDialog_skip_null_button);
324 generatetostringdialog.skipNullsButton.setSelection(skipNulls);
325 generatetostringdialog.skipNullsButton.addSelectionListener(new SelectionAdapter() {
326 @Override
327 public void widgetSelected(SelectionEvent event) {
328 skipNulls= ((Button)event.widget).getSelection();
329 }
330 });
331
332 final Button arrayButton= new Button(group, SWT.CHECK);
333 arrayButton.setText(JavaUIMessages.GenerateToStringDialog_ignore_default_button);
334 arrayButton.setSelection(customArrayToString);
335 arrayButton.addSelectionListener(new SelectionAdapter() {
336 @Override
337 public void widgetSelected(SelectionEvent e) {
338 customArrayToString= ((Button)e.widget).getSelection();
339 }
340 });
341
342 Composite limitRow= new Composite(group, SWT.NONE);
343 RowLayout rowLayout= new RowLayout();
344 rowLayout.center= true;
345 rowLayout.marginLeft= rowLayout.marginRight= rowLayout.marginTop= rowLayout.marginBottom= 0;
346 limitRow.setLayout(rowLayout);
347
348 final Button limitButton= new Button(limitRow, SWT.CHECK);
349 limitButton.setText(JavaUIMessages.GenerateToStringDialog_limit_elements_button);
350 limitButton.setSelection(limitElements);
351 limitButton.addSelectionListener(new SelectionAdapter() {
352 @Override
353 public void widgetSelected(SelectionEvent e) {
354 limitElements= ((Button)e.widget).getSelection();
355 }
356 });
357
358 final Spinner limitSpinner= new Spinner(limitRow, SWT.BORDER);
359 limitSpinner.setMinimum(0);
360 limitSpinner.setSelection(limitValue);
361 limitSpinner.addSelectionListener(new SelectionAdapter() {
362 @Override
363 public void widgetSelected(SelectionEvent e) {
364 limitValue= ((Spinner)e.widget).getSelection();
365 }
366 });
367
368 //invoked to change initial enable state of controls
369 generatetostringdialog.changeToStringStyle(styleCombo.getSelectionIndex());
370 }
371
372 public void generated_4135918593992476495(GenerateToStringDialog generatetostringdialog) {
373 generatetostringdialog.formatCombo.select(Math.min(stringFormatTemplateNumber, generatetostringdialog.formatCombo.getItemCount() - 1));
374 }
375
376 public boolean generated_7416749355982389519(GenerateToStringDialog generatetostringdialog, int style) {
377 toStringStyle= style;
378 generatetostringdialog.skipNullsButton.setEnabled(style != GenerateToStringOperation.STRING_FORMAT);
379 boolean enableFormat= (style != GenerateToStringOperation.CUSTOM_BUILDER);
380 return enableFormat;
381 }
382
383 public CodeGenerationSettings generated_8579084277108818275(GenerateToStringAction generatetostringaction, String version) {
384 is50orHigher= !JavaModelUtil.isVersionLessThan(version, JavaCore.VERSION_1_5);
385 is60orHigher= !JavaModelUtil.isVersionLessThan(version, JavaCore.VERSION_1_6);
386 return this;
387 }
388
389 public void generated_5495634267311453766(Object[] selectedMembers, ToStringGenerationContext tostringgenerationcontext, ITypeBinding type) {
390 tostringgenerationcontext.fSelectedMembers= selectedMembers;
391 tostringgenerationcontext.fSettings= this;
392 tostringgenerationcontext.fCustomBuilderSettings= getCustomBuilderSettings();
393 tostringgenerationcontext.fType= type;
394 }
395
396 public int generated_6203512945634259293(ToStringGenerationContext tostringgenerationcontext) {
397 return limitValue;
398 }
399
400 public boolean generated_4720457951684026118(ToStringGenerationContext tostringgenerationcontext) {
401 return is50orHigher;
402 }
403
404 public boolean generated_4408472360174240783(ToStringGenerationContext tostringgenerationcontext) {
405 return is60orHigher;
406 }
407
408 public boolean generated_3890522325313653657(ToStringGenerationContext tostringgenerationcontext) {
409 return createComments;
410 }
411
412 public boolean generated_8719427988826962334(ToStringGenerationContext tostringgenerationcontext) {
413 return customArrayToString;
414 }
415
416 public boolean generated_9053475721656625563(ToStringGenerationContext tostringgenerationcontext) {
417 return useBlocks;
418 }
419
420 public boolean generated_1061606803051821626(ToStringGenerationContext tostringgenerationcontext) {
421 return useKeywordThis;
422 }
423
424 public boolean generated_1165715848829217649(ToStringGenerationContext tostringgenerationcontext) {
425 return limitElements;
426 }
427
428 public boolean generated_5588797316267707432(ToStringGenerationContext tostringgenerationcontext) {
429 return overrideAnnotation;
430 }
431
432 public boolean generated_3397837620992553666(ToStringGenerationContext tostringgenerationcontext) {
433 return skipNulls;
434 }
435
436 private static int asInt(String string, int defaultValue) {
437 if (string != null) {
438 return StringConverter.asInt(string, defaultValue);
439 }
440 return defaultValue;
441 }
442
443 private static String asString(String string, String defaultValue) {
444 if (string != null) {
445 return string;
446 }
447 return defaultValue;
448 }
449}