1 11 package org.eclipse.jdt.internal.ui.preferences.formatter; 12 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 ; 18 import java.util.Map ; 19 import java.util.Observable ; 20 import java.util.Observer ; 21 22 import org.eclipse.core.runtime.IStatus; 23 import org.eclipse.core.runtime.Status; 24 25 import org.eclipse.swt.SWT; 26 import org.eclipse.swt.events.SelectionAdapter; 27 import org.eclipse.swt.events.SelectionEvent; 28 import org.eclipse.swt.layout.GridData; 29 import org.eclipse.swt.widgets.Button; 30 import org.eclipse.swt.widgets.Combo; 31 import org.eclipse.swt.widgets.Composite; 32 import org.eclipse.swt.widgets.Control; 33 import org.eclipse.swt.widgets.Group; 34 import org.eclipse.swt.widgets.Label; 35 36 import org.eclipse.jface.dialogs.IDialogSettings; 37 import org.eclipse.jface.viewers.DoubleClickEvent; 38 import org.eclipse.jface.viewers.IDoubleClickListener; 39 import org.eclipse.jface.viewers.ISelection; 40 import org.eclipse.jface.viewers.ISelectionChangedListener; 41 import org.eclipse.jface.viewers.IStructuredSelection; 42 import org.eclipse.jface.viewers.ITreeContentProvider; 43 import org.eclipse.jface.viewers.LabelProvider; 44 import org.eclipse.jface.viewers.SelectionChangedEvent; 45 import org.eclipse.jface.viewers.StructuredSelection; 46 import org.eclipse.jface.viewers.TreeViewer; 47 import org.eclipse.jface.viewers.Viewer; 48 49 import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; 50 51 import org.eclipse.jdt.internal.corext.util.Messages; 52 53 import org.eclipse.jdt.ui.JavaUI; 54 55 import org.eclipse.jdt.internal.ui.JavaPlugin; 56 57 58 61 public class LineWrappingTabPage extends FormatterTabPage { 62 65 private static String [] FALSE_TRUE = { 66 DefaultCodeFormatterConstants.FALSE, 67 DefaultCodeFormatterConstants.TRUE 68 }; 69 70 73 private final static class Category { 74 public final String key; 75 public final String name; 76 public final String previewText; 77 public final List children; 78 public final List preferences; 79 80 public int index; 81 82 public Category(String _key, String _previewText, String _name) { 83 this.key= _key; 84 this.name= _name; 85 this.previewText= _previewText != null ? createPreviewHeader(_name) + _previewText : null; 86 children= new ArrayList (); 87 preferences= new ArrayList (); 88 } 89 90 93 public Category(String _name) { 94 this(null, null, _name); 95 } 96 97 public String toString() { 98 return name; 99 } 100 101 public void addPreference(Preference specificPreference) { 102 preferences.add(specificPreference); 103 } 104 105 public Preference[] getSpecificPreferences() { 106 return (Preference[])preferences.toArray(new Preference[preferences.size()]); 107 } 108 } 109 110 111 private final static String PREF_CATEGORY_INDEX= JavaUI.ID_PLUGIN + "formatter_page.line_wrapping_tab_page.last_category_index"; 113 114 private final class CategoryListener implements ISelectionChangedListener, IDoubleClickListener { 115 116 private final List fCategoriesList; 117 118 private int fIndex= 0; 119 120 public CategoryListener(List categoriesTree) { 121 fCategoriesList= new ArrayList (); 122 flatten(fCategoriesList, categoriesTree); 123 } 124 125 private void flatten(List categoriesList, List categoriesTree) { 126 for (final Iterator iter= categoriesTree.iterator(); iter.hasNext(); ) { 127 final Category category= (Category) iter.next(); 128 category.index= fIndex++; 129 categoriesList.add(category); 130 flatten(categoriesList, category.children); 131 } 132 } 133 134 public void add(Category category) { 135 category.index= fIndex++; 136 fCategoriesList.add(category); 137 } 138 139 public void selectionChanged(SelectionChangedEvent event) { 140 if (event != null) 141 fSelection= (IStructuredSelection)event.getSelection(); 142 143 if (fSelection.size() == 0) { 144 disableAll(); 145 return; 146 } 147 148 if (!fOptionsGroup.isEnabled()) 149 enableDefaultComponents(true); 150 151 fSelectionState.refreshState(fSelection); 152 153 final Category category= (Category)fSelection.getFirstElement(); 154 fDialogSettings.put(PREF_CATEGORY_INDEX, category.index); 155 156 fOptionsGroup.setText(getGroupLabel(category)); 157 } 158 159 private String getGroupLabel(Category category) { 160 if (fSelection.size() == 1) { 161 if (fSelectionState.getElements().size() == 1) 162 return Messages.format(FormatterMessages.LineWrappingTabPage_group, category.name.toLowerCase()); 163 return Messages.format(FormatterMessages.LineWrappingTabPage_multi_group, new String [] {category.name.toLowerCase(), Integer.toString(fSelectionState.getElements().size())}); 164 } 165 return Messages.format(FormatterMessages.LineWrappingTabPage_multiple_selections, new String [] {Integer.toString(fSelectionState.getElements().size())}); 166 } 167 168 private void disableAll() { 169 enableDefaultComponents(false); 170 fIndentStyleCombo.setEnabled(false); 171 fForceSplit.setEnabled(false); 172 } 173 174 private void enableDefaultComponents(boolean enabled) { 175 fOptionsGroup.setEnabled(enabled); 176 fWrappingStyleCombo.setEnabled(enabled); 177 fWrappingStylePolicy.setEnabled(enabled); 178 } 179 180 public void restoreSelection() { 181 int index; 182 try { 183 index= fDialogSettings.getInt(PREF_CATEGORY_INDEX); 184 } catch (NumberFormatException ex) { 185 index= -1; 186 } 187 if (index < 0 || index > fCategoriesList.size() - 1) { 188 index= 1; } 190 final Category category= (Category)fCategoriesList.get(index); 191 fCategoriesViewer.setSelection(new StructuredSelection(new Category[] {category})); 192 } 193 194 public void doubleClick(DoubleClickEvent event) { 195 final ISelection selection= event.getSelection(); 196 if (selection instanceof IStructuredSelection) { 197 final Category node= (Category)((IStructuredSelection)selection).getFirstElement(); 198 fCategoriesViewer.setExpandedState(node, !fCategoriesViewer.getExpandedState(node)); 199 } 200 } 201 } 202 203 private class SelectionState { 204 private List fElements= new ArrayList (); 205 private boolean fRequiresRelayout; 206 207 public void refreshState(IStructuredSelection selection) { 208 Map wrappingStyleMap= new HashMap (); 209 Map indentStyleMap= new HashMap (); 210 Map forceWrappingMap= new HashMap (); 211 fRequiresRelayout= false; 212 showSpecificControls(false); 213 fElements.clear(); 214 evaluateElements(selection.iterator()); 215 evaluateMaps(wrappingStyleMap, indentStyleMap, forceWrappingMap); 216 setPreviewText(getPreviewText(wrappingStyleMap, indentStyleMap, forceWrappingMap)); 217 refreshControls(wrappingStyleMap, indentStyleMap, forceWrappingMap); 218 } 219 220 public List getElements() { 221 return fElements; 222 } 223 224 private void evaluateElements(Iterator iterator) { 225 Category category; 226 String value; 227 while (iterator.hasNext()) { 228 category= (Category) iterator.next(); 229 value= (String )fWorkingValues.get(category.key); 230 if (value != null) { 231 if (!fElements.contains(category)) 232 fElements.add(category); 233 } 234 else { 235 evaluateElements(category.children.iterator()); 236 } 237 } 238 } 239 240 private void evaluateMaps(Map wrappingStyleMap, Map indentStyleMap, Map forceWrappingMap) { 241 Iterator iterator= fElements.iterator(); 242 while (iterator.hasNext()) { 243 insertIntoMap(wrappingStyleMap, indentStyleMap, forceWrappingMap, (Category)iterator.next()); 244 } 245 } 246 247 private String getPreviewText(Map wrappingMap, Map indentMap, Map forceMap) { 248 Iterator iterator= fElements.iterator(); 249 String previewText= ""; while (iterator.hasNext()) { 251 Category category= (Category)iterator.next(); 252 previewText= previewText + category.previewText + "\n\n"; } 254 return previewText; 255 } 256 257 private void insertIntoMap(Map wrappingMap, Map indentMap, Map forceMap, Category category) { 258 final String value= (String )fWorkingValues.get(category.key); 259 Integer wrappingStyle; 260 Integer indentStyle; 261 Boolean forceWrapping; 262 263 try { 264 wrappingStyle= new Integer (DefaultCodeFormatterConstants.getWrappingStyle(value)); 265 indentStyle= new Integer (DefaultCodeFormatterConstants.getIndentStyle(value)); 266 forceWrapping= new Boolean (DefaultCodeFormatterConstants.getForceWrapping(value)); 267 } catch (IllegalArgumentException e) { 268 forceWrapping= new Boolean (false); 269 indentStyle= new Integer (DefaultCodeFormatterConstants.INDENT_DEFAULT); 270 wrappingStyle= new Integer (DefaultCodeFormatterConstants.WRAP_NO_SPLIT); 271 } 272 273 increaseMapEntry(wrappingMap, wrappingStyle); 274 increaseMapEntry(indentMap, indentStyle); 275 increaseMapEntry(forceMap, forceWrapping); 276 } 277 278 private void increaseMapEntry(Map map, Object type) { 279 Integer count= (Integer )map.get(type); 280 if (count == null) map.put(type, new Integer (1)); 282 else 283 map.put(type, new Integer (count.intValue() + 1)); 284 } 285 286 private void refreshControls(Map wrappingStyleMap, Map indentStyleMap, Map forceWrappingMap) { 287 updateCombos(wrappingStyleMap, indentStyleMap); 288 updateButton(forceWrappingMap); 289 Integer wrappingStyleMax= getWrappingStyleMax(wrappingStyleMap); 290 boolean isInhomogeneous= (fElements.size() != ((Integer )wrappingStyleMap.get(wrappingStyleMax)).intValue()); 291 updateControlEnablement(isInhomogeneous, wrappingStyleMax.intValue()); 292 showSpecificControls(true); 293 if (fRequiresRelayout) { 294 fOptionsComposite.layout(true, true); 295 } 296 doUpdatePreview(); 297 notifyValuesModified(); 298 } 299 300 private void showSpecificControls(boolean show) { 301 if (fElements.size() != 1) 302 return; 303 304 Preference[] preferences= ((Category)fElements.get(0)).getSpecificPreferences(); 305 if (preferences.length == 0) 306 return; 307 308 fRequiresRelayout= true; 309 for (int i= 0; i < preferences.length; i++) { 310 Preference preference= preferences[i]; 311 Control control= preference.getControl(); 312 control.setVisible(show); 313 ((GridData)control.getLayoutData()).exclude= !show; 314 } 315 } 316 317 private Integer getWrappingStyleMax(Map wrappingStyleMap) { 318 int maxCount= 0, maxStyle= 0; 319 for (int i=0; i<WRAPPING_NAMES.length; i++) { 320 Integer count= (Integer )wrappingStyleMap.get(new Integer (i)); 321 if (count == null) 322 continue; 323 if (count.intValue() > maxCount) { 324 maxCount= count.intValue(); 325 maxStyle= i; 326 } 327 } 328 return new Integer (maxStyle); 329 } 330 331 private void updateButton(Map forceWrappingMap) { 332 Integer nrOfTrue= (Integer )forceWrappingMap.get(Boolean.TRUE); 333 Integer nrOfFalse= (Integer )forceWrappingMap.get(Boolean.FALSE); 334 335 if (nrOfTrue == null || nrOfFalse == null) 336 fForceSplit.setSelection(nrOfTrue != null); 337 else 338 fForceSplit.setSelection(nrOfTrue.intValue() > nrOfFalse.intValue()); 339 340 int max= getMax(nrOfTrue, nrOfFalse); 341 String label= FormatterMessages.LineWrappingTabPage_force_split_checkbox_multi_text; 342 fForceSplit.setText(getLabelText(label, max, fElements.size())); 343 } 344 345 private String getLabelText(String label, int count, int nElements) { 346 if (nElements == 1 || count == 0) 347 return label; 348 return Messages.format(FormatterMessages.LineWrappingTabPage_occurences, new String [] {label, Integer.toString(count), Integer.toString(nElements)}); 349 } 350 351 private int getMax(Integer nrOfTrue, Integer nrOfFalse) { 352 if (nrOfTrue == null) 353 return nrOfFalse.intValue(); 354 if (nrOfFalse == null) 355 return nrOfTrue.intValue(); 356 if (nrOfTrue.compareTo(nrOfFalse) >= 0) 357 return nrOfTrue.intValue(); 358 return nrOfFalse.intValue(); 359 } 360 361 private void updateCombos(Map wrappingStyleMap, Map indentStyleMap) { 362 updateCombo(fWrappingStyleCombo, wrappingStyleMap, WRAPPING_NAMES); 363 updateCombo(fIndentStyleCombo, indentStyleMap, INDENT_NAMES); 364 } 365 366 private void updateCombo(Combo combo, Map map, final String [] items) { 367 String [] newItems= new String [items.length]; 368 int maxCount= 0, maxStyle= 0; 369 370 for(int i = 0; i < items.length; i++) { 371 Integer count= (Integer ) map.get(new Integer (i)); 372 int val= (count == null) ? 0 : count.intValue(); 373 if (val > maxCount) { 374 maxCount= val; 375 maxStyle= i; 376 } 377 newItems[i]= getLabelText(items[i], val, fElements.size()); 378 } 379 combo.setItems(newItems); 380 combo.setText(newItems[maxStyle]); 381 } 382 } 383 384 protected static final String [] INDENT_NAMES = { 385 FormatterMessages.LineWrappingTabPage_indentation_default, 386 FormatterMessages.LineWrappingTabPage_indentation_on_column, 387 FormatterMessages.LineWrappingTabPage_indentation_by_one 388 }; 389 390 391 protected static final String [] WRAPPING_NAMES = { 392 FormatterMessages.LineWrappingTabPage_splitting_do_not_split, 393 FormatterMessages.LineWrappingTabPage_splitting_wrap_when_necessary, FormatterMessages.LineWrappingTabPage_splitting_always_wrap_first_others_when_necessary, FormatterMessages.LineWrappingTabPage_splitting_wrap_always, FormatterMessages.LineWrappingTabPage_splitting_wrap_always_indent_all_but_first, FormatterMessages.LineWrappingTabPage_splitting_wrap_always_except_first_only_if_necessary 398 }; 399 400 401 private final Category fCompactIfCategory= new Category( 402 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_COMPACT_IF, 403 "class Example {" + "int foo(int argument) {" + " if (argument==0) return 0;" + " if (argument==1) return 42; else return 43;" + "}}", FormatterMessages.LineWrappingTabPage_compact_if_else 409 ); 410 411 412 private final Category fTypeDeclarationSuperclassCategory= new Category( 413 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_SUPERCLASS_IN_TYPE_DECLARATION, 414 "class Example extends OtherClass {}", FormatterMessages.LineWrappingTabPage_extends_clause 416 ); 417 418 419 private final Category fTypeDeclarationSuperinterfacesCategory= new Category( 420 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_SUPERINTERFACES_IN_TYPE_DECLARATION, 421 "class Example implements I1, I2, I3 {}", FormatterMessages.LineWrappingTabPage_implements_clause 423 ); 424 425 426 private final Category fConstructorDeclarationsParametersCategory= new Category( 427 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_PARAMETERS_IN_CONSTRUCTOR_DECLARATION, 428 "class Example {Example(int arg1, int arg2, int arg3, int arg4, int arg5, int arg6) { this();}" + "Example() {}}", FormatterMessages.LineWrappingTabPage_parameters 431 ); 432 433 private final Category fMethodDeclarationsParametersCategory= new Category( 434 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_PARAMETERS_IN_METHOD_DECLARATION, 435 "class Example {void foo(int arg1, int arg2, int arg3, int arg4, int arg5, int arg6) {}}", FormatterMessages.LineWrappingTabPage_parameters 437 ); 438 439 private final Category fMessageSendArgumentsCategory= new Category( 440 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_METHOD_INVOCATION, 441 "class Example {void foo() {Other.bar( 100, 200, 300, 400, 500, 600, 700, 800, 900 );}}", FormatterMessages.LineWrappingTabPage_arguments 443 ); 444 445 private final Category fMessageSendSelectorCategory= new Category( 446 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_SELECTOR_IN_METHOD_INVOCATION, 447 "class Example {int foo(Some a) {return a.getFirst();}}", FormatterMessages.LineWrappingTabPage_qualified_invocations 449 ); 450 451 private final Category fMethodThrowsClauseCategory= new Category( 452 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_THROWS_CLAUSE_IN_METHOD_DECLARATION, 453 "class Example {" + "int foo() throws FirstException, SecondException, ThirdException {" + " return Other.doSomething();}}", FormatterMessages.LineWrappingTabPage_throws_clause 457 ); 458 459 private final Category fConstructorThrowsClauseCategory= new Category( 460 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_THROWS_CLAUSE_IN_CONSTRUCTOR_DECLARATION, 461 "class Example {" + "Example() throws FirstException, SecondException, ThirdException {" + " return Other.doSomething();}}", FormatterMessages.LineWrappingTabPage_throws_clause 465 ); 466 467 468 private final Category fAllocationExpressionArgumentsCategory= new Category( 469 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_ALLOCATION_EXPRESSION, 470 "class Example {SomeClass foo() {return new SomeClass(100, 200, 300, 400, 500, 600, 700, 800, 900 );}}", FormatterMessages.LineWrappingTabPage_object_allocation 472 ); 473 474 private final Category fQualifiedAllocationExpressionCategory= new Category ( 475 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_QUALIFIED_ALLOCATION_EXPRESSION, 476 "class Example {SomeClass foo() {return SomeOtherClass.new SomeClass(100, 200, 300, 400, 500 );}}", FormatterMessages.LineWrappingTabPage_qualified_object_allocation 478 ); 479 480 private final Category fArrayInitializerExpressionsCategory= new Category( 481 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_EXPRESSIONS_IN_ARRAY_INITIALIZER, 482 "class Example {int [] fArray= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};}", FormatterMessages.LineWrappingTabPage_array_init 484 ); 485 486 private final Category fExplicitConstructorArgumentsCategory= new Category( 487 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_EXPLICIT_CONSTRUCTOR_CALL, 488 "class Example extends AnotherClass {Example() {super(100, 200, 300, 400, 500, 600, 700);}}", FormatterMessages.LineWrappingTabPage_explicit_constructor_invocations 490 ); 491 492 private final Category fConditionalExpressionCategory= new Category( 493 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_CONDITIONAL_EXPRESSION, 494 "class Example extends AnotherClass {int Example(boolean Argument) {return argument ? 100000 : 200000;}}", FormatterMessages.LineWrappingTabPage_conditionals 496 ); 497 498 private final Category fBinaryExpressionCategory= new Category( 499 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_BINARY_EXPRESSION, 500 "class Example extends AnotherClass {" + "int foo() {" + " int sum= 100 + 200 + 300 + 400 + 500 + 600 + 700 + 800;" + " int product= 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10;" + " boolean val= true && false && true && false && true;" + " return product / sum;}}", FormatterMessages.LineWrappingTabPage_binary_exprs 507 ); 508 509 private final Category fEnumConstArgumentsCategory= new Category( 510 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ARGUMENTS_IN_ENUM_CONSTANT, 511 "enum Example {" + "GREEN(0, 255, 0), RED(255, 0, 0) }", FormatterMessages.LineWrappingTabPage_enum_constant_arguments 514 ); 515 516 private final Category fEnumDeclInterfacesCategory= new Category( 517 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_SUPERINTERFACES_IN_ENUM_DECLARATION, 518 "enum Example implements A, B, C {" + "}", FormatterMessages.LineWrappingTabPage_enum_superinterfaces 521 ); 522 523 private final Category fEnumConstantsCategory= new Category( 524 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUM_CONSTANTS, 525 "enum Example {" + "CANCELLED, RUNNING, WAITING, FINISHED }" + "enum Example {" + "GREEN(0, 255, 0), RED(255, 0, 0) }", FormatterMessages.LineWrappingTabPage_enum_constants 530 ); 531 532 private final Category fAssignmentCategory= new Category( 533 DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ASSIGNMENT, 534 "class Example {" + "private static final String string = \"TextTextText\";" + "void foo() {" + "for (int i = 0; i < 10; i++) {}" + "String s;" + "s = \"TextTextText\";}}", FormatterMessages.LineWrappingTabPage_assignment_alignment 541 ); 542 543 546 private static int DEFAULT_PREVIEW_WINDOW_LINE_WIDTH= 40; 547 548 551 private static final String PREF_PREVIEW_LINE_WIDTH= JavaUI.ID_PLUGIN + ".codeformatter.line_wrapping_tab_page.preview_line_width"; 553 556 protected final IDialogSettings fDialogSettings; 557 558 protected TreeViewer fCategoriesViewer; 559 protected Label fWrappingStylePolicy; 560 protected Combo fWrappingStyleCombo; 561 protected Label fIndentStylePolicy; 562 protected Combo fIndentStyleCombo; 563 protected Button fForceSplit; 564 565 protected CompilationUnitPreview fPreview; 566 567 protected Group fOptionsGroup; 568 569 573 private final List fCategories; 574 575 578 protected final CategoryListener fCategoryListener; 579 580 583 protected IStructuredSelection fSelection; 584 585 588 SelectionState fSelectionState; 589 590 593 protected final Map fPreviewPreferences; 594 595 598 private final String LINE_SPLIT= DefaultCodeFormatterConstants.FORMATTER_LINE_SPLIT; 599 600 601 private Composite fOptionsComposite; 602 603 608 public LineWrappingTabPage(ModifyDialog modifyDialog, Map workingValues) { 609 super(modifyDialog, workingValues); 610 611 fDialogSettings= JavaPlugin.getDefault().getDialogSettings(); 612 613 final String previewLineWidth= fDialogSettings.get(PREF_PREVIEW_LINE_WIDTH); 614 615 fPreviewPreferences= new HashMap (); 616 fPreviewPreferences.put(LINE_SPLIT, previewLineWidth != null ? previewLineWidth : Integer.toString(DEFAULT_PREVIEW_WINDOW_LINE_WIDTH)); 617 618 fCategories= createCategories(); 619 fCategoryListener= new CategoryListener(fCategories); 620 } 621 622 625 protected List createCategories() { 626 627 final Category classDeclarations= new Category(FormatterMessages.LineWrappingTabPage_class_decls); 628 classDeclarations.children.add(fTypeDeclarationSuperclassCategory); 629 classDeclarations.children.add(fTypeDeclarationSuperinterfacesCategory); 630 631 final Category constructorDeclarations= new Category(null, null, FormatterMessages.LineWrappingTabPage_constructor_decls); 632 constructorDeclarations.children.add(fConstructorDeclarationsParametersCategory); 633 constructorDeclarations.children.add(fConstructorThrowsClauseCategory); 634 635 final Category methodDeclarations= new Category(null, null, FormatterMessages.LineWrappingTabPage_method_decls); 636 methodDeclarations.children.add(fMethodDeclarationsParametersCategory); 637 methodDeclarations.children.add(fMethodThrowsClauseCategory); 638 639 final Category enumDeclarations= new Category(FormatterMessages.LineWrappingTabPage_enum_decls); 640 enumDeclarations.children.add(fEnumConstantsCategory); 641 enumDeclarations.children.add(fEnumDeclInterfacesCategory); 642 enumDeclarations.children.add(fEnumConstArgumentsCategory); 643 644 final Category functionCalls= new Category(FormatterMessages.LineWrappingTabPage_function_calls); 645 functionCalls.children.add(fMessageSendArgumentsCategory); 646 functionCalls.children.add(fMessageSendSelectorCategory); 647 functionCalls.children.add(fExplicitConstructorArgumentsCategory); 648 functionCalls.children.add(fAllocationExpressionArgumentsCategory); 649 functionCalls.children.add(fQualifiedAllocationExpressionCategory); 650 651 final Category expressions= new Category(FormatterMessages.LineWrappingTabPage_expressions); 652 expressions.children.add(fBinaryExpressionCategory); 653 expressions.children.add(fConditionalExpressionCategory); 654 expressions.children.add(fArrayInitializerExpressionsCategory); 655 expressions.children.add(fAssignmentCategory); 656 657 final Category statements= new Category(FormatterMessages.LineWrappingTabPage_statements); 658 statements.children.add(fCompactIfCategory); 659 660 final List root= new ArrayList (); 661 root.add(classDeclarations); 662 root.add(constructorDeclarations); 663 root.add(methodDeclarations); 664 root.add(enumDeclarations); 665 root.add(functionCalls); 666 root.add(expressions); 667 root.add(statements); 668 669 return root; 670 } 671 672 protected void doCreatePreferences(Composite composite, int numColumns) { 673 674 fOptionsComposite= composite; 675 676 final Group lineWidthGroup= createGroup(numColumns, composite, FormatterMessages.LineWrappingTabPage_width_indent); 677 678 createNumberPref(lineWidthGroup, numColumns, FormatterMessages.LineWrappingTabPage_width_indent_option_max_line_width, DefaultCodeFormatterConstants.FORMATTER_LINE_SPLIT, 0, 9999); 679 createNumberPref(lineWidthGroup, numColumns, FormatterMessages.LineWrappingTabPage_width_indent_option_default_indent_wrapped, DefaultCodeFormatterConstants.FORMATTER_CONTINUATION_INDENTATION, 0, 9999); 680 createNumberPref(lineWidthGroup, numColumns, FormatterMessages.LineWrappingTabPage_width_indent_option_default_indent_array, DefaultCodeFormatterConstants.FORMATTER_CONTINUATION_INDENTATION_FOR_ARRAY_INITIALIZER, 0, 9999); 681 682 fCategoriesViewer= new TreeViewer(composite , SWT.MULTI | SWT.BORDER | SWT.READ_ONLY | SWT.V_SCROLL ); 683 fCategoriesViewer.setContentProvider(new ITreeContentProvider() { 684 public Object [] getElements(Object inputElement) { 685 return ((Collection )inputElement).toArray(); 686 } 687 public Object [] getChildren(Object parentElement) { 688 return ((Category)parentElement).children.toArray(); 689 } 690 public Object getParent(Object element) { return null; } 691 public boolean hasChildren(Object element) { 692 return !((Category)element).children.isEmpty(); 693 } 694 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {} 695 public void dispose() {} 696 }); 697 fCategoriesViewer.setLabelProvider(new LabelProvider()); 698 fCategoriesViewer.setInput(fCategories); 699 700 fCategoriesViewer.setExpandedElements(fCategories.toArray()); 701 702 final GridData gd= createGridData(numColumns, GridData.FILL_BOTH, SWT.DEFAULT); 703 gd.heightHint= fPixelConverter.convertHeightInCharsToPixels(5); 704 fCategoriesViewer.getControl().setLayoutData(gd); 705 706 fOptionsGroup = createGroup(numColumns, composite, ""); 708 fWrappingStylePolicy= createLabel(numColumns, fOptionsGroup, FormatterMessages.LineWrappingTabPage_wrapping_policy_label_text); 710 711 fWrappingStyleCombo= new Combo(fOptionsGroup, SWT.SINGLE | SWT.READ_ONLY); 713 fWrappingStyleCombo.setItems(WRAPPING_NAMES); 714 fWrappingStyleCombo.setLayoutData(createGridData(numColumns, GridData.HORIZONTAL_ALIGN_FILL, 0)); 715 716 fIndentStylePolicy= createLabel(numColumns, fOptionsGroup, FormatterMessages.LineWrappingTabPage_indentation_policy_label_text); 718 719 fIndentStyleCombo= new Combo(fOptionsGroup, SWT.SINGLE | SWT.READ_ONLY); 721 fIndentStyleCombo.setItems(INDENT_NAMES); 722 fIndentStyleCombo.setLayoutData(createGridData(numColumns, GridData.HORIZONTAL_ALIGN_FILL, 0)); 723 724 fForceSplit= new Button(fOptionsGroup, SWT.CHECK); 726 fForceSplit.setLayoutData(createGridData(numColumns - 1, GridData.HORIZONTAL_ALIGN_BEGINNING, SWT.DEFAULT)); 727 fForceSplit.setText(FormatterMessages.LineWrappingTabPage_force_split_checkbox_text); 728 729 Preference expressionWrapPositionPreference= createCheckboxPref(fOptionsGroup, 1, FormatterMessages.LineWrappingTabPage_binary_expression_wrap_operator, DefaultCodeFormatterConstants.FORMATTER_WRAP_BEFORE_BINARY_OPERATOR, FALSE_TRUE); 730 Control control= expressionWrapPositionPreference.getControl(); 731 control.setVisible(false); 732 ((GridData)control.getLayoutData()).exclude= true; 733 fBinaryExpressionCategory.addPreference(expressionWrapPositionPreference); 734 735 fSelectionState= new SelectionState(); 737 738 } 739 740 741 protected Composite doCreatePreviewPane(Composite composite, int numColumns) { 742 743 super.doCreatePreviewPane(composite, numColumns); 744 745 final NumberPreference previewLineWidth= new NumberPreference(composite, numColumns / 2, fPreviewPreferences, LINE_SPLIT, 746 0, 9999, FormatterMessages.LineWrappingTabPage_line_width_for_preview_label_text); 747 fDefaultFocusManager.add(previewLineWidth); 748 previewLineWidth.addObserver(fUpdater); 749 previewLineWidth.addObserver(new Observer () { 750 public void update(Observable o, Object arg) { 751 fDialogSettings.put(PREF_PREVIEW_LINE_WIDTH, (String )fPreviewPreferences.get(LINE_SPLIT)); 752 } 753 }); 754 755 return composite; 756 } 757 758 761 protected JavaPreview doCreateJavaPreview(Composite parent) { 762 fPreview= new CompilationUnitPreview(fWorkingValues, parent); 763 return fPreview; 764 } 765 766 767 protected void initializePage() { 768 769 fCategoriesViewer.addSelectionChangedListener(fCategoryListener); 770 fCategoriesViewer.addDoubleClickListener(fCategoryListener); 771 772 fForceSplit.addSelectionListener(new SelectionAdapter() { 773 public void widgetSelected(SelectionEvent e) { 774 forceSplitChanged(fForceSplit.getSelection()); 775 } 776 }); 777 fIndentStyleCombo.addSelectionListener( new SelectionAdapter() { 778 public void widgetSelected(SelectionEvent e) { 779 indentStyleChanged(((Combo)e.widget).getSelectionIndex()); 780 } 781 }); 782 fWrappingStyleCombo.addSelectionListener( new SelectionAdapter() { 783 public void widgetSelected(SelectionEvent e) { 784 wrappingStyleChanged(((Combo)e.widget).getSelectionIndex()); 785 } 786 }); 787 788 fCategoryListener.restoreSelection(); 789 790 fDefaultFocusManager.add(fCategoriesViewer.getControl()); 791 fDefaultFocusManager.add(fWrappingStyleCombo); 792 fDefaultFocusManager.add(fIndentStyleCombo); 793 fDefaultFocusManager.add(fForceSplit); 794 } 795 796 protected void doUpdatePreview() { 797 super.doUpdatePreview(); 798 final Object normalSetting= fWorkingValues.get(LINE_SPLIT); 799 fWorkingValues.put(LINE_SPLIT, fPreviewPreferences.get(LINE_SPLIT)); 800 fPreview.update(); 801 fWorkingValues.put(LINE_SPLIT, normalSetting); 802 } 803 804 protected void setPreviewText(String text) { 805 final Object normalSetting= fWorkingValues.get(LINE_SPLIT); 806 fWorkingValues.put(LINE_SPLIT, fPreviewPreferences.get(LINE_SPLIT)); 807 fPreview.setPreviewText(text); 808 fWorkingValues.put(LINE_SPLIT, normalSetting); 809 } 810 811 protected void forceSplitChanged(boolean forceSplit) { 812 Iterator iterator= fSelectionState.fElements.iterator(); 813 String currentKey; 814 while (iterator.hasNext()) { 815 currentKey= ((Category)iterator.next()).key; 816 try { 817 changeForceSplit(currentKey, forceSplit); 818 } catch (IllegalArgumentException e) { 819 fWorkingValues.put(currentKey, DefaultCodeFormatterConstants.createAlignmentValue(forceSplit, DefaultCodeFormatterConstants.WRAP_NO_SPLIT, DefaultCodeFormatterConstants.INDENT_DEFAULT)); 820 JavaPlugin.log(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK, 821 Messages.format(FormatterMessages.LineWrappingTabPage_error_invalid_value, currentKey), e)); 822 } 823 } 824 fSelectionState.refreshState(fSelection); 825 } 826 827 private void changeForceSplit(String currentKey, boolean forceSplit) throws IllegalArgumentException { 828 String value= (String )fWorkingValues.get(currentKey); 829 value= DefaultCodeFormatterConstants.setForceWrapping(value, forceSplit); 830 if (value == null) 831 throw new IllegalArgumentException (); 832 fWorkingValues.put(currentKey, value); 833 } 834 835 protected void wrappingStyleChanged(int wrappingStyle) { 836 Iterator iterator= fSelectionState.fElements.iterator(); 837 String currentKey; 838 while (iterator.hasNext()) { 839 currentKey= ((Category)iterator.next()).key; 840 try { 841 changeWrappingStyle(currentKey, wrappingStyle); 842 } catch (IllegalArgumentException e) { 843 fWorkingValues.put(currentKey, DefaultCodeFormatterConstants.createAlignmentValue(false, wrappingStyle, DefaultCodeFormatterConstants.INDENT_DEFAULT)); 844 JavaPlugin.log(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK, 845 Messages.format(FormatterMessages.LineWrappingTabPage_error_invalid_value, currentKey), e)); 846 } 847 } 848 fSelectionState.refreshState(fSelection); 849 } 850 851 private void changeWrappingStyle(String currentKey, int wrappingStyle) throws IllegalArgumentException { 852 String value= (String )fWorkingValues.get(currentKey); 853 value= DefaultCodeFormatterConstants.setWrappingStyle(value, wrappingStyle); 854 if (value == null) 855 throw new IllegalArgumentException (); 856 fWorkingValues.put(currentKey, value); 857 } 858 859 protected void indentStyleChanged(int indentStyle) { 860 Iterator iterator= fSelectionState.fElements.iterator(); 861 String currentKey; 862 while (iterator.hasNext()) { 863 currentKey= ((Category)iterator.next()).key; 864 try { 865 changeIndentStyle(currentKey, indentStyle); 866 } catch (IllegalArgumentException e) { 867 fWorkingValues.put(currentKey, DefaultCodeFormatterConstants.createAlignmentValue(false, DefaultCodeFormatterConstants.WRAP_NO_SPLIT, indentStyle)); 868 JavaPlugin.log(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK, 869 Messages.format(FormatterMessages.LineWrappingTabPage_error_invalid_value, currentKey), e)); 870 } 871 } 872 fSelectionState.refreshState(fSelection); 873 } 874 875 private void changeIndentStyle(String currentKey, int indentStyle) throws IllegalArgumentException { 876 String value= (String )fWorkingValues.get(currentKey); 877 value= DefaultCodeFormatterConstants.setIndentStyle(value, indentStyle); 878 if (value == null) 879 throw new IllegalArgumentException (); 880 fWorkingValues.put(currentKey, value); 881 } 882 883 protected void updateControlEnablement(boolean inhomogenous, int wrappingStyle) { 884 boolean doSplit= wrappingStyle != DefaultCodeFormatterConstants.WRAP_NO_SPLIT; 885 fIndentStylePolicy.setEnabled(true); 886 fIndentStyleCombo.setEnabled(inhomogenous || doSplit); 887 fForceSplit.setEnabled(inhomogenous || doSplit); 888 } 889 } 890 | Popular Tags |