KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > text > completion > XMLCompletionResultItemPaintComponent


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.xml.text.completion;
21
22 import java.awt.*;
23 import java.awt.font.TextAttribute JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.text.AttributedString JavaDoc;
27 import javax.swing.*;
28
29 /**
30  *
31  * @author Samaresh (Samaresh.Panda@Sun.Com)
32  */

33 public class XMLCompletionResultItemPaintComponent extends JPanel {
34     
35     /**
36      * Creates a new instance of XMLCompletionResultItemPaintComponent
37      */

38     public XMLCompletionResultItemPaintComponent() {
39         super();
40         setOpaque(true);
41         setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 3));
42     }
43     
44     
45     public void setString(String JavaDoc str){
46         this.str = str;
47     }
48     
49     public void setSelected(boolean isSelected){
50         this.isSelected = isSelected;
51     }
52     
53     protected void setDeprecated(boolean isDeprecated){
54         this.isDeprecated = isDeprecated;
55     }
56
57     public boolean isSelected(){
58         return isSelected;
59     }
60
61     protected boolean isDeprecated(){
62         return isDeprecated;
63     }
64
65     protected Icon getIcon() {
66         return null;
67     }
68     
69     public void paintComponent(Graphics g) {
70         // clear background
71
g.setColor(getBackground());
72         java.awt.Rectangle JavaDoc r = g.getClipBounds();
73         g.fillRect(r.x, r.y, r.width, r.height);
74         draw(g);
75     }
76
77     protected void draw(Graphics g){
78     }
79
80     /** Draw the icon if it is valid for the given type.
81      * Here the initial drawing assignments are also done.
82      */

83     protected void drawIcon(Graphics g, Icon icon) {
84         Insets i = getInsets();
85         if (i != null) {
86             drawX = i.left;
87             drawY = i.top;
88         } else {
89             drawX = 0;
90             drawY = 0;
91         }
92
93         if (icon != null) {
94             if (g != null) {
95                 icon.paintIcon(this, g, drawX, drawY);
96             }
97             drawX += icon.getIconWidth() + iconTextGap;
98             drawHeight = Math.max(fontHeight, icon.getIconHeight());
99         } else {
100             drawHeight = fontHeight;
101         }
102         if (i != null) {
103             drawHeight += i.bottom;
104         }
105         drawHeight += drawY;
106         drawY += ascent;
107     }
108
109     protected void drawString(Graphics g, String JavaDoc s){
110         drawString(g, s, false);
111     }
112
113     /** Draw string using the foreground color */
114     protected void drawString(Graphics g, String JavaDoc s, boolean strike) {
115         if (g != null) {
116             g.setColor(getForeground());
117         }
118         drawStringToGraphics(g, s, null, strike);
119     }
120
121
122     /** Draw string with given color which is first possibly modified
123      * by calling getColor() method to care about selection etc.
124      */

