author | vlivanov |
Thu, 18 Jan 2018 02:25:18 +0300 | |
changeset 48596 | 860326263d1f |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
12851
diff
changeset
|
2 |
* Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.tools.jconsole.inspector; |
|
27 |
||
28 |
import java.awt.Color; |
|
29 |
import java.awt.Component; |
|
30 |
import java.lang.reflect.Array; |
|
31 |
import java.util.Collection; |
|
32 |
import java.util.Map; |
|
33 |
import javax.swing.JEditorPane; |
|
34 |
import javax.swing.JScrollPane; |
|
35 |
||
36 |
class XArrayDataViewer { |
|
37 |
||
38 |
private XArrayDataViewer() {} |
|
39 |
||
40 |
public static boolean isViewableValue(Object value) { |
|
41 |
return Utils.canBeRenderedAsArray(value); |
|
42 |
} |
|
43 |
||
44 |
public static Component loadArray(Object value) { |
|
45 |
Component comp = null; |
|
46 |
if (isViewableValue(value)) { |
|
47 |
Object[] arr; |
|
48 |
if (value instanceof Collection) { |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
49 |
arr = ((Collection<?>) value).toArray(); |
2 | 50 |
} else if (value instanceof Map) { |
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
51 |
arr = ((Map<?,?>) value).entrySet().toArray(); |
2 | 52 |
} else if (value instanceof Object[]) { |
53 |
arr = (Object[]) value; |
|
54 |
} else { |
|
55 |
int length = Array.getLength(value); |
|
56 |
arr = new Object[length]; |
|
57 |
for (int i = 0; i < length; i++) { |
|
58 |
arr[i] = Array.get(value, i); |
|
59 |
} |
|
60 |
} |
|
61 |
JEditorPane arrayEditor = new JEditorPane(); |
|
62 |
arrayEditor.setContentType("text/html"); |
|
63 |
arrayEditor.setEditable(false); |
|
64 |
Color evenRowColor = arrayEditor.getBackground(); |
|
65 |
int red = evenRowColor.getRed(); |
|
66 |
int green = evenRowColor.getGreen(); |
|
67 |
int blue = evenRowColor.getBlue(); |
|
68 |
String evenRowColorStr = |
|
69 |
"rgb(" + red + "," + green + "," + blue + ")"; |
|
70 |
Color oddRowColor = new Color( |
|
71 |
red < 20 ? red + 20 : red - 20, |
|
72 |
green < 20 ? green + 20 : green - 20, |
|
73 |
blue < 20 ? blue + 20 : blue - 20); |
|
74 |
String oddRowColorStr = |
|
75 |
"rgb(" + oddRowColor.getRed() + "," + |
|
76 |
oddRowColor.getGreen() + "," + |
|
77 |
oddRowColor.getBlue() + ")"; |
|
78 |
Color foreground = arrayEditor.getForeground(); |
|
79 |
String textColor = String.format("%06x", |
|
80 |
foreground.getRGB() & 0xFFFFFF); |
|
81 |
StringBuilder sb = new StringBuilder(); |
|
27957
24b4e6082f19
8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents:
25859
diff
changeset
|
82 |
sb.append("<html><body text=#").append(textColor).append("><table width=\"100%\">"); |
2 | 83 |
for (int i = 0; i < arr.length; i++) { |
84 |
if (i % 2 == 0) { |
|
27957
24b4e6082f19
8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents:
25859
diff
changeset
|
85 |
sb.append("<tr style=\"background-color: ") |
24b4e6082f19
8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents:
25859
diff
changeset
|
86 |
.append(evenRowColorStr).append("\"><td><pre>") |
24b4e6082f19
8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents:
25859
diff
changeset
|
87 |
.append(arr[i] == null ? |
24b4e6082f19
8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents:
25859
diff
changeset
|
88 |
arr[i] : htmlize(arr[i].toString())) |
24b4e6082f19
8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents:
25859
diff
changeset
|
89 |
.append("</pre></td></tr>"); |
2 | 90 |
} else { |
27957
24b4e6082f19
8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents:
25859
diff
changeset
|
91 |
sb.append("<tr style=\"background-color: ") |
24b4e6082f19
8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents:
25859
diff
changeset
|
92 |
.append(oddRowColorStr).append("\"><td><pre>") |
24b4e6082f19
8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents:
25859
diff
changeset
|
93 |
.append(arr[i] == null ? |
24b4e6082f19
8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents:
25859
diff
changeset
|
94 |
arr[i] : htmlize(arr[i].toString())) |
24b4e6082f19
8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents:
25859
diff
changeset
|
95 |
.append("</pre></td></tr>"); |
2 | 96 |
} |
97 |
} |
|
98 |
if (arr.length == 0) { |
|
27957
24b4e6082f19
8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents:
25859
diff
changeset
|
99 |
sb.append("<tr style=\"background-color: ") |
24b4e6082f19
8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents:
25859
diff
changeset
|
100 |
.append(evenRowColorStr).append("\"><td></td></tr>"); |
2 | 101 |
} |
102 |
sb.append("</table></body></html>"); |
|
103 |
arrayEditor.setText(sb.toString()); |
|
104 |
JScrollPane scrollp = new JScrollPane(arrayEditor); |
|
105 |
comp = scrollp; |
|
106 |
} |
|
107 |
return comp; |
|
108 |
} |
|
109 |
||
110 |
private static String htmlize(String value) { |
|
111 |
return value.replace("&", "&").replace("<", "<"); |
|
112 |
} |
|
113 |
} |