KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006, 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.jdt.internal.debug.ui;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.jdt.internal.debug.core.HeapWalkingManager;
18 import org.eclipse.jface.preference.PreferencePage;
19 import org.eclipse.swt.events.ModifyEvent;
20 import org.eclipse.swt.events.ModifyListener;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Group;
26 import org.eclipse.swt.widgets.Text;
27 import org.eclipse.ui.IWorkbench;
28 import org.eclipse.ui.IWorkbenchPreferencePage;
29 import org.eclipse.ui.PlatformUI;
30
31 /**
32  * Provides a page for changing the heap walking options. Interacts
33  * with the HeapWalkingManager to get/set options.
34  *
35  * @see HeapWalkingManager
36  * @since 3.3
37  */

38 public class HeapWalkingPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
39
40     private Button fShowReferencesInVarView;
41     private Text fAllReferencesMaxCount;
42     private Text fAllInstancesMaxCount;
43     private Map JavaDoc fErrorMessages;
44     
45     /* (non-Javadoc)
46      * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
47      */

48     public void init(IWorkbench workbench) {
49         fErrorMessages = new HashMap JavaDoc();
50     }
51     
52     /* (non-Javadoc)
53      * @see org.eclipse.jface.preference.PreferencePage#createControl(org.eclipse.swt.widgets.Composite)
54      */

55     public void createControl(Composite parent) {
56         super.createControl(parent);
57         // TODO: Help must be updated
58
PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaDebugHelpContextIds.JAVA_HEAPWALKING_PREFERENCE_PAGE);
59     }
60     
61     /* (non-Javadoc)
62      * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
63      */

64     protected Control createContents(Composite parent) {
65         
66         Composite comp = SWTFactory.createComposite(parent, parent.getFont(), 1, 1, GridData.FILL_BOTH);
67         
68         SWTFactory.createWrapLabel(comp, DebugUIMessages.HeapWalkingPreferencePage_0, 1, 300);
69         SWTFactory.createVerticalSpacer(comp, 2);
70         
71         fShowReferencesInVarView = SWTFactory.createCheckButton(comp, DebugUIMessages.HeapWalkingPreferencePage_5, null, HeapWalkingManager.getDefault().isShowReferenceInVarView(), 1);
72         SWTFactory.createVerticalSpacer(comp, 2);
73         
74         Group group = SWTFactory.createGroup(comp, DebugUIMessages.HeapWalkingPreferencePage_3, 2, 1, GridData.FILL_HORIZONTAL);
75         SWTFactory.createLabel(group, DebugUIMessages.HeapWalkingPreferencePage_4, 2);
76         
77         SWTFactory.createLabel(group, DebugUIMessages.HeapWalkingPreferencePage_1, 1);
78         fAllInstancesMaxCount = SWTFactory.createSingleText(group, 1);
79         fAllInstancesMaxCount.addModifyListener(new ModifyListener(){
80             public void modifyText(ModifyEvent e) {
81                 try{
82                     int result = Integer.parseInt(fAllInstancesMaxCount.getText());
83                     if (result < 0) throw new NumberFormatException JavaDoc();
84                     clearErrorMessage(fAllInstancesMaxCount);
85                 } catch (NumberFormatException JavaDoc exception){
86                     setErrorMessage(fAllInstancesMaxCount, DebugUIMessages.HeapWalkingPreferencePage_6);
87                 }
88             }
89         });
90         fAllInstancesMaxCount.setText("" + HeapWalkingManager.getDefault().getAllInstancesMaxCount()); //$NON-NLS-1$
91

92         SWTFactory.createLabel(group, DebugUIMessages.HeapWalkingPreferencePage_2, 1);
93         fAllReferencesMaxCount = SWTFactory.createSingleText(group, 1);
94         fAllReferencesMaxCount.addModifyListener(new ModifyListener(){
95             public void modifyText(ModifyEvent e) {
96                 try{
97                     int result = Integer.parseInt(fAllReferencesMaxCount.getText());
98                     if (result < 0) throw new NumberFormatException JavaDoc();
99                     clearErrorMessage(fAllReferencesMaxCount);
100                 } catch (NumberFormatException JavaDoc exception){
101                     setErrorMessage(fAllReferencesMaxCount, DebugUIMessages.HeapWalkingPreferencePage_6);
102                 }
103             }
104         });
105         fAllReferencesMaxCount.setText("" + HeapWalkingManager.getDefault().getAllReferencesMaxCount()); //$NON-NLS-1$
106

107         return comp;
108     }
109     
110     /* (non-Javadoc)
111      * @see org.eclipse.jface.preference.PreferencePage#performOk()
112      */

113     public boolean performOk() {
114         boolean result = super.performOk();
115         if (result){
116             HeapWalkingManager.getDefault().setShowReferenceInVarView(fShowReferencesInVarView.getSelection());
117             try{
118                 int maxReferences = Integer.parseInt(fAllReferencesMaxCount.getText());
119                 HeapWalkingManager.getDefault().setAllReferencesMaxCount(maxReferences);
120             } catch (NumberFormatException JavaDoc exception){
121                 setErrorMessage(fAllReferencesMaxCount,DebugUIMessages.HeapWalkingPreferencePage_6);
122                 return false;
123             }
124             try{
125                 int maxReferences = Integer.parseInt(fAllInstancesMaxCount.getText());
126                 HeapWalkingManager.getDefault().setAllInstancesMaxCount(maxReferences);
127             } catch (NumberFormatException JavaDoc exception){
128                 setErrorMessage(fAllInstancesMaxCount,DebugUIMessages.HeapWalkingPreferencePage_6);
129                 result = false;
130             }
131             HeapWalkingManager.getDefault().setShowReferenceInVarView(fShowReferencesInVarView.getSelection());
132         }
133         return result;
134     }
135     
136     /**
137      * Sets an error message associated with a specific field. Allows several
138      * fields to have their own error message. The dialog's error message will
139      * be set to the given message.
140      *
141      * @param cause The field that the message is associated with
142      * @param message The error message to display to the user
143      */

144     private void setErrorMessage(Object JavaDoc cause, String JavaDoc message){
145         fErrorMessages.put(cause,message);
146         setErrorMessage(message);
147         setValid(false);
148     }
149     
150     /**
151      * Clears the error message associated with the given field. If there are other
152      * error messages, one will be displayed to the user. If there are no more error
153      * messages, the page becomes valid.
154      *
155      * @param cause The field associated with a current error message.
156      */

157     private void clearErrorMessage(Object JavaDoc cause){
158         fErrorMessages.remove(cause);
159         Iterator JavaDoc iter = fErrorMessages.values().iterator();
160         if (iter.hasNext()){
161             setErrorMessage((String JavaDoc)iter.next());
162         } else {
163             setErrorMessage(null);
164             setValid(true);
165         }
166     }
167     
168     /* (non-Javadoc)
169      * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
170      */

171     protected void performDefaults() {
172         HeapWalkingManager.getDefault().resetToDefaultSettings();
173         fAllReferencesMaxCount.setText(Integer.toString(HeapWalkingManager.getDefault().getAllReferencesMaxCount()));
174         fAllInstancesMaxCount.setText(Integer.toString(HeapWalkingManager.getDefault().getAllReferencesMaxCount()));
175         fShowReferencesInVarView.setSelection(HeapWalkingManager.getDefault().isShowReferenceInVarView());
176     }
177     
178     /* (non-Javadoc)
179      * @see org.eclipse.jface.dialogs.DialogPage#dispose()
180      */

181     public void dispose() {
182         super.dispose();
183         fErrorMessages.clear();
184     }
185
186 }
187
Popular Tags