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