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.coordinator;
25
26import com.sun.hotspot.igv.coordinator.actions.CloneGraphAction;
27import com.sun.hotspot.igv.coordinator.actions.DiffGraphAction;
28import com.sun.hotspot.igv.coordinator.actions.DiffGraphCookie;
29import com.sun.hotspot.igv.coordinator.actions.GraphCloneCookie;
30import com.sun.hotspot.igv.coordinator.actions.GraphOpenCookie;
31import com.sun.hotspot.igv.coordinator.actions.GraphRemoveCookie;
32import com.sun.hotspot.igv.data.InputGraph;
33import com.sun.hotspot.igv.data.Properties;
34import com.sun.hotspot.igv.data.services.GraphViewer;
35import com.sun.hotspot.igv.util.PropertiesSheet;
36import java.awt.Image;
37import javax.swing.Action;
38import org.openide.actions.OpenAction;
39import org.openide.nodes.AbstractNode;
40import org.openide.nodes.Children;
41import org.openide.nodes.NodeAdapter;
42import org.openide.nodes.NodeEvent;
43import org.openide.nodes.NodeMemberEvent;
44import org.openide.nodes.Sheet;
45import org.openide.util.ImageUtilities;
46import org.openide.util.Lookup;
47import org.openide.util.lookup.AbstractLookup;
48import org.openide.util.lookup.InstanceContent;
49
50/**
51 *
52 * @author Thomas Wuerthinger
53 */
54public class GraphNode extends AbstractNode {
55    private InputGraph graph;
56
57    /** Creates a new instance of GraphNode */
58    public GraphNode(InputGraph graph) {
59        this(graph, new InstanceContent());
60    }
61
62    private GraphNode(InputGraph graph, InstanceContent content) {
63        super(Children.LEAF, new AbstractLookup(content));
64        this.graph = graph;
65        this.setDisplayName(graph.getName());
66        content.add(graph);
67
68        final GraphViewer viewer = Lookup.getDefault().lookup(GraphViewer.class);
69
70        if (viewer != null) {
71            // Action for opening the graph
72            content.add(new GraphOpenCookie(viewer, graph));
73        }
74
75        // Action for removing a graph
76        content.add(new GraphRemoveCookie(graph));
77
78        // Action for diffing to the current graph
79        content.add(new DiffGraphCookie(graph));
80
81        // Action for cloning to the current graph
82        content.add(new GraphCloneCookie(viewer, graph));
83
84        this.addNodeListener(new NodeAdapter() {
85            @Override
86            public void childrenRemoved(NodeMemberEvent ev) {
87                GraphNode.this.graph = null;
88            }
89        });
90    }
91
92    @Override
93    protected Sheet createSheet() {
94        Sheet s = super.createSheet();
95        Properties p = new Properties();
96        p.add(graph.getProperties());
97        p.setProperty("nodeCount", Integer.toString(graph.getNodes().size()));
98        p.setProperty("edgeCount", Integer.toString(graph.getEdges().size()));
99        PropertiesSheet.initializeSheet(p, s);
100        return s;
101    }
102
103    @Override
104    public Image getIcon(int i) {
105        return ImageUtilities.loadImage("com/sun/hotspot/igv/coordinator/images/graph.png");
106    }
107
108    @Override
109    public Image getOpenedIcon(int i) {
110        return getIcon(i);
111    }
112
113    @Override
114    public Action[] getActions(boolean b) {
115        return new Action[]{(Action) DiffGraphAction.findObject(DiffGraphAction.class, true), (Action) CloneGraphAction.findObject(CloneGraphAction.class, true), (Action) OpenAction.findObject(OpenAction.class, true)};
116    }
117
118    @Override
119    public Action getPreferredAction() {
120        return (Action) OpenAction.findObject(OpenAction.class, true);
121    }
122
123    @Override
124    public boolean equals(Object obj) {
125        if (this == obj) {
126            return true;
127        }
128        if (obj instanceof GraphNode) {
129            return (graph == ((GraphNode) obj).graph);
130        }
131        return false;
132    }
133
134    @Override
135    public int hashCode() {
136        return graph.hashCode();
137    }
138}
139