KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > ui > WatchPanel


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 package org.netbeans.modules.debugger.jpda.ui;
20
21 import java.awt.Container JavaDoc;
22 import java.awt.Dialog JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.FontMetrics JavaDoc;
25 import java.awt.event.ComponentEvent JavaDoc;
26 import java.awt.event.ComponentListener JavaDoc;
27 import java.awt.event.KeyEvent JavaDoc;
28 import java.awt.event.KeyListener JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30 import java.net.URL JavaDoc;
31 import javax.swing.text.Document JavaDoc;
32 import javax.swing.text.EditorKit JavaDoc;
33 import javax.swing.text.Keymap JavaDoc;
34 import org.netbeans.api.debugger.DebuggerEngine;
35 import org.netbeans.api.debugger.DebuggerManager;
36 import org.netbeans.api.debugger.jpda.CallStackFrame;
37 import org.netbeans.api.debugger.jpda.JPDADebugger;
38 import org.openide.awt.Mnemonics;
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.util.NbBundle;
44
45 import javax.swing.*;
46 import javax.swing.border.EmptyBorder JavaDoc;
47 import javax.swing.border.CompoundBorder JavaDoc;
48 import java.util.*;
49 import java.awt.BorderLayout JavaDoc;
50
51 /**
52  * A GUI panel for customizing a Watch.
53
54  * @author Maros Sandor
55  */

56 public class WatchPanel {
57
58     private JPanel panel;
59     private JEditorPane editorPane;
60     private String JavaDoc expression;
61
62     public WatchPanel(String JavaDoc expression) {
63         this.expression = expression;
64     }
65     
66     public static void setupContext(JEditorPane editorPane) {
67         DebuggerEngine en = DebuggerManager.getDebuggerManager ().getCurrentEngine();
68         JPDADebugger d = (JPDADebugger) en.lookupFirst(null, JPDADebugger.class);
69         CallStackFrame csf = d.getCurrentCallStackFrame();
70         if (csf != null) {
71             DataObject dobj = null;
72             SourcePath sp = (SourcePath) en.lookupFirst(null, SourcePath.class);
73             String JavaDoc url = sp.getURL(csf, "Java");
74             FileObject file;
75             try {
76                 file = URLMapper.findFileObject (new URL JavaDoc (url));
77                 if (file != null) {
78                     try {
79                         dobj = DataObject.find (file);
80                     } catch (DataObjectNotFoundException ex) {
81                         // null dobj
82
}
83                 }
84             } catch (MalformedURLException JavaDoc e) {
85                 // null dobj
86
}
87             editorPane.getDocument().putProperty(javax.swing.text.Document.StreamDescriptionProperty, dobj);
88         }
89     }
90
91     public JComponent getPanel() {
92         if (panel != null) return panel;
93
94         panel = new JPanel();
95         ResourceBundle bundle = NbBundle.getBundle(WatchPanel.class);
96
97         panel.getAccessibleContext ().setAccessibleDescription (bundle.getString ("ACSD_WatchPanel")); // NOI18N
98
JLabel textLabel = new JLabel();
99         Mnemonics.setLocalizedText(textLabel, bundle.getString ("CTL_Watch_Name")); // NOI18N
100
editorPane = new JEditorPane("text/x-java", expression); // NOI18N
101
editorPane.setKeymap(new FilteredKeymap(editorPane.getKeymap()));
102         
103         setupContext(editorPane);
104         
105         JScrollPane sp = new JScrollPane(editorPane, JScrollPane.VERTICAL_SCROLLBAR_NEVER,
106                                                      JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
107         
108         textLabel.setBorder (new EmptyBorder JavaDoc (0, 0, 5, 0));
109         panel.setLayout (new BorderLayout JavaDoc ());
110         panel.setBorder (new EmptyBorder JavaDoc (11, 12, 1, 11));
111         panel.add (BorderLayout.NORTH, textLabel);
112         panel.add (BorderLayout.CENTER, sp);
113         
114         FontMetrics JavaDoc fm = editorPane.getFontMetrics(editorPane.getFont());
115         int size = 2*fm.getLeading() + fm.getMaxAscent() + fm.getMaxDescent() + 4;
116         
117         editorPane.setPreferredSize(new Dimension JavaDoc(30*size, (int) (1*size)));
118         
119         editorPane.getAccessibleContext ().setAccessibleDescription (bundle.getString ("ACSD_CTL_Watch_Name")); // NOI18N
120
editorPane.setBorder (
121             new CompoundBorder JavaDoc (editorPane.getBorder (),
122             new EmptyBorder JavaDoc (2, 0, 2, 0))
123         );
124         editorPane.setText (expression);
125         editorPane.selectAll ();
126
127         textLabel.setLabelFor (editorPane);
128         editorPane.requestFocus ();
129         
130         return panel;
131     }
132
133     public String JavaDoc getExpression() {
134         return editorPane.getText().trim();
135     }
136 }
137
Popular Tags