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