BlockWidget.java revision 1472:c18cbe5936b8
1220295Sadrian/* 2220295Sadrian * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved. 3220295Sadrian * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4220295Sadrian * 5220295Sadrian * This code is free software; you can redistribute it and/or modify it 6220295Sadrian * under the terms of the GNU General Public License version 2 only, as 7220295Sadrian * published by the Free Software Foundation. 8220295Sadrian * 9220295Sadrian * This code is distributed in the hope that it will be useful, but WITHOUT 10220295Sadrian * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11220295Sadrian * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12220295Sadrian * version 2 for more details (a copy is included in the LICENSE file that 13220295Sadrian * accompanied this code). 14220295Sadrian * 15220295Sadrian * You should have received a copy of the GNU General Public License version 16220295Sadrian * 2 along with this work; if not, write to the Free Software Foundation, 17220295Sadrian * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18220295Sadrian * 19220295Sadrian * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20220295Sadrian * or visit www.oracle.com if you need additional information or have any 21220295Sadrian * questions. 22220295Sadrian * 23220295Sadrian */ 24220295Sadrianpackage com.sun.hotspot.igv.controlflow; 25220295Sadrian 26220295Sadrianimport com.sun.hotspot.igv.data.InputBlock; 27220295Sadrianimport com.sun.hotspot.igv.layout.Cluster; 28220295Sadrianimport com.sun.hotspot.igv.layout.Port; 29220295Sadrianimport com.sun.hotspot.igv.layout.Vertex; 30220295Sadrianimport java.awt.Color; 31220295Sadrianimport java.awt.Dimension; 32220295Sadrianimport java.awt.Font; 33220295Sadrianimport java.awt.Point; 34220295Sadrianimport org.netbeans.api.visual.border.BorderFactory; 35227919Sadrianimport org.netbeans.api.visual.model.ObjectState; 36220295Sadrianimport org.netbeans.api.visual.widget.LabelWidget; 37220295Sadrian 38220295Sadrian/** 39220295Sadrian * 40220295Sadrian * @author Thomas Wuerthinger 41220295Sadrian */ 42220295Sadrianpublic class BlockWidget extends LabelWidget implements Vertex { 43220295Sadrian 44220295Sadrian public static final Dimension SIZE = new Dimension(20, 20); 45220295Sadrian private InputBlock block; 46220295Sadrian private Port inputSlot; 47220295Sadrian private Port outputSlot; 48220295Sadrian private Cluster cluster; 49220295Sadrian private boolean root; 50220295Sadrian private static final Font font = new Font(Font.SERIF, Font.PLAIN, 12); 51220295Sadrian private static final Font boldFont = font.deriveFont(Font.BOLD); 52220295Sadrian public static final Color NORMAL_FOREGROUND_COLOR = Color.BLACK; 53220295Sadrian public static final Color HOVER_FOREGROUND_COLOR = Color.BLUE; 54 55 /** Creates a new instance of BlockWidget */ 56 public BlockWidget(ControlFlowScene scene, InputBlock block) { 57 super(scene); 58 this.block = block; 59 this.setLabel(block.getName()); 60 this.setForeground(NORMAL_FOREGROUND_COLOR); 61 this.setBorder(BorderFactory.createLineBorder(1, NORMAL_FOREGROUND_COLOR)); 62 this.setMinimumSize(SIZE); 63 this.setMaximumSize(SIZE); 64 65 this.setFont(font); 66 67 final BlockWidget widget = this; 68 inputSlot = new Port() { 69 70 public Point getRelativePosition() { 71 return new Point((int) (SIZE.getWidth() / 2), (int) (SIZE.getHeight() / 2)); 72 } 73 74 public Vertex getVertex() { 75 return widget; 76 } 77 }; 78 79 outputSlot = new Port() { 80 81 public Point getRelativePosition() { 82 return new Point((int) (SIZE.getWidth() / 2), (int) (SIZE.getHeight() / 2)); 83 } 84 85 public Vertex getVertex() { 86 return widget; 87 } 88 }; 89 } 90 91 public Port getInputSlot() { 92 return inputSlot; 93 } 94 95 public Port getOutputSlot() { 96 return outputSlot; 97 } 98 99 public InputBlock getBlock() { 100 return block; 101 } 102 103 public Dimension getSize() { 104 return SIZE; 105 } 106 107 public void setPosition(Point p) { 108 this.setPreferredLocation(p); 109 } 110 111 @Override 112 public String toString() { 113 return block.getName(); 114 } 115 116 public Point getPosition() { 117 return this.getPreferredLocation(); 118 } 119 120 public Cluster getCluster() { 121 return cluster; 122 } 123 124 public boolean isRoot() { 125 return root; 126 } 127 128 public void setCluster(Cluster c) { 129 cluster = c; 130 } 131 132 public void setRoot(boolean b) { 133 root = b; 134 } 135 136 public int compareTo(Vertex o) { 137 return toString().compareTo(o.toString()); 138 } 139 140 @Override 141 protected void notifyStateChanged(ObjectState previousState, ObjectState state) { 142 super.notifyStateChanged(previousState, state); 143 144 if (previousState.isHovered() != state.isHovered()) { 145 if (state.isHovered()) { 146 this.setBorder(BorderFactory.createLineBorder(1, HOVER_FOREGROUND_COLOR)); 147 } else { 148 this.setBorder(BorderFactory.createLineBorder(1, NORMAL_FOREGROUND_COLOR)); 149 } 150 } 151 152 if (previousState.isSelected() != state.isSelected()) { 153 if (state.isSelected()) { 154 this.setFont(boldFont); 155 } else { 156 this.setFont(font); 157 } 158 } 159 } 160} 161