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.modules.xml.text.completion;
043:
044: import java.awt.*;
045: import java.awt.font.TextAttribute;
046: import java.util.Map;
047: import java.util.HashMap;
048: import java.text.AttributedString;
049: import javax.swing.*;
050:
051: /**
052: *
053: * @author Samaresh (Samaresh.Panda@Sun.Com)
054: */
055: public class XMLCompletionResultItemPaintComponent extends JPanel {
056:
057: /**
058: * Creates a new instance of XMLCompletionResultItemPaintComponent
059: */
060: public XMLCompletionResultItemPaintComponent() {
061: super ();
062: setOpaque(true);
063: setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 3));
064: }
065:
066: public void setString(String str) {
067: this .str = str;
068: }
069:
070: public void setSelected(boolean isSelected) {
071: this .isSelected = isSelected;
072: }
073:
074: protected void setDeprecated(boolean isDeprecated) {
075: this .isDeprecated = isDeprecated;
076: }
077:
078: public boolean isSelected() {
079: return isSelected;
080: }
081:
082: protected boolean isDeprecated() {
083: return isDeprecated;
084: }
085:
086: protected Icon getIcon() {
087: return null;
088: }
089:
090: public void paintComponent(Graphics g) {
091: // clear background
092: g.setColor(getBackground());
093: java.awt.Rectangle r = g.getClipBounds();
094: g.fillRect(r.x, r.y, r.width, r.height);
095: draw(g);
096: }
097:
098: protected void draw(Graphics g) {
099: }
100:
101: /** Draw the icon if it is valid for the given type.
102: * Here the initial drawing assignments are also done.
103: */
104: protected void drawIcon(Graphics g, Icon icon) {
105: Insets i = getInsets();
106: if (i != null) {
107: drawX = i.left;
108: drawY = i.top;
109: } else {
110: drawX = 0;
111: drawY = 0;
112: }
113:
114: if (icon != null) {
115: if (g != null) {
116: icon.paintIcon(this , g, drawX, drawY);
117: }
118: drawX += icon.getIconWidth() + iconTextGap;
119: drawHeight = Math.max(fontHeight, icon.getIconHeight());
120: } else {
121: drawHeight = fontHeight;
122: }
123: if (i != null) {
124: drawHeight += i.bottom;
125: }
126: drawHeight += drawY;
127: drawY += ascent;
128: }
129:
130: protected void drawString(Graphics g, String s) {
131: drawString(g, s, false);
132: }
133:
134: /** Draw string using the foreground color */
135: protected void drawString(Graphics g, String s, boolean strike) {
136: if (g != null) {
137: g.setColor(getForeground());
138: }
139: drawStringToGraphics(g, s, null, strike);
140: }
141:
142: /** Draw string with given color which is first possibly modified
143: * by calling getColor() method to care about selection etc.
144: */
145: protected void drawString(Graphics g, String s, Color c) {
146: if (g != null) {
147: g.setColor(getColor(s, c));
148: }
149: drawStringToGraphics(g, s);
150: }
151:
152: protected void drawString(Graphics g, String s, Color c, Font font,
153: boolean strike) {
154: if (g != null) {
155: g.setColor(getColor(s, c));
156: g.setFont(font);
157: }
158: drawStringToGraphics(g, s, font, strike);
159: if (g != null) {
160: g.setFont(drawFont);
161: }
162:
163: }
164:
165: protected void drawTypeName(Graphics g, String s, Color c) {
166: if (g == null) {
167: drawString(g, " "); // NOI18N
168: drawString(g, s, c);
169: } else {
170: int w = getWidth() - getWidth(s) - drawX;
171: int spaceWidth = getWidth(" "); // NOI18N
172: if (w > spaceWidth * 2) {
173: drawX = getWidth() - 2 * spaceWidth - getWidth(s);
174: } else {
175: drawX = getWidth() - 2 * spaceWidth - getWidth(s)
176: - getWidth("... "); // NOI18N
177: g.setColor(getBackground());
178: g.fillRect(drawX, 0, getWidth() - drawX, getHeight());
179: drawString(g, "... ", c); // NOI18N
180: }
181: drawString(g, s, c);
182: }
183: }
184:
185: protected void drawStringToGraphics(Graphics g, String s) {
186: drawStringToGraphics(g, s, null, false);
187: }
188:
189: protected void drawStringToGraphics(Graphics g, String s,
190: Font font, boolean strike) {
191: if (g != null) {
192: if (!strike) {
193: g.drawString(s, drawX, drawY);
194: } else {
195: Graphics2D g2 = ((Graphics2D) g);
196: AttributedString strikeText = new AttributedString(s);
197: strikeText.addAttribute(TextAttribute.STRIKETHROUGH,
198: TextAttribute.STRIKETHROUGH_ON);
199: strikeText
200: .addAttribute(TextAttribute.FONT, g.getFont());
201: g2.drawString(strikeText.getIterator(), drawX, drawY);
202: }
203: }
204: drawX += getWidth(s, font);
205: }
206:
207: protected int getWidth(String s) {
208: Integer i = (Integer) widths.get(s);
209: if (i != null) {
210: return i.intValue();
211: } else {
212: if (s == null) {
213: s = "";
214: }
215: return fontMetrics.stringWidth(s);
216: }
217: }
218:
219: protected int getWidth(String s, Font font) {
220: if (font == null)
221: return getWidth(s);
222: return getFontMetrics(font).stringWidth(s);
223: }
224:
225: protected Color getColor(String s, Color defaultColor) {
226: return isSelected ? getForeground() : defaultColor;
227: }
228:
229: private void storeWidth(String s) {
230: fontMetrics.stringWidth(s);
231: }
232:
233: public void setFont(Font font) {
234: super .setFont(font);
235:
236: fontMetrics = this .getFontMetrics(font);
237: fontHeight = fontMetrics.getHeight();
238: ascent = fontMetrics.getAscent();
239: if (widths != null) {
240: widths.clear();
241: } else {
242: widths = new HashMap();
243: }
244: for (int i = 0; i < frequentWords.length; i++) {
245: storeWidth(frequentWords[i]);
246: }
247: drawFont = font;
248: }
249:
250: protected Font getDrawFont() {
251: return drawFont;
252: }
253:
254: public Dimension getPreferredSize() {
255: draw(null);
256: Insets i = getInsets();
257: if (i != null) {
258: drawX += i.right;
259: }
260: if (drawX > getMaximumSize().width)
261: drawX = getMaximumSize().width;
262: return new Dimension(drawX, drawHeight);
263: }
264:
265: public static class StringPaintComponent extends
266: XMLCompletionResultItemPaintComponent {
267: private Color c;
268:
269: public StringPaintComponent(Color c) {
270: this .c = c;
271: }
272:
273: protected void draw(Graphics g) {
274: drawIcon(g, getIcon());
275: drawString(g, str, c);
276: }
277: }
278:
279: static final String PACKAGE = "org/netbeans/modules/editor/resources/completion/defaultFolder.gif"; // NOI18N
280: protected int drawX;
281: protected int drawY;
282: protected int drawHeight;
283: private Font drawFont;
284: private int iconTextGap = 5;
285: private int fontHeight;
286: private int ascent;
287: private Map widths;
288: private FontMetrics fontMetrics;
289: private boolean isSelected;
290: private boolean isDeprecated;
291: private static final String THROWS = " throws "; // NOI18N
292: private static String str; //completion item text
293: private static final String[] frequentWords = new String[] { "",
294: " ", "[]", "(", ")", ", ", "String", THROWS // NOI18N
295: };
296: public static final Color KEYWORD_COLOR = Color.darkGray;
297: public static final Color TYPE_COLOR = Color.black;
298:
299: }
|