KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > propertypages > JavaBreakpointPage


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.jdt.internal.debug.ui.propertypages;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.resources.IWorkspaceRunnable;
17 import org.eclipse.core.resources.ResourcesPlugin;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IAdaptable;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.jdt.debug.core.IJavaBreakpoint;
24 import org.eclipse.jdt.internal.debug.ui.IJavaDebugHelpContextIds;
25 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
26 import org.eclipse.jdt.internal.debug.ui.SWTFactory;
27 import org.eclipse.jdt.ui.JavaElementLabelProvider;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.events.ModifyEvent;
30 import org.eclipse.swt.events.ModifyListener;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.events.ShellEvent;
34 import org.eclipse.swt.events.ShellListener;
35 import org.eclipse.swt.layout.GridData;
36 import org.eclipse.swt.widgets.Button;
37 import org.eclipse.swt.widgets.Combo;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Control;
40 import org.eclipse.swt.widgets.Label;
41 import org.eclipse.swt.widgets.Shell;
42 import org.eclipse.swt.widgets.Text;
43 import org.eclipse.ui.PlatformUI;
44 import org.eclipse.ui.dialogs.PropertyPage;
45 import org.eclipse.ui.model.IWorkbenchAdapter;
46
47 import com.ibm.icu.text.MessageFormat;
48
49 /**
50  * Property page for configuring IJavaBreakpoints.
51  */

