KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > ext > html > HTMLCompletionResultItemPaintComponent


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.editor.ext.html;
21
22 import java.lang.reflect.Modifier JavaDoc;
23 import java.awt.*;
24 import java.awt.font.TextAttribute JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.text.AttributedString JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import javax.swing.*;
32 import org.openide.util.Utilities;
33
34 /**
35  *
36  * @author Dusan Balek
37  */

38 public class HTMLCompletionResultItemPaintComponent extends JPanel {
39
40     static final String JavaDoc PACKAGE = "org/netbeans/modules/editor/resources/completion/defaultFolder.gif"; // NOI18N
41

42     protected int drawX;
43
44     protected int drawY;
45
46     protected int drawHeight;
47
48     private Font drawFont;
49
50     private int iconTextGap = 5;
51
52     private int fontHeight;
53
54     private int ascent;
55
56     private Map JavaDoc widths;
57
58     private FontMetrics fontMetrics;
59
60     private boolean isSelected;
61
62     private boolean isDeprecated;
63
64     private static final String JavaDoc THROWS = " throws "; // NOI18N
65

66     private static String JavaDoc str; //completion item text
67

68     private static final String JavaDoc[] frequentWords = new String JavaDoc[] {
69         "", " ", "[]", "(", ")", ", ", "String", THROWS // NOI18N
70
};
71
72     public static final Color KEYWORD_COLOR = Color.darkGray;
73     public static final Color TYPE_COLOR = Color.black;
74
75     public HTMLCompletionResultItemPaintComponent(){
76         super();
77         setOpaque(true);
78         setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 3));
79     }
80     
81     public void setString(String JavaDoc str){
82         this.str = str;
83     }
84     
85     public void setSelected(boolean isSelected){
86         this.isSelected = isSelected;
87     }
88     
89     protected void setDeprecated(boolean isDeprecated){
90         this.isDeprecated = isDeprecated;
91     }
92
93     public boolean isSelected(){
94         return isSelected;
95     }
96
97     protected boolean isDeprecated(){
98         return isDeprecated;
99     }
100
101     protected Icon getIcon() {
102         return null;
103     }
104     
105     public void paintComponent(Graphics g) {
106         // clear background
107
g.setColor(getBackground());
108         java.awt.Rectangle JavaDoc r = g.getClipBounds();
109         g.fillRect(r.x, r.y, r.width, r.height);
110         draw(g);
111     }
112
113     protected void draw(Graphics g){
114     }
115
116     /** Draw the icon if it is valid for the given type.
117      * Here the initial drawing assignments are also done.
118      */

119     protected void drawIcon(Graphics g, Icon icon) {
120         Insets i = getInsets();
121         if (i != null) {
122             drawX = i.left;
123             drawY = i.top;
124         } else {
125             drawX = 0;
126             drawY = 0;
127         }
128
129         if (icon != null) {
130             if (g != null) {
131                 icon.paintIcon(this, g, drawX, drawY);
132             }
133             drawX += icon.getIconWidth() + iconTextGap;
134             drawHeight = Math.max(fontHeight, icon.getIconHeight());
135         } else {
136             drawHeight = fontHeight;
137         }
138         if (i != null) {
139             drawHeight += i.bottom;
140         }
141         drawHeight += drawY;
142         drawY += ascent;
143     }
144
145     protected void drawString(Graphics g, String JavaDoc s){
146         drawString(g, s, false);
147     }
148
149     /** Draw string using the foreground color */
150     protected void drawString(Graphics g, String JavaDoc s, boolean strike) {
151         if (g != null) {
152             g.setColor(getForeground());
153         }
154         drawStringToGraphics(g, s, null, strike);
155     }
156
157
158     /** Draw string with given color which is first possibly modified
159      * by calling getColor() method to care about selection etc.
160      */

