KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > ui > breakpoints > LineBreakpointPanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.debugger.jpda.ui.breakpoints;
21
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import javax.swing.JPanel JavaDoc;
25 import javax.swing.JOptionPane JavaDoc;
26 import javax.swing.text.Document JavaDoc;
27 import javax.swing.text.StyledDocument JavaDoc;
28 import org.netbeans.api.debugger.DebuggerManager;
29
30 import org.netbeans.api.debugger.jpda.LineBreakpoint;
31 import org.netbeans.modules.debugger.jpda.ui.EditorContextBridge;
32 import org.netbeans.modules.debugger.jpda.ui.FilteredKeymap;
33 import org.netbeans.spi.debugger.ui.Controller;
34
35 import java.net.URI JavaDoc;
36 import org.openide.DialogDisplayer;
37 import org.openide.NotifyDescriptor;
38 import org.openide.cookies.EditorCookie;
39 import org.openide.filesystems.FileObject;
40 import org.openide.filesystems.URLMapper;
41 import org.openide.loaders.DataObject;
42 import org.openide.loaders.DataObjectNotFoundException;
43 import org.openide.text.NbDocument;
44 import org.openide.util.NbBundle;
45
46 /**
47  * Panel for customizing line breakpoints.
48  *
49  * @author Maros Sandor
50  */

51 // <RAVE>
52
// Implement HelpCtx.Provider interface to provide help ids for help system
53
// public class LineBreakpointPanel extends JPanel implements Controller {
54
//
55
public class LineBreakpointPanel extends JPanel JavaDoc implements Controller, org.openide.util.HelpCtx.Provider {
56 // </RAVE>
57

58     private ActionsPanel actionsPanel;
59     private LineBreakpoint breakpoint;
60     private boolean createBreakpoint = false;
61     
62     
63     private static LineBreakpoint createBreakpoint () {
64         LineBreakpoint mb = LineBreakpoint.create (
65             EditorContextBridge.getCurrentURL (),
66             EditorContextBridge.getCurrentLineNumber ()
67         );
68         mb.setPrintText (
69             NbBundle.getBundle (LineBreakpointPanel.class).getString
70                 ("CTL_Line_Breakpoint_Print_Text")
71         );
72         return mb;
73     }
74     
75     
76     /** Creates new form LineBreakpointPanel */
77     public LineBreakpointPanel () {
78         this (createBreakpoint ());
79         createBreakpoint = true;
80     }
81     
82     /** Creates new form LineBreakpointPanel */
83     public LineBreakpointPanel (LineBreakpoint b) {
84         breakpoint = b;
85         initComponents ();
86
87         String JavaDoc url = b.getURL();
88         try {
89             URI JavaDoc uri = new URI JavaDoc(url);
90             tfFileName.setText(uri.getPath());
91         } catch (Exception JavaDoc e) {
92             tfFileName.setText(url);
93         }
94         tfLineNumber.setText(Integer.toString(b.getLineNumber()));
95         tfCondition.setText (b.getCondition ());
96         setupConditionPane();
97         
98         actionsPanel = new ActionsPanel (b);
99         pActions.add (actionsPanel, "Center");
100     }
101     
102     private static int findNumLines(String JavaDoc url) {
103         FileObject file;
104         try {
105             file = URLMapper.findFileObject (new URL JavaDoc(url));
106         } catch (MalformedURLException JavaDoc e) {
107             return 0;
108         }
109         if (file == null) return 0;
110         DataObject dataObject;
111         try {
112             dataObject = DataObject.find (file);
113         } catch (DataObjectNotFoundException ex) {
114             return 0;
115         }
116         EditorCookie ec = (EditorCookie) dataObject.getCookie(EditorCookie.class);
117         if (ec == null) return 0;
118         ec.prepareDocument().waitFinished();
119         Document JavaDoc d = ec.getDocument();
120         if (!(d instanceof StyledDocument JavaDoc)) return 0;
121         StyledDocument JavaDoc sd = (StyledDocument JavaDoc) d;
122         return NbDocument.findLineNumber(sd, sd.getLength());
123     }
124     
125     private void setupConditionPane() {
126         tfCondition.setKeymap(new FilteredKeymap(tfCondition.getKeymap()));
127         String JavaDoc url = breakpoint.getURL();
128         DataObject dobj = null;
129         FileObject file;
130         try {
131             file = URLMapper.findFileObject (new URL JavaDoc (url));
132             if (file != null) {
133                 try {
134                     dobj = DataObject.find (file);
135                 } catch (DataObjectNotFoundException ex) {
136                     // null dobj
137
}
138             }
139         } catch (MalformedURLException JavaDoc e) {
140             // null dobj
141
}
142         tfCondition.getDocument().putProperty(javax.swing.text.Document.StreamDescriptionProperty, dobj);
143     }
144     
145     // <RAVE>
146
// Implement getHelpCtx() with the correct helpID
147
//
148
public org.openide.util.HelpCtx getHelpCtx() {
149         return new org.openide.util.HelpCtx("NetbeansDebuggerBreakpointLineJPDA"); // NOI18N
150
}
151     // </RAVE>
152

153     /** This method is called from within the constructor to
154      * initialize the form.
155      * WARNING: Do NOT modify this code. The content of this method is
156      * always regenerated by the Form Editor.
157      */

158     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
159
private void initComponents() {
160         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
161
162         pSettings = new javax.swing.JPanel JavaDoc();
163         jLabel3 = new javax.swing.JLabel JavaDoc();
164         jLabel5 = new javax.swing.JLabel JavaDoc();
165         tfFileName = new javax.swing.JTextField JavaDoc();
166         jLabel1 = new javax.swing.JLabel JavaDoc();
167         tfLineNumber = new javax.swing.JTextField JavaDoc();
168         spCondition = new javax.swing.JScrollPane JavaDoc();
169         tfCondition = new javax.swing.JEditorPane JavaDoc();
170         pActions = new javax.swing.JPanel JavaDoc();
171         jPanel1 = new javax.swing.JPanel JavaDoc();
172
173         setLayout(new java.awt.GridBagLayout JavaDoc());
174
175         pSettings.setLayout(new java.awt.GridBagLayout JavaDoc());
176
177         java.util.ResourceBundle JavaDoc bundle = java.util.ResourceBundle.getBundle("org/netbeans/modules/debugger/jpda/ui/breakpoints/Bundle"); // NOI18N
178
pSettings.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("L_Line_Breakpoint_BorderTitle"))); // NOI18N
179
jLabel3.setLabelFor(tfFileName);
180         org.openide.awt.Mnemonics.setLocalizedText(jLabel3, bundle.getString("L_Line_Breakpoint_File_Name")); // NOI18N
181
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
182         gridBagConstraints.gridx = 0;
183         gridBagConstraints.gridy = 1;
184         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
185         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
186         gridBagConstraints.insets = new java.awt.Insets JavaDoc(3, 3, 3, 3);
187         pSettings.add(jLabel3, gridBagConstraints);
188         jLabel3.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_L_Line_Breakpoint_File_Name")); // NOI18N
189

