KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > preferences > ViewManagementPreferencePage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.debug.internal.ui.preferences;
12
13 import java.util.Iterator JavaDoc;
14
15 import org.eclipse.debug.internal.ui.IDebugHelpContextIds;
16 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
17 import org.eclipse.debug.internal.ui.SWTFactory;
18 import org.eclipse.debug.internal.ui.views.ViewContextService;
19 import org.eclipse.debug.ui.DebugUITools;
20 import org.eclipse.debug.ui.IDebugUIConstants;
21 import org.eclipse.jface.dialogs.Dialog;
22 import org.eclipse.jface.preference.PreferencePage;
23 import org.eclipse.jface.viewers.CheckboxTableViewer;
24 import org.eclipse.jface.viewers.IStructuredContentProvider;
25 import org.eclipse.jface.viewers.Viewer;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.events.SelectionAdapter;
28 import org.eclipse.swt.events.SelectionEvent;
29 import org.eclipse.swt.events.SelectionListener;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.layout.GridLayout;
32 import org.eclipse.swt.widgets.Button;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Control;
35 import org.eclipse.swt.widgets.Label;
36 import org.eclipse.swt.widgets.Table;
37 import org.eclipse.ui.IPerspectiveDescriptor;
38 import org.eclipse.ui.IPerspectiveRegistry;
39 import org.eclipse.ui.IWorkbench;
40 import org.eclipse.ui.IWorkbenchPreferencePage;
41 import org.eclipse.ui.PlatformUI;
42 import org.eclipse.ui.model.PerspectiveLabelProvider;
43
44 /**
45  * Preference page for configuring the debugger's automatic
46  * view management.
47  */

48 public class ViewManagementPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
49
50     private CheckboxTableViewer fPerspectiveViewer;
51     private Button fTrackViewsButton;
52     private Button fResetViewsButton;
53     // This flag indicates whether or not the user has pressed the reset button
54
private boolean fResetPressed= false;
55     private PerspectiveLabelProvider fLabelProvider= null;
56     private SelectionListener fSelectionListener= new SelectionAdapter() {
57     
58         public void widgetSelected(SelectionEvent e) {
59             Object JavaDoc source = e.getSource();
60             if (source == fResetViewsButton) {
61                 handleResetPressed();
62             } else if (source == fTrackViewsButton) {
63                 handleTrackViewsToggled();
64             }
65         }
66     
67     };
68     
69     public ViewManagementPreferencePage() {
70         super();
71         setTitle(DebugPreferencesMessages.ViewManagementPreferencePage_1);
72         setDescription(DebugPreferencesMessages.ViewManagementPreferencePage_0);
73         setPreferenceStore(DebugUITools.getPreferenceStore());
74     }
75
76     /* (non-Javadoc)
77      * @see org.eclipse.jface.preference.PreferencePage#createControl(org.eclipse.swt.widgets.Composite)
78      */

79     public void createControl(Composite parent) {
80         super.createControl(parent);
81         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IDebugHelpContextIds.VIEW_MANAGEMENT_PREFERENCE_PAGE);
82     }
83
84     /* (non-Javadoc)
85      * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
86      */

87     protected Control createContents(Composite parent) {
88         Composite composite= new Composite(parent, SWT.NONE);
89         composite.setLayout(new GridLayout());
90         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
91         composite.setFont(parent.getFont());
92         
93         createPerspectiveViewer(composite);
94         
95         createViewTrackingOptions(composite);
96         
97         Dialog.applyDialogFont(composite);
98         
99         return composite;
100     }
101
102     /**
103      * @param composite
104      */

105     private void createViewTrackingOptions(Composite composite) {
106         fTrackViewsButton= new Button(composite, SWT.CHECK);
107         fTrackViewsButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
108         fTrackViewsButton.setText(DebugPreferencesMessages.ViewManagementPreferencePage_3);
109         fTrackViewsButton.setSelection(DebugUITools.getPreferenceStore().getBoolean(IInternalDebugUIConstants.PREF_TRACK_VIEWS));
110         fTrackViewsButton.addSelectionListener(fSelectionListener);
111         
112         Label label= new Label(composite, SWT.WRAP);
113         label.setText(DebugPreferencesMessages.ViewManagementPreferencePage_4);
114         label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
115         
116         fResetViewsButton= SWTFactory.createPushButton(composite, DebugPreferencesMessages.ViewManagementPreferencePage_5, null);
117         ((GridData) fResetViewsButton.getLayoutData()).horizontalAlignment= GridData.BEGINNING;
118         fResetViewsButton.addSelectionListener(fSelectionListener);
119         updateResetButton();
120     }
121     
122     private void handleResetPressed() {
123         fResetPressed= true;
124         fResetViewsButton.setEnabled(false);
125     }
126     
127     protected void handleTrackViewsToggled() {
128         if (fTrackViewsButton.getSelection()) {
129             // When toggled on, possibly re-enable the reset button
130
updateResetButton();
131         } else {
132             // When toggled off, disable the reset button
133
fResetViewsButton.setEnabled(false);
134         }
135     }
136
137     /**
138      * @param parent
139      */

