1 19 20 package org.netbeans.modules.javahelp; 21 22 import com.sun.java.help.impl.ViewAwareComponent; 23 import java.awt.Color ; 24 import java.awt.Cursor ; 25 import java.awt.Font ; 26 import java.awt.FontMetrics ; 27 import java.awt.Insets ; 28 import java.awt.Rectangle ; 29 import java.awt.event.ActionListener ; 30 import java.awt.event.ActionEvent ; 31 import java.awt.event.MouseEvent ; 32 import java.awt.event.MouseListener ; 33 import javax.swing.Icon ; 34 import javax.swing.JButton ; 35 import javax.swing.UIManager ; 36 import javax.swing.SwingUtilities ; 37 import javax.swing.SwingConstants ; 38 import javax.swing.plaf.basic.BasicButtonUI ; 39 import javax.swing.border.EmptyBorder ; 40 import javax.swing.text.html.HTMLDocument ; 41 import javax.swing.text.html.StyleSheet ; 42 import javax.swing.text.AttributeSet ; 43 import javax.swing.text.StyleConstants ; 44 import javax.swing.text.SimpleAttributeSet ; 45 import javax.swing.text.View ; 46 import java.net.MalformedURLException ; 47 import java.net.URL ; 48 49 import org.openide.awt.HtmlBrowser; 50 import org.openide.util.NbBundle; 51 52 92 public class BrowserDisplayer extends JButton implements ActionListener , ViewAwareComponent { 93 private View myView; 94 private SimpleAttributeSet textAttribs; 95 private HTMLDocument doc; 96 private URL base; 97 98 private final static Cursor handCursor = 99 Cursor.getPredefinedCursor(Cursor.HAND_CURSOR); 100 101 private Cursor origCursor; 102 103 107 public BrowserDisplayer() { 108 super(); 109 setMargin(new Insets (0,0,0,0)); 110 createLinkLabel(); 111 addActionListener(this); 112 origCursor = getCursor(); 113 getAccessibleContext().setAccessibleDescription 114 (NbBundle.getMessage(BrowserDisplayer.class,"ACSD_Label")); 115 addMouseListener(new MouseListener () { 116 public void mouseClicked(MouseEvent e) { 117 } 118 119 public void mouseEntered(MouseEvent e) { 120 setCursor(handCursor); 121 } 122 123 public void mouseExited(MouseEvent e) { 124 setCursor(origCursor); 125 } 126 127 public void mousePressed(MouseEvent e) { 128 } 129 130 public void mouseReleased(MouseEvent e) { 131 } 132 }); 133 } 134 135 138 public void setViewData(View v) { 139 myView = v; 140 doc = (HTMLDocument ) myView.getDocument(); 141 base = doc.getBase(); 142 143 Font font = getFont(); 145 textAttribs = new SimpleAttributeSet (); 146 textAttribs.removeAttribute(StyleConstants.FontSize); 147 textAttribs.removeAttribute(StyleConstants.Bold); 148 textAttribs.removeAttribute(StyleConstants.Italic); 149 textAttribs.addAttribute(StyleConstants.FontFamily, 150 font.getName()); 151 textAttribs.addAttribute(StyleConstants.FontSize, 152 new Integer (font.getSize())); 153 textAttribs.addAttribute(StyleConstants.Bold, 154 Boolean.valueOf(font.isBold())); 155 textAttribs.addAttribute(StyleConstants.Italic, 156 Boolean.valueOf(font.isItalic())); 157 } 158 159 162 private String content = ""; 163 164 168 public void setContent(String content) { 169 this.content = content; 170 } 171 172 175 public String getContent() { 176 return content; 177 } 178 179 183 private void createLinkLabel() { 184 setBorder(new EmptyBorder (1,1,1,1)); 185 setBorderPainted(false); 186 setFocusPainted(false); 187 setAlignmentY(getPreferredLabelAlignment()); 188 setContentAreaFilled(false); 189 setHorizontalAlignment(SwingConstants.LEFT); 190 setBackground(UIManager.getColor("EditorPane.background")); 191 if (textAttribs != null && 192 textAttribs.isDefined(StyleConstants.Foreground)) { 193 setForeground((Color )textAttribs.getAttribute(StyleConstants.Foreground)); 194 } else { 195 setForeground(Color.blue); 196 } 197 invalidate(); 198 } 199 200 204 private float getPreferredLabelAlignment() { 205 Icon icon = (Icon )getIcon(); 206 String text = getText(); 207 208 Font font = getFont(); 209 FontMetrics fm = getToolkit().getFontMetrics(font); 210 211 Rectangle iconR = new Rectangle (); 212 Rectangle textR = new Rectangle (); 213 Rectangle viewR = new Rectangle (Short.MAX_VALUE, Short.MAX_VALUE); 214 215 SwingUtilities.layoutCompoundLabel( 216 this, fm, text, icon, 217 getVerticalAlignment(), getHorizontalAlignment(), 218 getVerticalTextPosition(), getHorizontalTextPosition(), 219 viewR, iconR, textR, 220 (text == null ? 0 : ((BasicButtonUI )ui).getDefaultTextIconGap(this)) 221 ); 222 223 Rectangle r = iconR.union(textR); 226 227 Insets insets = getInsets(); 228 r.height += insets.top + insets.bottom; 229 230 if(r.height % 2 == 0) { 233 r.height += 1; 234 } 235 236 float offAmt = fm.getMaxAscent() + insets.top; 237 return offAmt/(float)r.height; 238 } 239 240 245 public void setTextFontFamily(String family) { 246 textAttribs.removeAttribute(StyleConstants.FontFamily); 247 textAttribs.addAttribute(StyleConstants.FontFamily, family); 248 setFont(getAttributeSetFont(textAttribs)); 249 Font font = getFont(); 250 } 251 252 255 public String getTextFontFamily() { 256 return StyleConstants.getFontFamily(textAttribs); 257 } 258 259 280 public void setTextFontSize(String size) { 281 int newsize; 282 StyleSheet css = doc.getStyleSheet(); 283 try { 284 if (size.equals("xx-small")) { 285 newsize = (int)css.getPointSize(0); 286 } else if (size.equals("x-small")) { 287 newsize = (int)css.getPointSize(1); 288 } else if (size.equals("small")) { 289 newsize = (int)css.getPointSize(2); 290 } else if (size.equals("medium")) { 291 newsize = (int)css.getPointSize(3); 292 } else if (size.equals("large")) { 293 newsize = (int)css.getPointSize(4); 294 } else if (size.equals("x-large")) { 295 newsize = (int)css.getPointSize(5); 296 } else if (size.equals("xx-large")) { 297 newsize = (int)css.getPointSize(6); 298 } else if (size.equals("bigger")) { 299 newsize = (int)css.getPointSize("+1"); 300 } else if (size.equals("smaller")) { 301 newsize = (int)css.getPointSize("-1"); 302 } else if (size.endsWith("pt")) { 303 String sz = size.substring(0, size.length() - 2); 304 newsize = Integer.parseInt(sz); 305 } else { 306 newsize = (int) css.getPointSize(size); 307 } 308 } catch (NumberFormatException nfe) { 309 return; 310 } 311 if (newsize == 0) { 312 return; 313 } 314 textAttribs.removeAttribute(StyleConstants.FontSize); 315 textAttribs.addAttribute(StyleConstants.FontSize, 316 new Integer (newsize)); 317 setFont(getAttributeSetFont(textAttribs)); 318 Font font = getFont(); 319 } 320 321 324 public String getTextFontSize() { 325 return Integer.toString(StyleConstants.getFontSize(textAttribs)); 326 } 327 328 336 public void setTextFontWeight(String weight) { 337 boolean isBold=false; 338 if ("bold".equals(weight)) { 339 isBold = true; 340 } else { 341 isBold = false; 342 } 343 textAttribs.removeAttribute(StyleConstants.Bold); 344 textAttribs.addAttribute(StyleConstants.Bold, Boolean.valueOf(isBold)); 345 setFont(getAttributeSetFont(textAttribs)); 346 Font font = getFont(); 347 } 348 349 352 public String getTextFontWeight() { 353 if (StyleConstants.isBold(textAttribs)) { 354 return "bold"; 355 } 356 return "plain"; 357 } 358 359 367 public void setTextFontStyle(String style) { 368 boolean isItalic=false; 369 if ("italic".equals(style)) { 370 isItalic = true; 371 } else { 372 isItalic = false; 373 } 374 textAttribs.removeAttribute(StyleConstants.Italic); 375 textAttribs.addAttribute(StyleConstants.Italic, Boolean.valueOf(isItalic)); 376 setFont(getAttributeSetFont(textAttribs)); 377 Font font = getFont(); 378 } 379 380 383 public String getTextFontStyle() { 384 if (StyleConstants.isItalic(textAttribs)) { 385 return "italic"; 386 } 387 return "plain"; 388 } 389 390 409 public void setTextColor(String name) { 410 Color color=null; 411 if ("black".equals(name)) { 412 color = Color.black; 413 } else if ("blue".equals(name)) { 414 color = Color.blue; 415 } else if ("cyan".equals(name)) { 416 color = Color.cyan; 417 } else if ("darkGray".equals(name)) { 418 color = Color.darkGray; 419 } else if ("gray".equals(name)) { 420 color = Color.gray; 421 } else if ("green".equals(name)) { 422 color = Color.green; 423 } else if ("lightGray".equals(name)) { 424 color = Color.lightGray; 425 } else if ("magenta".equals(name)) { 426 color = Color.magenta; 427 } else if ("orange".equals(name)) { 428 color = Color.orange; 429 } else if ("pink".equals(name)) { 430 color = Color.pink; 431 } else if ("red".equals(name)) { 432 color = Color.red; 433 } else if ("white".equals(name)) { 434 color = Color.white; 435 } else if ("yellow".equals(name)) { 436 color = Color.yellow; 437 } 438 439 if (color == null) { 440 return; 441 } 442 textAttribs.removeAttribute(StyleConstants.Foreground); 443 textAttribs.addAttribute(StyleConstants.Foreground, color); 444 setForeground(color); 445 } 446 447 450 public String getTextColor() { 451 Color color = getForeground(); 452 return color.toString(); 453 } 454 455 466 private Font getAttributeSetFont(AttributeSet attr) { 467 int style = Font.PLAIN; 469 if (StyleConstants.isBold(attr)) { 470 style |= Font.BOLD; 471 } 472 if (StyleConstants.isItalic(attr)) { 473 style |= Font.ITALIC; 474 } 475 String family = StyleConstants.getFontFamily(attr); 476 int size = StyleConstants.getFontSize(attr); 477 478 483 if (StyleConstants.isSuperscript(attr) || 484 StyleConstants.isSubscript(attr)) { 485 size -= 2; 486 } 487 488 return doc.getStyleSheet().getFont(family, style, size); 490 } 491 492 495 public void actionPerformed(ActionEvent e) { 496 URL link; 497 try { 498 link = new URL (content); 499 } catch (MalformedURLException exc) { 500 return; 502 } 503 HtmlBrowser.URLDisplayer.getDefault().showURL(link); 504 } 505 506 } 507 | Popular Tags |