190         jLabel5.setLabelFor(tfCondition);
191         org.openide.awt.Mnemonics.setLocalizedText(jLabel5, bundle.getString("L_Line_Breakpoint_Condition")); // NOI18N
192
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
193         gridBagConstraints.gridx = 0;
194         gridBagConstraints.gridy = 5;
195         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
196         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
197         gridBagConstraints.insets = new java.awt.Insets JavaDoc(3, 3, 3, 3);
198         pSettings.add(jLabel5, gridBagConstraints);
199         jLabel5.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_L_Line_Breakpoint_Condition")); // NOI18N
200

201         tfFileName.setEditable(false);
202         tfFileName.setToolTipText(bundle.getString("TTT_TF_Line_Breakpoint_File_Name")); // NOI18N
203
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
204         gridBagConstraints.gridx = 1;
205         gridBagConstraints.gridy = 1;
206         gridBagConstraints.gridwidth = 2;
207         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
208         gridBagConstraints.weightx = 1.0;
209         gridBagConstraints.insets = new java.awt.Insets JavaDoc(3, 3, 3, 3);
210         pSettings.add(tfFileName, gridBagConstraints);
211         tfFileName.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_TF_Line_Breakpoint_File_Name")); // NOI18N
212

213         jLabel1.setLabelFor(tfLineNumber);
214         org.openide.awt.Mnemonics.setLocalizedText(jLabel1, bundle.getString("L_Line_Breakpoint_Line_Number")); // NOI18N
215
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
216         gridBagConstraints.gridx = 0;
217         gridBagConstraints.gridy = 3;
218         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
219         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
220         gridBagConstraints.insets = new java.awt.Insets JavaDoc(3, 3, 3, 3);
221         pSettings.add(jLabel1, gridBagConstraints);
222         jLabel1.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_L_Line_Breakpoint_Line_Number")); // NOI18N
223

224         tfLineNumber.setToolTipText(bundle.getString("TTT_TF_Line_Breakpoint_Line_Number")); // NOI18N
225
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
226         gridBagConstraints.gridx = 1;
227         gridBagConstraints.gridy = 3;
228         gridBagConstraints.gridwidth = 2;
229         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
230         gridBagConstraints.weightx = 1.0;
231         gridBagConstraints.insets = new java.awt.Insets JavaDoc(3, 3, 3, 3);
232         pSettings.add(tfLineNumber, gridBagConstraints);
233         tfLineNumber.getAccessibleContext().setAccessibleName("Line number");
234         tfLineNumber.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_TF_Line_Breakpoint_Line_Number")); // NOI18N
235

