Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2010 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* If you wish your version of this file to be governed by only the CDDL |
25 |
* or only the GPL Version 2, indicate your decision by adding |
26 |
* "[Contributor] elects to include this software in this distribution |
27 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
28 |
* single choice of license, a recipient has the option to distribute |
29 |
* your version of this file under either the CDDL, the GPL Version 2 or |
30 |
* to extend the choice of license to its licensees as provided above. |
31 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
32 |
* Version 2 license, then the option applies only if the new code is |
33 |
* made subject to such option by the copyright holder. |
34 |
* |
35 |
* Contributor(s): |
36 |
* |
37 |
* Portions Copyrighted 2010 Sun Microsystems, Inc. |
38 |
*/ |
39 |
|
40 |
package org.netbeans.spi.debugger.ui; |
41 |
|
42 |
import java.awt.AWTKeyStroke; |
43 |
import java.awt.GridBagConstraints; |
44 |
import java.awt.GridBagLayout; |
45 |
import java.awt.KeyboardFocusManager; |
46 |
import java.io.IOException; |
47 |
import java.util.Set; |
48 |
import javax.swing.JEditorPane; |
49 |
import javax.swing.JPanel; |
50 |
import javax.swing.JScrollPane; |
51 |
import javax.swing.JTextField; |
52 |
import javax.swing.SwingUtilities; |
53 |
import javax.swing.border.CompoundBorder; |
54 |
import javax.swing.border.EmptyBorder; |
55 |
import javax.swing.text.StyledDocument; |
56 |
import org.netbeans.api.editor.DialogBinding; |
57 |
import org.netbeans.editor.EditorUI; |
58 |
import org.netbeans.modules.debugger.ui.FilteredKeymap; |
59 |
import org.openide.ErrorManager; |
60 |
import org.openide.cookies.EditorCookie; |
61 |
import org.openide.filesystems.FileObject; |
62 |
import org.openide.loaders.DataObject; |
63 |
import org.openide.loaders.DataObjectNotFoundException; |
64 |
import org.openide.text.NbDocument; |
65 |
|
66 |
/** |
67 |
* UI Utilities. |
68 |
* |
69 |
* @since 2.23 |
70 |
* @author Martin Entlicher |
71 |
*/ |
72 |
public class Utilities { |
73 |
|
74 |
/** |
75 |
* Create a single-line scroll pane component containing the editor pane, |
76 |
* which is suitable as an entry field for dialogs. |
77 |
* |
78 |
* @param editorPane The editor pane |
79 |
* @return Scroll pane containing single-line editor. |
80 |
*/ |
81 |
public static JScrollPane createScrollableLineEditor(JEditorPane editorPane) { |
82 |
editorPane.setKeymap(new FilteredKeymap(editorPane)); |
83 |
final JScrollPane sp = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_NEVER, |
84 |
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); |
85 |
|
86 |
editorPane.setBorder ( |
87 |
new CompoundBorder (editorPane.getBorder(), |
88 |
new EmptyBorder (0, 0, 0, 0)) |
89 |
); |
90 |
|
91 |
JTextField referenceTextField = new JTextField("M"); |
92 |
JPanel panel = new JPanel(new GridBagLayout()); |
93 |
panel.setBackground(referenceTextField.getBackground()); |
94 |
sp.setBorder(referenceTextField.getBorder()); |
95 |
sp.setBackground(referenceTextField.getBackground()); |
96 |
|
97 |
GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints(); |
98 |
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; |
99 |
gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START; |
100 |
gridBagConstraints.weightx = 1.0; |
101 |
panel.add(editorPane, gridBagConstraints); |
102 |
sp.setViewportView(panel); |
103 |
|
104 |
int preferredHeight = referenceTextField.getPreferredSize().height; |
105 |
if (sp.getPreferredSize().height < preferredHeight) { |
106 |
sp.setPreferredSize(referenceTextField.getPreferredSize()); |
107 |
} |
108 |
sp.setMinimumSize(sp.getPreferredSize()); |
109 |
|
110 |
setupUI(editorPane); |
111 |
|
112 |
Set<AWTKeyStroke> tfkeys = referenceTextField.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS); |
113 |
editorPane.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, tfkeys); |
114 |
tfkeys = referenceTextField.getFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS); |
115 |
editorPane.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, tfkeys); |
116 |
return sp; |
117 |
} |
118 |
|
119 |
/** |
120 |
* Set the context of given file and line to the editor pane. |
121 |
* It adds code-completion corresponding to the file and line context. |
122 |
* |
123 |
* @param editorPane The editor pane |
124 |
* @param file The context file |
125 |
* @param line The context line |
126 |
*/ |
127 |
public static void setContext(JEditorPane editorPane, FileObject file, int line) { |
128 |
if (line < 1) { |
129 |
line = 1; |
130 |
} |
131 |
StyledDocument doc; |
132 |
if (file == null) { |
133 |
return; |
134 |
} |
135 |
try { |
136 |
DataObject dobj = DataObject.find (file); |
137 |
EditorCookie ec = dobj.getCookie(EditorCookie.class); |
138 |
if (ec == null) { |
139 |
return; |
140 |
} |
141 |
try { |
142 |
doc = ec.openDocument(); |
143 |
} catch (IOException ex) { |
144 |
ErrorManager.getDefault().notify(ex); |
145 |
return; |
146 |
} |
147 |
} catch (DataObjectNotFoundException ex) { |
148 |
// null dobj |
149 |
return; |
150 |
} |
151 |
try { |
152 |
int offset = NbDocument.findLineOffset(doc, line - 1); // findLineOffset() expects zero based line numbers |
153 |
//editorPane.getDocument().putProperty(javax.swing.text.Document.StreamDescriptionProperty, dobj); |
154 |
//System.err.println("WatchPanel.setupContext("+file+", "+line+", "+offset+")"); |
155 |
DialogBinding.bindComponentToDocument(doc, offset, 0, editorPane); |
156 |
} catch (IndexOutOfBoundsException ioobex) { |
157 |
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioobex); |
158 |
} |
159 |
setupUI(editorPane); |
160 |
} |
161 |
|
162 |
private static void setupUI(final JEditorPane editorPane) { |
163 |
Runnable runnable = new Runnable() { |
164 |
public void run() { |
165 |
EditorUI eui = org.netbeans.editor.Utilities.getEditorUI(editorPane); |
166 |
if (eui == null) { |
167 |
return; |
168 |
} |
169 |
editorPane.putClientProperty( |
170 |
"HighlightsLayerExcludes", //NOI18N |
171 |
"^org\\.netbeans\\.modules\\.editor\\.lib2\\.highlighting\\.CaretRowHighlighting$" //NOI18N |
172 |
); |
173 |
// Do not draw text limit line |
174 |
try { |
175 |
java.lang.reflect.Field textLimitLineField = EditorUI.class.getDeclaredField("textLimitLineVisible"); // NOI18N |
176 |
textLimitLineField.setAccessible(true); |
177 |
textLimitLineField.set(eui, false); |
178 |
} catch (Exception ex) {} |
179 |
} |
180 |
}; |
181 |
if (SwingUtilities.isEventDispatchThread()) { |
182 |
runnable.run(); |
183 |
} else { |
184 |
SwingUtilities.invokeLater(runnable); |
185 |
} |
186 |
} |
187 |
|
188 |
} |