jdk/src/jdk.jconsole/share/classes/sun/tools/jconsole/inspector/XArrayDataViewer.java
author weijun
Thu, 11 Dec 2014 15:23:02 +0800
changeset 27957 24b4e6082f19
parent 25859 3317bb8137f4
permissions -rw-r--r--
8055723: Replace concat String to append in StringBuilder parameters (dev) Reviewed-by: redestad, ulfzibis, weijun, prappo, igerasim, alanb Contributed-by: Otavio Santana <otaviojava@java.net>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.tools.jconsole.inspector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.Array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Collection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.JEditorPane;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.JScrollPane;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
class XArrayDataViewer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private XArrayDataViewer() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public static boolean isViewableValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        return Utils.canBeRenderedAsArray(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public static Component loadArray(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        Component comp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        if (isViewableValue(value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            Object[] arr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            } else if (value instanceof Object[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                arr = (Object[]) value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                int length = Array.getLength(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                arr = new Object[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                for (int i = 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                    arr[i] = Array.get(value, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            JEditorPane arrayEditor = new JEditorPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            arrayEditor.setContentType("text/html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            arrayEditor.setEditable(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            Color evenRowColor = arrayEditor.getBackground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            int red = evenRowColor.getRed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            int green = evenRowColor.getGreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            int blue = evenRowColor.getBlue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            String evenRowColorStr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                    "rgb(" + red + "," + green + "," + blue + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            Color oddRowColor = new Color(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    red < 20 ? red + 20 : red - 20,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    green < 20 ? green + 20 : green - 20,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    blue < 20 ? blue + 20 : blue - 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            String oddRowColorStr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    "rgb(" + oddRowColor.getRed() + "," +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                    oddRowColor.getGreen() + "," +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    oddRowColor.getBlue() + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            Color foreground = arrayEditor.getForeground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            String textColor = String.format("%06x",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                                             foreground.getRGB() & 0xFFFFFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            for (int i = 0; i < arr.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            sb.append("</table></body></html>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            arrayEditor.setText(sb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            JScrollPane scrollp = new JScrollPane(arrayEditor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            comp = scrollp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private static String htmlize(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return value.replace("&", "&amp;").replace("<", "&lt;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
}