Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/ui/Annotation.java
41161 views
/*1* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324package sun.jvm.hotspot.ui;2526import java.awt.*;27import java.awt.font.*;28import java.awt.geom.*;29import java.util.*;3031import sun.jvm.hotspot.debugger.*;32import sun.jvm.hotspot.utilities.*;3334/** This can be extended, along with AnnotatedMemoryPanel, to be an35arbitrarily complex mechanism, supporting user interaction,36etc. */3738public class Annotation {39private Interval interval;40private java.util.List<String> strings;41private java.util.List<Integer> heights;42private Color baseColor;43private int width;44private int height;45private int x;46private int y;4748/** The Annotation can handle the sense of lowAddress and49highAddress being swapped. */50public Annotation(Address lowAddress,51Address highAddress,52String s) {53strings = new ArrayList<>();54heights = new ArrayList<>();55for (StringTokenizer tok = new StringTokenizer(s, "\n"); tok.hasMoreTokens(); ) {56strings.add(tok.nextToken());57}58if (AddressOps.lessThan(highAddress, lowAddress)) {59Address temp = lowAddress;60lowAddress = highAddress;61highAddress = temp;62}63interval = new Interval(lowAddress, highAddress);64}6566public Interval getInterval() {67return interval;68}6970public Address getLowAddress() {71return (Address) getInterval().getLowEndpoint();72}7374public Address getHighAddress() {75return (Address) getInterval().getHighEndpoint();76}7778/** Draw the annotation at its current (x, y) position with its79current color */80public void draw(Graphics g) {81g.setColor(baseColor);82int tmpY = y;83for (int i = 0; i < strings.size(); i++) {84String s = (String) strings.get(i);85Integer h = (Integer) heights.get(i);86g.drawString(s, x, tmpY);87tmpY += h.intValue();88}89}9091/** Sets the base color of this annotation. The annotation may92render sub-portions in a different color if desired. */93public void setColor(Color c) {94this.baseColor = c;95}9697/** Returns the base color of this annotation. */98public Color getColor() {99return baseColor;100}101102/** Computes width and height for this Annotation. Retrieve the103computed information using getWidth() and getHeight(). Separated104because the width and height only need to be recomputed if the105font changes. */106public void computeWidthAndHeight(Graphics g) {107width = 0;108height = 0;109heights.clear();110for (Iterator iter = strings.iterator(); iter.hasNext(); ) {111String s = (String) iter.next();112Rectangle2D bounds = GraphicsUtilities.getStringBounds(s, g);113width = Math.max(width, (int) bounds.getWidth());114height += (int) bounds.getHeight();115heights.add((int) bounds.getHeight());116}117}118119public int getWidth() {120return width;121}122123public int getHeight() {124return height;125}126127/** Set the x and y position of this annotation */128public void setXAndY(int x, int y) {129this.x = x; this.y = y;130}131132public void setX(int x) {133this.x = x;134}135136public int getX() {137return x;138}139140public void setY(int y) {141this.y = y;142}143144public int getY() {145return y;146}147148public Rectangle getBounds() {149return new Rectangle(x, y, width, height);150}151public String toString() {152String result = "Annotation: lowAddr: " + getLowAddress() + " highAddr: " + getHighAddress() + " strings: " + strings.size();153for (int i = 0; i < strings.size(); i++) {154result += "\n" + (String) strings.get(i);155}156return result;157}158}159160161