161     protected void drawString(Graphics g, String JavaDoc s, Color c) {
162         if (g != null) {
163             g.setColor(getColor(s, c));
164         }
165         drawStringToGraphics(g, s);
166     }
167
168     protected void drawString(Graphics g, String JavaDoc s, Color c, Font font, boolean strike) {
169         if (g != null) {
170             g.setColor(getColor(s, c));
171             g.setFont(font);
172         }
173         drawStringToGraphics(g, s, font, strike);
174         if (g != null) {
175             g.setFont(drawFont);
176         }
177
178     }
179     
180     protected void drawTypeName(Graphics g, String JavaDoc s, Color c) {
181         if (g == null) {
182             drawString(g, " "); // NOI18N
183
drawString(g, s, c);
184         } else {
185             int w = getWidth() - getWidth(s) - drawX;
186             int spaceWidth = getWidth(" "); // NOI18N
187
if (w > spaceWidth * 2) {
188                 drawX = getWidth() - 2 * spaceWidth - getWidth(s);
189             } else {
190                 drawX = getWidth() - 2 * spaceWidth - getWidth(s) - getWidth("... "); // NOI18N
191
g.setColor(getBackground());
192                 g.fillRect(drawX, 0, getWidth() - drawX, getHeight());
193                 drawString(g, "... ", c); // NOI18N
194
}
195             drawString(g, s, c);
196         }
197     }
198
199     protected void drawStringToGraphics(Graphics g, String JavaDoc s) {
200         drawStringToGraphics(g, s, null, false);
201     }
202
203     protected void drawStringToGraphics(Graphics g, String JavaDoc s, Font font, boolean strike) {
204         if (g != null) {
205             if (!strike){
206                 g.drawString(s, drawX, drawY);
207             }else{
208                 Graphics2D g2 = ((Graphics2D)g);
209                 AttributedString JavaDoc strikeText = new AttributedString JavaDoc(s);
210                 strikeText.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
211                 strikeText.addAttribute(TextAttribute.FONT, g.getFont());
212                 g2.drawString(strikeText.getIterator(), drawX, drawY);
213             }
214         }
215         drawX += getWidth(s, font);
216     }
217
218     protected int getWidth(String JavaDoc s) {
219         Integer JavaDoc i = (Integer JavaDoc)widths.get(s);
220         if (i != null) {
221             return i.intValue();
222         } else {
223             if (s == null) {
224                 s = "";
225             }
226             return fontMetrics.stringWidth(s);
227         }
228     }
229
230     protected int getWidth(String JavaDoc s, Font font) {
231         if (font == null) return getWidth(s);
232         return getFontMetrics(font).stringWidth(s);
233     }
234
235     protected Color getColor(String JavaDoc s, Color defaultColor) {
236         return isSelected ? getForeground()
237         : defaultColor;
238     }
239
240     private void storeWidth(String JavaDoc s) {
241         fontMetrics.stringWidth(s);
242     }
243
244     public void setFont(Font font) {
245         super.setFont(font);
246
247         fontMetrics = this.getFontMetrics(font);
248         fontHeight = fontMetrics.getHeight();
249         ascent = fontMetrics.getAscent();
250         if (widths != null) {
251             widths.clear();
252         } else {
253             widths = new HashMap JavaDoc();
254         }
255         for (int i = 0; i < frequentWords.length; i++) {
256             storeWidth(frequentWords[i]);
257         }
258         drawFont = font;
259     }
260
261     protected Font getDrawFont(){
262         return drawFont;
263     }
264
265     public Dimension getPreferredSize() {
266         draw(null);
267         Insets i = getInsets();
268         if (i != null) {
269             drawX += i.right;
270         }
271         if (drawX > getMaximumSize().width)
272             drawX = getMaximumSize().width;
273         return new Dimension(drawX, drawHeight);
274     }
275
276     public static class StringPaintComponent extends HTMLCompletionResultItemPaintComponent {
277         private Color c;
278         
279         public StringPaintComponent(Color c) {
280             this.c = c;
281         }
282         
283         protected void draw(Graphics g){
284             drawIcon(g, getIcon());
285             drawString(g, str, c);
286         }
287     }
288
289 }
290
Popular Tags