View Javadoc
1   /*
2    * Copyright (c) 2000, 2003, 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  
25  package sun.jvm.hotspot.ui;
26  
27  import java.awt.*;
28  import java.awt.font.*;
29  import java.awt.geom.*;
30  import java.util.*;
31  
32  import sun.jvm.hotspot.debugger.*;
33  import sun.jvm.hotspot.utilities.*;
34  
35  /** This can be extended, along with AnnotatedMemoryPanel, to be an
36      arbitrarily complex mechanism, supporting user interaction,
37      etc. */
38  
39  public class Annotation {
40    private Interval interval;
41    // List<String>
42    private java.util.List strings;
43    // List<Integer>
44    private java.util.List heights;
45    private Color baseColor;
46    private int width;
47    private int height;
48    private int x;
49    private int y;
50  
51    /** The Annotation can handle the sense of lowAddress and
52        highAddress being swapped. */
53    public Annotation(Address lowAddress,
54                      Address highAddress,
55                      String s) {
56      strings = new ArrayList();
57      heights = new ArrayList();
58      for (StringTokenizer tok = new StringTokenizer(s, "\n"); tok.hasMoreTokens(); ) {
59        strings.add(tok.nextToken());
60      }
61      if (AddressOps.lessThan(highAddress, lowAddress)) {
62        Address temp = lowAddress;
63        lowAddress = highAddress;
64        highAddress = temp;
65      }
66      interval = new Interval(lowAddress, highAddress);
67    }
68  
69    public Interval getInterval() {
70      return interval;
71    }
72  
73    public Address getLowAddress() {
74      return (Address) getInterval().getLowEndpoint();
75    }
76  
77    public Address getHighAddress() {
78      return (Address) getInterval().getHighEndpoint();
79    }
80  
81    /** Draw the annotation at its current (x, y) position with its
82        current color */
83    public void draw(Graphics g) {
84      g.setColor(baseColor);
85      int tmpY = y;
86      for (int i = 0; i < strings.size(); i++) {
87        String s = (String) strings.get(i);
88        Integer h = (Integer) heights.get(i);
89        g.drawString(s, x, tmpY);
90        tmpY += h.intValue();
91      }
92    }
93  
94    /** Sets the base color of this annotation. The annotation may
95        render sub-portions in a different color if desired. */
96    public void setColor(Color c) {
97      this.baseColor = c;
98    }
99  
100   /** Returns the base color of this annotation. */
101   public Color getColor() {
102     return baseColor;
103   }
104 
105   /** Computes width and height for this Annotation. Retrieve the
106       computed information using getWidth() and getHeight(). Separated
107       because the width and height only need to be recomputed if the
108       font changes. */
109   public void computeWidthAndHeight(Graphics g) {
110     width = 0;
111     height = 0;
112     heights.clear();
113     for (Iterator iter = strings.iterator(); iter.hasNext(); ) {
114       String s = (String) iter.next();
115       Rectangle2D bounds = GraphicsUtilities.getStringBounds(s, g);
116       width  =  Math.max(width, (int) bounds.getWidth());
117       height += (int) bounds.getHeight();
118       heights.add(new Integer((int) bounds.getHeight()));
119     }
120   }
121 
122   public int getWidth() {
123     return width;
124   }
125 
126   public int getHeight() {
127     return height;
128   }
129 
130   /** Set the x and y position of this annotation */
131   public void setXAndY(int x, int y) {
132     this.x = x; this.y = y;
133   }
134 
135   public void setX(int x) {
136     this.x = x;
137   }
138 
139   public int getX() {
140     return x;
141   }
142 
143   public void setY(int y) {
144     this.y = y;
145   }
146 
147   public int getY() {
148     return y;
149   }
150 
151   public Rectangle getBounds() {
152     return new Rectangle(x, y, width, height);
153   }
154   public String toString() {
155     String result = "Annotation: lowAddr: " + getLowAddress() + " highAddr: " + getHighAddress() + " strings: "  + strings.size();
156     for (int i = 0; i < strings.size(); i++) {
157       result += "\n" + (String) strings.get(i);
158     }
159     return result;
160   }
161 }