140     private void createPerspectiveViewer(Composite parent) {
141         Label label= new Label(parent, SWT.WRAP);
142         label.setText(DebugPreferencesMessages.ViewManagementPreferencePage_2);
143         label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
144         
145         Table table= new Table(parent, SWT.CHECK | SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
146         table.setLayout(new GridLayout());
147         table.setLayoutData(new GridData(GridData.FILL_BOTH));
148         
149         fPerspectiveViewer= new CheckboxTableViewer(table);
150         PerspectiveProvider provider= new PerspectiveProvider();
151         fPerspectiveViewer.setContentProvider(provider);
152         fLabelProvider= new PerspectiveLabelProvider();
153         fPerspectiveViewer.setLabelProvider(fLabelProvider);
154         fPerspectiveViewer.setInput(this);
155         
156         checkPerspectives(getPreferenceStore().getString(IDebugUIConstants.PREF_MANAGE_VIEW_PERSPECTIVES));
157     }
158     
159     private void checkPerspectives(String JavaDoc perspectiveList) {
160         fPerspectiveViewer.setAllChecked(false);
161         IPerspectiveRegistry registry= PlatformUI.getWorkbench().getPerspectiveRegistry();
162         Iterator JavaDoc perspectiveIds= ViewContextService.parseList(perspectiveList).iterator();
163         while (perspectiveIds.hasNext()) {
164             IPerspectiveDescriptor descriptor = registry.findPerspectiveWithId((String JavaDoc) perspectiveIds.next());
165             if (descriptor != null) {
166                 fPerspectiveViewer.setChecked(descriptor, true);
167             }
168         }
169     }
170
171     /* (non-Javadoc)
172      * @see org.eclipse.jface.preference.IPreferencePage#performOk()
173      */

174     public boolean performOk() {
175         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
176         Object JavaDoc[] descriptors = fPerspectiveViewer.getCheckedElements();
177         for (int i = 0; i < descriptors.length; i++) {
178             buffer.append(((IPerspectiveDescriptor) descriptors[i]).getId()).append(',');
179         }
180         
181         getPreferenceStore().setValue(IDebugUIConstants.PREF_MANAGE_VIEW_PERSPECTIVES, buffer.toString());
182         boolean trackViews = fTrackViewsButton.getSelection();
183         getPreferenceStore().setValue(IInternalDebugUIConstants.PREF_TRACK_VIEWS, trackViews);
184         if (fResetPressed || !trackViews) {
185             // Reset if the user has pressed reset or chosen to no longer track views
186
getPreferenceStore().setValue(IInternalDebugUIConstants.PREF_USER_VIEW_BINDINGS, ""); //$NON-NLS-1$
187
}
188         return super.performOk();
189     }
190
191     /* (non-Javadoc)
192      * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
193      */

194     protected void performDefaults() {
195         checkPerspectives(getPreferenceStore().getDefaultString(IDebugUIConstants.PREF_MANAGE_VIEW_PERSPECTIVES));
196         fTrackViewsButton.setSelection(getPreferenceStore().getDefaultBoolean(IInternalDebugUIConstants.PREF_TRACK_VIEWS));
197         fResetPressed= false;
198         updateResetButton();
199         super.performDefaults();
200     }
201
202     /* (non-Javadoc)
203      * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
204      */

205     public void init(IWorkbench workbench) {
206     }
207     
208     /**
209      * Updates enablement of the reset button.
210      * Enable if either persisted view collection is not empty.
211      * Always disable if "track views" is turned off.
212      */

213     private void updateResetButton() {
214         boolean enableReset= !"".equals(getPreferenceStore().getString(IInternalDebugUIConstants.PREF_USER_VIEW_BINDINGS)); //$NON-NLS-1$
215
// Only enable the button if there are views to clear, the user hasn't pressed the reset
216
// button, and the option to "track views" is turned on.
217
fResetViewsButton.setEnabled(enableReset && !fResetPressed && fTrackViewsButton.getSelection());
218     }
219
220     private static class PerspectiveProvider implements IStructuredContentProvider {
221         
222         /* (non-Javadoc)
223          * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
224          */

225         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
226             return PlatformUI.getWorkbench().getPerspectiveRegistry().getPerspectives();
227         }
228         
229         /* (non-Javadoc)
230          * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
231          */

232         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
233         }
234         
235         /* (non-Javadoc)
236          * @see org.eclipse.jface.viewers.IContentProvider#dispose()
237          */

238         public void dispose() {
239         }
240         
241     }
242     /* (non-Javadoc)
243      * @see org.eclipse.jface.dialogs.IDialogPage#dispose()
244      */

245     public void dispose() {
246         super.dispose();
247         if (fLabelProvider != null) {
248             fLabelProvider.dispose();
249         }
250     }
251 }
252
Popular Tags