125     protected void drawString(Graphics g, String JavaDoc s, Color c) {
126         if (g != null) {
127             g.setColor(getColor(s, c));
128         }
129         drawStringToGraphics(g, s);
130     }
131
132     protected void drawString(Graphics g, String JavaDoc s, Color c, Font font, boolean strike) {
133         if (g != null) {
134             g.setColor(getColor(s, c));
135             g.setFont(font);
136         }
137         drawStringToGraphics(g, s, font, strike);
138         if (g != null) {
139             g.setFont(drawFont);
140         }
141
142     }
143     
144     protected void drawTypeName(Graphics g, String JavaDoc s, Color c) {
145         if (g == null) {
146             drawString(g, " "); // NOI18N
147
drawString(g, s, c);
148         } else {
149             int w = getWidth() - getWidth(s) - drawX;
150             int spaceWidth = getWidth(" "); // NOI18N
151
if (w > spaceWidth * 2) {
152                 drawX = getWidth() - 2 * spaceWidth - getWidth(s);
153             } else {
154                 drawX = getWidth() - 2 * spaceWidth - getWidth(s) - getWidth("... "); // NOI18N
155
g.setColor(getBackground());
156                 g.fillRect(drawX, 0, getWidth() - drawX, getHeight());
157                 drawString(g, "... ", c); // NOI18N
158
}
159             drawString(g, s, c);
160         }
161     }
162
163     protected void drawStringToGraphics(Graphics g, String JavaDoc s) {
164         drawStringToGraphics(g, s, null, false);
165     }
166
167     protected void drawStringToGraphics(Graphics g, String JavaDoc s, Font font, boolean strike) {
168         if (g != null) {
169             if (!strike){
170                 g.drawString(s, drawX, drawY);
171             }else{
172                 Graphics2D g2 = ((Graphics2D)g);
173                 AttributedString JavaDoc strikeText = new AttributedString JavaDoc(s);
174                 strikeText.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
175                 strikeText.addAttribute(TextAttribute.FONT, g.getFont());
176                 g2.drawString(strikeText.getIterator(), drawX, drawY);
177             }
178         }
179         drawX += getWidth(s, font);
180     }
181
182     protected int getWidth(String JavaDoc s) {
183         Integer JavaDoc i = (Integer JavaDoc)widths.get(s);
184         if (i != null) {
185             return i.intValue();
186         } else {
187             if (s == null) {
188                 s = "";
189             }
190             return fontMetrics.stringWidth(s);
191         }
192     }
193
194     protected int getWidth(String JavaDoc s, Font font) {
195         if (font == null) return getWidth(s);
196         return getFontMetrics(font).stringWidth(s);
197     }
198
199     protected Color getColor(String JavaDoc s, Color defaultColor) {
200         return isSelected ? getForeground()
201         : defaultColor;
202     }
203
204     private void storeWidth(String JavaDoc s) {
205         fontMetrics.stringWidth(s);
206     }
207
208     public void setFont(Font font) {
209         super.setFont(font);
210
211         fontMetrics = this.getFontMetrics(font);
212         fontHeight = fontMetrics.getHeight();
213         ascent = fontMetrics.getAscent();
214         if (widths != null) {
215             widths.clear();
216         } else {
217             widths = new HashMap JavaDoc();
218         }
219         for (int i = 0; i < frequentWords.length; i++) {
220             storeWidth(frequentWords[i]);
221         }
222         drawFont = font;
223     }
224
225     protected Font getDrawFont(){
226         return drawFont;
227     }
228
229     public Dimension getPreferredSize() {
230         draw(null);
231         Insets i = getInsets();
232         if (i != null) {
233             drawX += i.right;
234         }
235         if (drawX > getMaximumSize().width)
236             drawX = getMaximumSize().width;
237         return new Dimension(drawX, drawHeight);
238     }
239
240     public static class StringPaintComponent extends XMLCompletionResultItemPaintComponent {
241         private Color c;
242         
243         public StringPaintComponent(Color c) {
244             this.c = c;
245         }
246         
247         protected void draw(Graphics g){
248             drawIcon(g, getIcon());
249             drawString(g, str, c);
250         }
251     }
252     
253     static final String JavaDoc PACKAGE = "org/netbeans/modules/editor/resources/completion/defaultFolder.gif"; // NOI18N
254
protected int drawX;
255     protected int drawY;
256     protected int drawHeight;
257     private Font drawFont;
258     private int iconTextGap = 5;
259     private int fontHeight;
260     private int ascent;
261     private Map JavaDoc widths;
262     private FontMetrics fontMetrics;
263     private boolean isSelected;
264     private boolean isDeprecated;
265     private static final String JavaDoc THROWS = " throws "; // NOI18N
266
private static String JavaDoc str; //completion item text
267
private static final String JavaDoc[] frequentWords = new String JavaDoc[] {
268         "", " ", "[]", "(", ")", ", ", "String", THROWS // NOI18N
269
};
270     public static final Color KEYWORD_COLOR = Color.darkGray;
271     public static final Color TYPE_COLOR = Color.black;
272
273 }
274
Popular Tags