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