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.gsfret.editor.completion;
043:
044: import java.awt.*;
045: import java.util.List;
046: import javax.swing.*;
047: import javax.swing.text.JTextComponent;
048:
049: /**
050: *
051: * @author Dusan Balek
052: */
053: public class MethodParamsTipPaintComponent extends JToolTip {
054: private int drawX;
055: private int drawY;
056: private int drawHeight;
057: private int drawWidth;
058: private Font drawFont;
059: private int fontHeight;
060: private int descent;
061: private FontMetrics fontMetrics;
062:
063: private List<List<String>> params;
064: private int idx;
065: private JTextComponent component;
066:
067: public MethodParamsTipPaintComponent(List<List<String>> params,
068: int idx, JTextComponent component) {
069: super ();
070: this .params = params;
071: this .idx = idx;
072: this .component = component;
073: }
074:
075: public void paintComponent(Graphics g) {
076: // clear background
077: g.setColor(getBackground());
078: Rectangle r = g.getClipBounds();
079: g.fillRect(r.x, r.y, r.width, r.height);
080: g.setColor(getForeground());
081: draw(g);
082: }
083:
084: protected void draw(Graphics g) {
085: Insets in = getInsets();
086: GraphicsConfiguration gc = component.getGraphicsConfiguration();
087: int screenWidth = gc != null ? gc.getBounds().width
088: : Integer.MAX_VALUE;
089: if (in != null) {
090: drawX = in.left;
091: drawY = in.top;
092: } else {
093: drawX = 0;
094: drawY = 0;
095: }
096: drawY += (fontHeight - descent);
097:
098: int startX = drawX;
099: drawWidth = drawX;
100: // This code is deliberately a bit different from the Retouche version
101: // of this class. With the way I'm using the parameters list (a 2d set
102: // of parameters balanced by width) the code would incorrectly highlight
103: // the idx'th element on EVERY row. So I've modified the code by moving
104: // the i-count out outside the iteration over the rows, and I've removed
105: // the check to see if the index is creater than the list length.
106: int i = 0;
107: for (List<String> p : params) {
108: //int i = 0;
109: //int plen = p.size() - 1;
110: for (String s : p) {
111: if (getWidth(
112: s,
113: i == idx/* || i == plen && idx > plen*/? getDrawFont()
114: .deriveFont(Font.BOLD)
115: : getDrawFont())
116: + drawX > screenWidth) {
117: drawY += fontHeight;
118: drawX = startX + getWidth(" ", drawFont); //NOI18N
119: }
120: drawString(
121: g,
122: s,
123: i == idx/* || i == plen && idx > plen*/? getDrawFont()
124: .deriveFont(Font.BOLD)
125: : getDrawFont());
126: if (drawWidth < drawX)
127: drawWidth = drawX;
128: i++;
129: }
130: drawY += fontHeight;
131: drawX = startX;
132: }
133: drawHeight = drawY - fontHeight + descent;
134: if (in != null) {
135: drawHeight += in.bottom;
136: drawWidth += in.right;
137: }
138: }
139:
140: protected void drawString(Graphics g, String s, Font font) {
141: if (g != null) {
142: g.setFont(font);
143: g.drawString(s, drawX, drawY);
144: g.setFont(drawFont);
145: }
146: drawX += getWidth(s, font);
147: }
148:
149: protected int getWidth(String s, Font font) {
150: if (font == null)
151: return fontMetrics.stringWidth(s);
152: return getFontMetrics(font).stringWidth(s);
153: }
154:
155: protected int getHeight(String s, Font font) {
156: if (font == null)
157: return fontMetrics.stringWidth(s);
158: return getFontMetrics(font).stringWidth(s);
159: }
160:
161: public void setFont(Font font) {
162: super .setFont(font);
163: fontMetrics = this .getFontMetrics(font);
164: fontHeight = fontMetrics.getHeight();
165:
166: descent = fontMetrics.getDescent();
167: drawFont = font;
168: }
169:
170: protected Font getDrawFont() {
171: return drawFont;
172: }
173:
174: public Dimension getPreferredSize() {
175: draw(null);
176: Insets i = getInsets();
177: if (i != null) {
178: drawX += i.right;
179: }
180: return new Dimension(drawWidth, drawHeight);
181: }
182:
183: }
|