1/*
2 * Copyright (c) 2008, 2015, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24package com.sun.hotspot.igv.bytecodes;
25
26import com.sun.hotspot.igv.data.InputBytecode;
27import com.sun.hotspot.igv.data.InputGraph;
28import com.sun.hotspot.igv.data.InputMethod;
29import java.awt.Image;
30import org.openide.nodes.AbstractNode;
31import org.openide.nodes.Children;
32import org.openide.nodes.Node;
33import org.openide.util.ImageUtilities;
34
35/**
36 *
37 * @author Thomas Wuerthinger
38 */
39public class MethodNode extends AbstractNode {
40
41    private static class MethodNodeChildren extends Children.Keys<InputBytecode> {
42
43        private InputMethod method;
44        private InputGraph graph;
45        private String bciString;
46
47        public MethodNodeChildren(InputMethod method, InputGraph graph, String bciString) {
48            this.method = method;
49            this.bciString = bciString;
50            this.graph = graph;
51        }
52
53        @Override
54        protected Node[] createNodes(InputBytecode bc) {
55            if (bc.getInlined() == null) {
56                return new Node[]{new BytecodeNode(bc, graph, bciString)};
57            } else {
58                return new Node[]{new BytecodeNode(bc, graph, bciString), new MethodNode(bc.getInlined(), graph, bc.getBci() + " " + bciString)};
59            }
60        }
61
62        @Override
63        public void addNotify() {
64            if (method != null) {
65                setKeys(method.getBytecodes());
66            }
67        }
68
69        public void setMethod(InputMethod method, InputGraph graph) {
70            this.method = method;
71            this.graph = graph;
72            addNotify();
73        }
74    }
75
76    /** Creates a new instance of MethodNode */
77    public MethodNode(InputMethod method, InputGraph graph, String bciString) {
78        super((method != null && method.getBytecodes().size() == 0) ? Children.LEAF : new MethodNodeChildren(method, graph, bciString));
79        if (method != null) {
80            this.setDisplayName(method.getName());
81        }
82    }
83
84    @Override
85    public Image getIcon(int i) {
86        return ImageUtilities.loadImage("com/sun/hotspot/igv/bytecodes/images/method.png");
87    }
88
89    @Override
90    public Image getOpenedIcon(int i) {
91        return getIcon(i);
92    }
93
94    public void update(InputGraph graph, InputMethod method) {
95        ((MethodNodeChildren) this.getChildren()).setMethod(method, graph);
96        if (method != null) {
97            this.setDisplayName(method.getName());
98        }
99
100    }
101}
102