KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > editor > completion > support > CompletionUtilities


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.spi.editor.completion.support;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Font JavaDoc;
24 import java.awt.FontMetrics JavaDoc;
25 import java.awt.Graphics JavaDoc;
26 import javax.swing.ImageIcon JavaDoc;
27 import org.netbeans.modules.editor.completion.PatchedHtmlRenderer;
28
29 /**
30  * Various code completion utilities including completion item
31  * contents rendering.
32  *
33  * @author Miloslav Metelka
34  * @version 1.00
35  */

36
37 public final class CompletionUtilities {
38
39     /**
40      * The gap between left edge and icon.
41      */

42     private static final int BEFORE_ICON_GAP = 1;
43     
44     /**
45      * The gap between icon and the left text.
46      */

47     private static final int AFTER_ICON_GAP = 4;
48     
49     /**
50      * By default 16x16 icons should be used.
51      */

52     private static final int ICON_HEIGHT = 16;
53     private static final int ICON_WIDTH = 16;
54     
55     /**
56      * The gap between left and right text.
57      */

58     private static final int BEFORE_RIGHT_TEXT_GAP = 5;
59
60     /**
61      * The gap between right text and right edge.
62      */

63     private static final int AFTER_RIGHT_TEXT_GAP = 3;
64     
65     private CompletionUtilities() {
66         // no instances
67
}
68
69     /**
70      * Get preferred width of the item by knowing its left and right html texts.
71      * <br/>
72      * It is supposed that the item will have an icon 16x16 and an appropriate
73      * space is reserved for it.
74      *
75      * @param leftHtmlText html text displayed on the left side of the item
76      * next to the icon. It may be null which means no left text will be displayed.
77      * @param rightHtmlText html text aligned on the right edge of the item's
78      * rendering area. It may be null which means no right text will be displayed.
79      * @return &gt;=0 preferred rendering width of the item.
80      */

81     public static int getPreferredWidth(String JavaDoc leftHtmlText, String JavaDoc rightHtmlText,
82     Graphics JavaDoc g, Font JavaDoc defaultFont) {
83         int width = BEFORE_ICON_GAP + ICON_WIDTH + AFTER_ICON_GAP + AFTER_RIGHT_TEXT_GAP;
84         if (leftHtmlText != null && leftHtmlText.length() > 0) {
85             width += (int)PatchedHtmlRenderer.renderHTML(leftHtmlText, g, 0, 0, Integer.MAX_VALUE, 0,
86                     defaultFont, Color.black, PatchedHtmlRenderer.STYLE_CLIP, false, true);
87         }
88         if (rightHtmlText != null && rightHtmlText.length() > 0) {
89             if (leftHtmlText != null) {
90                 width += BEFORE_RIGHT_TEXT_GAP;
91             }
92             width += (int)PatchedHtmlRenderer.renderHTML(rightHtmlText, g, 0, 0, Integer.MAX_VALUE, 0,
93                     defaultFont, Color.black, PatchedHtmlRenderer.STYLE_CLIP, false, true);
94         }
95         return width;
96     }
97     
98     /**
99      * Render a completion item using the provided icon and left and right
100      * html texts.
101      *
102      * @param icon icon 16x16 that will be displayed on the left. It may be null
103      * which means that no icon will be displayed but the space for the icon
104      * will still be reserved (to properly align with other items
105      * that will provide an icon).
106      *
107      * @param leftHtmlText html text that will be displayed on the left side
108      * of the item's rendering area next to the icon.
109      * <br/>
110      * It may be null which indicates that no left text will be displayed.
111      * <br/>
112      * If there's not enough horizontal space in the rendering area
113      * the text will be shrinked and "..." will be displayed at the end.
114      *
115      * @param rightHtmlText html text that will be aligned to the right edge
116      * of the item's rendering area.
117      * <br/>
118      * It may be null which means that no right text will be displayed.
119      * <br/>
120      * The right text is always attempted to be fully displayed unlike
121      * the left text that may be shrinked if there's not enough rendering space
122      * in the horizontal direction.
123      * <br/>
124      * If there's not enough space even for the right text it will be shrinked
125      * and "..." will be displayed at the end of the rendered string.
126      * @param g non-null graphics through which the rendering happens.
127      * @param defaultFont non-null default font to be used for rendering.
128      * @param defaultColor non-null default color to be used for rendering.
129      * @param width &gt;=0 available width for rendering.
130      * @param height &gt;=0 available height for rendering.
131      * @param selected whether the item being rendered is currently selected
132      * in the completion's JList. If selected the foreground color is forced
133      * to be black for all parts of the rendered strings.
134      */

135     public static void renderHtml(ImageIcon JavaDoc icon, String JavaDoc leftHtmlText, String JavaDoc rightHtmlText,
136     Graphics JavaDoc g, Font JavaDoc defaultFont, Color JavaDoc defaultColor,
137     int width, int height, boolean selected) {
138         if (icon != null) {
139             // The image of the ImageIcon should already be loaded
140
// so no ImageObserver should be necessary
141
boolean done = g.drawImage(icon.getImage(), BEFORE_ICON_GAP, 0, null);
142             assert (done);
143         }
144         int iconWidth = BEFORE_ICON_GAP + ICON_WIDTH + AFTER_ICON_GAP;
145         int rightTextX = width - AFTER_RIGHT_TEXT_GAP;
146         FontMetrics JavaDoc fm = g.getFontMetrics(defaultFont);
147         int textY = (height - fm.getHeight())/2 + fm.getHeight() - fm.getDescent();
148         if (rightHtmlText != null && rightHtmlText.length() > 0) {
149             int rightTextWidth = (int)PatchedHtmlRenderer.renderHTML(rightHtmlText, g, 0, 0, Integer.MAX_VALUE, 0,
150                     defaultFont, defaultColor, PatchedHtmlRenderer.STYLE_CLIP, false, true);
151             rightTextX = Math.max(iconWidth, rightTextX - rightTextWidth);
152             // Render right text
153
PatchedHtmlRenderer.renderHTML(rightHtmlText, g, rightTextX, textY, rightTextWidth, textY,
154                 defaultFont, defaultColor, PatchedHtmlRenderer.STYLE_CLIP, true, selected);
155             rightTextX = Math.max(iconWidth, rightTextX - BEFORE_RIGHT_TEXT_GAP);
156         }
157
158         // Render left text
159
if (leftHtmlText != null && leftHtmlText.length() > 0 && rightTextX > iconWidth) { // any space for left text?
160
PatchedHtmlRenderer.renderHTML(leftHtmlText, g, iconWidth, textY, rightTextX - iconWidth, textY,
161                 defaultFont, defaultColor, PatchedHtmlRenderer.STYLE_TRUNCATE, true, selected);
162         }
163     }
164     
165 }
166
Popular Tags