KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > editor > completion > NNPaintComponent


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.j2ee.persistence.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.Insets JavaDoc;
29 import java.awt.font.TextAttribute JavaDoc;
30 import java.text.AttributedString JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Map JavaDoc;
33 import javax.swing.BorderFactory JavaDoc;
34 import javax.swing.Icon JavaDoc;
35 import javax.swing.ImageIcon JavaDoc;
36 import javax.swing.JPanel JavaDoc;
37 import org.openide.util.Utilities;
38
39 /**
40  *
41  * @author Dusan Balek, Andrei Badea, Marek Fukala
42  */

43 public class NNPaintComponent extends JPanel JavaDoc {
44     
45     static final String JavaDoc COLUMN_ICON = "org/netbeans/modules/j2ee/persistence/editor/completion/resources/column.gif"; //NOI18N
46
static final String JavaDoc TABLE_ICON = "org/netbeans/modules/j2ee/persistence/editor/completion/resources/table.gif"; //NOI18
47
static final String JavaDoc NOCONNECTION_ICON = "org/netbeans/modules/j2ee/persistence/editor/completion/resources/connectionDisconnected.gif"; //NOI18
48
static final String JavaDoc PU_ICON = "org/netbeans/modules/j2ee/persistence/ui/resources/EntityNodeIcon.gif"; //NOI18
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 NNPaintComponent(){
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 NNPaintComponent {
283         
284         private 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, TYPE_COLOR);
293         }
294         
295     }
296     
297     public static final class DBElementPaintComponent extends NbStringPaintComponent {
298         
299     }
300     
301     public static final class ColumnElementPaintComponent extends NbStringPaintComponent {
302         
303         private String JavaDoc tableName, columnName;
304         
305         public void setContent(String JavaDoc columnName, String JavaDoc tableName) {
306             this.tableName = tableName;
307             this.columnName = columnName;
308         }
309         
310         protected void draw(Graphics JavaDoc g){
311             drawIcon(g, new ImageIcon JavaDoc(Utilities.loadImage(COLUMN_ICON)));
312             drawString(g, tableName+".", Color.BLACK);
313             drawString(g, columnName, Color.BLACK, getDrawFont().deriveFont(Font.BOLD), false);
314         }
315         
316     }
317     
318     public static final class TableElementPaintComponent extends NbStringPaintComponent {
319         
320         private String JavaDoc tableName;
321         
322         public void setContent(String JavaDoc tableName) {
323             this.tableName = tableName;
324         }
325         
326         protected void draw(Graphics JavaDoc g){
327             drawIcon(g, new ImageIcon JavaDoc(Utilities.loadImage(TABLE_ICON)));
328             drawString(g, tableName, Color.BLACK, getDrawFont().deriveFont(Font.BOLD), false);
329         }
330         
331     }
332     
333     public static final class PersistenceUnitElementPaintComponent extends NbStringPaintComponent {
334         
335         private String JavaDoc puName;
336         
337         public void setContent(String JavaDoc puName) {
338             this.puName = puName;
339         }
340         
341         protected void draw(Graphics JavaDoc g){
342             drawIcon(g, new ImageIcon JavaDoc(Utilities.loadImage(PU_ICON)));
343             drawString(g, puName, Color.BLACK, getDrawFont().deriveFont(Font.BOLD), false);
344         }
345         
346     }
347     
348     public static final class EntityPropertyElementPaintComponent extends NbStringPaintComponent {
349         
350         private String JavaDoc elName;
351         
352         public void setContent(String JavaDoc elName) {
353             this.elName = elName;
354         }
355         
356         protected void draw(Graphics JavaDoc g){
357             drawIcon(g, new ImageIcon JavaDoc(Utilities.loadImage(PU_ICON)));
358             drawString(g, elName, Color.BLACK, getDrawFont().deriveFont(Font.BOLD), false);
359         }
360         
361     }
362     
363     public static final class NoConnectionItemPaintComponent extends NbStringPaintComponent {
364         
365         protected void draw(Graphics JavaDoc g){
366             drawIcon(g, new ImageIcon JavaDoc(Utilities.loadImage(NOCONNECTION_ICON)));
367             drawString(g, "Connect To Database", Color.RED, getDrawFont().deriveFont(Font.BOLD), false);
368         }
369         
370     }
371     
372     
373 }
374
Popular Tags