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.j2ee.persistence.editor.completion;
043:
044: import java.awt.Color;
045: import java.awt.Dimension;
046: import java.awt.Font;
047: import java.awt.FontMetrics;
048: import java.awt.Graphics;
049: import java.awt.Graphics2D;
050: import java.awt.Insets;
051: import java.awt.font.TextAttribute;
052: import java.text.AttributedString;
053: import java.util.HashMap;
054: import java.util.Map;
055: import javax.swing.BorderFactory;
056: import javax.swing.Icon;
057: import javax.swing.ImageIcon;
058: import javax.swing.JPanel;
059: import org.openide.util.NbBundle;
060: import org.openide.util.Utilities;
061:
062: /**
063: *
064: * @author Dusan Balek, Andrei Badea, Marek Fukala
065: */
066: public class NNPaintComponent extends JPanel {
067:
068: static final String COLUMN_ICON = "org/netbeans/modules/j2ee/persistence/editor/completion/resources/column.gif"; //NOI18N
069: static final String TABLE_ICON = "org/netbeans/modules/j2ee/persistence/editor/completion/resources/table.gif"; //NOI18
070: static final String NOCONNECTION_ICON = "org/netbeans/modules/j2ee/persistence/editor/completion/resources/connectionDisconnected.gif"; //NOI18
071: static final String PU_ICON = "org/netbeans/modules/j2ee/persistence/ui/resources/EntityNodeIcon.gif"; //NOI18
072:
073: private static final int ICON_WIDTH = 16;
074: private static final int ICON_TEXT_GAP = 5;
075:
076: protected int drawX;
077:
078: protected int drawY;
079:
080: protected int drawHeight;
081:
082: private Font drawFont;
083:
084: private int fontHeight;
085:
086: private int ascent;
087:
088: private Map widths;
089:
090: private FontMetrics fontMetrics;
091:
092: private boolean isSelected;
093:
094: private boolean isDeprecated;
095:
096: private static final String THROWS = " throws "; // NOI18N
097:
098: private static final String[] frequentWords = new String[] { "",
099: " ", "[]", "(", ")", ", ", "String", THROWS // NOI18N
100: };
101:
102: public static final Color KEYWORD_COLOR = Color.darkGray;
103: public static final Color TYPE_COLOR = Color.black;
104:
105: /** When an outer method/constructor is rendered. */
106: static final Color ENCLOSING_CALL_COLOR = Color.gray;
107: /** When an active parameter gets rendered. */
108: static final Color ACTIVE_PARAMETER_COLOR = Color.black;
109:
110: public NNPaintComponent() {
111: super ();
112: setOpaque(true);
113: setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 3));
114: }
115:
116: protected void setSelected(boolean isSelected) {
117: this .isSelected = isSelected;
118: }
119:
120: protected void setDeprecated(boolean isDeprecated) {
121: this .isDeprecated = isDeprecated;
122: }
123:
124: protected boolean isSelected() {
125: return isSelected;
126: }
127:
128: protected boolean isDeprecated() {
129: return isDeprecated;
130: }
131:
132: public void paintComponent(Graphics g) {
133: // clear background
134: g.setColor(getBackground());
135: java.awt.Rectangle r = g.getClipBounds();
136: g.fillRect(r.x, r.y, r.width, r.height);
137: draw(g);
138: }
139:
140: protected void draw(Graphics g) {
141: }
142:
143: /** Draw the icon if it is valid for the given type.
144: * Here the initial drawing assignments are also done.
145: */
146: protected void drawIcon(Graphics g, Icon icon) {
147: Insets i = getInsets();
148: if (i != null) {
149: drawX = i.left;
150: drawY = i.top;
151: } else {
152: drawX = 0;
153: drawY = 0;
154: }
155:
156: if (icon != null) {
157: if (g != null) {
158: icon.paintIcon(this , g, drawX, drawY);
159: }
160: drawHeight = Math.max(fontHeight, icon.getIconHeight());
161: } else {
162: drawHeight = fontHeight;
163: }
164: drawX += ICON_WIDTH + ICON_TEXT_GAP;
165: if (i != null) {
166: drawHeight += i.bottom;
167: }
168: drawHeight += drawY;
169: drawY += ascent;
170: }
171:
172: protected void drawString(Graphics g, String s) {
173: drawString(g, s, false);
174: }
175:
176: /** Draw string using the foreground color */
177: protected void drawString(Graphics g, String s, boolean strike) {
178: if (g != null) {
179: g.setColor(getForeground());
180: }
181: drawStringToGraphics(g, s, null, strike);
182: }
183:
184: /** Draw string with given color which is first possibly modified
185: * by calling getColor() method to care about selection etc.
186: */
187: protected void drawString(Graphics g, String s, Color c) {
188: if (g != null) {
189: g.setColor(getColor(s, c));
190: }
191: drawStringToGraphics(g, s);
192: }
193:
194: protected void drawString(Graphics g, String s, Color c, Font font,
195: boolean strike) {
196: if (g != null) {
197: g.setColor(getColor(s, c));
198: g.setFont(font);
199: }
200: drawStringToGraphics(g, s, font, strike);
201: if (g != null) {
202: g.setFont(drawFont);
203: }
204:
205: }
206:
207: protected void drawTypeName(Graphics g, String s, Color c) {
208: if (g == null) {
209: drawString(g, " "); // NOI18N
210: drawString(g, s, c);
211: } else {
212: int w = getWidth() - getWidth(s) - drawX;
213: int spaceWidth = getWidth(" "); // NOI18N
214: if (w > spaceWidth * 2) {
215: drawX = getWidth() - 2 * spaceWidth - getWidth(s);
216: } else {
217: drawX = getWidth() - 2 * spaceWidth - getWidth(s)
218: - getWidth("... "); // NOI18N
219: g.setColor(getBackground());
220: g.fillRect(drawX, 0, getWidth() - drawX, getHeight());
221: drawString(g, "... ", c); // NOI18N
222: }
223: drawString(g, s, c);
224: }
225: }
226:
227: protected void drawStringToGraphics(Graphics g, String s) {
228: drawStringToGraphics(g, s, null, false);
229: }
230:
231: protected void drawStringToGraphics(Graphics g, String s,
232: Font font, boolean strike) {
233: if (g != null) {
234: if (!strike) {
235: g.drawString(s, drawX, drawY);
236: } else {
237: Graphics2D g2 = ((Graphics2D) g);
238: AttributedString strikeText = new AttributedString(s);
239: strikeText.addAttribute(TextAttribute.STRIKETHROUGH,
240: TextAttribute.STRIKETHROUGH_ON);
241: strikeText
242: .addAttribute(TextAttribute.FONT, g.getFont());
243: g2.drawString(strikeText.getIterator(), drawX, drawY);
244: }
245: }
246: drawX += getWidth(s, font);
247: }
248:
249: protected int getWidth(String s) {
250: Integer i = (Integer) widths.get(s);
251: if (i != null) {
252: return i.intValue();
253: } else {
254: if (s == null) {
255: s = "";
256: }
257: return fontMetrics.stringWidth(s);
258: }
259: }
260:
261: protected int getWidth(String s, Font font) {
262: if (font == null)
263: return getWidth(s);
264: return getFontMetrics(font).stringWidth(s);
265: }
266:
267: protected Color getColor(String s, Color defaultColor) {
268: return isSelected ? getForeground() : defaultColor;
269: }
270:
271: private void storeWidth(String s) {
272: fontMetrics.stringWidth(s);
273: }
274:
275: public void setFont(Font font) {
276: super .setFont(font);
277:
278: fontMetrics = this .getFontMetrics(font);
279: fontHeight = fontMetrics.getHeight();
280: ascent = fontMetrics.getAscent();
281: if (widths != null) {
282: widths.clear();
283: } else {
284: widths = new HashMap();
285: }
286: for (int i = 0; i < frequentWords.length; i++) {
287: storeWidth(frequentWords[i]);
288: }
289: drawFont = font;
290: }
291:
292: protected Font getDrawFont() {
293: return drawFont;
294: }
295:
296: public Dimension getPreferredSize() {
297: draw(null);
298: Insets i = getInsets();
299: if (i != null) {
300: drawX += i.right;
301: }
302: if (drawX > getMaximumSize().width)
303: drawX = getMaximumSize().width;
304: return new Dimension(drawX, drawHeight);
305: }
306:
307: public static class NbStringPaintComponent extends NNPaintComponent {
308:
309: private String str;
310:
311: public void setString(String str) {
312: this .str = str;
313: }
314:
315: protected void draw(Graphics g) {
316: drawIcon(g, null);
317: drawString(g, str, TYPE_COLOR);
318: }
319:
320: }
321:
322: public static final class DBElementPaintComponent extends
323: NbStringPaintComponent {
324:
325: }
326:
327: public static final class ColumnElementPaintComponent extends
328: NbStringPaintComponent {
329:
330: private String tableName, columnName;
331:
332: public void setContent(String columnName, String tableName) {
333: this .tableName = tableName;
334: this .columnName = columnName;
335: }
336:
337: protected void draw(Graphics g) {
338: drawIcon(g, new ImageIcon(Utilities.loadImage(COLUMN_ICON)));
339: drawString(g, tableName + ".", Color.BLACK);
340: drawString(g, columnName, Color.BLACK, getDrawFont()
341: .deriveFont(Font.BOLD), false);
342: }
343:
344: }
345:
346: public static final class TableElementPaintComponent extends
347: NbStringPaintComponent {
348:
349: private String tableName;
350:
351: public void setContent(String tableName) {
352: this .tableName = tableName;
353: }
354:
355: protected void draw(Graphics g) {
356: drawIcon(g, new ImageIcon(Utilities.loadImage(TABLE_ICON)));
357: drawString(g, tableName, Color.BLACK, getDrawFont()
358: .deriveFont(Font.BOLD), false);
359: }
360:
361: }
362:
363: public static final class PersistenceUnitElementPaintComponent
364: extends NbStringPaintComponent {
365:
366: private String puName;
367:
368: public void setContent(String puName) {
369: this .puName = puName;
370: }
371:
372: protected void draw(Graphics g) {
373: drawIcon(g, new ImageIcon(Utilities.loadImage(PU_ICON)));
374: drawString(g, puName, Color.BLACK, getDrawFont()
375: .deriveFont(Font.BOLD), false);
376: }
377:
378: }
379:
380: public static final class EntityPropertyElementPaintComponent
381: extends NbStringPaintComponent {
382:
383: private String elName;
384:
385: public void setContent(String elName) {
386: this .elName = elName;
387: }
388:
389: protected void draw(Graphics g) {
390: drawIcon(g, new ImageIcon(Utilities.loadImage(PU_ICON)));
391: drawString(g, elName, Color.BLACK, getDrawFont()
392: .deriveFont(Font.BOLD), false);
393: }
394:
395: }
396:
397: public static final class NoConnectionItemPaintComponent extends
398: NbStringPaintComponent {
399:
400: protected void draw(Graphics g) {
401: drawIcon(g, new ImageIcon(Utilities
402: .loadImage(NOCONNECTION_ICON)));
403: drawString(g, NbBundle.getMessage(NNPaintComponent.class,
404: "LBL_ConnectToDatabase"), Color.RED, getDrawFont()
405: .deriveFont(Font.BOLD), false);
406: }
407:
408: }
409:
410: }
|