KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > actions > BreakpointHitCountAction


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.actions;
12
13
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.jdt.debug.core.IJavaBreakpoint;
18 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
19 import org.eclipse.jface.action.IAction;
20 import org.eclipse.jface.dialogs.IDialogConstants;
21 import org.eclipse.jface.dialogs.IInputValidator;
22 import org.eclipse.jface.dialogs.InputDialog;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.jface.window.Window;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.events.SelectionEvent;
27 import org.eclipse.swt.events.SelectionListener;
28 import org.eclipse.swt.layout.GridData;
29 import org.eclipse.swt.widgets.Button;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Shell;
33
34 public class BreakpointHitCountAction extends ObjectActionDelegate {
35
36     private static final String JavaDoc INITIAL_VALUE= "1"; //$NON-NLS-1$
37

38     /**
39      * A dialog that sets the focus to the text area.
40      */

41     class HitCountDialog extends InputDialog {
42         
43         private boolean fHitCountEnabled;
44         
45         protected HitCountDialog(Shell parentShell,
46                                     String JavaDoc dialogTitle,
47                                     String JavaDoc dialogMessage,
48                                     String JavaDoc initialValue,
49                                     IInputValidator validator) {
50             super(parentShell, dialogTitle, dialogMessage, initialValue, validator);
51         }
52         
53         
54         /**
55          * @see Dialog#createDialogArea(Composite)
56          */

57         protected Control createDialogArea(Composite parent) {
58             Composite area= (Composite)super.createDialogArea(parent);
59             
60             final Button checkbox = new Button(area, SWT.CHECK);
61             GridData data = new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL);
62             data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
63             checkbox.setLayoutData(data);
64             checkbox.setFont(parent.getFont());
65             checkbox.setText(ActionMessages.BreakpointHitCountAction_Enable_Hit_Count_1);
66             checkbox.setSelection(true);
67             fHitCountEnabled = true;
68             checkbox.addSelectionListener(new SelectionListener() {
69                 public void widgetSelected(SelectionEvent e) {
70                     fHitCountEnabled = checkbox.getSelection();
71                     getText().setEnabled(fHitCountEnabled);
72                     if (fHitCountEnabled) {
73                         validateInput();
74                     } else {
75                         setErrorMessage(null);
76                     }
77                 }
78                 
79                 public void widgetDefaultSelected(SelectionEvent e) {
80                 }
81             });
82             
83             return area;
84         }
85
86         protected boolean isHitCountEnabled() {
87             return fHitCountEnabled;
88         }
89     }
90
91
92     /**
93      * @see IActionDelegate#run(IAction)
94      */

95     public void run(IAction action) {
96         IStructuredSelection selection= getCurrentSelection();
97         if (selection == null) {
98             return;
99         }
100         Iterator JavaDoc itr= selection.iterator();
101         if (!itr.hasNext()) {
102             return;
103         }
104
105         while (itr.hasNext()) {
106             IJavaBreakpoint breakpoint= (IJavaBreakpoint)itr.next();
107             try {
108                 int oldHitCount= breakpoint.getHitCount();
109                 int newHitCount= hitCountDialog(breakpoint);
110                 if (newHitCount != -1) {
111                     if (oldHitCount == newHitCount && newHitCount == 0) {
112                         return;
113                     }
114                     breakpoint.setHitCount(newHitCount);
115                 }
116             } catch (CoreException ce) {
117                 JDIDebugUIPlugin.statusDialog(ce.getStatus());
118             }
119         }
120     }
121     
122     protected int hitCountDialog(IJavaBreakpoint breakpoint) {
123         String JavaDoc title= ActionMessages.BreakpointHitCountAction_Set_Breakpoint_Hit_Count_2;
124         String JavaDoc message= ActionMessages.BreakpointHitCountAction__Enter_the_new_hit_count_for_the_breakpoint__3;
125         IInputValidator validator= new IInputValidator() {
126             int hitCount= -1;
127             public String JavaDoc isValid(String JavaDoc value) {
128                 try {
129                     hitCount= Integer.valueOf(value.trim()).intValue();
130                 } catch (NumberFormatException JavaDoc nfe) {
131                     hitCount= -1;
132                 }
133                 if (hitCount < 1) {
134                     return ActionMessages.BreakpointHitCountAction_Value_must_be_positive_integer;
135                 }
136                 //no error
137
return null;
138             }
139         };
140
141         int currentHitCount= 0;
142         try {
143             currentHitCount = breakpoint.getHitCount();
144         } catch (CoreException e) {
145             JDIDebugUIPlugin.log(e);
146         }
147         String JavaDoc initialValue;
148         if (currentHitCount > 0) {
149             initialValue= Integer.toString(currentHitCount);
150         } else {
151             initialValue= INITIAL_VALUE;
152         }
153         Shell activeShell= JDIDebugUIPlugin.getActiveWorkbenchShell();
154         HitCountDialog dialog= new HitCountDialog(activeShell, title, message, initialValue, validator);
155         if (dialog.open() != Window.OK) {
156             return -1;
157         }
158         if (dialog.isHitCountEnabled()) {
159             return Integer.parseInt(dialog.getValue().trim());
160         }
161         return 0;
162     }
163 }
164
Popular Tags