KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > JavaDetailFormattersPreferencePage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.ui;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Set JavaDoc;
17 import java.util.TreeSet JavaDoc;
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 JavaDoc DETAIL_FORMATTER_IS_ENABLED= "1"; //$NON-NLS-1$
60
static public final String JavaDoc DETAIL_FORMATTER_IS_DISABLED= "0"; //$NON-NLS-1$
61

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     /* (non-Javadoc)
81      * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
82      */

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         // top level container
90
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     /**
108      * @see IWorkbenchPreferencePage#init(IWorkbench)
109      */

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 JavaDoc preference= getPreferenceStore().getString(IJDIPreferencesConstants.PREF_SHOW_DETAILS);
122         
123         // Create the 3 detail option radio buttons
124
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     /**
138      * Create a group to contain the detail formatters related widgetry
139      */

140     private Control createDetailFormatsPreferences(Composite parent) {
141         Font font= parent.getFont();
142         
143         //table label
144
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.heightHint= convertHeightInCharsToPixels(5);
155
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 JavaDoc getText(Object JavaDoc 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         // button container
213
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         // Add type button
223
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         // Edit button
236
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         // Remove button
249
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         //gd.heightHint= convertHeightInCharsToPixels(5);
276
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(""); //$NON-NLS-1$
290
}
291     }
292
293     public void addType() {
294         DetailFormatter detailFormat= new DetailFormatter("", "", true); //$NON-NLS-1$ //$NON-NLS-2$
295
if (new DetailFormatterDialog(getShell(), detailFormat, fFormatViewerContentProvider.getDefinedTypes(), false).open() == Window.OK) {
296             fFormatViewerContentProvider.addDetailFormatter(detailFormat);
297         }
298     }
299     
300     public void removeTypes() {
301         Object JavaDoc[] all = fFormatViewerContentProvider.getElements(null);
302         IStructuredSelection selection= (IStructuredSelection)fFormatterListViewer.getSelection();
303         Object JavaDoc first = selection.getFirstElement();
304         int index = -1;
305         for (int i = 0; i < all.length; i++) {
306             Object JavaDoc 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 JavaDoc 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 JavaDoc fDetailFormattersSet;
350         
351         private List JavaDoc fDefinedTypes;
352         
353         private CheckboxTableViewer fViewer;
354         
355         /**
356          * FormatterListViewerContentProvider constructor.
357          */

358         public FormatterListViewerContentProvider(CheckboxTableViewer viewer) {
359             fViewer= viewer;
360             // load the current formatters
361
String JavaDoc[] detailFormattersList= JavaDebugOptionsManager.parseList(JDIDebugUIPlugin.getDefault().getPreferenceStore().getString(IJDIPreferencesConstants.PREF_DETAIL_FORMATTERS_LIST));
362             fDetailFormattersSet= new TreeSet JavaDoc();
363             fDefinedTypes= new ArrayList JavaDoc(detailFormattersList.length / 3);
364             for (int i= 0, length= detailFormattersList.length; i < length;) {
365                 String JavaDoc typeName= detailFormattersList[i++];
366                 String JavaDoc 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         /**
375          * Save the detail formatter list.
376          */

377         public void saveDetailFormatters() {
378             String JavaDoc[] values= new String JavaDoc[fDetailFormattersSet.size() * 3];
379             int i= 0;
380             for (Iterator JavaDoc 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 JavaDoc pref = JavaDebugOptionsManager.serializeList(values);
387             getPreferenceStore().setValue(IJDIPreferencesConstants.PREF_DETAIL_FORMATTERS_LIST, pref);
388             
389         }
390         
391         /**
392          * Add a detail formatter.
393          */

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         /**
405          * Remove a detailFormatter
406          */

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         /**
417          * Remove detailFormatters
418          */

419         public void removeDetailFormatters(Object JavaDoc[] 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         /**
431          * Refresh the formatter list viewer.
432          */

433         private void refreshViewer() {
434             DetailFormatter[] checkedElementsTmp= new DetailFormatter[fDetailFormattersSet.size()];
435             int i= 0;
436             for (Iterator JavaDoc 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         /**
449          * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(Object)
450          */

451         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
452             return fDetailFormattersSet.toArray();
453         }
454         
455         public List JavaDoc getDefinedTypes() {
456             return fDefinedTypes;
457         }
458         
459         /**
460          * @see org.eclipse.jface.viewers.IContentProvider#dispose()
461          */

462         public void dispose() {
463         }
464
465         /**
466          * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(Viewer, Object, Object)
467          */

468         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
469         }
470     }
471     
472     /* (non-Javadoc)
473      * @see org.eclipse.jface.preference.PreferencePage#performCancel()
474      */

475     public boolean performCancel() {
476         if (fCodeViewer != null) {
477             fCodeViewer.dispose();
478         }
479         return super.performCancel();
480     }
481 }
482
Popular Tags