1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.preferences.formatter;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.HashMap;
16 import java.util.Iterator;
17 import java.util.List;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.SelectionAdapter;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.widgets.Button;
25 import org.eclipse.swt.widgets.Combo;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Group;
29 import org.eclipse.swt.widgets.Label;
31 import org.eclipse.core.runtime.IStatus;
32 import org.eclipse.core.runtime.Status;
34 import org.eclipse.jface.dialogs.IDialogSettings;
35 import org.eclipse.jface.viewers.DoubleClickEvent;
36 import org.eclipse.jface.viewers.IDoubleClickListener;
37 import org.eclipse.jface.viewers.ISelection;
38 import org.eclipse.jface.viewers.ISelectionChangedListener;
39 import org.eclipse.jface.viewers.IStructuredSelection;
40 import org.eclipse.jface.viewers.ITreeContentProvider;
41 import org.eclipse.jface.viewers.LabelProvider;
42 import org.eclipse.jface.viewers.SelectionChangedEvent;
43 import org.eclipse.jface.viewers.StructuredSelection;
44 import org.eclipse.jface.viewers.TreeViewer;
45 import org.eclipse.jface.viewers.Viewer;
47 import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
49 import org.eclipse.jdt.internal.corext.util.Messages;
51 import org.eclipse.jdt.ui.JavaUI;
53 import org.eclipse.jdt.internal.ui.JavaPlugin;
54 import org.eclipse.jdt.internal.ui.util.SWTUtil;
58 * The line wrapping tab page.
60 public class LineWrappingTabPage extends FormatterTabPage {
62 * Represents a line wrapping category. All members are final.
64 final static class Category {
65 public final String key;
66 public final String name;
67 public final String previewText;
68 public final List<Category> children;
69 public final List<Preference> preferences;
73 public Category(String _key, String _previewText, String _name) {
76 this.previewText= _previewText != null ? createPreviewHeader(_name) + _previewText : null;
77 children= new ArrayList<Category>();
78 preferences= new ArrayList<Preference>();
82 * @param _name Category name
84 public Category(String _name) {
85 this(null, null, _name);
89 public String toString() {
93 public void addPreference(Preference specificPreference) {
94 preferences.add(specificPreference);
97 public Preference[] getSpecificPreferences() {
98 return preferences.toArray(new Preference[preferences.size()]);
101 public void setEnabled(boolean state) {
102 for (Preference preference : preferences)
103 preference.setEnabled(state);
106 public List<Category> generated_5026085346794046769(final Category annotations, final Category classDeclarations, final Category constructorDeclarations, final Category methodDeclarations, final Category enumDeclarations, LineWrappingTabPage linewrappingtabpage) {
107 children.add(linewrappingtabpage.fMessageSendArgumentsCategory);
108 children.add(linewrappingtabpage.fMessageSendSelectorCategory);
109 children.add(linewrappingtabpage.fExplicitConstructorArgumentsCategory);
110 children.add(linewrappingtabpage.fAllocationExpressionArgumentsCategory);
111 children.add(linewrappingtabpage.fQualifiedAllocationExpressionCategory);
113 final Category expressions= new Category(FormatterMessages.LineWrappingTabPage_expressions);
114 expressions.children.add(linewrappingtabpage.fBinaryExpressionCategory);
115 expressions.children.add(linewrappingtabpage.fConditionalExpressionCategory);
116 expressions.children.add(linewrappingtabpage.fArrayInitializerExpressionsCategory);
117 expressions.children.add(linewrappingtabpage.fAssignmentCategory);
119 final Category statements= new Category(FormatterMessages.LineWrappingTabPage_statements);
120 statements.children.add(linewrappingtabpage.fCompactIfCategory);
121 statements.children.add(linewrappingtabpage.fTryCategory);
122 statements.children.add(linewrappingtabpage.fCatchCategory);
124 final List<Category> root= new ArrayList<Category>();
125 root.add(annotations);
126 root.add(classDeclarations);
127 root.add(constructorDeclarations);
128 root.add(methodDeclarations);
129 root.add(enumDeclarations);
131 root.add(expressions);
132 root.add(statements);
136 public void generated_6148527569529654916(List<Category> categoriesList, CategoryListener categorylistener) {
137 index= categorylistener.fIndex++;
138 categoriesList.add(this);
139 categorylistener.flatten(categoriesList, children);
142 public String generated_2284278817571461453(CategoryListener categorylistener) {
143 return Messages.format(FormatterMessages.LineWrappingTabPage_group, name.toLowerCase());
146 public void generated_5019980853133752670(SelectionState selectionstate) {
148 value= fWorkingValues.get(key);
150 if (!selectionstate.fElements.contains(this))
151 selectionstate.fElements.add(this);
154 selectionstate.evaluateElements(children.iterator());
158 public String generated_659765559651578846(String previewText, SelectionState selectionstate) {
159 previewText= previewText + this.previewText + "\n\n";
163 public void generated_6071857012505743656(Map<Object, Integer> wrappingMap, Map<Object, Integer> indentMap, Map<Object, Integer> forceMap, SelectionState selectionstate) {
164 final String value= fWorkingValues.get(key);
165 Integer wrappingStyle;
167 Boolean forceWrapping;
170 wrappingStyle= new Integer(DefaultCodeFormatterConstants.getWrappingStyle(value));
171 indentStyle= new Integer(DefaultCodeFormatterConstants.getIndentStyle(value));
172 forceWrapping= new Boolean(DefaultCodeFormatterConstants.getForceWrapping(value));
173 } catch (IllegalArgumentException e) {
174 forceWrapping= new Boolean(false);
175 indentStyle= new Integer(DefaultCodeFormatterConstants.INDENT_DEFAULT);
176 wrappingStyle= new Integer(DefaultCodeFormatterConstants.WRAP_NO_SPLIT);
179 selectionstate.increaseMapEntry(wrappingMap, wrappingStyle);
180 selectionstate.increaseMapEntry(indentMap, indentStyle);
181 selectionstate.increaseMapEntry(forceMap, forceWrapping);
186 private final static String PREF_CATEGORY_INDEX= JavaUI.ID_PLUGIN + "formatter_page.line_wrapping_tab_page.last_category_index"; //$NON-NLS-1$
189 final class CategoryListener implements ISelectionChangedListener, IDoubleClickListener {
191 private final List<Category> fCategoriesList;
193 private int fIndex= 0;
195 public CategoryListener(List<Category> categoriesTree) {
196 fCategoriesList= new ArrayList<Category>();
197 flatten(fCategoriesList, categoriesTree);
200 void flatten(List<Category> categoriesList, List<Category> categoriesTree) {
201 for (final Iterator<Category> iter= categoriesTree.iterator(); iter.hasNext(); ) {
202 final Category category= iter.next();
203 category.generated_6148527569529654916(categoriesList, CategoryListener.this);
207 public void selectionChanged(SelectionChangedEvent event) {
209 fSelection= (IStructuredSelection)event.getSelection();
211 if (fSelection.size() == 0) {
216 if (!fOptionsGroup.isEnabled())
217 enableDefaultComponents(true);
219 fSelectionState.refreshState(fSelection);
221 final Category category= (Category)fSelection.getFirstElement();
222 fDialogSettings.put(PREF_CATEGORY_INDEX, category.index);
224 fOptionsGroup.setText(getGroupLabel(category));
227 private String getGroupLabel(Category category) {
228 if (fSelection.size() == 1) {
229 if (fSelectionState.getElements().size() == 1)
230 return category.generated_2284278817571461453(CategoryListener.this);
231 return Messages.format(FormatterMessages.LineWrappingTabPage_multi_group, new String[] {category.name.toLowerCase(), Integer.toString(fSelectionState.getElements().size())});
233 return Messages.format(FormatterMessages.LineWrappingTabPage_multiple_selections, new String[] {Integer.toString(fSelectionState.getElements().size())});
236 private void disableAll() {
237 enableDefaultComponents(false);
238 fIndentStyleCombo.setEnabled(false);
239 fForceSplit.setEnabled(false);
242 private void enableDefaultComponents(boolean enabled) {
243 fOptionsGroup.setEnabled(enabled);
244 fWrappingStyleCombo.setEnabled(enabled);
245 fWrappingStylePolicy.setEnabled(enabled);
248 public void restoreSelection() {
251 index= fDialogSettings.getInt(PREF_CATEGORY_INDEX);
252 } catch (NumberFormatException ex) {
255 if (index < 0 || index > fCategoriesList.size() - 1) {
256 index= 1; // In order to select a category with preview initially
258 final Category category= fCategoriesList.get(index);
259 fCategoriesViewer.setSelection(new StructuredSelection(new Category[] {category}));
262 public void doubleClick(DoubleClickEvent event) {
263 final ISelection selection= event.getSelection();
264 if (selection instanceof IStructuredSelection) {
265 final Category node= (Category)((IStructuredSelection)selection).getFirstElement();
266 fCategoriesViewer.setExpandedState(node, !fCategoriesViewer.getExpandedState(node));
271 class SelectionState {
272 private List<Category> fElements= new ArrayList<Category>();
273 private boolean fRequiresRelayout;
275 public void refreshState(IStructuredSelection selection) {
276 Map<Object, Integer> wrappingStyleMap= new HashMap<Object, Integer>();
277 Map<Object, Integer> indentStyleMap= new HashMap<Object, Integer>();
278 Map<Object, Integer> forceWrappingMap= new HashMap<Object, Integer>();
279 fRequiresRelayout= false;
280 showSpecificControls(false);
282 evaluateElements(selection.iterator());
283 evaluateMaps(wrappingStyleMap, indentStyleMap, forceWrappingMap);
284 setPreviewText(getPreviewText());
285 refreshControls(wrappingStyleMap, indentStyleMap, forceWrappingMap);
288 public List<Category> getElements() {
292 void evaluateElements(Iterator<Category> iterator) {
295 while (iterator.hasNext()) {
296 category= iterator.next();
297 category.generated_5019980853133752670(SelectionState.this);
301 private void evaluateMaps(Map<Object, Integer> wrappingStyleMap, Map<Object, Integer> indentStyleMap, Map<Object, Integer> forceWrappingMap) {
302 Iterator<Category> iterator= fElements.iterator();
303 while (iterator.hasNext()) {
304 insertIntoMap(wrappingStyleMap, indentStyleMap, forceWrappingMap, iterator.next());
308 private String getPreviewText() {
309 Iterator<Category> iterator= fElements.iterator();
310 String previewText= ""; //$NON-NLS-1$
311 while (iterator.hasNext()) {
312 Category category= iterator.next();
313 previewText= category.generated_659765559651578846(previewText, SelectionState.this); //$NON-NLS-1$
318 private void insertIntoMap(Map<Object, Integer> wrappingMap, Map<Object, Integer> indentMap, Map<Object, Integer> forceMap, Category category) {
319 category.generated_6071857012505743656(wrappingMap, indentMap, forceMap, SelectionState.this);
322 void increaseMapEntry(Map<Object, Integer> map, Object type) {
323 Integer count= map.get(type);
324 if (count == null) // not in map yet -> count == 0
325 map.put(type, new Integer(1));
327 map.put(type, new Integer(count.intValue() + 1));
330 private void refreshControls(Map<Object, Integer> wrappingStyleMap, Map<Object, Integer> indentStyleMap, Map<Object, Integer> forceWrappingMap) {
331 updateCombos(wrappingStyleMap, indentStyleMap);
332 updateButton(forceWrappingMap);
333 Integer wrappingStyleMax= getWrappingStyleMax(wrappingStyleMap);
334 boolean isInhomogeneous= (fElements.size() != wrappingStyleMap.get(wrappingStyleMax).intValue());
335 updateControlEnablement(isInhomogeneous, wrappingStyleMax.intValue());
336 showSpecificControls(true);
337 if (fRequiresRelayout) {
338 fOptionsComposite.layout(true, true);
341 notifyValuesModified();
344 private void showSpecificControls(boolean show) {
345 if (fElements.size() != 1)
348 Preference[] preferences= fElements.get(0).getSpecificPreferences();
349 if (preferences.length == 0)
352 fRequiresRelayout= true;
353 for (int i= 0; i < preferences.length; i++) {
354 Preference preference= preferences[i];
355 Control control= preference.getControl();
356 control.setVisible(show);
357 ((GridData)control.getLayoutData()).exclude= !show;
361 private Integer getWrappingStyleMax(Map<Object, Integer> wrappingStyleMap) {
362 int maxCount= 0, maxStyle= 0;
363 for (int i=0; i<WRAPPING_NAMES.length; i++) {
364 Integer count= wrappingStyleMap.get(new Integer(i));
367 if (count.intValue() > maxCount) {
368 maxCount= count.intValue();
372 return new Integer(maxStyle);
375 private void updateButton(Map<Object, Integer> forceWrappingMap) {
376 Integer nrOfTrue= forceWrappingMap.get(Boolean.TRUE);
377 Integer nrOfFalse= forceWrappingMap.get(Boolean.FALSE);
379 if (nrOfTrue == null || nrOfFalse == null)
380 fForceSplit.setSelection(nrOfTrue != null);
382 fForceSplit.setSelection(nrOfTrue.intValue() > nrOfFalse.intValue());
384 int max= getMax(nrOfTrue, nrOfFalse);
385 String label= FormatterMessages.LineWrappingTabPage_force_split_checkbox_text;
386 fForceSplit.setText(getLabelText(label, max, fElements.size()));
389 private String getLabelText(String label, int count, int nElements) {
390 if (nElements == 1 || count == 0)
392 return Messages.format(FormatterMessages.LineWrappingTabPage_occurences, new String[] {label, Integer.toString(count), Integer.toString(nElements)});
395 private int getMax(Integer nrOfTrue, Integer nrOfFalse) {
396 if (nrOfTrue == null)
397 return nrOfFalse.intValue();
398 if (nrOfFalse == null)
399 return nrOfTrue.intValue();
400 if (nrOfTrue.compareTo(nrOfFalse) >= 0)
401 return nrOfTrue.intValue();
402 return nrOfFalse.intValue();
405 private void updateCombos(Map<Object, Integer> wrappingStyleMap, Map<Object, Integer> indentStyleMap) {
406 updateCombo(fWrappingStyleCombo, wrappingStyleMap, WRAPPING_NAMES);
407 updateCombo(fIndentStyleCombo, indentStyleMap, INDENT_NAMES);
410 private void updateCombo(Combo combo, Map<Object, Integer> map, final String[] items) {
411 String[] newItems= new String[items.length];
412 int maxCount= 0, maxStyle= 0;
414 for(int i = 0; i < items.length; i++) {
415 Integer count= map.get(new Integer(i));
416 int val= (count == null) ? 0 : count.intValue();
417 if (val > maxCount) {
421 newItems[i]= getLabelText(items[i], val, fElements.size());
423 combo.setItems(newItems);
424 combo.setText(newItems[maxStyle]);
427 public void generated_5638102687846375772(LineWrappingTabPage linewrappingtabpage, boolean forceSplit) {
428 Iterator<Category> iterator= fElements.iterator();
430 while (iterator.hasNext()) {
431 currentKey= iterator.next().key;
433 linewrappingtabpage.changeForceSplit(currentKey, forceSplit);
434 } catch (IllegalArgumentException e) {
435 linewrappingtabpage.fWorkingValues.put(currentKey, DefaultCodeFormatterConstants.createAlignmentValue(forceSplit, DefaultCodeFormatterConstants.WRAP_NO_SPLIT, DefaultCodeFormatterConstants.INDENT_DEFAULT));
436 JavaPlugin.log(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK,
437 Messages.format(FormatterMessages.LineWrappingTabPage_error_invalid_value, currentKey), e));
440 refreshState(linewrappingtabpage.fSelection);
443 public void generated_6265589040558390253(LineWrappingTabPage linewrappingtabpage, int wrappingStyle) {
444 Iterator<Category> iterator= fElements.iterator();
446 while (iterator.hasNext()) {
447 currentKey= iterator.next().key;
449 linewrappingtabpage.changeWrappingStyle(currentKey, wrappingStyle);
450 } catch (IllegalArgumentException e) {
451 linewrappingtabpage.fWorkingValues.put(currentKey, DefaultCodeFormatterConstants.createAlignmentValue(false, wrappingStyle, DefaultCodeFormatterConstants.INDENT_DEFAULT));
452 JavaPlugin.log(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK,
453 Messages.format(FormatterMessages.LineWrappingTabPage_error_invalid_value, currentKey), e));
456 refreshState(linewrappingtabpage.fSelection);
459 public void generated_5207790117885213284(LineWrappingTabPage linewrappingtabpage, int indentStyle) {
460 Iterator<Category> iterator= fElements.iterator();
462 while (iterator.hasNext()) {
463 currentKey= iterator.next().key;
465 linewrappingtabpage.changeIndentStyle(currentKey, indentStyle);
466 } catch (IllegalArgumentException e) {
467 linewrappingtabpage.fWorkingValues.put(currentKey, DefaultCodeFormatterConstants.createAlignmentValue(false, DefaultCodeFormatterConstants.WRAP_NO_SPLIT, indentStyle));
468 JavaPlugin.log(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK,
469 Messages.format(FormatterMessages.LineWrappingTabPage_error_invalid_value, currentKey), e));
472 refreshState(linewrappingtabpage.fSelection);
476 protected static final String[] INDENT_NAMES = {
477 FormatterMessages.LineWrappingTabPage_indentation_default,
478 FormatterMessages.LineWrappingTabPage_indentation_on_column,
479 FormatterMessages.LineWrappingTabPage_indentation_by_one
483 protected static final String[] WRAPPING_NAMES = {
484 FormatterMessages.LineWrappingTabPage_splitting_do_not_split,
485 FormatterMessages.LineWrappingTabPage_splitting_wrap_when_necessary, // COMPACT_SPLIT
486 FormatterMessages.LineWrappingTabPage_splitting_always_wrap_first_others_when_necessary, // COMPACT_FIRST_BREAK_SPLIT
487 FormatterMessages.LineWrappingTabPage_splitting_wrap_always, // ONE_PER_LINE_SPLIT
488 FormatterMessages.LineWrappingTabPage_splitting_wrap_always_indent_all_but_first, // NEXT_SHIFTED_SPLIT
489 FormatterMessages.LineWrappingTabPage_splitting_wrap_always_except_first_only_if_necessary
493 private final Category fCompactIfCategory= new Category(
494 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_COMPACT_IF,
495 "class Example {" + //$NON-NLS-1$
496 "int foo(int argument) {" + //$NON-NLS-1$
497 " if (argument==0) return 0;" + //$NON-NLS-1$
498 " if (argument==1) return 42; else return 43;" + //$NON-NLS-1$
500 FormatterMessages.LineWrappingTabPage_compact_if_else
503 private final Category fTryCategory= new Category(
504 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_RESOURCES_IN_TRY,
505 "class Example {" + //$NON-NLS-1$
506 "void foo() {" + //$NON-NLS-1$
507 "try (FileReader reader1 = new FileReader(\"file1\"); " + //$NON-NLS-1$
508 " FileReader reader2 = new FileReader(\"file2\")) {" + //$NON-NLS-1$
511 FormatterMessages.LineWrappingTabPage_try
514 final Category fCatchCategory= new Category(
515 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_UNION_TYPE_IN_MULTICATCH,
516 "class Example {" + //$NON-NLS-1$
517 "void foo() {" + //$NON-NLS-1$
518 "try {" + //$NON-NLS-1$
519 "} catch (IllegalArgumentException | NullPointerException | ClassCastException e) {" + //$NON-NLS-1$
520 " e.printStackTrace();" + //$NON-NLS-1$
523 FormatterMessages.LineWrappingTabPage_catch
527 private final Category fTypeDeclarationSuperclassCategory= new Category(
528 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_SUPERCLASS_IN_TYPE_DECLARATION,
529 "class Example extends OtherClass {}", //$NON-NLS-1$
530 FormatterMessages.LineWrappingTabPage_extends_clause
534 private final Category fTypeDeclarationSuperinterfacesCategory= new Category(
535 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_SUPERINTERFACES_IN_TYPE_DECLARATION,
536 "class Example implements I1, I2, I3 {}", //$NON-NLS-1$
537 FormatterMessages.LineWrappingTabPage_implements_clause
541 private final Category fConstructorDeclarationsParametersCategory= new Category(
542 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_PARAMETERS_IN_CONSTRUCTOR_DECLARATION,
543 "class Example {Example(int arg1, int arg2, int arg3, int arg4, int arg5, int arg6) { this();}" + //$NON-NLS-1$
544 "Example() {}}", //$NON-NLS-1$
545 FormatterMessages.LineWrappingTabPage_parameters
548 private final Category fMethodDeclarationsCategory= new Category(
549 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_METHOD_DECLARATION,
550 "class Example {public final synchronized java.lang.String a_method_with_a_long_name() {}}", //$NON-NLS-1$
551 FormatterMessages.LineWrappingTabPage_declaration
554 private final Category fMethodDeclarationsParametersCategory= new Category(
555 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_PARAMETERS_IN_METHOD_DECLARATION,
556 "class Example {void foo(int arg1, int arg2, int arg3, int arg4, int arg5, int arg6) {}}", //$NON-NLS-1$
557 FormatterMessages.LineWrappingTabPage_parameters
560 private final Category fMessageSendArgumentsCategory= new Category(
561 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_METHOD_INVOCATION,
562 "class Example {void foo() {Other.bar( 100,\nnested(200,\n300,\n400,\n500,\n600,\n700,\n800,\n900 ));}}", //$NON-NLS-1$
563 FormatterMessages.LineWrappingTabPage_arguments
566 private final Category fMessageSendSelectorCategory= new Category(
567 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_SELECTOR_IN_METHOD_INVOCATION,
568 "class Example {int foo(Some a) {return a.getFirst();}}", //$NON-NLS-1$
569 FormatterMessages.LineWrappingTabPage_qualified_invocations
572 private final Category fMethodThrowsClauseCategory= new Category(
573 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_THROWS_CLAUSE_IN_METHOD_DECLARATION,
574 "class Example {" + //$NON-NLS-1$
575 "int foo() throws FirstException, SecondException, ThirdException {" + //$NON-NLS-1$
576 " return Other.doSomething();}}", //$NON-NLS-1$
577 FormatterMessages.LineWrappingTabPage_throws_clause
580 private final Category fConstructorThrowsClauseCategory= new Category(
581 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_THROWS_CLAUSE_IN_CONSTRUCTOR_DECLARATION,
582 "class Example {" + //$NON-NLS-1$
583 "Example() throws FirstException, SecondException, ThirdException {" + //$NON-NLS-1$
584 " return Other.doSomething();}}", //$NON-NLS-1$
585 FormatterMessages.LineWrappingTabPage_throws_clause
589 private final Category fAllocationExpressionArgumentsCategory= new Category(
590 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_ALLOCATION_EXPRESSION,
591 "class Example {SomeClass foo() {return new SomeClass(100,\n200,\n300,\n400,\n500,\n600,\n700,\n800,\n900 );}}", //$NON-NLS-1$
592 FormatterMessages.LineWrappingTabPage_object_allocation
595 private final Category fQualifiedAllocationExpressionCategory= new Category (
596 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_QUALIFIED_ALLOCATION_EXPRESSION,
597 "class Example {SomeClass foo() {return SomeOtherClass.new SomeClass(100,\n200,\n300,\n400,\n500 );}}", //$NON-NLS-1$
598 FormatterMessages.LineWrappingTabPage_qualified_object_allocation
601 private final Category fArrayInitializerExpressionsCategory= new Category(
602 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_EXPRESSIONS_IN_ARRAY_INITIALIZER,
603 "class Example {int [] fArray= {1,\n2,\n3,\n4,\n5,\n6,\n7,\n8,\n9,\n10,\n11,\n12};}", //$NON-NLS-1$
604 FormatterMessages.LineWrappingTabPage_array_init
607 private final Category fExplicitConstructorArgumentsCategory= new Category(
608 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_EXPLICIT_CONSTRUCTOR_CALL,
609 "class Example extends AnotherClass {Example() {super(100,\n200,\n300,\n400,\n500,\n600,\n700);}}", //$NON-NLS-1$
610 FormatterMessages.LineWrappingTabPage_explicit_constructor_invocations
613 private final Category fConditionalExpressionCategory= new Category(
614 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_CONDITIONAL_EXPRESSION,
615 "class Example extends AnotherClass {int Example(boolean Argument) {return argument ? 100000 : 200000;}}", //$NON-NLS-1$
616 FormatterMessages.LineWrappingTabPage_conditionals
619 private final Category fBinaryExpressionCategory= new Category(
620 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_BINARY_EXPRESSION,
621 "class Example extends AnotherClass {" + //$NON-NLS-1$
622 "int foo() {" + //$NON-NLS-1$
623 " int sum= 100\n + 200\n + 300\n + 400\n + 500\n + 600\n + 700\n + 800;" + //$NON-NLS-1$
624 " int product= 1\n * 2\n * 3\n * 4\n * 5\n * 6\n * 7\n * 8\n * 9\n * 10;" + //$NON-NLS-1$
625 " boolean val= true && false && true && false && true;" + //$NON-NLS-1$
626 " return product / sum;}}", //$NON-NLS-1$
627 FormatterMessages.LineWrappingTabPage_binary_exprs
630 private final Category fAnnotationArgumentsCategory= new Category(
631 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_ANNOTATION,
632 "@MyAnnotation(value1 = \"this is an example\", value2 = \"of an annotation\", value3 = \"with several arguments\", value4 = \"which may need to be wrapped\")\n" + //$NON-NLS-1$
633 "class Example {}", //$NON-NLS-1$
634 FormatterMessages.LineWrappingTabPage_annotations_arguments
637 private final Category fEnumConstArgumentsCategory= new Category(
638 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_ENUM_CONSTANT,
639 "enum Example {" + //$NON-NLS-1$
640 "GREEN(0, 255, 0), RED(255, 0, 0) }", //$NON-NLS-1$
641 FormatterMessages.LineWrappingTabPage_enum_constant_arguments
644 private final Category fEnumDeclInterfacesCategory= new Category(
645 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_SUPERINTERFACES_IN_ENUM_DECLARATION,
646 "enum Example implements A, B, C {" + //$NON-NLS-1$
648 FormatterMessages.LineWrappingTabPage_enum_superinterfaces
651 private final Category fEnumConstantsCategory= new Category(
652 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUM_CONSTANTS,
653 "enum Example {" + //$NON-NLS-1$
654 "CANCELLED, RUNNING, WAITING, FINISHED }" + //$NON-NLS-1$
655 "enum Example {" + //$NON-NLS-1$
656 "GREEN(0, 255, 0), RED(255, 0, 0) }", //$NON-NLS-1$
657 FormatterMessages.LineWrappingTabPage_enum_constants
660 private final Category fAssignmentCategory= new Category(
661 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ASSIGNMENT,
662 "class Example {" + //$NON-NLS-1$
663 "private static final String string = \"TextTextText\";" + //$NON-NLS-1$
664 "void foo() {" + //$NON-NLS-1$
665 "for (int i = 0; i < 10; i++) {}" + //$NON-NLS-1$
666 "String s;" + //$NON-NLS-1$
667 "s = \"TextTextText\";}}", //$NON-NLS-1$
668 FormatterMessages.LineWrappingTabPage_assignment_alignment
672 * The default preview line width.
674 private static int DEFAULT_PREVIEW_WINDOW_LINE_WIDTH= 40;
677 * The key to save the user's preview window width in the dialog settings.
679 static final String PREF_PREVIEW_LINE_WIDTH= JavaUI.ID_PLUGIN + ".codeformatter.line_wrapping_tab_page.preview_line_width"; //$NON-NLS-1$
682 * The dialog settings.
684 protected final IDialogSettings fDialogSettings;
686 protected TreeViewer fCategoriesViewer;
687 protected Label fWrappingStylePolicy;
688 protected Combo fWrappingStyleCombo;
689 protected Label fIndentStylePolicy;
690 protected Combo fIndentStyleCombo;
691 protected Button fForceSplit;
693 protected CompilationUnitPreview fPreview;
695 protected Group fOptionsGroup;
698 * A collection containing the categories tree. This is used as model for the tree viewer.
701 private final List<Category> fCategories;
704 * The category listener which makes the selection persistent.
706 protected final CategoryListener fCategoryListener;
709 * The current selection of elements.
711 protected IStructuredSelection fSelection;
714 * An object containing the state for the UI.
716 SelectionState fSelectionState;
719 * A special options store wherein the preview line width is kept.
721 protected final Map<String, String> fPreviewPreferences;
724 * The key for the preview line width.
726 final String LINE_SPLIT= DefaultCodeFormatterConstants.FORMATTER_LINE_SPLIT;
729 private Composite fOptionsComposite;
732 * Create a new line wrapping tab page.
734 * @param modifyDialog the modify dialog
735 * @param workingValues the values
737 public LineWrappingTabPage(ModifyDialog modifyDialog, Map<String, String> workingValues) {
738 super(modifyDialog, workingValues);
740 fDialogSettings= JavaPlugin.getDefault().getDialogSettings();
742 final String previewLineWidth= fDialogSettings.get(PREF_PREVIEW_LINE_WIDTH);
744 fPreviewPreferences= new HashMap<String, String>();
745 fPreviewPreferences.put(LINE_SPLIT, previewLineWidth != null ? previewLineWidth : Integer.toString(DEFAULT_PREVIEW_WINDOW_LINE_WIDTH));
747 fCategories= createCategories();
748 fCategoryListener= new CategoryListener(fCategories);
752 * @return Create the categories tree.
754 protected List<Category> createCategories() {
756 final Category annotations = new Category(FormatterMessages.LineWrappingTabPage_annotations);
757 annotations.children.add(fAnnotationArgumentsCategory);
759 final Category classDeclarations= new Category(FormatterMessages.LineWrappingTabPage_class_decls);
760 classDeclarations.children.add(fTypeDeclarationSuperclassCategory);
761 classDeclarations.children.add(fTypeDeclarationSuperinterfacesCategory);
763 final Category constructorDeclarations= new Category(null, null, FormatterMessages.LineWrappingTabPage_constructor_decls);
764 constructorDeclarations.children.add(fConstructorDeclarationsParametersCategory);
765 constructorDeclarations.children.add(fConstructorThrowsClauseCategory);
767 final Category methodDeclarations= new Category(null, null, FormatterMessages.LineWrappingTabPage_method_decls);
768 methodDeclarations.children.add(fMethodDeclarationsCategory);
769 methodDeclarations.children.add(fMethodDeclarationsParametersCategory);
770 methodDeclarations.children.add(fMethodThrowsClauseCategory);
772 final Category enumDeclarations= new Category(FormatterMessages.LineWrappingTabPage_enum_decls);
773 enumDeclarations.children.add(fEnumConstantsCategory);
774 enumDeclarations.children.add(fEnumDeclInterfacesCategory);
775 enumDeclarations.children.add(fEnumConstArgumentsCategory);
777 final Category functionCalls= new Category(FormatterMessages.LineWrappingTabPage_function_calls);
778 final List<Category> root= functionCalls.generated_5026085346794046769(annotations, classDeclarations, constructorDeclarations, methodDeclarations, enumDeclarations, this);
784 protected void doCreatePreferences(Composite composite, int numColumns) {
786 fOptionsComposite= composite;
788 final Group lineWidthGroup= createGroup(numColumns, composite, FormatterMessages.LineWrappingTabPage_general_settings);
790 createNumberPref(lineWidthGroup, numColumns, FormatterMessages.LineWrappingTabPage_width_indent_option_max_line_width, DefaultCodeFormatterConstants.FORMATTER_LINE_SPLIT, 0, 9999);
791 createNumberPref(lineWidthGroup, numColumns, FormatterMessages.LineWrappingTabPage_width_indent_option_default_indent_wrapped, DefaultCodeFormatterConstants.FORMATTER_CONTINUATION_INDENTATION, 0, 9999);
792 createNumberPref(lineWidthGroup, numColumns, FormatterMessages.LineWrappingTabPage_width_indent_option_default_indent_array, DefaultCodeFormatterConstants.FORMATTER_CONTINUATION_INDENTATION_FOR_ARRAY_INITIALIZER, 0, 9999);
793 createCheckboxPref(lineWidthGroup, numColumns, FormatterMessages.LineWrappingTabPage_do_not_join_lines, DefaultCodeFormatterConstants.FORMATTER_JOIN_WRAPPED_LINES, TRUE_FALSE);
794 createCheckboxPref(lineWidthGroup, numColumns, FormatterMessages.LineWrappingTabPage_wrap_outer_expressions_when_nested, DefaultCodeFormatterConstants.FORMATTER_WRAP_OUTER_EXPRESSIONS_WHEN_NESTED, FALSE_TRUE);
796 fCategoriesViewer= new TreeViewer(composite /*categoryGroup*/, SWT.MULTI | SWT.BORDER | SWT.READ_ONLY | SWT.V_SCROLL );
797 fCategoriesViewer.setContentProvider(new ITreeContentProvider() {
798 public Object[] getElements(Object inputElement) {
799 return ((Collection<?>)inputElement).toArray();
801 public Object[] getChildren(Object parentElement) {
802 return ((Category)parentElement).children.toArray();
804 public Object getParent(Object element) { return null; }
805 public boolean hasChildren(Object element) {
806 return !((Category)element).children.isEmpty();
808 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
809 public void dispose() {}
811 fCategoriesViewer.setLabelProvider(new LabelProvider());
812 fCategoriesViewer.setInput(fCategories);
814 fCategoriesViewer.setExpandedElements(fCategories.toArray());
816 final GridData gd= createGridData(numColumns, GridData.FILL_BOTH, SWT.DEFAULT);
817 gd.heightHint= fPixelConverter.convertHeightInCharsToPixels(5);
818 fCategoriesViewer.getControl().setLayoutData(gd);
820 fOptionsGroup = createGroup(numColumns, composite, ""); //$NON-NLS-1$
822 // label "Select split style:"
823 fWrappingStylePolicy= createLabel(numColumns, fOptionsGroup, FormatterMessages.LineWrappingTabPage_wrapping_policy_label_text);
825 // combo SplitStyleCombo
826 fWrappingStyleCombo= new Combo(fOptionsGroup, SWT.SINGLE | SWT.READ_ONLY);
827 SWTUtil.setDefaultVisibleItemCount(fWrappingStyleCombo);
828 fWrappingStyleCombo.setItems(WRAPPING_NAMES);
829 fWrappingStyleCombo.setLayoutData(createGridData(numColumns, GridData.HORIZONTAL_ALIGN_FILL, 0));
831 // button "Force split"
832 fForceSplit= new Button(fOptionsGroup, SWT.CHECK);
833 String label= FormatterMessages.LineWrappingTabPage_force_split_checkbox_text;
834 fForceSplit.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, numColumns - 1, 1));
835 fForceSplit.setText(label);
837 // button "Wrap before operator"
838 Preference expressionWrapPositionPreference= createCheckboxPref(fOptionsGroup, 1, FormatterMessages.LineWrappingTabPage_binary_expression_wrap_operator, DefaultCodeFormatterConstants.FORMATTER_WRAP_BEFORE_BINARY_OPERATOR, FALSE_TRUE);
839 Control control= expressionWrapPositionPreference.getControl();
840 control.setVisible(false);
841 GridData layoutData= (GridData)control.getLayoutData();
842 layoutData.exclude= true;
843 layoutData.horizontalAlignment= SWT.BEGINNING;
844 layoutData.horizontalSpan= numColumns - 1;
845 layoutData.grabExcessHorizontalSpace= false;
846 fBinaryExpressionCategory.addPreference(expressionWrapPositionPreference);
848 // button "Wrap before '|' operator" in multi-catch
849 Preference expressionWrapMulticatchPositionPreference= createCheckboxPref(fOptionsGroup, 1, FormatterMessages.LineWrappingTabPage_multicatch_wrap_operator, DefaultCodeFormatterConstants.FORMATTER_WRAP_BEFORE_OR_OPERATOR_MULTICATCH, FALSE_TRUE);
850 expressionWrapMulticatchPositionPreference.generated_8795498692018477341(numColumns, this);
852 // label "Select indentation style:"
853 fIndentStylePolicy= createLabel(numColumns, fOptionsGroup, FormatterMessages.LineWrappingTabPage_indentation_policy_label_text);
855 // combo IndentStyleCombo
856 fIndentStyleCombo= new Combo(fOptionsGroup, SWT.SINGLE | SWT.READ_ONLY);
857 SWTUtil.setDefaultVisibleItemCount(fIndentStyleCombo);
858 fIndentStyleCombo.setItems(INDENT_NAMES);
859 fIndentStyleCombo.setLayoutData(createGridData(numColumns, GridData.HORIZONTAL_ALIGN_FILL, 0));
861 // selection state object
862 fSelectionState= new SelectionState();
867 protected Composite doCreatePreviewPane(Composite composite, int numColumns) {
869 super.doCreatePreviewPane(composite, numColumns);
871 Composite previewLineWidthContainer= new Composite(composite, SWT.NONE);
872 previewLineWidthContainer.setLayout(createGridLayout(2, false));
874 final NumberPreference previewLineWidth= new NumberPreference(previewLineWidthContainer, 2, fPreviewPreferences, LINE_SPLIT,
875 0, 9999, FormatterMessages.LineWrappingTabPage_line_width_for_preview_label_text);
876 previewLineWidth.generated_1702283291770472824(this);
882 * @see org.eclipse.jdt.internal.ui.preferences.formatter.ModifyDialogTabPage#doCreateJavaPreview(org.eclipse.swt.widgets.Composite)
885 protected JavaPreview doCreateJavaPreview(Composite parent) {
886 fPreview= new CompilationUnitPreview(fWorkingValues, parent);
892 protected void initializePage() {
894 fCategoriesViewer.addSelectionChangedListener(fCategoryListener);
895 fCategoriesViewer.addDoubleClickListener(fCategoryListener);
897 fForceSplit.addSelectionListener(new SelectionAdapter() {
899 public void widgetSelected(SelectionEvent e) {
900 forceSplitChanged(fForceSplit.getSelection());
903 fIndentStyleCombo.addSelectionListener( new SelectionAdapter() {
905 public void widgetSelected(SelectionEvent e) {
906 indentStyleChanged(((Combo)e.widget).getSelectionIndex());
909 fWrappingStyleCombo.addSelectionListener( new SelectionAdapter() {
911 public void widgetSelected(SelectionEvent e) {
912 wrappingStyleChanged(((Combo)e.widget).getSelectionIndex());
916 fCategoryListener.restoreSelection();
918 fDefaultFocusManager.generated_4449091096061266128(this);
922 protected void doUpdatePreview() {
923 super.doUpdatePreview();
924 final String normalSetting= fWorkingValues.get(LINE_SPLIT);
925 fWorkingValues.put(LINE_SPLIT, fPreviewPreferences.get(LINE_SPLIT));
927 fWorkingValues.put(LINE_SPLIT, normalSetting);
930 protected void setPreviewText(String text) {
931 final String normalSetting= fWorkingValues.get(LINE_SPLIT);
932 fWorkingValues.put(LINE_SPLIT, fPreviewPreferences.get(LINE_SPLIT));
933 fPreview.setPreviewText(text);
934 fWorkingValues.put(LINE_SPLIT, normalSetting);
937 protected void forceSplitChanged(boolean forceSplit) {
938 fSelectionState.generated_5638102687846375772(this, forceSplit);
941 private void changeForceSplit(String currentKey, boolean forceSplit) throws IllegalArgumentException{
942 String value= fWorkingValues.get(currentKey);
943 value= DefaultCodeFormatterConstants.setForceWrapping(value, forceSplit);
945 throw new IllegalArgumentException();
946 fWorkingValues.put(currentKey, value);
949 protected void wrappingStyleChanged(int wrappingStyle) {
950 fSelectionState.generated_6265589040558390253(this, wrappingStyle);
953 private void changeWrappingStyle(String currentKey, int wrappingStyle) throws IllegalArgumentException {
954 String value= fWorkingValues.get(currentKey);
955 value= DefaultCodeFormatterConstants.setWrappingStyle(value, wrappingStyle);
957 throw new IllegalArgumentException();
958 fWorkingValues.put(currentKey, value);
961 protected void indentStyleChanged(int indentStyle) {
962 fSelectionState.generated_5207790117885213284(this, indentStyle);
965 private void changeIndentStyle(String currentKey, int indentStyle) throws IllegalArgumentException{
966 String value= fWorkingValues.get(currentKey);
967 value= DefaultCodeFormatterConstants.setIndentStyle(value, indentStyle);
969 throw new IllegalArgumentException();
970 fWorkingValues.put(currentKey, value);
973 protected void updateControlEnablement(boolean inhomogenous, int wrappingStyle) {
974 boolean doSplit= wrappingStyle != DefaultCodeFormatterConstants.WRAP_NO_SPLIT;
975 fIndentStylePolicy.setEnabled(true);
977 boolean isEnabled= inhomogenous || doSplit;
978 fIndentStyleCombo.setEnabled(isEnabled);
979 fForceSplit.setEnabled(isEnabled);
980 fBinaryExpressionCategory.setEnabled(isEnabled);
981 fCatchCategory.setEnabled(isEnabled);