KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > core > syntax > completion > ResultItemPaintComponent


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.web.core.syntax.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 java.util.ArrayList JavaDoc;
28 import javax.swing.*;
29
30 /**
31  *
32  * @author Dusan Balek
33  */

34 public class ResultItemPaintComponent extends JPanel {
35
36     static final String JavaDoc PACKAGE = "org/netbeans/modules/editor/resources/completion/defaultFolder.gif"; // NOI18N
37

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

62     private static String JavaDoc str; //completion item text
63

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

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

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

275     public static class StringPaintComponent extends ResultItemPaintComponent {
276
277         protected void draw(Graphics g){
278             drawIcon(g, getIcon());
279             drawString(g, str, TYPE_COLOR);
280         }
281     }
282
283     
284     public static class AbbrevPaintComponent extends ResultItemPaintComponent {
285         private String JavaDoc abbrev;
286         
287         protected void draw(Graphics g){
288             drawIcon(g, getIcon());
289             drawString(g, abbrev, Color.BLUE, getDrawFont().deriveFont(Font.BOLD), false);
290             drawString(g, " " + str, TYPE_COLOR);
291         }
292         
293         public void setAbbrev(String JavaDoc abbrev) {
294             this.abbrev = abbrev;
295         }
296     }
297     
298     public static class JspTagPaintComponent extends ResultItemPaintComponent {
299         private boolean isEmpty;
300         
301         public JspTagPaintComponent(boolean isEmpty) {
302             this.isEmpty = isEmpty;
303         }
304         
305         protected void draw(Graphics g){
306             drawIcon(g, getIcon());
307             drawString(g, "<");
308             drawString(g, str, Color.BLUE, getDrawFont().deriveFont(Font.BOLD), false );
309             if(isEmpty) drawString(g, "/>");
310                 else drawString(g, ">");
311         }
312     }
313
314     public static class JspDirectivePaintComponent extends ResultItemPaintComponent {
315         protected void draw(Graphics g){
316             drawIcon(g, getIcon());
317             drawString(g, "<%@");
318             drawString(g, str, Color.BLUE, getDrawFont().deriveFont(Font.BOLD), false );
319             drawString(g, " ... ", Color.GREEN.darker());
320             drawString(g, "%>");
321         }
322     }
323
324     public static class AttributePaintComponent extends ResultItemPaintComponent {
325         private boolean mandatory;
326         
327         public AttributePaintComponent(boolean mandatory) {
328             this.mandatory = mandatory;
329         }
330         
331         protected void draw(Graphics g){
332             drawIcon(g, getIcon());
333             drawString(g, str, (mandatory ? Color.RED: Color.GREEN.darker()));
334         }
335     }
336
337     // ------------------- EL CLASSES -----------------------------
338

339     public static class ELPaintComponent extends ResultItemPaintComponent{
340         private String JavaDoc typeName = null;
341            
342         public void draw(Graphics g){
343             drawIcon(g, getIcon());
344             drawString(g, str, getExpressionColor(), new Font(getDrawFont().getName(), getDrawFont().getStyle() | Font.BOLD, getDrawFont().getSize()), false);
345             if (getTypeName() != null)
346                 drawTypeName(g, getTypeName(), getTypeColor());
347         }
348
349         public Color getExpressionColor() {
350             return Color.blue;
351         }
352
353         public Color getTypeColor() {
354             return Color.BLACK;
355         }
356
357         public String JavaDoc getTypeName() {
358             return typeName;
359         }
360
361         public void setTypeName(String JavaDoc typeName) {
362             this.typeName = typeName;
363         }
364     }
365     
366     public static class ELImplicitObjectPaintComponent extends ELPaintComponent {
367         private int type = 0;
368        
369         private static final String JavaDoc OBJECT_PATH = "org/netbeans/modules/web/core/syntax/completion/resources/class_16.png"; //NOI18N
370
private static final String JavaDoc MAP_PATH = "org/netbeans/modules/web/core/syntax/completion/resources/map_16.png"; //NOI18N
371

372         protected Icon getIcon(){
373             Icon icon = null;
374             switch (type){
375                 case ELImplicitObjects.OBJECT_TYPE:
376                     icon = new ImageIcon(org.openide.util.Utilities.loadImage(OBJECT_PATH)); break;
377                 case ELImplicitObjects.MAP_TYPE:
378                     icon = new ImageIcon(org.openide.util.Utilities.loadImage(MAP_PATH)); break;
379             }
380             return icon;
381         }
382
383         public int getType() {
384             return type;
385         }
386
387         public void setType(int type) {
388             this.type = type;
389             
390         }
391         
392     }
393     
394     public static class ELBeanPaintComponent extends ELPaintComponent {
395         
396         public static final Color BEAN_NAME_COLOR = Color.blue.darker().darker();
397         private static final String JavaDoc BEAN_PATH = "org/netbeans/modules/web/core/syntax/completion/resources/bean_16.png"; //NOI18N
398

399         protected Icon getIcon(){
400             return new ImageIcon(org.openide.util.Utilities.loadImage(BEAN_PATH));
401         }
402         
403         public Color getExpressionColor(){
404             return BEAN_NAME_COLOR;
405         }
406         
407     }
408     
409     public static class ELPropertyPaintComponent extends ELPaintComponent {
410         
411         public static final Color PROPERTY_NAME_COLOR = Color.blue.darker().darker();
412         private static final String JavaDoc PROPERTY_PATH = "org/netbeans/modules/web/core/syntax/completion/resources/property_16.png"; //NOI18N
413

414         
415         protected Icon getIcon(){
416             return new ImageIcon(org.openide.util.Utilities.loadImage(PROPERTY_PATH));
417         }
418    
419         public Color getExpressionColor(){
420             return PROPERTY_NAME_COLOR;
421         }
422     }
423     
424     public static class ELFunctionPaintComponent extends ELPaintComponent {
425         
426         private String JavaDoc prefix = null;
427         private String JavaDoc parameters = null;
428         private static final Color PREFIX_COLOR = Color.blue.darker().darker();
429         private static final Color FUNCTION_NAME_COLOR = Color.black;
430         private static final Color PARAMETER_COLOR = Color.black;
431         
432         private static final String JavaDoc ICON_PATH = "org/netbeans/modules/web/core/syntax/completion/resources/function_16.png";
433
434         
435         protected Icon getIcon(){
436             return new ImageIcon(org.openide.util.Utilities.loadImage(ICON_PATH));
437         }
438    
439         public Color getExpressionColor(){
440             return FUNCTION_NAME_COLOR;
441         }
442         
443         public void draw(Graphics g){
444             drawIcon(g, getIcon());
445             drawString(g, prefix, PREFIX_COLOR, new Font(getDrawFont().getName(), getDrawFont().getStyle() | Font.BOLD, getDrawFont().getSize()), false);
446             drawString(g, ":"+str, FUNCTION_NAME_COLOR, new Font(getDrawFont().getName(), getDrawFont().getStyle() | Font.BOLD, getDrawFont().getSize()), false);
447             drawParameters(g, parameters);
448             if (getTypeName() != null)
449                 drawTypeName(g, getTypeName(), getTypeColor());
450         }
451         
452         protected void drawParameters(Graphics g, String JavaDoc parList) {
453             drawString(g, "(", PARAMETER_COLOR); // NOI18N
454
if (parameters != null)
455                 drawString(g, parameters, PARAMETER_COLOR);
456             drawString(g, ")", PARAMETER_COLOR);
457         }
458         
459         public void setPrefix(String JavaDoc prefix){
460             this.prefix = prefix;
461         }
462         
463         public void setParameters(String JavaDoc parameters){
464             this.parameters = parameters;
465         }
466
467     }
468 }
469
Popular Tags