1 19 20 package org.netbeans.modules.editor.java; 21 22 import java.awt.*; 23 import java.util.List ; 24 import javax.swing.*; 25 import javax.swing.text.JTextComponent ; 26 27 31 public class MethodParamsTipPaintComponent extends JToolTip { 32 33 private int drawX; 34 private int drawY; 35 private int drawHeight; 36 private int drawWidth; 37 private Font drawFont; 38 private int fontHeight; 39 private int descent; 40 private FontMetrics fontMetrics; 41 42 private List <List <String >> params; 43 private int idx; 44 private JTextComponent component; 45 46 public MethodParamsTipPaintComponent(List <List <String >> params, int idx, JTextComponent component){ 47 super(); 48 this.params = params; 49 this.idx = idx; 50 this.component = component; 51 } 52 53 public void paintComponent(Graphics g) { 54 g.setColor(getBackground()); 56 Rectangle r = g.getClipBounds(); 57 g.fillRect(r.x, r.y, r.width, r.height); 58 g.setColor(getForeground()); 59 draw(g); 60 } 61 62 protected void draw(Graphics g) { 63 Insets in = getInsets(); 64 GraphicsConfiguration gc = component.getGraphicsConfiguration(); 65 int screenWidth = gc != null ? gc.getBounds().width : Integer.MAX_VALUE; 66 if (in != null) { 67 drawX = in.left; 68 drawY = in.top; 69 } else { 70 drawX = 0; 71 drawY = 0; 72 } 73 drawY += (fontHeight - descent); 74 75 int startX = drawX; 76 drawWidth = drawX; 77 for (List <String > p : params) { 78 int i = 0; 79 int plen = p.size() - 1; 80 for (String s : p) { 81 if (getWidth(s, i == idx || i == plen && idx > plen ? getDrawFont().deriveFont(Font.BOLD) : getDrawFont()) + drawX > screenWidth) { 82 drawY += fontHeight; 83 drawX = startX + getWidth(" ", drawFont); } 85 drawString(g, s, i == idx || i == plen && idx > plen ? getDrawFont().deriveFont(Font.BOLD) : getDrawFont()); 86 if (drawWidth < drawX) 87 drawWidth = drawX; 88 i++; 89 } 90 drawY += fontHeight; 91 drawX = startX; 92 } 93 drawHeight = drawY - fontHeight + descent; 94 if (in != null) { 95 drawHeight += in.bottom; 96 drawWidth += in.right; 97 } 98 } 99 100 protected void drawString(Graphics g, String s, Font font) { 101 if (g != null) { 102 g.setFont(font); 103 g.drawString(s, drawX, drawY); 104 g.setFont(drawFont); 105 } 106 drawX += getWidth(s, font); 107 } 108 109 protected int getWidth(String s, Font font) { 110 if (font == null) return fontMetrics.stringWidth(s); 111 return getFontMetrics(font).stringWidth(s); 112 } 113 114 protected int getHeight(String s, Font font) { 115 if (font == null) return fontMetrics.stringWidth(s); 116 return getFontMetrics(font).stringWidth(s); 117 } 118 119 public void setFont(Font font) { 120 super.setFont(font); 121 fontMetrics = this.getFontMetrics(font); 122 fontHeight = fontMetrics.getHeight(); 123 124 descent = fontMetrics.getDescent(); 125 drawFont = font; 126 } 127 128 protected Font getDrawFont(){ 129 return drawFont; 130 } 131 132 public Dimension getPreferredSize() { 133 draw(null); 134 Insets i = getInsets(); 135 if (i != null) { 136 drawX += i.right; 137 } 138 return new Dimension(drawWidth, drawHeight); 139 } 140 141 } 142 | Popular Tags |