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.controlflow;
25
26import com.sun.hotspot.igv.data.InputBlock;
27import com.sun.hotspot.igv.layout.Cluster;
28import com.sun.hotspot.igv.layout.Port;
29import com.sun.hotspot.igv.layout.Vertex;
30import java.awt.Color;
31import java.awt.Dimension;
32import java.awt.Font;
33import java.awt.Point;
34import java.awt.Rectangle;
35import org.netbeans.api.visual.border.BorderFactory;
36import org.netbeans.api.visual.model.ObjectState;
37import org.netbeans.api.visual.widget.LabelWidget;
38
39/**
40 *
41 * @author Thomas Wuerthinger
42 */
43public class BlockWidget extends LabelWidget implements Vertex {
44
45    public static final Dimension MIN_SIZE = new Dimension(20, 20);
46    private InputBlock block;
47    private Port inputSlot;
48    private Port outputSlot;
49    private Cluster cluster;
50    private boolean root;
51    private static final Font font = new Font(Font.SANS_SERIF, Font.PLAIN, 12);
52    private static final Font boldFont = font.deriveFont(Font.BOLD);
53    public static final Color NORMAL_FOREGROUND_COLOR = Color.BLACK;
54    public static final Color HOVER_FOREGROUND_COLOR = Color.BLUE;
55
56    /** Creates a new instance of BlockWidget */
57    public BlockWidget(ControlFlowScene scene, InputBlock block) {
58        super(scene);
59        this.block = block;
60        this.setLabel(block.getName());
61        this.setForeground(NORMAL_FOREGROUND_COLOR);
62        this.setBorder(BorderFactory.createLineBorder(1, NORMAL_FOREGROUND_COLOR));
63        this.setMinimumSize(MIN_SIZE);
64
65        this.setFont(font);
66        this.setAlignment(Alignment.CENTER);
67
68        final BlockWidget widget = this;
69        inputSlot = new Port() {
70            public Point getRelativePosition() {
71                return new Point((int) (getSize().getWidth() / 2), (int) (getSize().getHeight() / 2));
72            }
73            public Vertex getVertex() {
74                return widget;
75            }
76        };
77        outputSlot = new Port() {
78            public Point getRelativePosition() {
79                return new Point((int) (getSize().getWidth() / 2), (int) (getSize().getHeight() / 2));
80            }
81            public Vertex getVertex() {
82                return widget;
83            }
84        };
85    }
86
87    public Port getInputSlot() {
88        return inputSlot;
89    }
90
91    public Port getOutputSlot() {
92        return outputSlot;
93    }
94
95    public InputBlock getBlock() {
96        return block;
97    }
98
99    public Dimension getSize() {
100        Rectangle bounds = getBounds();
101        if (bounds != null) {
102            return bounds.getSize();
103        } else {
104            return MIN_SIZE;
105        }
106    }
107
108    public void setPosition(Point p) {
109        this.setPreferredLocation(p);
110    }
111
112    @Override
113    public String toString() {
114        return block.getName();
115    }
116
117    public Point getPosition() {
118        return this.getPreferredLocation();
119    }
120
121    public Cluster getCluster() {
122        return cluster;
123    }
124
125    public boolean isRoot() {
126        return root;
127    }
128
129    public void setCluster(Cluster c) {
130        cluster = c;
131    }
132
133    public void setRoot(boolean b) {
134        root = b;
135    }
136
137    public int compareTo(Vertex o) {
138        return toString().compareTo(o.toString());
139    }
140
141    @Override
142    protected void notifyStateChanged(ObjectState previousState, ObjectState state) {
143        super.notifyStateChanged(previousState, state);
144
145        if (previousState.isHovered() != state.isHovered()) {
146            if (state.isHovered()) {
147                this.setBorder(BorderFactory.createLineBorder(1, HOVER_FOREGROUND_COLOR));
148            } else {
149                this.setBorder(BorderFactory.createLineBorder(1, NORMAL_FOREGROUND_COLOR));
150            }
151        }
152
153        if (previousState.isSelected() != state.isSelected()) {
154            if (state.isSelected()) {
155                this.setFont(boldFont);
156            } else {
157                this.setFont(font);
158            }
159        }
160    }
161}
162