236         spCondition.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
237         spCondition.setToolTipText(org.openide.util.NbBundle.getMessage(LineBreakpointPanel.class, "ACSD_TF_Line_Breakpoint_Condition")); // NOI18N
238
spCondition.setMinimumSize(spCondition.getPreferredSize());
239         tfCondition.setContentType("text/x-java");
240         tfCondition.setToolTipText(org.openide.util.NbBundle.getMessage(LineBreakpointPanel.class, "ACSD_TF_Line_Breakpoint_Condition")); // NOI18N
241
spCondition.setViewportView(tfCondition);
242
243         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
244         gridBagConstraints.gridx = 1;
245         gridBagConstraints.gridy = 5;
246         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
247         gridBagConstraints.weightx = 1.0;
248         gridBagConstraints.insets = new java.awt.Insets JavaDoc(3, 3, 3, 3);
249         pSettings.add(spCondition, gridBagConstraints);
250
251         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
252         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
253         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
254         gridBagConstraints.weightx = 1.0;
255         add(pSettings, gridBagConstraints);
256
257         pActions.setLayout(new java.awt.BorderLayout JavaDoc());
258
259         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
260         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
261         gridBagConstraints.weightx = 1.0;
262         add(pActions, gridBagConstraints);
263
264         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
265         gridBagConstraints.gridx = 0;
266         gridBagConstraints.gridy = 2;
267         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
268         gridBagConstraints.weightx = 1.0;
269         gridBagConstraints.weighty = 1.0;
270         add(jPanel1, gridBagConstraints);
271
272     }// </editor-fold>//GEN-END:initComponents
273

274     
275     // Controller implementation ...............................................
276

277     /**
278      * Called when "Ok" button is pressed.
279      *
280      * @return whether customizer can be closed
281      */

282     public boolean ok () {
283         String JavaDoc msg = valiadateMsg();
284         if (msg != null) {
285             DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(msg));
286             return false;
287         }
288         actionsPanel.ok ();
289         breakpoint.setLineNumber(Integer.parseInt(tfLineNumber.getText().trim()));
290         breakpoint.setCondition (tfCondition.getText ());
291         if (createBreakpoint)
292             DebuggerManager.getDebuggerManager ().addBreakpoint (breakpoint);
293         return true;
294     }
295     
296     /**
297      * Called when "Cancel" button is pressed.
298      *
299      * @return whether customizer can be closed
300      */

301     public boolean cancel () {
302         return true;
303     }
304     
305     /**
306      * Return <code>true</code> whether value of this customizer
307      * is valid (and OK button can be enabled).
308      *
309      * @return <code>true</code> whether value of this customizer
310      * is valid
311      */

312     public boolean isValid () {
313         return true;
314     }
315     
316     private String JavaDoc valiadateMsg () {
317         try {
318             int line = Integer.parseInt(tfLineNumber.getText().trim());
319             if (line <= 0) {
320                 return NbBundle.getMessage(LineBreakpointPanel.class, "MSG_NonPositive_Line_Number_Spec");
321             }
322             int maxLine = findNumLines(breakpoint.getURL());
323             if (maxLine == 0) { // Not found
324
maxLine = Integer.MAX_VALUE; // Not to bother the user when we did not find it
325
}
326             if (line > maxLine) {
327                 return NbBundle.getMessage(LineBreakpointPanel.class, "MSG_TooBig_Line_Number_Spec",
328                         Integer.toString(line), Integer.toString(maxLine));
329             }
330         } catch (NumberFormatException JavaDoc e) {
331             return NbBundle.getMessage(LineBreakpointPanel.class, "MSG_No_Line_Number_Spec");
332         }
333         return null;
334     }
335     
336     
337     // Variables declaration - do not modify//GEN-BEGIN:variables
338
private javax.swing.JLabel JavaDoc jLabel1;
339     private javax.swing.JLabel JavaDoc jLabel3;
340     private javax.swing.JLabel JavaDoc jLabel5;
341     private javax.swing.JPanel JavaDoc jPanel1;
342     private javax.swing.JPanel JavaDoc pActions;
343     private javax.swing.JPanel JavaDoc pSettings;
344     private javax.swing.JScrollPane JavaDoc spCondition;
345     private javax.swing.JEditorPane JavaDoc tfCondition;
346     private javax.swing.JTextField JavaDoc tfFileName;
347     private javax.swing.JTextField JavaDoc tfLineNumber;
348     // End of variables declaration//GEN-END:variables
349

350 }
351
Popular Tags