1/*
2 * Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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.
24 */
25
26package sun.tools.jconsole.inspector;
27
28import java.awt.Color;
29import java.awt.Component;
30import java.lang.reflect.Array;
31import java.util.Collection;
32import java.util.Map;
33import javax.swing.JEditorPane;
34import javax.swing.JScrollPane;
35
36class 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) {
49                arr = ((Collection<?>) value).toArray();
50            } else if (value instanceof Map) {
51                arr = ((Map<?,?>) value).entrySet().toArray();
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();
82            sb.append("<html><body text=#").append(textColor).append("><table width=\"100%\">");
83            for (int i = 0; i < arr.length; i++) {
84                if (i % 2 == 0) {
85                    sb.append("<tr style=\"background-color: ")
86                            .append(evenRowColorStr).append("\"><td><pre>")
87                            .append(arr[i] == null ?
88                                    arr[i] : htmlize(arr[i].toString()))
89                      .append("</pre></td></tr>");
90                } else {
91                    sb.append("<tr style=\"background-color: ")
92                            .append(oddRowColorStr).append("\"><td><pre>")
93                            .append(arr[i] == null ?
94                                    arr[i] : htmlize(arr[i].toString()))
95                            .append("</pre></td></tr>");
96                }
97            }
98            if (arr.length == 0) {
99                sb.append("<tr style=\"background-color: ")
100                        .append(evenRowColorStr).append("\"><td></td></tr>");
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("&", "&amp;").replace("<", "&lt;");
112    }
113}
114