Removed
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* Contributor(s): |
28 |
* |
29 |
* The Original Software is NetBeans. The Initial Developer of the Original |
30 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun |
31 |
* Microsystems, Inc. All Rights Reserved. |
32 |
* |
33 |
* If you wish your version of this file to be governed by only the CDDL |
34 |
* or only the GPL Version 2, indicate your decision by adding |
35 |
* "[Contributor] elects to include this software in this distribution |
36 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
37 |
* single choice of license, a recipient has the option to distribute |
38 |
* your version of this file under either the CDDL, the GPL Version 2 or |
39 |
* to extend the choice of license to its licensees as provided above. |
40 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
41 |
* Version 2 license, then the option applies only if the new code is |
42 |
* made subject to such option by the copyright holder. |
43 |
*/ |
44 |
|
45 |
package org.netbeans.modules.debugger.jpda.projectsui; |
46 |
|
47 |
import java.awt.BorderLayout; |
48 |
import java.awt.Color; |
49 |
import java.awt.Dimension; |
50 |
import java.awt.Font; |
51 |
import java.awt.Insets; |
52 |
import java.awt.Point; |
53 |
import java.awt.event.ActionEvent; |
54 |
import java.awt.event.ActionListener; |
55 |
import java.awt.event.KeyEvent; |
56 |
import java.beans.PropertyChangeEvent; |
57 |
import java.beans.PropertyChangeListener; |
58 |
import javax.swing.AbstractAction; |
59 |
import javax.swing.ActionMap; |
60 |
import javax.swing.BorderFactory; |
61 |
import javax.swing.BoxLayout; |
62 |
import javax.swing.Icon; |
63 |
import javax.swing.InputMap; |
64 |
import javax.swing.JButton; |
65 |
import javax.swing.JComponent; |
66 |
import javax.swing.JPanel; |
67 |
import javax.swing.JTextArea; |
68 |
import javax.swing.KeyStroke; |
69 |
import javax.swing.SwingUtilities; |
70 |
import javax.swing.UIManager; |
71 |
import javax.swing.text.BadLocationException; |
72 |
import javax.swing.text.Document; |
73 |
import javax.swing.text.Keymap; |
74 |
import org.netbeans.api.debugger.jpda.JPDADebugger; |
75 |
import org.netbeans.api.debugger.jpda.ObjectVariable; |
76 |
import org.netbeans.editor.ext.ToolTipSupport; |
77 |
import org.netbeans.spi.debugger.ui.ViewFactory; |
78 |
import org.openide.util.ImageUtilities; |
79 |
|
80 |
|
81 |
// <RAVE> |
82 |
// Implement HelpCtx.Provider interface to provide help ids for help system |
83 |
// public class CallStackView extends TopComponent { |
84 |
// ==== |
85 |
public class ToolTipView extends JComponent implements org.openide.util.HelpCtx.Provider { |
86 |
// </RAVE> |
87 |
|
88 |
public static final String TOOLTIP_VIEW_NAME = "ToolTipView"; |
89 |
|
90 |
private static volatile String expression; |
91 |
private static volatile ObjectVariable variable; |
92 |
|
93 |
private transient JComponent contentComponent; |
94 |
private transient JPDADebugger debugger; |
95 |
private transient ToolTipSupport toolTipSupport; |
96 |
private transient DebuggerStateChangeListener debuggerStateChangeListener; |
97 |
private String name; // Store just the name persistently, we'll create the component from that |
98 |
|
99 |
private ToolTipView(JPDADebugger debugger, String expression, ObjectVariable v, String icon) { |
100 |
this.debugger = debugger; |
101 |
ToolTipView.expression = expression; |
102 |
variable = v; |
103 |
this.name = TOOLTIP_VIEW_NAME; |
104 |
JComponent c = ViewFactory.getDefault().createViewComponent( |
105 |
icon, |
106 |
ToolTipView.TOOLTIP_VIEW_NAME, |
107 |
"NetbeansDebuggerJPDAToolTipNode", |
108 |
null); |
109 |
setLayout (new BorderLayout ()); |
110 |
add (c, BorderLayout.CENTER); //NOI18N |
111 |
debuggerStateChangeListener = new DebuggerStateChangeListener(); |
112 |
debugger.addPropertyChangeListener(JPDADebugger.PROP_STATE, debuggerStateChangeListener); |
113 |
} |
114 |
|
115 |
static String getExpression() { |
116 |
return expression; |
117 |
} |
118 |
|
119 |
static ObjectVariable getVariable() { |
120 |
return variable; |
121 |
} |
122 |
|
123 |
void setToolTipSupport(ToolTipSupport toolTipSupport) { |
124 |
this.toolTipSupport = toolTipSupport; |
125 |
} |
126 |
|
127 |
private void closeToolTip() { |
128 |
toolTipSupport.setToolTipVisible(false); |
129 |
} |
130 |
|
131 |
//protected void componentHidden () { |
132 |
@Override |
133 |
public void removeNotify() { |
134 |
super.removeNotify();//componentHidden (); |
135 |
variable = null; |
136 |
debugger.removePropertyChangeListener(JPDADebugger.PROP_STATE, debuggerStateChangeListener); |
137 |
} |
138 |
|
139 |
// <RAVE> |
140 |
// Implement getHelpCtx() with the correct help ID |
141 |
@Override |
142 |
public org.openide.util.HelpCtx getHelpCtx() { |
143 |
return new org.openide.util.HelpCtx("NetbeansDebuggerJPDAToolTipNode"); |
144 |
} |
145 |
// </RAVE> |
146 |
|
147 |
@Override |
148 |
public boolean requestFocusInWindow () { |
149 |
super.requestFocusInWindow (); |
150 |
if (contentComponent == null) { |
151 |
return false; |
152 |
} |
153 |
return contentComponent.requestFocusInWindow (); |
154 |
} |
155 |
|
156 |
/* |
157 |
public void requestActive() { |
158 |
super.requestActive(); |
159 |
if (contentComponent != null) { |
160 |
contentComponent.requestFocusInWindow (); |
161 |
} |
162 |
} |
163 |
*/ |
164 |
|
165 |
/* |
166 |
public String getName () { |
167 |
return NbBundle.getMessage (ToolTipView.class, displayNameResource); |
168 |
} |
169 |
|
170 |
public String getToolTipText () { |
171 |
return NbBundle.getMessage (ToolTipView.class, toolTipResource);// NOI18N |
172 |
} |
173 |
*/ |
174 |
|
175 |
private class DebuggerStateChangeListener implements PropertyChangeListener, Runnable { |
176 |
|
177 |
@Override |
178 |
public void propertyChange(PropertyChangeEvent evt) { |
179 |
int state = ((Integer) evt.getNewValue()).intValue(); |
180 |
if (JPDADebugger.STATE_DISCONNECTED == state || |
181 |
JPDADebugger.STATE_RUNNING == state) { |
182 |
SwingUtilities.invokeLater(this); |
183 |
} |
184 |
} |
185 |
|
186 |
@Override |
187 |
public void run() { |
188 |
closeToolTip(); |
189 |
} |
190 |
|
191 |
} |
192 |
|
193 |
|
194 |
|
195 |
/** Creates the view. */ |
196 |
public static synchronized ToolTipView getToolTipView(JPDADebugger debugger, String expression, ObjectVariable v) { |
197 |
return new ToolTipView( |
198 |
debugger, |
199 |
expression, |
200 |
v, |
201 |
"org/netbeans/modules/debugger/resources/localsView/local_variable_16.png" |
202 |
); |
203 |
} |
204 |
|
205 |
|
206 |
static ExpandableTooltip createExpandableTooltip(String toolTipText, boolean expandable) { |
207 |
return new ExpandableTooltip(toolTipText, expandable); |
208 |
} |
209 |
|
210 |
static class ExpandableTooltip extends JPanel { |
211 |
|
212 |
private static final String UI_PREFIX = "ToolTip"; // NOI18N |
213 |
|
214 |
private JButton expButton; |
215 |
private JButton pinButton; |
216 |
private JComponent textToolTip; |
217 |
private boolean widthCheck = true; |
218 |
private boolean sizeSet = false; |
219 |
|
220 |
public ExpandableTooltip(String toolTipText, boolean expandable) { |
221 |
Font font = UIManager.getFont(UI_PREFIX + ".font"); // NOI18N |
222 |
Color backColor = UIManager.getColor(UI_PREFIX + ".background"); // NOI18N |
223 |
Color foreColor = UIManager.getColor(UI_PREFIX + ".foreground"); // NOI18N |
224 |
|
225 |
if (backColor != null) { |
226 |
setBackground(backColor); |
227 |
} |
228 |
setOpaque(true); |
229 |
setBorder(BorderFactory.createCompoundBorder( |
230 |
BorderFactory.createLineBorder(getForeground()), |
231 |
BorderFactory.createEmptyBorder(0, 3, 0, 3) |
232 |
)); |
233 |
|
234 |
setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); |
235 |
pinButton = new JButton(ImageUtilities.loadImageIcon("org/netbeans/editor/resources/pin.png", false)); |
236 |
pinButton.setBorder(new javax.swing.border.EmptyBorder(0, 3, 0, 0)); |
237 |
pinButton.setBorderPainted(false); |
238 |
pinButton.setContentAreaFilled(false); |
239 |
add(pinButton); |
240 |
if (expandable) { |
241 |
Icon expIcon = UIManager.getIcon ("Tree.collapsedIcon"); // NOI18N |
242 |
expButton = new JButton(expIcon); |
243 |
expButton.setBorder(new javax.swing.border.EmptyBorder(0, 0, 0, 5)); |
244 |
expButton.setBorderPainted(false); |
245 |
expButton.setContentAreaFilled(false); |
246 |
add(expButton); |
247 |
} |
248 |
//JLabel l = new JLabel(toolTipText); |
249 |
// Multi-line tooltip: |
250 |
JTextArea l = createMultiLineToolTip(toolTipText, true); |
251 |
if (font != null) { |
252 |
l.setFont(font); |
253 |
} |
254 |
if (foreColor != null) { |
255 |
l.setForeground(foreColor); |
256 |
} |
257 |
if (backColor != null) { |
258 |
l.setBackground(backColor); |
259 |
} |
260 |
l.setBorder(new javax.swing.border.EmptyBorder(0, 3, 0, 3)); |
261 |
textToolTip = l; |
262 |
add(l); |
263 |
InputMap im = new InputMap(); |
264 |
im.setParent(getInputMap()); |
265 |
setInputMap(WHEN_FOCUSED, im); |
266 |
ActionMap am = new ActionMap(); |
267 |
am.setParent(getActionMap()); |
268 |
setActionMap(am); |
269 |
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "expand"); // NOI18N |
270 |
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "pin"); // NOI18N |
271 |
if (expandable) { |
272 |
am.put("expand", new AbstractAction() { // NOI18N |
273 |
@Override |
274 |
public void actionPerformed(ActionEvent e) { |
275 |
expButton.doClick(); |
276 |
} |
277 |
}); |
278 |
} |
279 |
am.put("pin", new AbstractAction() { // NOI18N |
280 |
@Override |
281 |
public void actionPerformed(ActionEvent e) { |
282 |
SwingUtilities.invokeLater(() -> pinButton.doClick()); |
283 |
} |
284 |
}); |
285 |
} |
286 |
|
287 |
void addExpansionListener(ActionListener treeExpansionListener) { |
288 |
expButton.addActionListener(treeExpansionListener); |
289 |
} |
290 |
|
291 |
void addPinListener(ActionListener treeExpansionListener) { |
292 |
pinButton.addActionListener(treeExpansionListener); |
293 |
} |
294 |
|
295 |
void setWidthCheck(boolean widthCheck) { |
296 |
this.widthCheck = widthCheck; |
297 |
} |
298 |
|
299 |
@Override |
300 |
public Dimension getPreferredSize() { |
301 |
if (!sizeSet) { |
302 |
// Be big enough initially. |
303 |
return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE); |
304 |
} |
305 |
Dimension preferredSize = super.getPreferredSize(); |
306 |
// Let the width be as long as it can be |
307 |
return new Dimension(Integer.MAX_VALUE, preferredSize.height); |
308 |
} |
309 |
|
310 |
@Override |
311 |
public void setSize(int width, int height) { |
312 |
Dimension prefSize = getPreferredSize(); |
313 |
Dimension button1Size = (expButton != null) ? expButton.getPreferredSize() : new Dimension(0, 0); |
314 |
Dimension button2Size = pinButton.getPreferredSize(); |
315 |
if (widthCheck) { |
316 |
Insets insets = getInsets(); |
317 |
int textWidth = width - insets.left - button1Size.width - button2Size.width - insets.right; |
318 |
height = Math.max(Math.max(height, button1Size.height), button2Size.height); |
319 |
textToolTip.setSize(textWidth, height); |
320 |
Dimension textPreferredSize = textToolTip.getPreferredSize(); |
321 |
super.setSize( |
322 |
insets.left + button1Size.width + button2Size.width + textPreferredSize.width + insets.right, |
323 |
insets.top + Math.max(Math.max(button1Size.height, textPreferredSize.height), button2Size.height) + insets.bottom); |
324 |
} else { |
325 |
if (height >= prefSize.height) { // enough height |
326 |
height = prefSize.height; |
327 |
} |
328 |
super.setSize(width, height); |
329 |
} |
330 |
sizeSet = true; |
331 |
} |
332 |
|
333 |
private static JTextArea createMultiLineToolTip(String toolTipText, boolean wrapLines) { |
334 |
JTextArea ta = new TextToolTip(wrapLines); |
335 |
ta.setText(toolTipText); |
336 |
return ta; |
337 |
} |
338 |
|
339 |
private static class TextToolTip extends JTextArea { |
340 |
|
341 |
private static final String ELIPSIS = "..."; //NOI18N |
342 |
|
343 |
private final boolean wrapLines; |
344 |
|
345 |
public TextToolTip(boolean wrapLines) { |
346 |
this.wrapLines = wrapLines; |
347 |
setLineWrap(false); // It's necessary to have a big width of preferred size first. |
348 |
} |
349 |
|
350 |
public @Override void setSize(int width, int height) { |
351 |
Dimension prefSize = getPreferredSize(); |
352 |
if (width >= prefSize.width) { |
353 |
width = prefSize.width; |
354 |
} else { // smaller available width |
355 |
// Set line wrapping and do super.setSize() to determine |
356 |
// the real height (it will change due to line wrapping) |
357 |
if (wrapLines) { |
358 |
setLineWrap(true); |
359 |
setWrapStyleWord(true); |
360 |
} |
361 |
|
362 |
super.setSize(width, Integer.MAX_VALUE); // the height is unimportant |
363 |
prefSize = getPreferredSize(); // re-read new pref width |
364 |
} |
365 |
if (height >= prefSize.height) { // enough height |
366 |
height = prefSize.height; |
367 |
} else { // smaller available height |
368 |
// Check how much can be displayed - cannot rely on line count |
369 |
// because line wrapping may display single physical line |
370 |
// into several visual lines |
371 |
// Before using viewToModel() a setSize() must be called |
372 |
// because otherwise the viewToModel() would return -1. |
373 |
super.setSize(width, Integer.MAX_VALUE); |
374 |
int offset = viewToModel(new Point(0, height)); |
375 |
Document doc = getDocument(); |
376 |
try { |
377 |
if (offset > ELIPSIS.length()) { |
378 |
offset -= ELIPSIS.length(); |
379 |
doc.remove(offset, doc.getLength() - offset); |
380 |
doc.insertString(offset, ELIPSIS, null); |
381 |
} |
382 |
} catch (BadLocationException ble) { |
383 |
// "..." will likely not be displayed but otherwise should be ok |
384 |
} |
385 |
// Recalculate the prefSize as it may be smaller |
386 |
// than the present preferred height |
387 |
height = Math.min(height, getPreferredSize().height); |
388 |
} |
389 |
super.setSize(width, height); |
390 |
} |
391 |
|
392 |
@Override |
393 |
public void setKeymap(Keymap map) { |
394 |
//#181722: keymaps are shared among components with the same UI |
395 |
//a default action will be set to the Keymap of this component below, |
396 |
//so it is necessary to use a Keymap that is not shared with other JTextAreas |
397 |
super.setKeymap(addKeymap(null, map)); |
398 |
} |
399 |
} |
400 |
} |
401 |
|
402 |
} |