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.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 18 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.custom.SashForm; 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.Combo; 25 import org.eclipse.swt.widgets.Composite; 26 import org.eclipse.swt.widgets.Control; 27 28 import org.eclipse.jface.dialogs.IDialogSettings; 29 import org.eclipse.jface.viewers.ArrayContentProvider; 30 import org.eclipse.jface.viewers.CheckStateChangedEvent; 31 import org.eclipse.jface.viewers.CheckboxTableViewer; 32 import org.eclipse.jface.viewers.DoubleClickEvent; 33 import org.eclipse.jface.viewers.ICheckStateListener; 34 import org.eclipse.jface.viewers.IDoubleClickListener; 35 import org.eclipse.jface.viewers.ISelection; 36 import org.eclipse.jface.viewers.ISelectionChangedListener; 37 import org.eclipse.jface.viewers.IStructuredSelection; 38 import org.eclipse.jface.viewers.ITreeContentProvider; 39 import org.eclipse.jface.viewers.LabelProvider; 40 import org.eclipse.jface.viewers.SelectionChangedEvent; 41 import org.eclipse.jface.viewers.StructuredSelection; 42 import org.eclipse.jface.viewers.TreeViewer; 43 import org.eclipse.jface.viewers.Viewer; 44 45 import org.eclipse.ui.dialogs.ContainerCheckedTreeViewer; 46 import org.eclipse.ui.part.PageBook; 47 48 import org.eclipse.jdt.ui.JavaUI; 49 50 import org.eclipse.jdt.internal.ui.JavaPlugin; 51 import org.eclipse.jdt.internal.ui.preferences.formatter.WhiteSpaceOptions.InnerNode; 52 import org.eclipse.jdt.internal.ui.preferences.formatter.WhiteSpaceOptions.Node; 53 import org.eclipse.jdt.internal.ui.preferences.formatter.WhiteSpaceOptions.OptionNode; 54 55 56 public class WhiteSpaceTabPage extends FormatterTabPage { 57 58 59 63 64 private final class SyntaxComponent implements ISelectionChangedListener, ICheckStateListener, IDoubleClickListener { 65 66 private final String PREF_NODE_KEY= JavaUI.ID_PLUGIN + "formatter_page.white_space_tab_page.node"; 68 private final List fIndexedNodeList; 69 private final List fTree; 70 71 private ContainerCheckedTreeViewer fTreeViewer; 72 private Composite fComposite; 73 74 private Node fLastSelected= null; 75 76 public SyntaxComponent() { 77 fIndexedNodeList= new ArrayList (); 78 fTree= new WhiteSpaceOptions().createAltTree(fWorkingValues); 79 WhiteSpaceOptions.makeIndexForNodes(fTree, fIndexedNodeList); 80 } 81 82 public void createContents(final int numColumns, final Composite parent) { 83 fComposite= new Composite(parent, SWT.NONE); 84 fComposite.setLayoutData(createGridData(numColumns, GridData.HORIZONTAL_ALIGN_FILL, SWT.DEFAULT)); 85 fComposite.setLayout(createGridLayout(numColumns, false)); 86 87 createLabel(numColumns, fComposite, FormatterMessages.WhiteSpaceTabPage_insert_space); 88 89 fTreeViewer= new ContainerCheckedTreeViewer(fComposite, SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL); 90 fTreeViewer.setContentProvider(new ITreeContentProvider() { 91 public Object [] getElements(Object inputElement) { 92 return ((Collection )inputElement).toArray(); 93 } 94 public Object [] getChildren(Object parentElement) { 95 return ((Node)parentElement).getChildren().toArray(); 96 } 97 public Object getParent(Object element) { 98 return ((Node)element).getParent(); 99 } 100 public boolean hasChildren(Object element) { 101 return ((Node)element).hasChildren(); 102 } 103 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {} 104 public void dispose() {} 105 }); 106 fTreeViewer.setLabelProvider(new LabelProvider()); 107 fTreeViewer.getControl().setLayoutData(createGridData(numColumns, GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL, SWT.DEFAULT)); 108 fDefaultFocusManager.add(fTreeViewer.getControl()); 109 } 110 111 public void initialize() { 112 fTreeViewer.addCheckStateListener(this); 113 fTreeViewer.addSelectionChangedListener(this); 114 fTreeViewer.addDoubleClickListener(this); 115 fTreeViewer.setInput(fTree); 116 restoreSelection(); 117 refreshState(); 118 } 119 120 public void refreshState() { 121 final ArrayList checked= new ArrayList (100); 122 for (Iterator iter= fTree.iterator(); iter.hasNext();) 123 ((Node) iter.next()).getCheckedLeafs(checked); 124 fTreeViewer.setGrayedElements(new Object [0]); 125 fTreeViewer.setCheckedElements(checked.toArray()); 126 fPreview.clear(); 127 if (fLastSelected != null) { 128 fPreview.addAll(fLastSelected.getSnippets()); 129 } 130 doUpdatePreview(); 131 } 132 133 public void selectionChanged(SelectionChangedEvent event) { 134 final IStructuredSelection selection= (IStructuredSelection)event.getSelection(); 135 if (selection.isEmpty()) 136 return; 137 final Node node= (Node)selection.getFirstElement(); 138 if (node == fLastSelected) 139 return; 140 fDialogSettings.put(PREF_NODE_KEY, node.index); 141 fPreview.clear(); 142 fPreview.addAll(node.getSnippets()); 143 doUpdatePreview(); 144 fLastSelected= node; 145 } 146 147 public void checkStateChanged(CheckStateChangedEvent event) { 148 final Node node= (Node)event.getElement(); 149 node.setChecked(event.getChecked()); 150 doUpdatePreview(); 151 notifyValuesModified(); 152 } 153 154 public void restoreSelection() { 155 int index; 156 try { 157 index= fDialogSettings.getInt(PREF_NODE_KEY); 158 } catch (NumberFormatException ex) { 159 index= -1; 160 } 161 if (index < 0 || index > fIndexedNodeList.size() - 1) { 162 index= 0; 163 } 164 final Node node= (Node)fIndexedNodeList.get(index); 165 if (node != null) { 166 fTreeViewer.expandToLevel(node, 0); 167 fTreeViewer.setSelection(new StructuredSelection(new Node [] {node})); 168 fLastSelected= node; 169 } 170 } 171 172 public void doubleClick(DoubleClickEvent event) { 173 final ISelection selection= event.getSelection(); 174 if (selection instanceof IStructuredSelection) { 175 final Node node= (Node)((IStructuredSelection)selection).getFirstElement(); 176 fTreeViewer.setExpandedState(node, !fTreeViewer.getExpandedState(node)); 177 } 178 } 179 180 public Control getControl() { 181 return fComposite; 182 } 183 } 184 185 186 187 private final class JavaElementComponent implements ISelectionChangedListener, ICheckStateListener { 188 189 private final String PREF_INNER_INDEX= JavaUI.ID_PLUGIN + "formatter_page.white_space.java_view.inner"; private final String PREF_OPTION_INDEX= JavaUI.ID_PLUGIN + "formatter_page.white_space.java_view.option"; 192 private final ArrayList fIndexedNodeList; 193 private final ArrayList fTree; 194 195 private InnerNode fLastSelected; 196 197 private TreeViewer fInnerViewer; 198 private CheckboxTableViewer fOptionsViewer; 199 200 private Composite fComposite; 201 202 public JavaElementComponent() { 203 fIndexedNodeList= new ArrayList (); 204 fTree= new WhiteSpaceOptions().createTreeByJavaElement(fWorkingValues); 205 WhiteSpaceOptions.makeIndexForNodes(fTree, fIndexedNodeList); 206 } 207 208 public void createContents(int numColumns, Composite parent) { 209 210 fComposite= new Composite(parent, SWT.NONE); 211 fComposite.setLayoutData(createGridData(numColumns, GridData.HORIZONTAL_ALIGN_FILL, SWT.DEFAULT)); 212 fComposite.setLayout(createGridLayout(numColumns, false)); 213 214 createLabel(numColumns, fComposite, FormatterMessages.WhiteSpaceTabPage_insert_space, GridData.HORIZONTAL_ALIGN_BEGINNING); 215 216 final SashForm sashForm= new SashForm(fComposite, SWT.VERTICAL); 217 sashForm.setLayoutData(createGridData(numColumns, GridData.FILL_BOTH, SWT.DEFAULT)); 218 219 fInnerViewer= new TreeViewer(sashForm, SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL); 220 221 fInnerViewer.setContentProvider(new ITreeContentProvider() { 222 public Object [] getElements(Object inputElement) { 223 return ((Collection )inputElement).toArray(); 224 } 225 public Object [] getChildren(Object parentElement) { 226 final List children= ((Node)parentElement).getChildren(); 227 final ArrayList innerChildren= new ArrayList (); 228 for (final Iterator iter= children.iterator(); iter.hasNext();) { 229 final Object o= iter.next(); 230 if (o instanceof InnerNode) innerChildren.add(o); 231 } 232 return innerChildren.toArray(); 233 } 234 public Object getParent(Object element) { 235 if (element instanceof InnerNode) 236 return ((InnerNode)element).getParent(); 237 return null; 238 } 239 public boolean hasChildren(Object element) { 240 final List children= ((Node)element).getChildren(); 241 for (final Iterator iter= children.iterator(); iter.hasNext();) 242 if (iter.next() instanceof InnerNode) return true; 243 return false; 244 } 245 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {} 246 public void dispose() {} 247 }); 248 249 fInnerViewer.setLabelProvider(new LabelProvider()); 250 251 final GridData innerGd= createGridData(numColumns, GridData.HORIZONTAL_ALIGN_FILL | GridData.FILL_VERTICAL, SWT.DEFAULT); 252 innerGd.heightHint= fPixelConverter.convertHeightInCharsToPixels(3); 253 fInnerViewer.getControl().setLayoutData(innerGd); 254 255 fOptionsViewer= CheckboxTableViewer.newCheckList(sashForm, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL); 256 fOptionsViewer.setContentProvider(new ArrayContentProvider()); 257 fOptionsViewer.setLabelProvider(new LabelProvider()); 258 259 final GridData optionsGd= createGridData(numColumns, GridData.HORIZONTAL_ALIGN_FILL | GridData.FILL_VERTICAL, SWT.DEFAULT); 260 optionsGd.heightHint= fPixelConverter.convertHeightInCharsToPixels(3); 261 fOptionsViewer.getControl().setLayoutData(optionsGd); 262 263 fDefaultFocusManager.add(fInnerViewer.getControl()); 264 fDefaultFocusManager.add(fOptionsViewer.getControl()); 265 266 fInnerViewer.setInput(fTree); 267 } 268 269 public void refreshState() { 270 if (fLastSelected != null) { 271 innerViewerChanged(fLastSelected); 272 } 273 } 274 275 public void initialize() { 276 fInnerViewer.addSelectionChangedListener(this); 277 fOptionsViewer.addSelectionChangedListener(this); 278 fOptionsViewer.addCheckStateListener(this); 279 restoreSelections(); 280 refreshState(); 281 } 282 283 private void restoreSelections() { 284 Node node; 285 final int innerIndex= getValidatedIndex(PREF_INNER_INDEX); 286 node= (Node)fIndexedNodeList.get(innerIndex); 287 if (node instanceof InnerNode) { 288 fInnerViewer.expandToLevel(node, 0); 289 fInnerViewer.setSelection(new StructuredSelection(new Object [] {node})); 290 fLastSelected= (InnerNode)node; 291 } 292 293 final int optionIndex= getValidatedIndex(PREF_OPTION_INDEX); 294 node= (Node)fIndexedNodeList.get(optionIndex); 295 if (node instanceof OptionNode) { 296 fOptionsViewer.setSelection(new StructuredSelection(new Object [] {node})); 297 } 298 299 } 300 301 private int getValidatedIndex(String key) { 302 int index; 303 try { 304 index= fDialogSettings.getInt(key); 305 } catch (NumberFormatException ex) { 306 index= 0; 307 } 308 if (index < 0 || index > fIndexedNodeList.size() - 1) { 309 index= 0; 310 } 311 return index; 312 } 313 314 public Control getControl() { 315 return fComposite; 316 } 317 318 public void selectionChanged(SelectionChangedEvent event) { 319 final IStructuredSelection selection= (IStructuredSelection)event.getSelection(); 320 321 if (selection.isEmpty() || !(selection.getFirstElement() instanceof Node)) 322 return; 323 324 final Node selected= (Node)selection.getFirstElement(); 325 326 if (selected == null || selected == fLastSelected) 327 return; 328 329 330 if (event.getSource() == fInnerViewer && selected instanceof InnerNode) { 331 fLastSelected= (InnerNode)selected; 332 fDialogSettings.put(PREF_INNER_INDEX, selected.index); 333 innerViewerChanged((InnerNode)selected); 334 } 335 else if (event.getSource() == fOptionsViewer && selected instanceof OptionNode) 336 fDialogSettings.put(PREF_OPTION_INDEX, selected.index); 337 } 338 339 private void innerViewerChanged(InnerNode selectedNode) { 340 341 final List children= selectedNode.getChildren(); 342 343 final ArrayList optionsChildren= new ArrayList (); 344 for (final Iterator iter= children.iterator(); iter.hasNext();) { 345 final Object o= iter.next(); 346 if (o instanceof OptionNode) optionsChildren.add(o); 347 } 348 349 fOptionsViewer.setInput(optionsChildren.toArray()); 350 351 for (final Iterator iter= optionsChildren.iterator(); iter.hasNext();) { 352 final OptionNode child= (OptionNode)iter.next(); 353 fOptionsViewer.setChecked(child, child.getChecked()); 354 } 355 356 fPreview.clear(); 357 fPreview.addAll(selectedNode.getSnippets()); 358 doUpdatePreview(); 359 } 360 361 public void checkStateChanged(CheckStateChangedEvent event) { 362 final OptionNode option= (OptionNode)event.getElement(); 363 if (option != null) 364 option.setChecked(event.getChecked()); 365 doUpdatePreview(); 366 notifyValuesModified(); 367 } 368 } 369 370 371 372 376 private final class SwitchComponent extends SelectionAdapter { 377 private final String PREF_VIEW_KEY= JavaUI.ID_PLUGIN + "formatter_page.white_space_tab_page.view"; private final String [] fItems= new String [] { 379 FormatterMessages.WhiteSpaceTabPage_sort_by_java_element, 380 FormatterMessages.WhiteSpaceTabPage_sort_by_syntax_element 381 }; 382 383 private Combo fSwitchCombo; 384 private PageBook fPageBook; 385 private final SyntaxComponent fSyntaxComponent; 386 private final JavaElementComponent fJavaElementComponent; 387 388 public SwitchComponent() { 389 fSyntaxComponent= new SyntaxComponent(); 390 fJavaElementComponent= new JavaElementComponent(); 391 } 392 393 public void widgetSelected(SelectionEvent e) { 394 final int index= fSwitchCombo.getSelectionIndex(); 395 if (index == 0) { 396 fDialogSettings.put(PREF_VIEW_KEY, false); 397 fJavaElementComponent.refreshState(); 398 fPageBook.showPage(fJavaElementComponent.getControl()); 399 } 400 else if (index == 1) { 401 fDialogSettings.put(PREF_VIEW_KEY, true); 402 fSyntaxComponent.refreshState(); 403 fPageBook.showPage(fSyntaxComponent.getControl()); 404 } 405 } 406 407 public void createContents(int numColumns, Composite parent) { 408 409 fPageBook= new PageBook(parent, SWT.NONE); 410 fPageBook.setLayoutData(createGridData(numColumns, GridData.FILL_BOTH, SWT.DEFAULT)); 411 412 fJavaElementComponent.createContents(numColumns, fPageBook); 413 fSyntaxComponent.createContents(numColumns, fPageBook); 414 415 fSwitchCombo= new Combo(parent, SWT.READ_ONLY); 416 final GridData gd= createGridData(numColumns, GridData.HORIZONTAL_ALIGN_END, SWT.DEFAULT); 417 fSwitchCombo.setLayoutData(gd); 418 fSwitchCombo.setItems(fItems); 419 } 420 421 public void initialize() { 422 fSwitchCombo.addSelectionListener(this); 423 fJavaElementComponent.initialize(); 424 fSyntaxComponent.initialize(); 425 restoreSelection(); 426 } 427 428 private void restoreSelection() { 429 final boolean selectSyntax= fDialogSettings.getBoolean(PREF_VIEW_KEY); 430 if (selectSyntax) { 431 fSyntaxComponent.refreshState(); 432 fSwitchCombo.setText(fItems[1]); 433 fPageBook.showPage(fSyntaxComponent.getControl()); 434 } else { 435 fJavaElementComponent.refreshState(); 436 fSwitchCombo.setText(fItems[0]); 437 fPageBook.showPage(fJavaElementComponent.getControl()); 438 } 439 } 440 } 441 442 443 444 445 private final SwitchComponent fSwitchComponent; 446 protected final IDialogSettings fDialogSettings; 447 448 protected SnippetPreview fPreview; 449 450 451 456 public WhiteSpaceTabPage(ModifyDialog modifyDialog, Map workingValues) { 457 super(modifyDialog, workingValues); 458 fDialogSettings= JavaPlugin.getDefault().getDialogSettings(); 459 fSwitchComponent= new SwitchComponent(); 460 } 461 462 protected void doCreatePreferences(Composite composite, int numColumns) { 463 fSwitchComponent.createContents(numColumns, composite); 464 } 465 466 protected void initializePage() { 467 fSwitchComponent.initialize(); 468 } 469 470 protected JavaPreview doCreateJavaPreview(Composite parent) { 471 fPreview= new SnippetPreview(fWorkingValues, parent); 472 return fPreview; 473 } 474 475 protected void doUpdatePreview() { 476 super.doUpdatePreview(); 477 fPreview.update(); 478 } 479 } 480 | Popular Tags |