KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > java > MethodParamsTipPaintComponent


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.editor.java;
21
22 import java.awt.*;
23 import java.util.List JavaDoc;
24 import javax.swing.*;
25 import javax.swing.text.JTextComponent JavaDoc;
26
27 /**
28  *
29  * @author Dusan Balek
30  */

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 JavaDoc<List JavaDoc<String JavaDoc>> params;
43     private int idx;
44     private JTextComponent JavaDoc component;
45
46     public MethodParamsTipPaintComponent(List JavaDoc<List JavaDoc<String JavaDoc>> params, int idx, JTextComponent JavaDoc component){
47         super();
48         this.params = params;
49         this.idx = idx;
50         this.component = component;
51     }
52     
53     public void paintComponent(Graphics g) {
54         // clear background
55
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 JavaDoc<String JavaDoc> p : params) {
78             int i = 0;
79             int plen = p.size() - 1;
80             for (String JavaDoc 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); //NOI18N
84
}
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 JavaDoc 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 JavaDoc s, Font font) {
110         if (font == null) return fontMetrics.stringWidth(s);
111         return getFontMetrics(font).stringWidth(s);
112     }
113
114     protected int getHeight(String JavaDoc 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