Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2016 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.ui.views; |
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 javax.swing.AbstractAction; |
57 |
import javax.swing.ActionMap; |
58 |
import javax.swing.BorderFactory; |
59 |
import javax.swing.BoxLayout; |
60 |
import javax.swing.Icon; |
61 |
import javax.swing.InputMap; |
62 |
import javax.swing.JButton; |
63 |
import javax.swing.JComponent; |
64 |
import static javax.swing.JComponent.WHEN_FOCUSED; |
65 |
import javax.swing.JPanel; |
66 |
import javax.swing.JTextArea; |
67 |
import javax.swing.KeyStroke; |
68 |
import javax.swing.SwingUtilities; |
69 |
import javax.swing.UIManager; |
70 |
import javax.swing.text.BadLocationException; |
71 |
import javax.swing.text.Document; |
72 |
import javax.swing.text.Keymap; |
73 |
import org.netbeans.spi.debugger.ui.ViewFactory; |
74 |
import org.openide.util.ImageUtilities; |
75 |
|
76 |
public final class ToolTipView extends JComponent implements org.openide.util.HelpCtx.Provider { |
77 |
|
78 |
public static final String TOOLTIP_VIEW_NAME = "ToolTipView"; // NOI18N |
79 |
private static final String TOOLTIP_HELP_ID = "NetbeansDebuggerToolTipNode";// NOI18N |
80 |
|
81 |
private static volatile String expression; |
82 |
private static volatile Object variable; |
83 |
|
84 |
private transient JComponent contentComponent; |
85 |
|
86 |
private ToolTipView(String expression, Object v, String icon) { |
87 |
ToolTipView.expression = expression; |
88 |
variable = v; |
89 |
JComponent c = ViewFactory.getDefault().createViewComponent( |
90 |
icon, |
91 |
ToolTipView.TOOLTIP_VIEW_NAME, |
92 |
TOOLTIP_HELP_ID, |
93 |
null); |
94 |
setLayout (new BorderLayout ()); |
95 |
add (c, BorderLayout.CENTER); //NOI18N |
96 |
} |
97 |
|
98 |
public static String getExpression() { |
99 |
return expression; |
100 |
} |
101 |
|
102 |
public static Object getVariable() { |
103 |
return variable; |
104 |
} |
105 |
|
106 |
@Override |
107 |
public void removeNotify() { |
108 |
super.removeNotify(); |
109 |
variable = null; |
110 |
} |
111 |
|
112 |
// <RAVE> |
113 |
// Implement getHelpCtx() with the correct help ID |
114 |
@Override |
115 |
public org.openide.util.HelpCtx getHelpCtx() { |
116 |
return new org.openide.util.HelpCtx(TOOLTIP_HELP_ID); |
117 |
} |
118 |
// </RAVE> |
119 |
|
120 |
@Override |
121 |
public boolean requestFocusInWindow () { |
122 |
super.requestFocusInWindow (); |
123 |
if (contentComponent == null) { |
124 |
return false; |
125 |
} |
126 |
return contentComponent.requestFocusInWindow (); |
127 |
} |
128 |
|
129 |
/** Creates the view. */ |
130 |
public static synchronized ToolTipView createToolTipView(String expression, Object variable) { |
131 |
return new ToolTipView( |
132 |
expression, |
133 |
variable, |
134 |
"org/netbeans/modules/debugger/resources/localsView/local_variable_16.png" |
135 |
); |
136 |
} |
137 |
|
138 |
public static class ExpandableTooltip extends JPanel { |
139 |
|
140 |
private static final String UI_PREFIX = "ToolTip"; // NOI18N |
141 |
|
142 |
private JButton expButton; |
143 |
private JButton pinButton; |
144 |
private JComponent textToolTip; |
145 |
private boolean widthCheck = true; |
146 |
private boolean sizeSet = false; |
147 |
|
148 |
public ExpandableTooltip(String toolTipText, boolean expandable, boolean pinnable) { |
149 |
Font font = UIManager.getFont(UI_PREFIX + ".font"); // NOI18N |
150 |
Color backColor = UIManager.getColor(UI_PREFIX + ".background"); // NOI18N |
151 |
Color foreColor = UIManager.getColor(UI_PREFIX + ".foreground"); // NOI18N |
152 |
|
153 |
if (backColor != null) { |
154 |
setBackground(backColor); |
155 |
} |
156 |
setOpaque(true); |
157 |
setBorder(BorderFactory.createCompoundBorder( |
158 |
BorderFactory.createLineBorder(getForeground()), |
159 |
BorderFactory.createEmptyBorder(0, 3, 0, 3) |
160 |
)); |
161 |
|
162 |
setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); |
163 |
if (pinnable) { |
164 |
pinButton = new JButton(ImageUtilities.loadImageIcon("org/netbeans/editor/resources/pin.png", false)); |
165 |
pinButton.setBorder(new javax.swing.border.EmptyBorder(0, 3, 0, 0)); |
166 |
pinButton.setBorderPainted(false); |
167 |
pinButton.setContentAreaFilled(false); |
168 |
add(pinButton); |
169 |
} |
170 |
if (expandable) { |
171 |
Icon expIcon = UIManager.getIcon ("Tree.collapsedIcon"); // NOI18N |
172 |
expButton = new JButton(expIcon); |
173 |
expButton.setBorder(new javax.swing.border.EmptyBorder(0, 0, 0, 5)); |
174 |
expButton.setBorderPainted(false); |
175 |
expButton.setContentAreaFilled(false); |
176 |
add(expButton); |
177 |
} |
178 |
//JLabel l = new JLabel(toolTipText); |
179 |
// Multi-line tooltip: |
180 |
JTextArea l = createMultiLineToolTip(toolTipText, true); |
181 |
if (font != null) { |
182 |
l.setFont(font); |
183 |
} |
184 |
if (foreColor != null) { |
185 |
l.setForeground(foreColor); |
186 |
} |
187 |
if (backColor != null) { |
188 |
l.setBackground(backColor); |
189 |
} |
190 |
l.setBorder(new javax.swing.border.EmptyBorder(0, 3, 0, 3)); |
191 |
textToolTip = l; |
192 |
add(l); |
193 |
if (expandable || pinnable) { |
194 |
InputMap im = new InputMap(); |
195 |
im.setParent(getInputMap()); |
196 |
setInputMap(WHEN_FOCUSED, im); |
197 |
ActionMap am = new ActionMap(); |
198 |
am.setParent(getActionMap()); |
199 |
setActionMap(am); |
200 |
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "expand"); // NOI18N |
201 |
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "pin"); // NOI18N |
202 |
if (expandable) { |
203 |
am.put("expand", new AbstractAction() { // NOI18N |
204 |
@Override |
205 |
public void actionPerformed(ActionEvent e) { |
206 |
expButton.doClick(); |
207 |
} |
208 |
}); |
209 |
} |
210 |
if (pinnable) { |
211 |
am.put("pin", new AbstractAction() { // NOI18N |
212 |
@Override |
213 |
public void actionPerformed(ActionEvent e) { |
214 |
SwingUtilities.invokeLater(() -> pinButton.doClick()); |
215 |
} |
216 |
}); |
217 |
} |
218 |
} |
219 |
} |
220 |
|
221 |
public void addExpansionListener(ActionListener treeExpansionListener) { |
222 |
expButton.addActionListener(treeExpansionListener); |
223 |
} |
224 |
|
225 |
public void addPinListener(ActionListener treeExpansionListener) { |
226 |
pinButton.addActionListener(treeExpansionListener); |
227 |
} |
228 |
|
229 |
public void setWidthCheck(boolean widthCheck) { |
230 |
this.widthCheck = widthCheck; |
231 |
} |
232 |
|
233 |
@Override |
234 |
public Dimension getPreferredSize() { |
235 |
if (!sizeSet) { |
236 |
// Be big enough initially. |
237 |
return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE); |
238 |
} |
239 |
Dimension preferredSize = super.getPreferredSize(); |
240 |
// Let the width be as long as it can be |
241 |
return new Dimension(Integer.MAX_VALUE, preferredSize.height); |
242 |
} |
243 |
|
244 |
@Override |
245 |
public void setSize(int width, int height) { |
246 |
Dimension prefSize = getPreferredSize(); |
247 |
Dimension button1Size = (expButton != null) ? expButton.getPreferredSize() : new Dimension(0, 0); |
248 |
Dimension button2Size = pinButton.getPreferredSize(); |
249 |
if (widthCheck) { |
250 |
Insets insets = getInsets(); |
251 |
int textWidth = width - insets.left - button1Size.width - button2Size.width - insets.right; |
252 |
height = Math.max(Math.max(height, button1Size.height), button2Size.height); |
253 |
textToolTip.setSize(textWidth, height); |
254 |
Dimension textPreferredSize = textToolTip.getPreferredSize(); |
255 |
super.setSize( |
256 |
insets.left + button1Size.width + button2Size.width + textPreferredSize.width + insets.right, |
257 |
insets.top + Math.max(Math.max(button1Size.height, textPreferredSize.height), button2Size.height) + insets.bottom); |
258 |
} else { |
259 |
if (height >= prefSize.height) { // enough height |
260 |
height = prefSize.height; |
261 |
} |
262 |
super.setSize(width, height); |
263 |
} |
264 |
sizeSet = true; |
265 |
} |
266 |
|
267 |
private static JTextArea createMultiLineToolTip(String toolTipText, boolean wrapLines) { |
268 |
JTextArea ta = new TextToolTip(wrapLines); |
269 |
ta.setText(toolTipText); |
270 |
return ta; |
271 |
} |
272 |
|
273 |
private static class TextToolTip extends JTextArea { |
274 |
|
275 |
private static final String ELIPSIS = "..."; //NOI18N |
276 |
|
277 |
private final boolean wrapLines; |
278 |
|
279 |
public TextToolTip(boolean wrapLines) { |
280 |
this.wrapLines = wrapLines; |
281 |
setLineWrap(false); // It's necessary to have a big width of preferred size first. |
282 |
} |
283 |
|
284 |
public @Override void setSize(int width, int height) { |
285 |
Dimension prefSize = getPreferredSize(); |
286 |
if (width >= prefSize.width) { |
287 |
width = prefSize.width; |
288 |
} else { // smaller available width |
289 |
// Set line wrapping and do super.setSize() to determine |
290 |
// the real height (it will change due to line wrapping) |
291 |
if (wrapLines) { |
292 |
setLineWrap(true); |
293 |
setWrapStyleWord(true); |
294 |
} |
295 |
|
296 |
super.setSize(width, Integer.MAX_VALUE); // the height is unimportant |
297 |
prefSize = getPreferredSize(); // re-read new pref width |
298 |
} |
299 |
if (height >= prefSize.height) { // enough height |
300 |
height = prefSize.height; |
301 |
} else { // smaller available height |
302 |
// Check how much can be displayed - cannot rely on line count |
303 |
// because line wrapping may display single physical line |
304 |
// into several visual lines |
305 |
// Before using viewToModel() a setSize() must be called |
306 |
// because otherwise the viewToModel() would return -1. |
307 |
super.setSize(width, Integer.MAX_VALUE); |
308 |
int offset = viewToModel(new Point(0, height)); |
309 |
Document doc = getDocument(); |
310 |
try { |
311 |
if (offset > ELIPSIS.length()) { |
312 |
offset -= ELIPSIS.length(); |
313 |
doc.remove(offset, doc.getLength() - offset); |
314 |
doc.insertString(offset, ELIPSIS, null); |
315 |
} |
316 |
} catch (BadLocationException ble) { |
317 |
// "..." will likely not be displayed but otherwise should be ok |
318 |
} |
319 |
// Recalculate the prefSize as it may be smaller |
320 |
// than the present preferred height |
321 |
height = Math.min(height, getPreferredSize().height); |
322 |
} |
323 |
super.setSize(width, height); |
324 |
} |
325 |
|
326 |
@Override |
327 |
public void setKeymap(Keymap map) { |
328 |
//#181722: keymaps are shared among components with the same UI |
329 |
//a default action will be set to the Keymap of this component below, |
330 |
//so it is necessary to use a Keymap that is not shared with other JTextAreas |
331 |
super.setKeymap(addKeymap(null, map)); |
332 |
} |
333 |
} |
334 |
} |
335 |
|
336 |
} |