1 19 20 package org.netbeans.editor.ext.html; 21 22 import java.lang.reflect.Modifier ; 23 import java.awt.*; 24 import java.awt.font.TextAttribute ; 25 import java.util.Map ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.text.AttributedString ; 30 import java.util.ArrayList ; 31 import javax.swing.*; 32 import org.openide.util.Utilities; 33 34 38 public class HTMLCompletionResultItemPaintComponent extends JPanel { 39 40 static final String PACKAGE = "org/netbeans/modules/editor/resources/completion/defaultFolder.gif"; 42 protected int drawX; 43 44 protected int drawY; 45 46 protected int drawHeight; 47 48 private Font drawFont; 49 50 private int iconTextGap = 5; 51 52 private int fontHeight; 53 54 private int ascent; 55 56 private Map widths; 57 58 private FontMetrics fontMetrics; 59 60 private boolean isSelected; 61 62 private boolean isDeprecated; 63 64 private static final String THROWS = " throws "; 66 private static String str; 68 private static final String [] frequentWords = new String [] { 69 "", " ", "[]", "(", ")", ", ", "String", THROWS }; 71 72 public static final Color KEYWORD_COLOR = Color.darkGray; 73 public static final Color TYPE_COLOR = Color.black; 74 75 public HTMLCompletionResultItemPaintComponent(){ 76 super(); 77 setOpaque(true); 78 setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 3)); 79 } 80 81 public void setString(String str){ 82 this.str = str; 83 } 84 85 public void setSelected(boolean isSelected){ 86 this.isSelected = isSelected; 87 } 88 89 protected void setDeprecated(boolean isDeprecated){ 90 this.isDeprecated = isDeprecated; 91 } 92 93 public boolean isSelected(){ 94 return isSelected; 95 } 96 97 protected boolean isDeprecated(){ 98 return isDeprecated; 99 } 100 101 protected Icon getIcon() { 102 return null; 103 } 104 105 public void paintComponent(Graphics g) { 106 g.setColor(getBackground()); 108 java.awt.Rectangle r = g.getClipBounds(); 109 g.fillRect(r.x, r.y, r.width, r.height); 110 draw(g); 111 } 112 113 protected void draw(Graphics g){ 114 } 115 116 119 protected void drawIcon(Graphics g, Icon icon) { 120 Insets i = getInsets(); 121 if (i != null) { 122 drawX = i.left; 123 drawY = i.top; 124 } else { 125 drawX = 0; 126 drawY = 0; 127 } 128 129 if (icon != null) { 130 if (g != null) { 131 icon.paintIcon(this, g, drawX, drawY); 132 } 133 drawX += icon.getIconWidth() + iconTextGap; 134 drawHeight = Math.max(fontHeight, icon.getIconHeight()); 135 } else { 136 drawHeight = fontHeight; 137 } 138 if (i != null) { 139 drawHeight += i.bottom; 140 } 141 drawHeight += drawY; 142 drawY += ascent; 143 } 144 145 protected void drawString(Graphics g, String s){ 146 drawString(g, s, false); 147 } 148 149 150 protected void drawString(Graphics g, String s, boolean strike) { 151 if (g != null) { 152 g.setColor(getForeground()); 153 } 154 drawStringToGraphics(g, s, null, strike); 155 } 156 157 158 161 protected void drawString(Graphics g, String s, Color c) { 162 if (g != null) { 163 g.setColor(getColor(s, c)); 164 } 165 drawStringToGraphics(g, s); 166 } 167 168 protected void drawString(Graphics g, String s, Color c, Font font, boolean strike) { 169 if (g != null) { 170 g.setColor(getColor(s, c)); 171 g.setFont(font); 172 } 173 drawStringToGraphics(g, s, font, strike); 174 if (g != null) { 175 g.setFont(drawFont); 176 } 177 178 } 179 180 protected void drawTypeName(Graphics g, String s, Color c) { 181 if (g == null) { 182 drawString(g, " "); drawString(g, s, c); 184 } else { 185 int w = getWidth() - getWidth(s) - drawX; 186 int spaceWidth = getWidth(" "); if (w > spaceWidth * 2) { 188 drawX = getWidth() - 2 * spaceWidth - getWidth(s); 189 } else { 190 drawX = getWidth() - 2 * spaceWidth - getWidth(s) - getWidth("... "); g.setColor(getBackground()); 192 g.fillRect(drawX, 0, getWidth() - drawX, getHeight()); 193 drawString(g, "... ", c); } 195 drawString(g, s, c); 196 } 197 } 198 199 protected void drawStringToGraphics(Graphics g, String s) { 200 drawStringToGraphics(g, s, null, false); 201 } 202 203 protected void drawStringToGraphics(Graphics g, String s, Font font, boolean strike) { 204 if (g != null) { 205 if (!strike){ 206 g.drawString(s, drawX, drawY); 207 }else{ 208 Graphics2D g2 = ((Graphics2D)g); 209 AttributedString strikeText = new AttributedString (s); 210 strikeText.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON); 211 strikeText.addAttribute(TextAttribute.FONT, g.getFont()); 212 g2.drawString(strikeText.getIterator(), drawX, drawY); 213 } 214 } 215 drawX += getWidth(s, font); 216 } 217 218 protected int getWidth(String s) { 219 Integer i = (Integer )widths.get(s); 220 if (i != null) { 221 return i.intValue(); 222 } else { 223 if (s == null) { 224 s = ""; 225 } 226 return fontMetrics.stringWidth(s); 227 } 228 } 229 230 protected int getWidth(String s, Font font) { 231 if (font == null) return getWidth(s); 232 return getFontMetrics(font).stringWidth(s); 233 } 234 235 protected Color getColor(String s, Color defaultColor) { 236 return isSelected ? getForeground() 237 : defaultColor; 238 } 239 240 private void storeWidth(String s) { 241 fontMetrics.stringWidth(s); 242 } 243 244 public void setFont(Font font) { 245 super.setFont(font); 246 247 fontMetrics = this.getFontMetrics(font); 248 fontHeight = fontMetrics.getHeight(); 249 ascent = fontMetrics.getAscent(); 250 if (widths != null) { 251 widths.clear(); 252 } else { 253 widths = new HashMap (); 254 } 255 for (int i = 0; i < frequentWords.length; i++) { 256 storeWidth(frequentWords[i]); 257 } 258 drawFont = font; 259 } 260 261 protected Font getDrawFont(){ 262 return drawFont; 263 } 264 265 public Dimension getPreferredSize() { 266 draw(null); 267 Insets i = getInsets(); 268 if (i != null) { 269 drawX += i.right; 270 } 271 if (drawX > getMaximumSize().width) 272 drawX = getMaximumSize().width; 273 return new Dimension(drawX, drawHeight); 274 } 275 276 public static class StringPaintComponent extends HTMLCompletionResultItemPaintComponent { 277 private Color c; 278 279 public StringPaintComponent(Color c) { 280 this.c = c; 281 } 282 283 protected void draw(Graphics g){ 284 drawIcon(g, getIcon()); 285 drawString(g, str, c); 286 } 287 } 288 289 } 290 | Popular Tags |