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 javax.swing.JTable;
29import javax.swing.JScrollPane;
30import javax.swing.JButton;
31
32import java.awt.event.MouseListener;
33import java.awt.Component;
34import java.awt.Container;
35
36import sun.tools.jconsole.MBeansTab;
37import sun.tools.jconsole.Messages;
38
39public class XDataViewer {
40
41    public static final int OPEN = 1;
42    public static final int ARRAY = 2;
43    public static final int NUMERIC = 3;
44    public static final int NOT_SUPPORTED = 4;
45
46    private MBeansTab tab;
47    public XDataViewer(MBeansTab tab) {
48        this.tab = tab;
49    }
50
51    public static void registerForMouseEvent(Component comp,
52                                             MouseListener mouseListener) {
53        if(comp instanceof JScrollPane) {
54            JScrollPane pane = (JScrollPane) comp;
55            comp = pane.getViewport().getView();
56        }
57        if(comp instanceof Container) {
58            Container container = (Container) comp;
59            Component[] components = container.getComponents();
60            for(int i = 0; i < components.length; i++) {
61                registerForMouseEvent(components[i], mouseListener);
62            }
63        }
64
65        //No registration for XOpenTypedata that are themselves clickable.
66        //No registration for JButton that are themselves clickable.
67        if(comp != null &&
68           (!(comp instanceof XOpenTypeViewer.XOpenTypeData) &&
69            !(comp instanceof JButton)) )
70            comp.addMouseListener(mouseListener);
71    }
72
73    public static void dispose(MBeansTab tab) {
74        XPlottingViewer.dispose(tab);
75    }
76
77    public static boolean isViewableValue(Object value) {
78        boolean ret = false;
79        if((ret = XArrayDataViewer.isViewableValue(value)))
80            return ret;
81        if((ret = XOpenTypeViewer.isViewableValue(value)))
82            return ret;
83        if((ret = XPlottingViewer.isViewableValue(value)))
84            return ret;
85
86        return ret;
87    }
88
89    public static int getViewerType(Object data) {
90        if(XArrayDataViewer.isViewableValue(data))
91            return ARRAY;
92        if(XOpenTypeViewer.isViewableValue(data))
93            return OPEN;
94        if(XPlottingViewer.isViewableValue(data))
95            return NUMERIC;
96
97        return NOT_SUPPORTED;
98    }
99
100    public static String getActionLabel(int type) {
101        if(type == ARRAY ||
102           type == OPEN)
103            return Messages.VISUALIZE;
104        if(type == NUMERIC)
105            return Messages.PLOT;
106        return Messages.EXPAND;
107    }
108
109    public Component createOperationViewer(Object value,
110                                           XMBean mbean) {
111        if(value instanceof Number) return null;
112        if(value instanceof Component) return (Component) value;
113        return createAttributeViewer(value, mbean, null, null);
114    }
115
116    public static Component createNotificationViewer(Object value) {
117        Component comp = null;
118
119        if(value instanceof Number) return null;
120
121        if((comp = XArrayDataViewer.loadArray(value)) != null)
122            return comp;
123
124        if((comp = XOpenTypeViewer.loadOpenType(value)) != null)
125            return comp;
126
127        return comp;
128    }
129
130    public Component createAttributeViewer(Object value,
131                                           XMBean mbean,
132                                           String attributeName,
133                                           JTable table) {
134        Component comp = null;
135        if((comp = XArrayDataViewer.loadArray(value)) != null)
136            return comp;
137        if((comp = XOpenTypeViewer.loadOpenType(value)) != null)
138            return comp;
139        if((comp = XPlottingViewer.loadPlotting(mbean,
140                                                attributeName,
141                                                value,
142                                                table,
143                                                tab)) != null)
144            return comp;
145
146        return comp;
147    }
148}
149