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.InputNode;
29import com.sun.hotspot.igv.data.Properties;
30import com.sun.hotspot.igv.data.Properties.StringPropertyMatcher;
31import java.awt.Image;
32import java.util.LinkedHashSet;
33import java.util.List;
34import java.util.Set;
35import javax.swing.Action;
36import org.openide.nodes.AbstractNode;
37import org.openide.nodes.Children;
38import org.openide.nodes.Node;
39import org.openide.util.ImageUtilities;
40
41/**
42 *
43 * @author Thomas Wuerthinger
44 */
45public class BytecodeNode extends AbstractNode {
46
47    private Set<InputNode> nodes;
48
49    public BytecodeNode(InputBytecode bytecode, InputGraph graph, String bciValue) {
50
51        super(Children.LEAF);
52        String displayName = bytecode.getBci() + " " + bytecode.getName() + " " + bytecode.getOperands();
53
54        bciValue = bytecode.getBci() + " " + bciValue;
55        bciValue = bciValue.trim();
56
57        Properties.PropertySelector<InputNode> selector = new Properties.PropertySelector<>(graph.getNodes());
58        StringPropertyMatcher matcher = new StringPropertyMatcher("bci", bciValue);
59        List<InputNode> nodeList = selector.selectMultiple(matcher);
60        if (nodeList.size() > 0) {
61            nodes = new LinkedHashSet<>();
62            for (InputNode n : nodeList) {
63                nodes.add(n);
64            }
65            displayName += " (" + nodes.size() + " nodes)";
66        }
67
68        if (bytecode.getComment() != null) {
69            displayName += " // " + bytecode.getComment();
70        }
71
72        this.setDisplayName(displayName);
73    }
74
75    @Override
76    public Image getIcon(int i) {
77        if (nodes != null) {
78            return ImageUtilities.loadImage("com/sun/hotspot/igv/bytecodes/images/link.png");
79        } else {
80            return ImageUtilities.loadImage("com/sun/hotspot/igv/bytecodes/images/bytecode.png");
81        }
82    }
83
84    @Override
85    public Image getOpenedIcon(int i) {
86        return getIcon(i);
87    }
88
89    @Override
90    public Action[] getActions(boolean b) {
91        return new Action[]{(Action) SelectBytecodesAction.findObject(SelectBytecodesAction.class, true)};
92    }
93
94    @Override
95    public Action getPreferredAction() {
96        return (Action) SelectBytecodesAction.findObject(SelectBytecodesAction.class, true);
97    }
98
99    @Override
100    @SuppressWarnings("unchecked")
101    public <T extends Node.Cookie> T getCookie(Class<T> aClass) {
102        if (aClass == SelectBytecodesCookie.class && nodes != null) {
103            return (T) (new SelectBytecodesCookie(nodes));
104        }
105        return super.getCookie(aClass);
106    }
107}
108