001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.editor.ext.html;
043:
044: import java.lang.reflect.Modifier;
045: import java.awt.*;
046: import java.awt.font.TextAttribute;
047: import java.util.Map;
048: import java.util.HashMap;
049: import java.util.Iterator;
050: import java.util.List;
051: import java.text.AttributedString;
052: import java.util.ArrayList;
053: import javax.swing.*;
054: import org.openide.util.Utilities;
055:
056: /**
057: *
058: * @author Dusan Balek
059: */
060: public class HTMLCompletionResultItemPaintComponent extends JPanel {
061:
062: static final String PACKAGE = "org/netbeans/modules/editor/resources/completion/defaultFolder.gif"; // NOI18N
063:
064: protected int drawX;
065:
066: protected int drawY;
067:
068: protected int drawHeight;
069:
070: private Font drawFont;
071:
072: private int iconTextGap = 5;
073:
074: private int fontHeight;
075:
076: private int ascent;
077:
078: private Map widths;
079:
080: private FontMetrics fontMetrics;
081:
082: private boolean isSelected;
083:
084: private boolean isDeprecated;
085:
086: private static final String THROWS = " throws "; // NOI18N
087:
088: private static String str; //completion item text
089:
090: private static final String[] frequentWords = new String[] { "",
091: " ", "[]", "(", ")", ", ", "String", THROWS // NOI18N
092: };
093:
094: public static final Color KEYWORD_COLOR = Color.darkGray;
095: public static final Color TYPE_COLOR = Color.black;
096:
097: public HTMLCompletionResultItemPaintComponent() {
098: super ();
099: setOpaque(true);
100: setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 3));
101: }
102:
103: public void setString(String str) {
104: this .str = str;
105: }
106:
107: public void setSelected(boolean isSelected) {
108: this .isSelected = isSelected;
109: }
110:
111: protected void setDeprecated(boolean isDeprecated) {
112: this .isDeprecated = isDeprecated;
113: }
114:
115: public boolean isSelected() {
116: return isSelected;
117: }
118:
119: protected boolean isDeprecated() {
120: return isDeprecated;
121: }
122:
123: protected Icon getIcon() {
124: return null;
125: }
126:
127: public void paintComponent(Graphics g) {
128: // clear background
129: g.setColor(getBackground());
130: java.awt.Rectangle r = g.getClipBounds();
131: g.fillRect(r.x, r.y, r.width, r.height);
132: draw(g);
133: }
134:
135: protected void draw(Graphics g) {
136: }
137:
138: /** Draw the icon if it is valid for the given type.
139: * Here the initial drawing assignments are also done.
140: */
141: protected void drawIcon(Graphics g, Icon icon) {
142: Insets i = getInsets();
143: if (i != null) {
144: drawX = i.left;
145: drawY = i.top;
146: } else {
147: drawX = 0;
148: drawY = 0;
149: }
150:
151: if (icon != null) {
152: if (g != null) {
153: icon.paintIcon(this , g, drawX, drawY);
154: }
155: drawX += icon.getIconWidth() + iconTextGap;
156: drawHeight = Math.max(fontHeight, icon.getIconHeight());
157: } else {
158: drawHeight = fontHeight;
159: }
160: if (i != null) {
161: drawHeight += i.bottom;
162: }
163: drawHeight += drawY;
164: drawY += ascent;
165: }
166:
167: protected void drawString(Graphics g, String s) {
168: drawString(g, s, false);
169: }
170:
171: /** Draw string using the foreground color */
172: protected void drawString(Graphics g, String s, boolean strike) {
173: if (g != null) {
174: g.setColor(getForeground());
175: }
176: drawStringToGraphics(g, s, null, strike);
177: }
178:
179: /** Draw string with given color which is first possibly modified
180: * by calling getColor() method to care about selection etc.
181: */
182: protected void drawString(Graphics g, String s, Color c) {
183: if (g != null) {
184: g.setColor(getColor(s, c));
185: }
186: drawStringToGraphics(g, s);
187: }
188:
189: protected void drawString(Graphics g, String s, Color c, Font font,
190: boolean strike) {
191: if (g != null) {
192: g.setColor(getColor(s, c));
193: g.setFont(font);
194: }
195: drawStringToGraphics(g, s, font, strike);
196: if (g != null) {
197: g.setFont(drawFont);
198: }
199:
200: }
201:
202: protected void drawTypeName(Graphics g, String s, Color c) {
203: if (g == null) {
204: drawString(g, " "); // NOI18N
205: drawString(g, s, c);
206: } else {
207: int spaceWidth = getWidth(" "); // NOI18N
208: if (getWidth() - getWidth(s) < drawX) {
209: drawX += spaceWidth * 2;
210: } else {
211: drawX = getWidth() - getWidth(s) - 4; //4px between the type text end and the frame of the completion window
212: }
213: drawString(g, s, c);
214: }
215: }
216:
217: protected void drawStringToGraphics(Graphics g, String s) {
218: drawStringToGraphics(g, s, null, false);
219: }
220:
221: protected void drawStringToGraphics(Graphics g, String s,
222: Font font, boolean strike) {
223: if (g != null) {
224: if (!strike) {
225: g.drawString(s, drawX, drawY);
226: } else {
227: Graphics2D g2 = ((Graphics2D) g);
228: AttributedString strikeText = new AttributedString(s);
229: strikeText.addAttribute(TextAttribute.STRIKETHROUGH,
230: TextAttribute.STRIKETHROUGH_ON);
231: strikeText
232: .addAttribute(TextAttribute.FONT, g.getFont());
233: g2.drawString(strikeText.getIterator(), drawX, drawY);
234: }
235: }
236: drawX += getWidth(s, font);
237: }
238:
239: protected int getWidth(String s) {
240: Integer i = (Integer) widths.get(s);
241: if (i != null) {
242: return i.intValue();
243: } else {
244: if (s == null) {
245: s = "";
246: }
247: return fontMetrics.stringWidth(s);
248: }
249: }
250:
251: protected int getWidth(String s, Font font) {
252: if (font == null)
253: return getWidth(s);
254: return getFontMetrics(font).stringWidth(s);
255: }
256:
257: protected Color getColor(String s, Color defaultColor) {
258: return isSelected ? getForeground() : defaultColor;
259: }
260:
261: private void storeWidth(String s) {
262: fontMetrics.stringWidth(s);
263: }
264:
265: public void setFont(Font font) {
266: super .setFont(font);
267:
268: fontMetrics = this .getFontMetrics(font);
269: fontHeight = fontMetrics.getHeight();
270: ascent = fontMetrics.getAscent();
271: if (widths != null) {
272: widths.clear();
273: } else {
274: widths = new HashMap();
275: }
276: for (int i = 0; i < frequentWords.length; i++) {
277: storeWidth(frequentWords[i]);
278: }
279: drawFont = font;
280: }
281:
282: protected Font getDrawFont() {
283: return drawFont;
284: }
285:
286: public int getPreferredWidth(Graphics g, Font f) {
287: setFont(f);
288: draw(g);
289: Insets i = getInsets();
290: if (i != null) {
291: drawX += i.right;
292: }
293: if (drawX > getMaximumSize().width)
294: drawX = getMaximumSize().width;
295: return drawX;
296: }
297:
298: public static class StringPaintComponent extends
299: HTMLCompletionResultItemPaintComponent {
300: private Color c;
301:
302: public StringPaintComponent(Color c) {
303: this .c = c;
304: }
305:
306: protected void draw(Graphics g) {
307: drawIcon(g, getIcon());
308: drawString(g, str, c);
309: }
310: }
311:
312: }
|