KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > editor > completion > WSPaintComponent


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.websvc.editor.completion;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Font JavaDoc;
25 import java.awt.FontMetrics JavaDoc;
26 import java.awt.Graphics JavaDoc;
27 import java.awt.Graphics2D JavaDoc;
28 import java.awt.Image JavaDoc;
29 import java.awt.Insets JavaDoc;
30 import java.awt.font.TextAttribute JavaDoc;
31 import java.text.AttributedString JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Map JavaDoc;
34 import javax.swing.BorderFactory JavaDoc;
35 import javax.swing.Icon JavaDoc;
36 import javax.swing.ImageIcon JavaDoc;
37 import javax.swing.JPanel JavaDoc;
38 import org.openide.util.Utilities;
39
40 /**
41  * @author Marek Fukala
42  * @author Dusan Balek
43  * @author Andrei Badea
44  */

45 public class WSPaintComponent extends JPanel JavaDoc {
46     
47     static final String JavaDoc PACKAGE = "org/netbeans/modules/editor/resources/completion/defaultFolder.gif"; // NOI18N
48
static final String JavaDoc FILE_PROTOCOL= "org/netbeans/modules/websvc/editor/completion/resources/fileProtocol.gif"; // NOI18N
49

50     private static final int ICON_WIDTH = 16;
51     private static final int ICON_TEXT_GAP = 5;
52     
53     protected int drawX;
54
55     protected int drawY;
56
57     protected int drawHeight;
58
59     private Font JavaDoc drawFont;
60
61     private int fontHeight;
62
63     private int ascent;
64
65     private Map JavaDoc widths;
66
67     private FontMetrics JavaDoc fontMetrics;
68
69     private boolean isSelected;
70
71     private boolean isDeprecated;
72     
73     private static final String JavaDoc THROWS = " throws "; // NOI18N
74

75
76     private static final String JavaDoc[] frequentWords = new String JavaDoc[] {
77         "", " ", "[]", "(", ")", ", ", "String", THROWS // NOI18N
78
};
79
80     public static final Color JavaDoc KEYWORD_COLOR = Color.darkGray;
81     public static final Color JavaDoc TYPE_COLOR = Color.black;
82
83     /** When an outer method/constructor is rendered. */
84     static final Color JavaDoc ENCLOSING_CALL_COLOR = Color.gray;
85     /** When an active parameter gets rendered. */
86     static final Color JavaDoc ACTIVE_PARAMETER_COLOR = Color.black;
87
88     public WSPaintComponent(){
89         super();
90         setOpaque(true);
91         setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 3));
92     }
93     
94     protected void setSelected(boolean isSelected){
95         this.isSelected = isSelected;
96     }
97     
98     protected void setDeprecated(boolean isDeprecated){
99         this.isDeprecated = isDeprecated;
100     }
101
102     protected boolean isSelected(){
103         return isSelected;
104     }
105
106     protected boolean isDeprecated(){
107         return isDeprecated;
108     }
109
110     public void paintComponent(Graphics JavaDoc g) {
111         // clear background
112
g.setColor(getBackground());
113         java.awt.Rectangle JavaDoc r = g.getClipBounds();
114         g.fillRect(r.x, r.y, r.width, r.height);
115         draw(g);
116     }
117
118     protected void draw(Graphics JavaDoc g){
119     }
120
121
122     /** Draw the icon if it is valid for the given type.
123      * Here the initial drawing assignments are also done.
124      */

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

