KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > completion > CompletionPaintComponent


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.schema.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 import org.netbeans.modules.xml.axi.AbstractAttribute;
29 import org.netbeans.modules.xml.axi.Attribute;
30 import org.netbeans.modules.xml.schema.model.Attribute.Use;
31
32 /**
33  *
34  * @author Samaresh (Samaresh.Panda@Sun.Com)
35  */

36 public abstract class CompletionPaintComponent extends JPanel {
37
38     /**
39      * Creates a new instance of CompletionPaintComponent
40      */

41     public CompletionPaintComponent(CompletionResultItem item) {
42         this.completionItem = item;
43         setOpaque(true);
44         setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 3));
45     }
46     
47     public void setSelected(boolean selected) {
48         this.isSelected = selected;
49     }
50     
51     public boolean isSelected() {
52         return isSelected;
53     }
54     
55     public void paintComponent(Graphics g) {
56         // clear background
57
g.setColor(getBackground());
58         java.awt.Rectangle JavaDoc r = g.getClipBounds();
59         g.fillRect(r.x, r.y, r.width, r.height);
60         draw(g);
61     }
62         
63     protected void draw(Graphics g) {
64         drawIcon(g, completionItem.getIcon());
65         drawString(g, completionItem.getItemText(), completionItem.getPaintColor(),
66             getDrawFont(), false);
67     }
68
69     /**
70      * Draw the icon if it is valid for the given type.
71      * Here the initial drawing assignments are also done.
72      */

73     protected void drawIcon(Graphics g, Icon icon) {
74         Insets i = getInsets();
75         if (i != null) {
76             drawX = i.left;
77             drawY = i.top;
78         } else {
79             drawX = 0;
80             drawY = 0;
81         }
82
83         if (icon != null) {
84             if (g != null) {
85                 icon.paintIcon(this, g, drawX, drawY);
86             }
87             drawX += icon.getIconWidth() + iconTextGap;
88             drawHeight = Math.max(fontHeight, icon.getIconHeight());
89         } else {
90             drawHeight = fontHeight;
91         }
92         if (i != null) {
93             drawHeight += i.bottom;
94         }
95         drawHeight += drawY;
96         drawY += ascent;
97     }
98
99     /**
100      * Draw string using the foreground color
101      */

102     protected void drawString(Graphics g, String JavaDoc s, boolean strike) {
103         if (g != null) {
104             g.setColor(getForeground());
105         }
106         drawStringToGraphics(g, s, null, strike);
107     }
108
109
110     /**
111      * Draw string with given color which is first possibly modified
112      * by calling getColor() method to care about selection etc.
113      */

114     protected void drawString(Graphics g, String JavaDoc s, Color c) {
115         if (g != null) {
116             g.setColor(getColor(s, c));
117         }
118         drawStringToGraphics(g, s, getDrawFont(), false);
119     }
120
121     protected void drawString(Graphics g, String JavaDoc s, Color c, Font font, boolean strike) {
122         if (g != null) {
123             g.setColor(getColor(s, c));
124             g.setFont(font);
125         }
126         drawStringToGraphics(g, s, font, strike);
127         if (g != null) {
128             g.setFont(drawFont);
129         }
130
131     }
132     
133     protected void drawTypeName(Graphics g, String JavaDoc s, Color c) {
134         if (g == null) {
135             drawString(g, " ", false); // NOI18N
136
drawString(g, s, c);
137         } else {
138             int w = getWidth() - getWidth(s) - drawX;
139             int spaceWidth = getWidth(" "); // NOI18N
140
if (w > spaceWidth * 2) {
141                 drawX = getWidth() - 2 * spaceWidth - getWidth(s);
142             } else {
143                 drawX = getWidth() - 2 * spaceWidth - getWidth(s) - getWidth(".. "); // NOI18N
144
g.setColor(getBackground());
145                 g.fillRect(drawX, 0, getWidth() - drawX, getHeight());
146                 drawString(g, ".. ", c); // NOI18N
147
}
148             drawString(g, s, c);
149         }
150     }
151
152     protected void drawStringToGraphics(Graphics g, String JavaDoc s, Font font, boolean strike) {
153         if (g != null) {
154             if (!strike){
155                 g.drawString(s, drawX, drawY);
156             } else {
157                 Graphics2D g2 = ((Graphics2D)g);
158                 AttributedString JavaDoc strikeText = new AttributedString JavaDoc(s);
159                 strikeText.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
160                 strikeText.addAttribute(TextAttribute.FONT, g.getFont());
161                 g2.drawString(strikeText.getIterator(), drawX, drawY);
162             }
163         }
164         drawX += getWidth(s, font);
165     }
166
167     protected int getWidth(String JavaDoc s) {
168         Integer JavaDoc i = (Integer JavaDoc)widths.get(s);
169         if (i != null) {
170             return i.intValue();
171         } else {
172             if (s == null) {
173                 s = "";
174             }
175             return fontMetrics.stringWidth(s);
176         }
177     }
178
179     protected int getWidth(String JavaDoc s, Font font) {
180         if (font == null) return getWidth(s);
181         return getFontMetrics(font).stringWidth(s);
182     }
183
184     protected Color getColor(String JavaDoc s, Color defaultColor) {
185         return isSelected ? getForeground()
186         : defaultColor;
187     }
188
189     private void storeWidth(String JavaDoc s) {
190         fontMetrics.stringWidth(s);
191     }
192
193     public void setFont(Font font) {
194         super.setFont(font);
195
196         fontMetrics = this.getFontMetrics(font);
197         fontHeight = fontMetrics.getHeight();
198         ascent = fontMetrics.getAscent();
199         if (widths != null) {
200             widths.clear();
201         } else {
202             widths = new HashMap JavaDoc();
203         }
204         for (int i = 0; i < frequentWords.length; i++) {
205             storeWidth(frequentWords[i]);
206         }
207         drawFont = font;
208     }
209
210     protected Font getDrawFont(){
211         return drawFont;
212     }
213
214     public Dimension getPreferredSize() {
215         draw(null);
216         Insets i = getInsets();
217         if (i != null) {
218             drawX += i.right;
219         }
220         if (drawX > getMaximumSize().width)
221             drawX = getMaximumSize().width;
222         return new Dimension(drawX, drawHeight);
223     }
224     
225     CompletionResultItem getCompletionItem() {
226         return completionItem;
227     }
228     
229     public static class AttributePaintComponent extends CompletionPaintComponent {
230         public AttributePaintComponent(CompletionResultItem item) {
231             super(item);
232         }
233         
234         protected Font getDrawFont() {
235             AbstractAttribute aa = (AbstractAttribute)getCompletionItem().
236                     getAXIComponent();
237             if(aa instanceof Attribute JavaDoc) {
238                 if(((Attribute JavaDoc)aa).getUse() == Use.REQUIRED) {
239                     return super.getDrawFont().deriveFont(Font.BOLD);
240                 }
241             }
242             
243             return super.getFont();
244         }
245     }
246     
247     public static class ElementPaintComponent extends CompletionPaintComponent {
248         public ElementPaintComponent(CompletionResultItem item) {
249             super(item);
250         }
251     }
252     
253     protected int drawX;
254     protected int drawY;
255     protected int drawHeight;
256     private Font drawFont;
257     private int iconTextGap = 5;
258     private int fontHeight;
259     private int ascent;
260     private Map JavaDoc widths;
261     private FontMetrics fontMetrics;
262     private boolean isSelected;
263     private CompletionResultItem completionItem;
264     
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 }
271
Popular Tags