52 public class JavaBreakpointPage extends PropertyPage {
53     
54     protected JavaElementLabelProvider fJavaLabelProvider= new JavaElementLabelProvider(JavaElementLabelProvider.SHOW_DEFAULT);
55     protected Button fEnabledButton;
56     protected Button fHitCountButton;
57     protected Text fHitCountText;
58     protected Combo fSuspendPolicy;
59     protected List JavaDoc fErrorMessages= new ArrayList JavaDoc();
60     
61     /**
62      * Attribute used to indicate that a breakpoint should be deleted
63      * when cancel is pressed.
64      */

65     public static final String JavaDoc ATTR_DELETE_ON_CANCEL = JDIDebugUIPlugin.getUniqueIdentifier() + ".ATTR_DELETE_ON_CANCEL"; //$NON-NLS-1$
66

67     /**
68      * Constant for the empty string
69      */

70     protected static final String JavaDoc EMPTY_STRING = ""; //$NON-NLS-1$
71

72     /**
73      * the hit count error message
74      */

75     private static final String JavaDoc fgHitCountErrorMessage = PropertyPageMessages.JavaBreakpointPage_0;
76     
77     /**
78      * Store the breakpoint properties.
79      * @see org.eclipse.jface.preference.IPreferencePage#performOk()
80      */

81     public boolean performOk() {
82         IWorkspaceRunnable wr = new IWorkspaceRunnable() {
83             public void run(IProgressMonitor monitor) throws CoreException {
84                 IJavaBreakpoint breakpoint = getBreakpoint();
85                 boolean delOnCancel = breakpoint.getMarker().getAttribute(ATTR_DELETE_ON_CANCEL) != null;
86                 if (delOnCancel) {
87                     // if this breakpoint is being created, remove the "delete on cancel" attribute
88
// and register with the breakpoint manager
89
breakpoint.getMarker().setAttribute(ATTR_DELETE_ON_CANCEL, (String JavaDoc)null);
90                     breakpoint.setRegistered(true);
91                 }
92                 doStore();
93             }
94         };
95         try {
96             ResourcesPlugin.getWorkspace().run(wr, null, 0, null);
97         }
98         catch (CoreException e) {
99             JDIDebugUIPlugin.statusDialog(e.getStatus());
100             JDIDebugUIPlugin.log(e);
101         }
102         return super.performOk();
103     }
104     
105     /**
106      * Adds the given error message to the errors currently displayed on this page.
107      * The page displays the most recently added error message.
108      * Clients should retain messages that are passed into this method as the
109      * message should later be passed into removeErrorMessage(String) to clear the error.
110      * This method should be used instead of setErrorMessage(String).
111      * @param message the error message to display on this page.
112      */

113     protected void addErrorMessage(String JavaDoc message) {
114         fErrorMessages.remove(message);
115         fErrorMessages.add(message);
116         setErrorMessage(message);
117         setValid(message == null);
118     }
119     
120     /**
121      * Removes the given error message from the errors currently displayed on this page.
122      * When an error message is removed, the page displays the error that was added
123      * before the given message. This is akin to popping the message from a stack.
124      * Clients should call this method instead of setErrorMessage(null).
125      * @param message the error message to clear
126      */

127     protected void removeErrorMessage(String JavaDoc message) {
128         fErrorMessages.remove(message);
129         if (fErrorMessages.isEmpty()) {
130             addErrorMessage(null);
131         } else {
132             addErrorMessage((String JavaDoc) fErrorMessages.get(fErrorMessages.size() - 1));
133         }
134     }
135     
136     /**
137      * Stores the values configured in this page. This method
138      * should be called from within a workspace runnable to
139      * reduce the number of resource deltas.
140      */

141     protected void doStore() throws CoreException {
142         IJavaBreakpoint breakpoint = getBreakpoint();
143         storeSuspendPolicy(breakpoint);
144         storeHitCount(breakpoint);
145         storeEnabled(breakpoint);
146     }
147
148     /**
149      * Stores the value of the enabled state in the breakpoint.
150      * @param breakpoint the breakpoint to update
151      * @throws CoreException if an exception occurs while setting
152      * the enabled state
153      */

154     private void storeEnabled(IJavaBreakpoint breakpoint) throws CoreException {
155         breakpoint.setEnabled(fEnabledButton.getSelection());
156     }
157
158     /**
159      * Stores the value of the hit count in the breakpoint.
160      * @param breakpoint the breakpoint to update
161      * @throws CoreException if an exception occurs while setting
162      * the hit count
163      */

164     private void storeHitCount(IJavaBreakpoint breakpoint) throws CoreException {
165         int hitCount = -1;
166         if (fHitCountButton.getSelection()) {
167             try {
168                 hitCount = Integer.parseInt(fHitCountText.getText());
169             }
170             catch (NumberFormatException JavaDoc e) {
171                 JDIDebugUIPlugin.log(new Status(IStatus.ERROR, JDIDebugUIPlugin.getUniqueIdentifier(), IStatus.ERROR, MessageFormat.format("JavaBreakpointPage allowed input of invalid string for hit count value: {0}.", new String JavaDoc[] { fHitCountText.getText() }), e)); //$NON-NLS-1$
172
}
173         }
174         breakpoint.setHitCount(hitCount);
175     }
176
177     /**
178      * Stores the value of the suspend policy in the breakpoint.
179      * @param breakpoint the breakpoint to update
180      * @throws CoreException if an exception occurs while setting the suspend policy
181      */

182     private void storeSuspendPolicy(IJavaBreakpoint breakpoint) throws CoreException {
183         int suspendPolicy = IJavaBreakpoint.SUSPEND_VM;
184         if(fSuspendPolicy.getSelectionIndex() == 0) {
185             suspendPolicy = IJavaBreakpoint.SUSPEND_THREAD;
186         }
187         breakpoint.setSuspendPolicy(suspendPolicy);
188     }
189
190     /**
191      * Creates the labels and editors displayed for the breakpoint.
192      * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
193      */

194     protected Control createContents(Composite parent) {
195         setTitle(PropertyPageMessages.JavaBreakpointPage_11);
196         noDefaultAndApplyButton();
197         Composite mainComposite = createComposite(parent, 1);
198         createLabels(mainComposite);
199         try {
200             createEnabledButton(mainComposite);
201             createHitCountEditor(mainComposite);
202             createTypeSpecificEditors(mainComposite);
203             createSuspendPolicyEditor(mainComposite); // Suspend policy is considered uncommon. Add it last.
204
}
205         catch (CoreException e) {JDIDebugUIPlugin.log(e);}
206         setValid(true);
207         // if this breakpoint is being created, change the shell title to indicate 'creation'
208
try {
209             if (getBreakpoint().getMarker().getAttribute(ATTR_DELETE_ON_CANCEL) != null) {
210                 getShell().addShellListener(new ShellListener() {
211                     public void shellActivated(ShellEvent e) {
212                         Shell shell = (Shell)e.getSource();
213                         shell.setText(MessageFormat.format(PropertyPageMessages.JavaBreakpointPage_10, new String JavaDoc[]{getName(getBreakpoint())}));
214                         shell.removeShellListener(this);
215                     }
216                     public void shellClosed(ShellEvent e) {
217                     }
218                     public void shellDeactivated(ShellEvent e) {
219                     }
220                     public void shellDeiconified(ShellEvent e) {
221                     }
222                     public void shellIconified(ShellEvent e) {
223                     }
224                 });
225             }
226         } catch (CoreException e) {
227         }
228         return mainComposite;
229     }
230     
231     /**
232      * Returns the name of the given element.
233      *
234      * @param element the element
235      * @return the name of the element
236      */

237     private String JavaDoc getName(IAdaptable element) {
238         IWorkbenchAdapter adapter = (IWorkbenchAdapter) element.getAdapter(IWorkbenchAdapter.class);
239         if (adapter != null) {
240             return adapter.getLabel(element);
241         }
242         return EMPTY_STRING;
243     }
244     
245     /**
246      * Creates the labels displayed for the breakpoint.
247      * @param parent
248      */

249     protected void createLabels(Composite parent) {
250         Composite labelComposite = createComposite(parent, 2);
251         try {
252             String JavaDoc typeName = ((IJavaBreakpoint) getElement()).getTypeName();
253             if (typeName != null) {
254                 createLabel(labelComposite, PropertyPageMessages.JavaBreakpointPage_3);
255                 Text text = SWTFactory.createText(labelComposite, SWT.READ_ONLY, 1, typeName);
256                 text.setBackground(parent.getBackground());
257             }
258             createTypeSpecificLabels(labelComposite);
259         } catch (CoreException ce) {
260             JDIDebugUIPlugin.log(ce);
261         }
262     }
263
264     /**
265      * Creates the editor for configuring the suspend policy (suspend
266      * VM or suspend thread) of the breakpoint.
267      * @param parent the composite in which the suspend policy
268      * editor will be created.
269      */

270     private void createSuspendPolicyEditor(Composite parent) throws CoreException {
271         Composite comp = createComposite(parent, 2);
272         createLabel(comp, PropertyPageMessages.JavaBreakpointPage_6);
273         boolean suspendThread= getBreakpoint().getSuspendPolicy() == IJavaBreakpoint.SUSPEND_THREAD;
274         fSuspendPolicy = new Combo(comp, SWT.BORDER | SWT.READ_ONLY);
275         fSuspendPolicy.add(PropertyPageMessages.JavaBreakpointPage_7);
276         fSuspendPolicy.add(PropertyPageMessages.JavaBreakpointPage_8);
277         fSuspendPolicy.select(1);
278         if(suspendThread) {
279             fSuspendPolicy.select(0);
280         }
281     }
282
283     /**
284      * @param parent the composite in which the hit count editor
285      * will be created
286      */

287     private void createHitCountEditor(Composite parent) throws CoreException {
288         Composite hitCountComposite = createComposite(parent, 2);
289         fHitCountButton= createCheckButton(hitCountComposite, PropertyPageMessages.JavaBreakpointPage_4);
290         fHitCountButton.addSelectionListener(new SelectionAdapter() {
291             public void widgetSelected(SelectionEvent event) {
292                 fHitCountText.setEnabled(fHitCountButton.getSelection());
293                 hitCountChanged();
294             }
295         });
296         int hitCount = getBreakpoint().getHitCount();
297         String JavaDoc hitCountString= EMPTY_STRING;
298         if (hitCount > 0) {
299             hitCountString = new Integer JavaDoc(hitCount).toString();
300             fHitCountButton.setSelection(true);
301         } else {
302             fHitCountButton.setSelection(false);
303         }
304         fHitCountText= createText(hitCountComposite, hitCountString);
305         if (hitCount <= 0) {
306             fHitCountText.setEnabled(false);
307         }
308         fHitCountText.addModifyListener(new ModifyListener() {
309             public void modifyText(ModifyEvent e) {
310                 hitCountChanged();
311             }
312         });
313     }
314     
315     /**
316      * Validates the current state of the hit count editor.
317      * Hit count value must be a positive integer.
318      */

319     private void hitCountChanged() {
320         if (!fHitCountButton.getSelection()) {
321             removeErrorMessage(fgHitCountErrorMessage);
322             return;
323         }
324         String JavaDoc hitCountText= fHitCountText.getText();
325         int hitCount= -1;
326         try {
327             hitCount = Integer.parseInt(hitCountText);
328         }
329         catch (NumberFormatException JavaDoc e1) {
330             addErrorMessage(fgHitCountErrorMessage);
331             return;
332         }
333         if (hitCount < 1) {
334             addErrorMessage(fgHitCountErrorMessage);
335         } else {
336             removeErrorMessage(fgHitCountErrorMessage);
337         }
338     }
339
340     /**
341      * Creates the button to toggle enablement of the breakpoint
342      * @param parent
343      * @throws CoreException
344      */

345     protected void createEnabledButton(Composite parent) throws CoreException {
346         fEnabledButton = createCheckButton(parent, PropertyPageMessages.JavaBreakpointPage_5);
347         fEnabledButton.setSelection(getBreakpoint().isEnabled());
348     }
349     
350     /**
351      * Returns the breakpoint that this preference page configures
352      * @return the breakpoint this page configures
353      */

354     protected IJavaBreakpoint getBreakpoint() {
355         return (IJavaBreakpoint) getElement();
356     }
357     
358     /**
359      * Allows subclasses to add type specific labels to the common Java
360      * breakpoint page.
361      * @param parent
362      */

363     protected void createTypeSpecificLabels(Composite parent) {}
364     
365     /**
366     * Allows subclasses to add type specific editors to the common Java
367     * breakpoint page.
368     * @param parent
369     */

370    protected void createTypeSpecificEditors(Composite parent) throws CoreException {}
371     
372     /**
373      * Creates a fully configured text editor with the given initial value
374      * @param parent
375      * @param initialValue
376      * @return the configured text editor
377      */

378     protected Text createText(Composite parent, String JavaDoc initialValue) {
379         return SWTFactory.createText(parent, SWT.SINGLE | SWT.BORDER, 1, initialValue);
380     }
381     
382     /**
383      * Creates a fully configured composite with the given number of columns
384      * @param parent
385      * @param numColumns
386      * @return the configured composite
387      */

388     protected Composite createComposite(Composite parent, int numColumns) {
389         return SWTFactory.createComposite(parent, parent.getFont(), numColumns, 1, GridData.FILL_HORIZONTAL, 0, 0);
390     }
391
392     /**
393      * Creates a fully configured check button with the given text.
394      * @param parent the parent composite
395      * @param text the label of the returned check button
396      * @return a fully configured check button
397      */

398     protected Button createCheckButton(Composite parent, String JavaDoc text) {
399         return SWTFactory.createCheckButton(parent, text, null, false, 1);
400     }
401
402     /**
403      * Creates a fully configured label with the given text.
404      * @param parent the parent composite
405      * @param text the test of the returned label
406      * @return a fully configured label
407      */

408     protected Label createLabel(Composite parent, String JavaDoc text) {
409         return SWTFactory.createLabel(parent, text, 1);
410     }
411
412     /**
413      * Creates a fully configured radio button with the given text.
414      * @param parent the parent composite
415      * @param text the label of the returned radio button
416      * @return a fully configured radio button
417      */

418     protected Button createRadioButton(Composite parent, String JavaDoc text) {
419         return SWTFactory.createRadioButton(parent, text, 1);
420     }
421     
422     /**
423      * Check to see if the breakpoint should be deleted.
424      */

425     public boolean performCancel() {
426         try {
427             if (getBreakpoint().getMarker().getAttribute(ATTR_DELETE_ON_CANCEL) != null) {
428                 // if this breakpoint is being created, delete on cancel
429
getBreakpoint().delete();
430             }
431         } catch (CoreException e) {
432             JDIDebugUIPlugin.statusDialog(PropertyPageMessages.JavaBreakpointPage_9, e.getStatus());
433         }
434         return super.performCancel();
435     }
436     
437     /* (non-Javadoc)
438      * @see org.eclipse.jface.preference.PreferencePage#createControl(org.eclipse.swt.widgets.Composite)
439      */

440     public void createControl(Composite parent) {
441         super.createControl(parent);
442         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaDebugHelpContextIds.JAVA_BREAKPOINT_PROPERTY_PAGE);
443     }
444 }
445
Popular Tags