1 11 package org.eclipse.jdt.internal.debug.ui; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import java.util.Set ; 17 import java.util.TreeSet ; 18 19 import org.eclipse.jdt.internal.debug.ui.display.DisplayViewerConfiguration; 20 import org.eclipse.jdt.ui.text.IJavaPartitions; 21 import org.eclipse.jdt.ui.text.JavaTextTools; 22 import org.eclipse.jface.dialogs.Dialog; 23 import org.eclipse.jface.preference.PreferencePage; 24 import org.eclipse.jface.text.Document; 25 import org.eclipse.jface.text.IDocument; 26 import org.eclipse.jface.viewers.CheckStateChangedEvent; 27 import org.eclipse.jface.viewers.CheckboxTableViewer; 28 import org.eclipse.jface.viewers.DoubleClickEvent; 29 import org.eclipse.jface.viewers.ICheckStateListener; 30 import org.eclipse.jface.viewers.IDoubleClickListener; 31 import org.eclipse.jface.viewers.ISelectionChangedListener; 32 import org.eclipse.jface.viewers.IStructuredContentProvider; 33 import org.eclipse.jface.viewers.IStructuredSelection; 34 import org.eclipse.jface.viewers.LabelProvider; 35 import org.eclipse.jface.viewers.SelectionChangedEvent; 36 import org.eclipse.jface.viewers.StructuredSelection; 37 import org.eclipse.jface.viewers.Viewer; 38 import org.eclipse.jface.window.Window; 39 import org.eclipse.swt.SWT; 40 import org.eclipse.swt.events.KeyAdapter; 41 import org.eclipse.swt.events.KeyEvent; 42 import org.eclipse.swt.graphics.Font; 43 import org.eclipse.swt.layout.GridData; 44 import org.eclipse.swt.layout.GridLayout; 45 import org.eclipse.swt.widgets.Button; 46 import org.eclipse.swt.widgets.Composite; 47 import org.eclipse.swt.widgets.Control; 48 import org.eclipse.swt.widgets.Event; 49 import org.eclipse.swt.widgets.Group; 50 import org.eclipse.swt.widgets.Label; 51 import org.eclipse.swt.widgets.Listener; 52 import org.eclipse.swt.widgets.Table; 53 import org.eclipse.ui.IWorkbench; 54 import org.eclipse.ui.IWorkbenchPreferencePage; 55 import org.eclipse.ui.PlatformUI; 56 57 public class JavaDetailFormattersPreferencePage extends PreferencePage implements IWorkbenchPreferencePage { 58 59 static public final String DETAIL_FORMATTER_IS_ENABLED= "1"; static public final String DETAIL_FORMATTER_IS_DISABLED= "0"; 62 private CheckboxTableViewer fFormatterListViewer; 63 private Button fAddFormatterButton; 64 private Button fRemoveFormatterButton; 65 private Button fEditFormatterButton; 66 private JDISourceViewer fCodeViewer; 67 private Label fTableLabel; 68 69 private FormatterListViewerContentProvider fFormatViewerContentProvider; 70 private Button fInlineFormattersButton; 71 private Button fInlineAllButton; 72 73 public JavaDetailFormattersPreferencePage() { 74 super(); 75 setTitle(DebugUIMessages.JavaDetailFormattersPreferencePage_0); 76 setPreferenceStore(JDIDebugUIPlugin.getDefault().getPreferenceStore()); 77 setDescription(DebugUIMessages.JavaDetailFormattersPreferencePage_Override_default___toString_____for_Variables_and_Expressions_view_details__1); 78 } 79 80 83 protected Control createContents(Composite parent) { 84 noDefaultAndApplyButton(); 85 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaDebugHelpContextIds.JAVA_DETAIL_FORMATTER_PREFERENCE_PAGE); 86 Font font = parent.getFont(); 87 initializeDialogUnits(parent); 88 89 Composite container = new Composite(parent, SWT.NONE); 91 GridLayout layout = new GridLayout(); 92 layout.numColumns = 2; 93 layout.marginHeight = 0; 94 layout.marginWidth = 0; 95 container.setLayout(layout); 96 GridData gd = new GridData(GridData.FILL_BOTH); 97 container.setLayoutData(gd); 98 container.setFont(font); 99 100 createDetailFormatsPreferences(container); 101 createLabelPreferences(container); 102 103 Dialog.applyDialogFont(container); 104 return container; 105 } 106 107 110 public void init(IWorkbench workbench) { 111 } 112 113 private void createLabelPreferences(Composite parent) { 114 Group group= new Group(parent, SWT.NONE); 115 GridData gridData= new GridData(GridData.FILL_HORIZONTAL); 116 gridData.horizontalSpan= 2; 117 group.setLayoutData(gridData); 118 group.setLayout(new GridLayout()); 119 group.setText(DebugUIMessages.JavaDetailFormattersPreferencePage_1); 120 121 String preference= getPreferenceStore().getString(IJDIPreferencesConstants.PREF_SHOW_DETAILS); 122 123 fInlineFormattersButton = new Button(group, SWT.RADIO); 125 fInlineFormattersButton.setText(DebugUIMessages.JavaDetailFormattersPreferencePage_2); 126 fInlineFormattersButton.setSelection(preference.equals(IJDIPreferencesConstants.INLINE_FORMATTERS)); 127 128 fInlineAllButton = new Button(group, SWT.RADIO); 129 fInlineAllButton.setText(DebugUIMessages.JavaDetailFormattersPreferencePage_3); 130 fInlineAllButton.setSelection(preference.equals(IJDIPreferencesConstants.INLINE_ALL)); 131 132 Button detailPane = new Button(group, SWT.RADIO); 133 detailPane.setText(DebugUIMessages.JavaDetailFormattersPreferencePage_4); 134 detailPane.setSelection(preference.equals(IJDIPreferencesConstants.DETAIL_PANE)); 135 } 136 137 140 private Control createDetailFormatsPreferences(Composite parent) { 141 Font font= parent.getFont(); 142 143 fTableLabel= new Label(parent, SWT.NONE); 145 fTableLabel.setText(DebugUIMessages.JavaDetailFormattersPreferencePage__Types_with_detail_formatters__2); 146 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING); 147 gd.horizontalSpan = 2; 148 fTableLabel.setLayoutData(gd); 149 fTableLabel.setFont(font); 150 151 fFormatterListViewer= CheckboxTableViewer.newCheckList(parent, SWT.CHECK | SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION); 152 Table table = (Table)fFormatterListViewer.getControl(); 153 gd = new GridData(GridData.FILL_BOTH); 154 gd.widthHint= convertWidthInCharsToPixels(10); 156 table.setLayoutData(gd); 157 table.setFont(font); 158 fFormatViewerContentProvider= new FormatterListViewerContentProvider(fFormatterListViewer); 159 fFormatterListViewer.setContentProvider(fFormatViewerContentProvider); 160 fFormatterListViewer.setLabelProvider(new LabelProvider() { 161 public String getText(Object element) { 162 if (element instanceof DetailFormatter) { 163 return ((DetailFormatter)element).getTypeName(); 164 } 165 return null; 166 } 167 }); 168 fFormatterListViewer.addCheckStateListener(new ICheckStateListener() { 169 public void checkStateChanged(CheckStateChangedEvent event) { 170 ((DetailFormatter)event.getElement()).setEnabled(event.getChecked()); 171 } 172 }); 173 fFormatterListViewer.addSelectionChangedListener(new ISelectionChangedListener() { 174 public void selectionChanged(SelectionChangedEvent event) { 175 updatePage((IStructuredSelection)event.getSelection()); 176 } 177 }); 178 fFormatterListViewer.addDoubleClickListener(new IDoubleClickListener() { 179 public void doubleClick(DoubleClickEvent event) { 180 if (!event.getSelection().isEmpty()) { 181 editType(); 182 } 183 } 184 }); 185 table.addKeyListener(new KeyAdapter() { 186 public void keyPressed(KeyEvent event) { 187 if (event.character == SWT.DEL && event.stateMask == 0) { 188 removeTypes(); 189 } 190 } 191 }); 192 fFormatterListViewer.setInput(this); 193 194 createDetailFormatsButtons(parent); 195 196 Label label = new Label(parent, SWT.NONE); 197 label.setText(DebugUIMessages.JavaDetailFormattersPreferencePage_Detail_formatter_code_snippet_defined_for_selected_type__3); 198 label.setFont(font); 199 gd= new GridData(GridData.FILL_HORIZONTAL); 200 gd.horizontalSpan= 2; 201 label.setLayoutData(gd); 202 createSourceViewer(parent); 203 204 fFormatViewerContentProvider.refreshViewer(); 205 return parent; 206 } 207 208 209 private void createDetailFormatsButtons(Composite container) { 210 Font font = container.getFont(); 211 212 Composite buttonContainer = new Composite(container, SWT.NONE); 214 GridData gd = new GridData(GridData.FILL_VERTICAL); 215 buttonContainer.setLayoutData(gd); 216 GridLayout buttonLayout = new GridLayout(); 217 buttonLayout.numColumns = 1; 218 buttonLayout.marginHeight = 0; 219 buttonLayout.marginWidth = 0; 220 buttonContainer.setLayout(buttonLayout); 221 222 fAddFormatterButton = new Button(buttonContainer, SWT.PUSH); 224 fAddFormatterButton.setText(DebugUIMessages.JavaDetailFormattersPreferencePage_Add__Formatter____5); 225 fAddFormatterButton.setToolTipText(DebugUIMessages.JavaDetailFormattersPreferencePage_Allow_you_to_create_a_new_detail_formatter_6); 226 fAddFormatterButton.setLayoutData(gd); 227 fAddFormatterButton.setFont(font); 228 setButtonLayoutData(fAddFormatterButton); 229 fAddFormatterButton.addListener(SWT.Selection, new Listener() { 230 public void handleEvent(Event e) { 231 addType(); 232 } 233 }); 234 235 fEditFormatterButton = new Button(buttonContainer, SWT.PUSH); 237 fEditFormatterButton.setText(DebugUIMessages.JavaDetailFormattersPreferencePage__Edit____9); 238 fEditFormatterButton.setToolTipText(DebugUIMessages.JavaDetailFormattersPreferencePage_Edit_the_selected_detail_formatter_10); 239 fEditFormatterButton.setFont(font); 240 setButtonLayoutData(fEditFormatterButton); 241 fEditFormatterButton.addListener(SWT.Selection, new Listener() { 242 public void handleEvent(Event e) { 243 editType(); 244 } 245 }); 246 fEditFormatterButton.setEnabled(false); 247 248 fRemoveFormatterButton = new Button(buttonContainer, SWT.PUSH); 250 fRemoveFormatterButton.setText(DebugUIMessages.JavaDetailFormattersPreferencePage__Remove_7); 251 fRemoveFormatterButton.setToolTipText(DebugUIMessages.JavaDetailFormattersPreferencePage_Remove_all_selected_detail_formatters_8); 252 fRemoveFormatterButton.setFont(font); 253 setButtonLayoutData(fRemoveFormatterButton); 254 fRemoveFormatterButton.addListener(SWT.Selection, new Listener() { 255 public void handleEvent(Event e) { 256 removeTypes(); 257 } 258 }); 259 fRemoveFormatterButton.setEnabled(false); 260 } 261 262 public void createSourceViewer(Composite container) { 263 fCodeViewer= new JDISourceViewer(container, null, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); 264 265 JavaTextTools tools= JDIDebugUIPlugin.getDefault().getJavaTextTools(); 266 IDocument document= new Document(); 267 tools.setupJavaDocumentPartitioner(document, IJavaPartitions.JAVA_PARTITIONING); 268 fCodeViewer.configure(new DisplayViewerConfiguration()); 269 fCodeViewer.setEditable(false); 270 fCodeViewer.setDocument(document); 271 272 Control control= fCodeViewer.getControl(); 273 GridData gd= new GridData(GridData.FILL_BOTH); 274 gd.horizontalSpan= 2; 275 control.setLayoutData(gd); 277 } 278 279 private void updatePage(IStructuredSelection selection) { 280 fRemoveFormatterButton.setEnabled(!selection.isEmpty()); 281 fEditFormatterButton.setEnabled(selection.size() == 1); 282 updateFormatViewer(selection); 283 } 284 285 private void updateFormatViewer(IStructuredSelection selection) { 286 if (selection.size() == 1) { 287 fCodeViewer.getDocument().set(((DetailFormatter)selection.getFirstElement()).getSnippet()); 288 } else { 289 fCodeViewer.getDocument().set(""); } 291 } 292 293 public void addType() { 294 DetailFormatter detailFormat= new DetailFormatter("", "", true); if (new DetailFormatterDialog(getShell(), detailFormat, fFormatViewerContentProvider.getDefinedTypes(), false).open() == Window.OK) { 296 fFormatViewerContentProvider.addDetailFormatter(detailFormat); 297 } 298 } 299 300 public void removeTypes() { 301 Object [] all = fFormatViewerContentProvider.getElements(null); 302 IStructuredSelection selection= (IStructuredSelection)fFormatterListViewer.getSelection(); 303 Object first = selection.getFirstElement(); 304 int index = -1; 305 for (int i = 0; i < all.length; i++) { 306 Object object = all[i]; 307 if (object.equals(first)) { 308 index = i; 309 break; 310 } 311 } 312 fFormatViewerContentProvider.removeDetailFormatters(selection.toArray()); 313 all = fFormatViewerContentProvider.getElements(null); 314 if (index > all.length - 1) { 315 index = all.length - 1; 316 } 317 if (index >= 0) { 318 fFormatterListViewer.setSelection(new StructuredSelection(all[index])); 319 } 320 } 321 322 public void editType() { 323 IStructuredSelection selection= (IStructuredSelection)fFormatterListViewer.getSelection(); 324 if (new DetailFormatterDialog(getShell(), (DetailFormatter)(selection).getFirstElement(), null, true, true).open() == Window.OK) { 325 fFormatterListViewer.refresh(); 326 fFormatViewerContentProvider.refreshViewer(); 327 updatePage(selection); 328 } 329 } 330 331 public boolean performOk() { 332 if (fFormatViewerContentProvider != null) { 333 fFormatViewerContentProvider.saveDetailFormatters(); 334 335 String value= IJDIPreferencesConstants.DETAIL_PANE; 336 if (fInlineAllButton.getSelection()) { 337 value= IJDIPreferencesConstants.INLINE_ALL; 338 } else if (fInlineFormattersButton.getSelection()) { 339 value= IJDIPreferencesConstants.INLINE_FORMATTERS; 340 } 341 JDIDebugUIPlugin.getDefault().getPreferenceStore().setValue(IJDIPreferencesConstants.PREF_SHOW_DETAILS, value); 342 fCodeViewer.dispose(); 343 } 344 return true; 345 } 346 347 class FormatterListViewerContentProvider implements IStructuredContentProvider { 348 349 private Set fDetailFormattersSet; 350 351 private List fDefinedTypes; 352 353 private CheckboxTableViewer fViewer; 354 355 358 public FormatterListViewerContentProvider(CheckboxTableViewer viewer) { 359 fViewer= viewer; 360 String [] detailFormattersList= JavaDebugOptionsManager.parseList(JDIDebugUIPlugin.getDefault().getPreferenceStore().getString(IJDIPreferencesConstants.PREF_DETAIL_FORMATTERS_LIST)); 362 fDetailFormattersSet= new TreeSet (); 363 fDefinedTypes= new ArrayList (detailFormattersList.length / 3); 364 for (int i= 0, length= detailFormattersList.length; i < length;) { 365 String typeName= detailFormattersList[i++]; 366 String snippet= detailFormattersList[i++].replace('\u0000', ','); 367 boolean enabled= ! DETAIL_FORMATTER_IS_DISABLED.equals(detailFormattersList[i++]); 368 DetailFormatter detailFormatter= new DetailFormatter(typeName, snippet, enabled); 369 fDetailFormattersSet.add(detailFormatter); 370 fDefinedTypes.add(typeName); 371 } 372 } 373 374 377 public void saveDetailFormatters() { 378 String [] values= new String [fDetailFormattersSet.size() * 3]; 379 int i= 0; 380 for (Iterator iter= fDetailFormattersSet.iterator(); iter.hasNext();) { 381 DetailFormatter detailFormatter= (DetailFormatter) iter.next(); 382 values[i++]= detailFormatter.getTypeName(); 383 values[i++]= detailFormatter.getSnippet().replace(',','\u0000'); 384 values[i++]= detailFormatter.isEnabled() ? DETAIL_FORMATTER_IS_ENABLED : DETAIL_FORMATTER_IS_DISABLED; 385 } 386 String pref = JavaDebugOptionsManager.serializeList(values); 387 getPreferenceStore().setValue(IJDIPreferencesConstants.PREF_DETAIL_FORMATTERS_LIST, pref); 388 389 } 390 391 394 public void addDetailFormatter(DetailFormatter detailFormatter) { 395 fDetailFormattersSet.add(detailFormatter); 396 fDefinedTypes.add(detailFormatter.getTypeName()); 397 fViewer.refresh(); 398 refreshViewer(); 399 IStructuredSelection selection= new StructuredSelection(detailFormatter); 400 fViewer.setSelection(selection); 401 updatePage(selection); 402 } 403 404 407 public void removeDetailFormatter(DetailFormatter detailFormatter) { 408 fDetailFormattersSet.remove(detailFormatter); 409 fDefinedTypes.remove(detailFormatter.getTypeName()); 410 fViewer.refresh(); 411 IStructuredSelection selection= new StructuredSelection(); 412 fViewer.setSelection(selection); 413 updatePage(selection); 414 } 415 416 419 public void removeDetailFormatters(Object [] detailFormatters) { 420 for (int i= 0, length= detailFormatters.length; i < length; i++) { 421 fDetailFormattersSet.remove(detailFormatters[i]); 422 fDefinedTypes.remove(((DetailFormatter)detailFormatters[i]).getTypeName()); 423 } 424 fViewer.refresh(); 425 IStructuredSelection selection= new StructuredSelection(); 426 fViewer.setSelection(selection); 427 updatePage(selection); 428 } 429 430 433 private void refreshViewer() { 434 DetailFormatter[] checkedElementsTmp= new DetailFormatter[fDetailFormattersSet.size()]; 435 int i= 0; 436 for (Iterator iter= fDetailFormattersSet.iterator(); iter.hasNext();) { 437 DetailFormatter detailFormatter= (DetailFormatter) iter.next(); 438 if (detailFormatter.isEnabled()) { 439 checkedElementsTmp[i++]= detailFormatter; 440 } 441 } 442 DetailFormatter[] checkedElements= new DetailFormatter[i]; 443 System.arraycopy(checkedElementsTmp, 0, checkedElements, 0, i); 444 fViewer.setAllChecked(false); 445 fViewer.setCheckedElements(checkedElements); 446 } 447 448 451 public Object [] getElements(Object inputElement) { 452 return fDetailFormattersSet.toArray(); 453 } 454 455 public List getDefinedTypes() { 456 return fDefinedTypes; 457 } 458 459 462 public void dispose() { 463 } 464 465 468 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 469 } 470 } 471 472 475 public boolean performCancel() { 476 if (fCodeViewer != null) { 477 fCodeViewer.dispose(); 478 } 479 return super.performCancel(); 480 } 481 } 482 | Popular Tags |