Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9272a1fb470a1588adc21f9e31ac1813ba48691d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*******************************************************************************
 * Copyright (c) 2006, 2010 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.internal.debug.ui;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.debug.internal.ui.SWTFactory;
import org.eclipse.jdt.internal.debug.core.HeapWalkingManager;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.PlatformUI;

/**
 * Provides a page for changing the heap walking options. Interacts
 * with the HeapWalkingManager to get/set options.
 * 
 * @see HeapWalkingManager
 * @since 3.3
 */
public class HeapWalkingPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {

	private Button fShowReferencesInVarView;
	private Text fAllReferencesMaxCount;
	private Text fAllInstancesMaxCount;
	private Map fErrorMessages;
	
	/* (non-Javadoc)
	 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
	 */
	public void init(IWorkbench workbench) {
		fErrorMessages = new HashMap();
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.PreferencePage#createControl(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void createControl(Composite parent) {
		super.createControl(parent);
		// TODO: Help must be updated
		PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaDebugHelpContextIds.JAVA_HEAPWALKING_PREFERENCE_PAGE);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected Control createContents(Composite parent) {
		
		Composite comp = SWTFactory.createComposite(parent, parent.getFont(), 1, 1, GridData.FILL_BOTH);
		
		SWTFactory.createWrapLabel(comp, DebugUIMessages.HeapWalkingPreferencePage_0, 1, 300);
		SWTFactory.createVerticalSpacer(comp, 2);
		
		fShowReferencesInVarView = SWTFactory.createCheckButton(comp, DebugUIMessages.HeapWalkingPreferencePage_5, null, HeapWalkingManager.getDefault().isShowReferenceInVarView(), 1);
		SWTFactory.createVerticalSpacer(comp, 2);
		
		Group group = SWTFactory.createGroup(comp, DebugUIMessages.HeapWalkingPreferencePage_3, 2, 1, GridData.FILL_HORIZONTAL);
		SWTFactory.createLabel(group, DebugUIMessages.HeapWalkingPreferencePage_4, 2);
		
		SWTFactory.createLabel(group, DebugUIMessages.HeapWalkingPreferencePage_1, 1);
		fAllInstancesMaxCount = SWTFactory.createSingleText(group, 1);
		fAllInstancesMaxCount.addModifyListener(new ModifyListener(){
			public void modifyText(ModifyEvent e) {
				try{
					int result = Integer.parseInt(fAllInstancesMaxCount.getText());
					if (result < 0) throw new NumberFormatException();
					clearErrorMessage(fAllInstancesMaxCount);
				} catch (NumberFormatException exception){
					setErrorMessage(fAllInstancesMaxCount, DebugUIMessages.HeapWalkingPreferencePage_6);
				}
			}
		});
		fAllInstancesMaxCount.setText("" + HeapWalkingManager.getDefault().getAllInstancesMaxCount()); //$NON-NLS-1$
		
		SWTFactory.createLabel(group, DebugUIMessages.HeapWalkingPreferencePage_2, 1);
		fAllReferencesMaxCount = SWTFactory.createSingleText(group, 1);
		fAllReferencesMaxCount.addModifyListener(new ModifyListener(){
			public void modifyText(ModifyEvent e) {
				try{
					int result = Integer.parseInt(fAllReferencesMaxCount.getText());
					if (result < 0) throw new NumberFormatException();
					clearErrorMessage(fAllReferencesMaxCount);
				} catch (NumberFormatException exception){
					setErrorMessage(fAllReferencesMaxCount, DebugUIMessages.HeapWalkingPreferencePage_6);
				}
			}
		});
		fAllReferencesMaxCount.setText("" + HeapWalkingManager.getDefault().getAllReferencesMaxCount()); //$NON-NLS-1$
		
		return comp;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.PreferencePage#performOk()
	 */
	@Override
	public boolean performOk() {
		boolean result = super.performOk();
		if (result){
			HeapWalkingManager.getDefault().setShowReferenceInVarView(fShowReferencesInVarView.getSelection());
			try{
				int maxReferences = Integer.parseInt(fAllReferencesMaxCount.getText());
				HeapWalkingManager.getDefault().setAllReferencesMaxCount(maxReferences);
			} catch (NumberFormatException exception){
				setErrorMessage(fAllReferencesMaxCount,DebugUIMessages.HeapWalkingPreferencePage_6);
				return false;
			}
			try{
				int maxReferences = Integer.parseInt(fAllInstancesMaxCount.getText());
				HeapWalkingManager.getDefault().setAllInstancesMaxCount(maxReferences);
			} catch (NumberFormatException exception){
				setErrorMessage(fAllInstancesMaxCount,DebugUIMessages.HeapWalkingPreferencePage_6);
				result = false;
			}
			HeapWalkingManager.getDefault().setShowReferenceInVarView(fShowReferencesInVarView.getSelection());
		}
		return result;
	}
	
	/**
	 * Sets an error message associated with a specific field.  Allows several
	 * fields to have their own error message.  The dialog's error message will
	 * be set to the given message.
	 * 
	 * @param cause The field that the message is associated with
	 * @param message The error message to display to the user
	 */
	private void setErrorMessage(Object cause, String message){
		fErrorMessages.put(cause,message);
		setErrorMessage(message);
		setValid(false);
	}
	
	/**
	 * Clears the error message associated with the given field.  If there are other
	 * error messages, one will be displayed to the user.  If there are no more error
	 * messages, the page becomes valid.
	 * 
	 * @param cause The field associated with a current error message.
	 */
	private void clearErrorMessage(Object cause){
		fErrorMessages.remove(cause);
		Iterator iter = fErrorMessages.values().iterator();
		if (iter.hasNext()){
			setErrorMessage((String)iter.next());
		} else {
			setErrorMessage(null);
			setValid(true);
		}
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
	 */
	@Override
	protected void performDefaults() {
		HeapWalkingManager.getDefault().resetToDefaultSettings();
		fAllReferencesMaxCount.setText(Integer.toString(HeapWalkingManager.getDefault().getAllReferencesMaxCount()));
		fAllInstancesMaxCount.setText(Integer.toString(HeapWalkingManager.getDefault().getAllReferencesMaxCount())); 
		fShowReferencesInVarView.setSelection(HeapWalkingManager.getDefault().isShowReferenceInVarView());
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.DialogPage#dispose()
	 */
	@Override
	public void dispose() {
		super.dispose();
		fErrorMessages.clear();
	}

}

Back to the top