1 19 20 package org.netbeans.modules.j2ee.persistence.editor.completion; 21 22 import java.awt.Color ; 23 import java.awt.Dimension ; 24 import java.awt.Font ; 25 import java.awt.FontMetrics ; 26 import java.awt.Graphics ; 27 import java.awt.Graphics2D ; 28 import java.awt.Insets ; 29 import java.awt.font.TextAttribute ; 30 import java.text.AttributedString ; 31 import java.util.HashMap ; 32 import java.util.Map ; 33 import javax.swing.BorderFactory ; 34 import javax.swing.Icon ; 35 import javax.swing.ImageIcon ; 36 import javax.swing.JPanel ; 37 import org.openide.util.Utilities; 38 39 43 public class NNPaintComponent extends JPanel { 44 45 static final String COLUMN_ICON = "org/netbeans/modules/j2ee/persistence/editor/completion/resources/column.gif"; static final String TABLE_ICON = "org/netbeans/modules/j2ee/persistence/editor/completion/resources/table.gif"; static final String NOCONNECTION_ICON = "org/netbeans/modules/j2ee/persistence/editor/completion/resources/connectionDisconnected.gif"; static final String PU_ICON = "org/netbeans/modules/j2ee/persistence/ui/resources/EntityNodeIcon.gif"; 50 private static final int ICON_WIDTH = 16; 51 private static final int ICON_TEXT_GAP = 5; 52 53 protected int drawX; 54 55 protected int drawY; 56 57 protected int drawHeight; 58 59 private Font drawFont; 60 61 private int fontHeight; 62 63 private int ascent; 64 65 private Map widths; 66 67 private FontMetrics fontMetrics; 68 69 private boolean isSelected; 70 71 private boolean isDeprecated; 72 73 private static final String THROWS = " throws "; 75 76 private static final String [] frequentWords = new String [] { 77 "", " ", "[]", "(", ")", ", ", "String", THROWS }; 79 80 public static final Color KEYWORD_COLOR = Color.darkGray; 81 public static final Color TYPE_COLOR = Color.black; 82 83 84 static final Color ENCLOSING_CALL_COLOR = Color.gray; 85 86 static final Color ACTIVE_PARAMETER_COLOR = Color.black; 87 88 public NNPaintComponent(){ 89 super(); 90 setOpaque(true); 91 setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 3)); 92 } 93 94 protected void setSelected(boolean isSelected){ 95 this.isSelected = isSelected; 96 } 97 98 protected void setDeprecated(boolean isDeprecated){ 99 this.isDeprecated = isDeprecated; 100 } 101 102 protected boolean isSelected(){ 103 return isSelected; 104 } 105 106 protected boolean isDeprecated(){ 107 return isDeprecated; 108 } 109 110 public void paintComponent(Graphics g) { 111 g.setColor(getBackground()); 113 java.awt.Rectangle r = g.getClipBounds(); 114 g.fillRect(r.x, r.y, r.width, r.height); 115 draw(g); 116 } 117 118 protected void draw(Graphics g){ 119 } 120 121 122 125 protected void drawIcon(Graphics g, Icon icon) { 126 Insets i = getInsets(); 127 if (i != null) { 128 drawX = i.left; 129 drawY = i.top; 130 } else { 131 drawX = 0; 132 drawY = 0; 133 } 134 135 if (icon != null) { 136 if (g != null) { 137 icon.paintIcon(this, g, drawX, drawY); 138 } 139 drawHeight = Math.max(fontHeight, icon.getIconHeight()); 140 } else { 141 drawHeight = fontHeight; 142 } 143 drawX += ICON_WIDTH + ICON_TEXT_GAP; 144 if (i != null) { 145 drawHeight += i.bottom; 146 } 147 drawHeight += drawY; 148 drawY += ascent; 149 } 150 151 protected void drawString(Graphics g, String s){ 152 drawString(g, s, false); 153 } 154 155 156 protected void drawString(Graphics g, String s, boolean strike) { 157 if (g != null) { 158 g.setColor(getForeground()); 159 } 160 drawStringToGraphics(g, s, null, strike); 161 } 162 163 164 167 protected void drawString(Graphics g, String s, Color c) { 168 if (g != null) { 169 g.setColor(getColor(s, c)); 170 } 171 drawStringToGraphics(g, s); 172 } 173 174 protected void drawString(Graphics g, String s, Color c, Font font, boolean strike) { 175 if (g != null) { 176 g.setColor(getColor(s, c)); 177 g.setFont(font); 178 } 179 drawStringToGraphics(g, s, font, strike); 180 if (g != null) { 181 g.setFont(drawFont); 182 } 183 184 } 185 186 protected void drawTypeName(Graphics g, String s, Color c) { 187 if (g == null) { 188 drawString(g, " "); drawString(g, s, c); 190 } else { 191 int w = getWidth() - getWidth(s) - drawX; 192 int spaceWidth = getWidth(" "); if (w > spaceWidth * 2) { 194 drawX = getWidth() - 2 * spaceWidth - getWidth(s); 195 } else { 196 drawX = getWidth() - 2 * spaceWidth - getWidth(s) - getWidth("... "); g.setColor(getBackground()); 198 g.fillRect(drawX, 0, getWidth() - drawX, getHeight()); 199 drawString(g, "... ", c); } 201 drawString(g, s, c); 202 } 203 } 204 205 protected void drawStringToGraphics(Graphics g, String s) { 206 drawStringToGraphics(g, s, null, false); 207 } 208 209 protected void drawStringToGraphics(Graphics g, String s, Font font, boolean strike) { 210 if (g != null) { 211 if (!strike){ 212 g.drawString(s, drawX, drawY); 213 }else{ 214 Graphics2D g2 = ((Graphics2D )g); 215 AttributedString strikeText = new AttributedString (s); 216 strikeText.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON); 217 strikeText.addAttribute(TextAttribute.FONT, g.getFont()); 218 g2.drawString(strikeText.getIterator(), drawX, drawY); 219 } 220 } 221 drawX += getWidth(s, font); 222 } 223 224 protected int getWidth(String s) { 225 Integer i = (Integer )widths.get(s); 226 if (i != null) { 227 return i.intValue(); 228 } else { 229 if (s == null) { 230 s = ""; 231 } 232 return fontMetrics.stringWidth(s); 233 } 234 } 235 236 protected int getWidth(String s, Font font) { 237 if (font == null) return getWidth(s); 238 return getFontMetrics(font).stringWidth(s); 239 } 240 241 protected Color getColor(String s, Color defaultColor) { 242 return isSelected ? getForeground() 243 : defaultColor; 244 } 245 246 private void storeWidth(String s) { 247 fontMetrics.stringWidth(s); 248 } 249 250 public void setFont(Font font) { 251 super.setFont(font); 252 253 fontMetrics = this.getFontMetrics(font); 254 fontHeight = fontMetrics.getHeight(); 255 ascent = fontMetrics.getAscent(); 256 if (widths != null) { 257 widths.clear(); 258 } else { 259 widths = new HashMap (); 260 } 261 for (int i = 0; i < frequentWords.length; i++) { 262 storeWidth(frequentWords[i]); 263 } 264 drawFont = font; 265 } 266 267 protected Font getDrawFont(){ 268 return drawFont; 269 } 270 271 public Dimension getPreferredSize() { 272 draw(null); 273 Insets i = getInsets(); 274 if (i != null) { 275 drawX += i.right; 276 } 277 if (drawX > getMaximumSize().width) 278 drawX = getMaximumSize().width; 279 return new Dimension (drawX, drawHeight); 280 } 281 282 public static class NbStringPaintComponent extends NNPaintComponent { 283 284 private String str; 285 286 public void setString(String str){ 287 this.str = str; 288 } 289 290 protected void draw(Graphics g){ 291 drawIcon(g, null); 292 drawString(g, str, TYPE_COLOR); 293 } 294 295 } 296 297 public static final class DBElementPaintComponent extends NbStringPaintComponent { 298 299 } 300 301 public static final class ColumnElementPaintComponent extends NbStringPaintComponent { 302 303 private String tableName, columnName; 304 305 public void setContent(String columnName, String tableName) { 306 this.tableName = tableName; 307 this.columnName = columnName; 308 } 309 310 protected void draw(Graphics g){ 311 drawIcon(g, new ImageIcon (Utilities.loadImage(COLUMN_ICON))); 312 drawString(g, tableName+".", Color.BLACK); 313 drawString(g, columnName, Color.BLACK, getDrawFont().deriveFont(Font.BOLD), false); 314 } 315 316 } 317 318 public static final class TableElementPaintComponent extends NbStringPaintComponent { 319 320 private String tableName; 321 322 public void setContent(String tableName) { 323 this.tableName = tableName; 324 } 325 326 protected void draw(Graphics g){ 327 drawIcon(g, new ImageIcon (Utilities.loadImage(TABLE_ICON))); 328 drawString(g, tableName, Color.BLACK, getDrawFont().deriveFont(Font.BOLD), false); 329 } 330 331 } 332 333 public static final class PersistenceUnitElementPaintComponent extends NbStringPaintComponent { 334 335 private String puName; 336 337 public void setContent(String puName) { 338 this.puName = puName; 339 } 340 341 protected void draw(Graphics g){ 342 drawIcon(g, new ImageIcon (Utilities.loadImage(PU_ICON))); 343 drawString(g, puName, Color.BLACK, getDrawFont().deriveFont(Font.BOLD), false); 344 } 345 346 } 347 348 public static final class EntityPropertyElementPaintComponent extends NbStringPaintComponent { 349 350 private String elName; 351 352 public void setContent(String elName) { 353 this.elName = elName; 354 } 355 356 protected void draw(Graphics g){ 357 drawIcon(g, new ImageIcon (Utilities.loadImage(PU_ICON))); 358 drawString(g, elName, Color.BLACK, getDrawFont().deriveFont(Font.BOLD), false); 359 } 360 361 } 362 363 public static final class NoConnectionItemPaintComponent extends NbStringPaintComponent { 364 365 protected void draw(Graphics g){ 366 drawIcon(g, new ImageIcon (Utilities.loadImage(NOCONNECTION_ICON))); 367 drawString(g, "Connect To Database", Color.RED, getDrawFont().deriveFont(Font.BOLD), false); 368 } 369 370 } 371 372 373 } 374 | Popular Tags |