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  */
  24 package com.sun.hotspot.igv.graph;
  25 
  26 import com.sun.hotspot.igv.data.InputNode;
  27 import com.sun.hotspot.igv.data.Properties;
  28 import com.sun.hotspot.igv.data.Source;
  29 import com.sun.hotspot.igv.layout.Port;
  30 import com.sun.hotspot.igv.layout.Vertex;
  31 import java.awt.Color;
  32 import java.awt.Font;
  33 import java.awt.FontMetrics;
  34 import java.awt.Graphics;
  35 import java.awt.image.BufferedImage;
  36 import java.util.ArrayList;
  37 import java.util.Collections;
  38 import java.util.Comparator;
  39 import java.util.List;
  40 
  41 /**
  42  *
  43  * @author Thomas Wuerthinger
  44  */
  45 public abstract class Slot implements Port, Source.Provider, Properties.Provider {
  46 
  47     private int wantedIndex;
  48     private Source source;
  49     protected List<Connection> connections;
  50     private InputNode associatedNode;
  51     private Color color;
  52     private String text;
  53     private String shortName;
  54     private Figure figure;
  55 
  56     protected Slot(Figure figure, int wantedIndex) {
  57         this.figure = figure;
  58         connections = new ArrayList<>(2);
  59         source = new Source();
  60         this.wantedIndex = wantedIndex;
  61         text = "";
  62         shortName = "";
  63         assert figure != null;
  64     }
  65 
  66     @Override
  67     public Properties getProperties() {
  68         Properties p = new Properties();
  69         if (source.getSourceNodes().size() > 0) {
  70             for (InputNode n : source.getSourceNodes()) {
  71                 p.add(n.getProperties());
  72             }
  73         } else {
  74             p.setProperty("name", "Slot");
  75             p.setProperty("figure", figure.getProperties().get("name"));
  76             p.setProperty("connectionCount", Integer.toString(connections.size()));
  77         }
  78         return p;
  79     }
  80     public static final Comparator<Slot> slotIndexComparator = new Comparator<Slot>() {
  81 
  82         @Override
  83         public int compare(Slot o1, Slot o2) {
  84             return o1.wantedIndex - o2.wantedIndex;
  85         }
  86     };
  87     public static final Comparator<Slot> slotFigureComparator = new Comparator<Slot>() {
  88 
  89         @Override
  90         public int compare(Slot o1, Slot o2) {
  91             return o1.figure.getId() - o2.figure.getId();
  92         }
  93     };
  94 
  95     public InputNode getAssociatedNode() {
  96         return associatedNode;
  97     }
  98 
  99     public void setAssociatedNode(InputNode node) {
 100         associatedNode = node;
 101     }
 102 
 103     public int getWidth() {
 104         if (shortName == null || shortName.length() <= 1) {
 105             return Figure.SLOT_WIDTH;
 106         } else {
 107             BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
 108             Graphics g = image.getGraphics();
 109             g.setFont(figure.getDiagram().getSlotFont().deriveFont(Font.BOLD));
 110             FontMetrics metrics = g.getFontMetrics();
 111             return Math.max(Figure.SLOT_WIDTH, metrics.stringWidth(shortName) + 6);
 112         }
 113     }
 114 
 115     public int getWantedIndex() {
 116         return wantedIndex;
 117     }
 118 
 119     @Override
 120     public Source getSource() {
 121         return source;
 122     }
 123 
 124     public String getText() {
 125         return text;
 126     }
 127 
 128     public void setShortName(String s) {
 129         assert s != null;
 130 //        assert s.length() <= 2;
 131         this.shortName = s;
 132 
 133     }
 134 
 135     public String getShortName() {
 136         return shortName;
 137     }
 138 
 139     public String getToolTipText() {
 140         StringBuilder sb = new StringBuilder();
 141         sb.append(text);
 142 
 143         for (InputNode n : getSource().getSourceNodes()) {
 144             sb.append("Node (ID=" + n.getId() + "): " + n.getProperties().get("name"));
 145             sb.append("<br>");
 146         }
 147 
 148         return sb.toString();
 149     }
 150 
 151     public boolean shouldShowName() {
 152         return getShortName() != null && getShortName().length() > 0;
 153     }
 154 
 155     public void setText(String s) {
 156         if (s == null) {
 157             s = "";
 158         }
 159         this.text = s;
 160     }
 161 
 162     public Figure getFigure() {
 163         assert figure != null;
 164         return figure;
 165     }
 166 
 167     public Color getColor() {
 168         return this.color;
 169     }
 170 
 171     public void setColor(Color c) {
 172         color = c;
 173     }
 174 
 175     public List<Connection> getConnections() {
 176         return Collections.unmodifiableList(connections);
 177     }
 178 
 179     public void removeAllConnections() {
 180         List<Connection> connectionsCopy = new ArrayList<>(this.connections);
 181         for (Connection c : connectionsCopy) {
 182             c.remove();
 183         }
 184     }
 185 
 186     @Override
 187     public Vertex getVertex() {
 188         return figure;
 189     }
 190 
 191     public abstract int getPosition();
 192 
 193     public abstract void setPosition(int position);
 194 }
 195