167     protected void drawString(Graphics JavaDoc g, String JavaDoc s, Color JavaDoc c) {
168         if (g != null) {
169             g.setColor(getColor(s, c));
170         }
171         drawStringToGraphics(g, s);
172     }
173
174     protected void drawString(Graphics JavaDoc g, String JavaDoc s, Color JavaDoc c, Font JavaDoc font, boolean strike) {
175         if (g != null) {
176             g.setColor(getColor(s, c));
177             g.setFont(font);
178         }
179         drawStringToGraphics(g, s, font, strike);
180         if (g != null) {
181             g.setFont(drawFont);
182         }
183
184     }
185     
186     protected void drawTypeName(Graphics JavaDoc g, String JavaDoc s, Color JavaDoc c) {
187         if (g == null) {
188             drawString(g, " "); // NOI18N
189
drawString(g, s, c);
190         } else {
191             int w = getWidth() - getWidth(s) - drawX;
192             int spaceWidth = getWidth(" "); // NOI18N
193
if (w > spaceWidth * 2) {
194                 drawX = getWidth() - 2 * spaceWidth - getWidth(s);
195             } else {
196                 drawX = getWidth() - 2 * spaceWidth - getWidth(s) - getWidth("... "); // NOI18N
197
g.setColor(getBackground());
198                 g.fillRect(drawX, 0, getWidth() - drawX, getHeight());
199                 drawString(g, "... ", c); // NOI18N
200
}
201             drawString(g, s, c);
202         }
203     }
204
205     protected void drawStringToGraphics(Graphics JavaDoc g, String JavaDoc s) {
206         drawStringToGraphics(g, s, null, false);
207     }
208
209     protected void drawStringToGraphics(Graphics JavaDoc g, String JavaDoc s, Font JavaDoc font, boolean strike) {
210         if (g != null) {
211             if (!strike){
212                 g.drawString(s, drawX, drawY);
213             }else{
214                 Graphics2D JavaDoc g2 = ((Graphics2D JavaDoc)g);
215                 AttributedString JavaDoc strikeText = new AttributedString JavaDoc(s);
216                 strikeText.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
217                 strikeText.addAttribute(TextAttribute.FONT, g.getFont());
218                 g2.drawString(strikeText.getIterator(), drawX, drawY);
219             }
220         }
221         drawX += getWidth(s, font);
222     }
223
224     protected int getWidth(String JavaDoc s) {
225         Integer JavaDoc i = (Integer JavaDoc)widths.get(s);
226         if (i != null) {
227             return i.intValue();
228         } else {
229             if (s == null) {
230                 s = "";
231             }
232             return fontMetrics.stringWidth(s);
233         }
234     }
235
236     protected int getWidth(String JavaDoc s, Font JavaDoc font) {
237         if (font == null) return getWidth(s);
238         return getFontMetrics(font).stringWidth(s);
239     }
240
241     protected Color JavaDoc getColor(String JavaDoc s, Color JavaDoc defaultColor) {
242         return isSelected ? getForeground()
243         : defaultColor;
244     }
245
246     private void storeWidth(String JavaDoc s) {
247         fontMetrics.stringWidth(s);
248     }
249
250     public void setFont(Font JavaDoc font) {
251         super.setFont(font);
252
253         fontMetrics = this.getFontMetrics(font);
254         fontHeight = fontMetrics.getHeight();
255         ascent = fontMetrics.getAscent();
256         if (widths != null) {
257             widths.clear();
258         } else {
259             widths = new HashMap JavaDoc();
260         }
261         for (int i = 0; i < frequentWords.length; i++) {
262             storeWidth(frequentWords[i]);
263         }
264         drawFont = font;
265     }
266
267     protected Font JavaDoc getDrawFont(){
268         return drawFont;
269     }
270
271     public Dimension JavaDoc getPreferredSize() {
272         draw(null);
273         Insets JavaDoc i = getInsets();
274         if (i != null) {
275             drawX += i.right;
276         }
277         if (drawX > getMaximumSize().width)
278             drawX = getMaximumSize().width;
279         return new Dimension JavaDoc(drawX, drawHeight);
280     }
281
282     public static class NbStringPaintComponent extends WSPaintComponent {
283
284         protected String JavaDoc str;
285
286         public void setString(String JavaDoc str){
287             this.str = str;
288         }
289         
290         protected void draw(Graphics JavaDoc g){
291             drawIcon(g, null);
292             drawString(g, str, Color.BLACK);
293             //drawString(g, columnName, Color.BLACK, getDrawFont().deriveFont(Font.BOLD), false);
294
}
295     }
296
297     public static final class PackageItemPaintComponent extends NbStringPaintComponent {
298         
299         protected void draw(Graphics JavaDoc g){
300             drawIcon(g, new ImageIcon JavaDoc(Utilities.loadImage(PACKAGE)));
301             drawString(g, str, Color.BLACK);
302             //drawString(g, columnName, Color.BLACK, getDrawFont().deriveFont(Font.BOLD), false);
303
}
304         
305     }
306     
307    public static final class FileItemPaintComponent extends NbStringPaintComponent {
308         
309        private Image JavaDoc icon;
310        
311        public FileItemPaintComponent(Image JavaDoc icon) {
312            this.icon = icon;
313        }
314        
315         protected void draw(Graphics JavaDoc g){
316             drawIcon(g, new ImageIcon JavaDoc(icon));
317             drawString(g, str, Color.BLACK);
318         }
319         
320     }
321    
322    public static final class FileProtocolItemPaintComponent extends NbStringPaintComponent {
323         
324         protected void draw(Graphics JavaDoc g){
325             drawIcon(g, new ImageIcon JavaDoc(Utilities.loadImage(FILE_PROTOCOL)));
326             drawString(g, str, Color.BLACK, getDrawFont().deriveFont(Font.BOLD), false);
327         }
328         
329     }
330     
331 }
332
Popular Tags