1 19 20 package org.netbeans.modules.websvc.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.Image ; 29 import java.awt.Insets ; 30 import java.awt.font.TextAttribute ; 31 import java.text.AttributedString ; 32 import java.util.HashMap ; 33 import java.util.Map ; 34 import javax.swing.BorderFactory ; 35 import javax.swing.Icon ; 36 import javax.swing.ImageIcon ; 37 import javax.swing.JPanel ; 38 import org.openide.util.Utilities; 39 40 45 public class WSPaintComponent extends JPanel { 46 47 static final String PACKAGE = "org/netbeans/modules/editor/resources/completion/defaultFolder.gif"; static final String FILE_PROTOCOL= "org/netbeans/modules/websvc/editor/completion/resources/fileProtocol.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 WSPaintComponent(){ 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 WSPaintComponent { 283 284 protected 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, Color.BLACK); 293 } 295 } 296 297 public static final class PackageItemPaintComponent extends NbStringPaintComponent { 298 299 protected void draw(Graphics g){ 300 drawIcon(g, new ImageIcon (Utilities.loadImage(PACKAGE))); 301 drawString(g, str, Color.BLACK); 302 } 304 305 } 306 307 public static final class FileItemPaintComponent extends NbStringPaintComponent { 308 309 private Image icon; 310 311 public FileItemPaintComponent(Image icon) { 312 this.icon = icon; 313 } 314 315 protected void draw(Graphics g){ 316 drawIcon(g, new ImageIcon (icon)); 317 drawString(g, str, Color.BLACK); 318 } 319 320 } 321 322 public static final class FileProtocolItemPaintComponent extends NbStringPaintComponent { 323 324 protected void draw(Graphics g){ 325 drawIcon(g, new ImageIcon (Utilities.loadImage(FILE_PROTOCOL))); 326 drawString(g, str, Color.BLACK, getDrawFont().deriveFont(Font.BOLD), false); 327 } 328 329 } 330 331 } 332 | Popular Tags |