1 19 20 package org.netbeans.modules.web.core.syntax.completion; 21 22 import java.awt.*; 23 import java.awt.font.TextAttribute ; 24 import java.util.Map ; 25 import java.util.HashMap ; 26 import java.text.AttributedString ; 27 import java.util.ArrayList ; 28 import javax.swing.*; 29 30 34 public class ResultItemPaintComponent extends JPanel { 35 36 static final String PACKAGE = "org/netbeans/modules/editor/resources/completion/defaultFolder.gif"; 38 protected int drawX; 39 40 protected int drawY; 41 42 protected int drawHeight; 43 44 private Font drawFont; 45 46 private int iconTextGap = 5; 47 48 private int fontHeight; 49 50 private int ascent; 51 52 private Map widths; 53 54 private FontMetrics fontMetrics; 55 56 private boolean isSelected; 57 58 private boolean isDeprecated; 59 60 private static final String THROWS = " throws "; 62 private static String str; 64 private static final String [] frequentWords = new String [] { 65 "", " ", "[]", "(", ")", ", ", "String", THROWS }; 67 68 public static final Color KEYWORD_COLOR = Color.darkGray; 69 public static final Color TYPE_COLOR = Color.black; 70 71 public ResultItemPaintComponent(){ 72 super(); 73 setOpaque(true); 74 setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 3)); 75 } 76 77 public void setString(String str){ 78 this.str = str; 79 } 80 81 public void setSelected(boolean isSelected){ 82 this.isSelected = isSelected; 83 } 84 85 protected void setDeprecated(boolean isDeprecated){ 86 this.isDeprecated = isDeprecated; 87 } 88 89 public boolean isSelected(){ 90 return isSelected; 91 } 92 93 protected boolean isDeprecated(){ 94 return isDeprecated; 95 } 96 97 protected Icon getIcon() { 98 return null; 99 } 100 101 public void paintComponent(Graphics g) { 102 g.setColor(getBackground()); 104 java.awt.Rectangle r = g.getClipBounds(); 105 g.fillRect(r.x, r.y, r.width, r.height); 106 draw(g); 107 } 108 109 protected void draw(Graphics g){ 110 } 111 112 115 protected void drawIcon(Graphics g, Icon icon) { 116 Insets i = getInsets(); 117 if (i != null) { 118 drawX = i.left; 119 drawY = i.top; 120 } else { 121 drawX = 0; 122 drawY = 0; 123 } 124 125 if (icon != null) { 126 if (g != null) { 127 icon.paintIcon(this, g, drawX, drawY); 128 } 129 drawX += icon.getIconWidth() + iconTextGap; 130 drawHeight = Math.max(fontHeight, icon.getIconHeight()); 131 } else { 132 drawHeight = fontHeight; 133 } 134 if (i != null) { 135 drawHeight += i.bottom; 136 } 137 drawHeight += drawY; 138 drawY += ascent; 139 } 140 141 protected void drawString(Graphics g, String s){ 142 drawString(g, s, false); 143 } 144 145 146 protected void drawString(Graphics g, String s, boolean strike) { 147 if (g != null) { 148 g.setColor(getForeground()); 149 } 150 drawStringToGraphics(g, s, null, strike); 151 } 152 153 154 157 protected void drawString(Graphics g, String s, Color c) { 158 if (g != null) { 159 g.setColor(getColor(s, c)); 160 } 161 drawStringToGraphics(g, s); 162 } 163 164 protected void drawString(Graphics g, String s, Color c, Font font, boolean strike) { 165 if (g != null) { 166 g.setColor(getColor(s, c)); 167 g.setFont(font); 168 } 169 drawStringToGraphics(g, s, font, strike); 170 if (g != null) { 171 g.setFont(drawFont); 172 } 173 174 } 175 176 protected void drawTypeName(Graphics g, String s, Color c) { 177 if (g == null) { 178 drawString(g, " "); drawString(g, s, c); 180 } else { 181 int w = getWidth() - getWidth(s) - drawX; 182 int spaceWidth = getWidth(" "); if (w > spaceWidth * 2) { 184 drawX = getWidth() - 2 * spaceWidth - getWidth(s); 185 } else { 186 drawX = getWidth() - 2 * spaceWidth - getWidth(s) - getWidth("... "); g.setColor(getBackground()); 188 g.fillRect(drawX, 0, getWidth() - drawX, getHeight()); 189 drawString(g, "... ", c); } 191 drawString(g, s, c); 192 } 193 } 194 195 protected void drawStringToGraphics(Graphics g, String s) { 196 drawStringToGraphics(g, s, null, false); 197 } 198 199 protected void drawStringToGraphics(Graphics g, String s, Font font, boolean strike) { 200 if (g != null) { 201 if (!strike){ 202 g.drawString(s, drawX, drawY); 203 }else{ 204 Graphics2D g2 = ((Graphics2D)g); 205 AttributedString strikeText = new AttributedString (s); 206 strikeText.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON); 207 strikeText.addAttribute(TextAttribute.FONT, g.getFont()); 208 g2.drawString(strikeText.getIterator(), drawX, drawY); 209 } 210 } 211 drawX += getWidth(s, font); 212 } 213 214 protected int getWidth(String s) { 215 Integer i = (Integer )widths.get(s); 216 if (i != null) { 217 return i.intValue(); 218 } else { 219 if (s == null) { 220 s = ""; 221 } 222 return fontMetrics.stringWidth(s); 223 } 224 } 225 226 protected int getWidth(String s, Font font) { 227 if (font == null) return getWidth(s); 228 return getFontMetrics(font).stringWidth(s); 229 } 230 231 protected Color getColor(String s, Color defaultColor) { 232 return isSelected ? getForeground() 233 : defaultColor; 234 } 235 236 private void storeWidth(String s) { 237 fontMetrics.stringWidth(s); 238 } 239 240 public void setFont(Font font) { 241 super.setFont(font); 242 243 fontMetrics = this.getFontMetrics(font); 244 fontHeight = fontMetrics.getHeight(); 245 ascent = fontMetrics.getAscent(); 246 if (widths != null) { 247 widths.clear(); 248 } else { 249 widths = new HashMap (); 250 } 251 for (int i = 0; i < frequentWords.length; i++) { 252 storeWidth(frequentWords[i]); 253 } 254 drawFont = font; 255 } 256 257 protected Font getDrawFont(){ 258 return drawFont; 259 } 260 261 public Dimension getPreferredSize() { 262 draw(null); 263 Insets i = getInsets(); 264 if (i != null) { 265 drawX += i.right; 266 } 267 if (drawX > getMaximumSize().width) 268 drawX = getMaximumSize().width; 269 return new Dimension(drawX, drawHeight); 270 } 271 272 273 275 public static class StringPaintComponent extends ResultItemPaintComponent { 276 277 protected void draw(Graphics g){ 278 drawIcon(g, getIcon()); 279 drawString(g, str, TYPE_COLOR); 280 } 281 } 282 283 284 public static class AbbrevPaintComponent extends ResultItemPaintComponent { 285 private String abbrev; 286 287 protected void draw(Graphics g){ 288 drawIcon(g, getIcon()); 289 drawString(g, abbrev, Color.BLUE, getDrawFont().deriveFont(Font.BOLD), false); 290 drawString(g, " " + str, TYPE_COLOR); 291 } 292 293 public void setAbbrev(String abbrev) { 294 this.abbrev = abbrev; 295 } 296 } 297 298 public static class JspTagPaintComponent extends ResultItemPaintComponent { 299 private boolean isEmpty; 300 301 public JspTagPaintComponent(boolean isEmpty) { 302 this.isEmpty = isEmpty; 303 } 304 305 protected void draw(Graphics g){ 306 drawIcon(g, getIcon()); 307 drawString(g, "<"); 308 drawString(g, str, Color.BLUE, getDrawFont().deriveFont(Font.BOLD), false ); 309 if(isEmpty) drawString(g, "/>"); 310 else drawString(g, ">"); 311 } 312 } 313 314 public static class JspDirectivePaintComponent extends ResultItemPaintComponent { 315 protected void draw(Graphics g){ 316 drawIcon(g, getIcon()); 317 drawString(g, "<%@"); 318 drawString(g, str, Color.BLUE, getDrawFont().deriveFont(Font.BOLD), false ); 319 drawString(g, " ... ", Color.GREEN.darker()); 320 drawString(g, "%>"); 321 } 322 } 323 324 public static class AttributePaintComponent extends ResultItemPaintComponent { 325 private boolean mandatory; 326 327 public AttributePaintComponent(boolean mandatory) { 328 this.mandatory = mandatory; 329 } 330 331 protected void draw(Graphics g){ 332 drawIcon(g, getIcon()); 333 drawString(g, str, (mandatory ? Color.RED: Color.GREEN.darker())); 334 } 335 } 336 337 339 public static class ELPaintComponent extends ResultItemPaintComponent{ 340 private String typeName = null; 341 342 public void draw(Graphics g){ 343 drawIcon(g, getIcon()); 344 drawString(g, str, getExpressionColor(), new Font(getDrawFont().getName(), getDrawFont().getStyle() | Font.BOLD, getDrawFont().getSize()), false); 345 if (getTypeName() != null) 346 drawTypeName(g, getTypeName(), getTypeColor()); 347 } 348 349 public Color getExpressionColor() { 350 return Color.blue; 351 } 352 353 public Color getTypeColor() { 354 return Color.BLACK; 355 } 356 357 public String getTypeName() { 358 return typeName; 359 } 360 361 public void setTypeName(String typeName) { 362 this.typeName = typeName; 363 } 364 } 365 366 public static class ELImplicitObjectPaintComponent extends ELPaintComponent { 367 private int type = 0; 368 369 private static final String OBJECT_PATH = "org/netbeans/modules/web/core/syntax/completion/resources/class_16.png"; private static final String MAP_PATH = "org/netbeans/modules/web/core/syntax/completion/resources/map_16.png"; 372 protected Icon getIcon(){ 373 Icon icon = null; 374 switch (type){ 375 case ELImplicitObjects.OBJECT_TYPE: 376 icon = new ImageIcon(org.openide.util.Utilities.loadImage(OBJECT_PATH)); break; 377 case ELImplicitObjects.MAP_TYPE: 378 icon = new ImageIcon(org.openide.util.Utilities.loadImage(MAP_PATH)); break; 379 } 380 return icon; 381 } 382 383 public int getType() { 384 return type; 385 } 386 387 public void setType(int type) { 388 this.type = type; 389 390 } 391 392 } 393 394 public static class ELBeanPaintComponent extends ELPaintComponent { 395 396 public static final Color BEAN_NAME_COLOR = Color.blue.darker().darker(); 397 private static final String BEAN_PATH = "org/netbeans/modules/web/core/syntax/completion/resources/bean_16.png"; 399 protected Icon getIcon(){ 400 return new ImageIcon(org.openide.util.Utilities.loadImage(BEAN_PATH)); 401 } 402 403 public Color getExpressionColor(){ 404 return BEAN_NAME_COLOR; 405 } 406 407 } 408 409 public static class ELPropertyPaintComponent extends ELPaintComponent { 410 411 public static final Color PROPERTY_NAME_COLOR = Color.blue.darker().darker(); 412 private static final String PROPERTY_PATH = "org/netbeans/modules/web/core/syntax/completion/resources/property_16.png"; 414 415 protected Icon getIcon(){ 416 return new ImageIcon(org.openide.util.Utilities.loadImage(PROPERTY_PATH)); 417 } 418 419 public Color getExpressionColor(){ 420 return PROPERTY_NAME_COLOR; 421 } 422 } 423 424 public static class ELFunctionPaintComponent extends ELPaintComponent { 425 426 private String prefix = null; 427 private String parameters = null; 428 private static final Color PREFIX_COLOR = Color.blue.darker().darker(); 429 private static final Color FUNCTION_NAME_COLOR = Color.black; 430 private static final Color PARAMETER_COLOR = Color.black; 431 432 private static final String ICON_PATH = "org/netbeans/modules/web/core/syntax/completion/resources/function_16.png"; 433 434 435 protected Icon getIcon(){ 436 return new ImageIcon(org.openide.util.Utilities.loadImage(ICON_PATH)); 437 } 438 439 public Color getExpressionColor(){ 440 return FUNCTION_NAME_COLOR; 441 } 442 443 public void draw(Graphics g){ 444 drawIcon(g, getIcon()); 445 drawString(g, prefix, PREFIX_COLOR, new Font(getDrawFont().getName(), getDrawFont().getStyle() | Font.BOLD, getDrawFont().getSize()), false); 446 drawString(g, ":"+str, FUNCTION_NAME_COLOR, new Font(getDrawFont().getName(), getDrawFont().getStyle() | Font.BOLD, getDrawFont().getSize()), false); 447 drawParameters(g, parameters); 448 if (getTypeName() != null) 449 drawTypeName(g, getTypeName(), getTypeColor()); 450 } 451 452 protected void drawParameters(Graphics g, String parList) { 453 drawString(g, "(", PARAMETER_COLOR); if (parameters != null) 455 drawString(g, parameters, PARAMETER_COLOR); 456 drawString(g, ")", PARAMETER_COLOR); 457 } 458 459 public void setPrefix(String prefix){ 460 this.prefix = prefix; 461 } 462 463 public void setParameters(String parameters){ 464 this.parameters = parameters; 465 } 466 467 } 468 } 469 | Popular Tags |