alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.javahelp;

import com.sun.java.help.impl.ViewAwareComponent;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.UIManager;
import javax.swing.SwingUtilities;
import javax.swing.SwingConstants;
import javax.swing.plaf.basic.BasicButtonUI;
import javax.swing.border.EmptyBorder;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.StyleSheet;
import javax.swing.text.AttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.View;
import java.net.MalformedURLException;
import java.net.URL;

import org.openide.awt.HtmlBrowser;
import org.openide.util.NbBundle;

/**
 * This class is a lightweight component to be included in HTML content within
 * JHContentViewer. It invokes default IDE html browser to show external URL.
 * (Default browser should be external browser to show external URL properly.
 * Component is displayed as a mouse enabled Label. Only text is supported.
 * 

* To use this class within HTML content use the <object> tag. Below is an * example usage: *

 * <object CLASSID="java:org.netbeans.module.javahelp.BrowserDisplayer">
 * <param name="content" value="http://www.netbeans.org">
 * <param name="text" value="Click here">
 * <param name="textFontFamily" value="SansSerif">
 * <param name="textFontSize" value="x-large">
 * <param name="textFontWeight" value="plain">
 * <param name="textFontStyle" value="italic">
 * <param name="textColor" value="red">
 * </object>
 * 

* Valid parameters are: *

    *
  • content - a valid external url like http://java.sun.com * @see setContent *
  • text - the text of the activator * @see setText *
  • textFontFamily - the font family of the activator text * @see setTextFontFamily *
  • textFontSize - the size of the activator text font. Size is specified * in a css terminology. See the setTextFontSize for acceptable syntax. * @see setTextFontSize *
  • textFontWeight - the activator text font weight * @see setTextFontWeight *
  • textFontStyle - the activator text font style * @see setTextFontStyle *
  • textColor - the activator text color * @see setTextColor *
      * * @author Marek Slama */ public class BrowserDisplayer extends JButton implements ActionListener, ViewAwareComponent { private View myView; private SimpleAttributeSet textAttribs; private HTMLDocument doc; private URL base; private final static Cursor handCursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR); private Cursor origCursor; /** * Create a secondaryviewer. By default the viewer creates a button with * the text of ">" */ public BrowserDisplayer() { super(); setMargin(new Insets(0,0,0,0)); createLinkLabel(); addActionListener(this); origCursor = getCursor(); getAccessibleContext().setAccessibleDescription (NbBundle.getMessage(BrowserDisplayer.class,"ACSD_Label")); addMouseListener(new MouseListener() { public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { setCursor(handCursor); } public void mouseExited(MouseEvent e) { setCursor(origCursor); } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } }); } /** * Sets data optained from the View */ public void setViewData(View v) { myView = v; doc = (HTMLDocument) myView.getDocument(); base = doc.getBase(); // Set the current font information in the local text attributes Font font = getFont(); textAttribs = new SimpleAttributeSet(); textAttribs.removeAttribute(StyleConstants.FontSize); textAttribs.removeAttribute(StyleConstants.Bold); textAttribs.removeAttribute(StyleConstants.Italic); textAttribs.addAttribute(StyleConstants.FontFamily, font.getName()); textAttribs.addAttribute(StyleConstants.FontSize, new Integer(font.getSize())); textAttribs.addAttribute(StyleConstants.Bold, new Boolean(font.isBold())); textAttribs.addAttribute(StyleConstants.Italic, new Boolean(font.isItalic())); } /** * properties */ private String content = ""; /** * Set the content for the secondary viewer * @param content a valid URL */ public void setContent(String content) { this.content = content; } /** * Returns the content of the secondary viewer */ public String getContent() { return content; } /** * Creates a link label. A link label is a form of a JButton but without a * button like appearance. */ private void createLinkLabel() { setBorder(new EmptyBorder(1,1,1,1)); setBorderPainted(false); setFocusPainted(false); setAlignmentY(getPreferredLabelAlignment()); setContentAreaFilled(false); setHorizontalAlignment(SwingConstants.LEFT); setBackground(UIManager.getColor("EditorPane.background")); if (textAttribs != null && textAttribs.isDefined(StyleConstants.Foreground)) { setForeground((Color)textAttribs.getAttribute(StyleConstants.Foreground)); } else { setForeground(Color.blue); } invalidate(); } /** * Determine the alignment offset so the text is aligned with other views * correctly. */ private float getPreferredLabelAlignment() { Icon icon = (Icon)getIcon(); String text = getText(); Font font = getFont(); FontMetrics fm = getToolkit().getFontMetrics(font); Rectangle iconR = new Rectangle(); Rectangle textR = new Rectangle(); Rectangle viewR = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE); SwingUtilities.layoutCompoundLabel( this, fm, text, icon, getVerticalAlignment(), getHorizontalAlignment(), getVerticalTextPosition(), getHorizontalTextPosition(), viewR, iconR, textR, (text == null ? 0 : ((BasicButtonUI)ui).getDefaultTextIconGap(this)) ); // The preferred size of the button is the size of // the text and icon rectangles plus the buttons insets. Rectangle r = iconR.union(textR); Insets insets = getInsets(); r.height += insets.top + insets.bottom; // Ensure that the height of the button is odd, // to allow for the focus line. if(r.height % 2 == 0) { r.height += 1; } float offAmt = fm.getMaxAscent() + insets.top; return offAmt/(float)r.height; } /** * Sets the text Font family for the activator text. * For JDK 1.1 this must a family name of Dialog, DialogInput, Monospaced, * Serif, SansSerif, or Symbol. */ public void setTextFontFamily(String family) { textAttribs.removeAttribute(StyleConstants.FontFamily); textAttribs.addAttribute(StyleConstants.FontFamily, family); setFont(getAttributeSetFont(textAttribs)); Font font = getFont(); } /** * Returns the text Font family name of the activator text */ public String getTextFontFamily() { return StyleConstants.getFontFamily(textAttribs); } /** * Sets the text size for the activator text. * The String size is a valid Cascading Style Sheet value for * text size. Acceptable values are as follows: *
        *
      • xx-small *
      • x-small *
      • small *
      • medium *
      • large *
      • x-large *
      • xx-large *
      • bigger - increase the current base font size by 1 *
      • smaller - decrease the current base font size by 1 *
      • xxpt - set the font size to a specific pt value of "xx" *
      • +x - increase the current base font size by a value of "x" *
      • -x - decrease the current base font size by a value of "x" *
      • x - set the font size to the point size associated with * the index "x" *
      */ public void setTextFontSize(String size) { int newsize, tmp; StyleSheet css = doc.getStyleSheet(); try { if (size.equals("xx-small")) { newsize = (int)css.getPointSize(0); } else if (size.equals("x-small")) { newsize = (int)css.getPointSize(1); } else if (size.equals("small")) { newsize = (int)css.getPointSize(2); } else if (size.equals("medium")) { newsize = (int)css.getPointSize(3); } else if (size.equals("large")) { newsize = (int)css.getPointSize(4); } else if (size.equals("x-large")) { newsize = (int)css.getPointSize(5); } else if (size.equals("xx-large")) { newsize = (int)css.getPointSize(6); } else if (size.equals("bigger")) { newsize = (int)css.getPointSize("+1"); } else if (size.equals("smaller")) { newsize = (int)css.getPointSize("-1"); } else if (size.endsWith("pt")) { String sz = size.substring(0, size.length() - 2); newsize = Integer.parseInt(sz); } else { newsize = (int) css.getPointSize(size); } } catch (NumberFormatException nfe) { return; } if (newsize == 0) { return; } textAttribs.removeAttribute(StyleConstants.FontSize); textAttribs.addAttribute(StyleConstants.FontSize, new Integer(newsize)); setFont(getAttributeSetFont(textAttribs)); Font font = getFont(); } /** * Returns the text Font family name of the activator text */ public String getTextFontSize() { return Integer.toString(StyleConstants.getFontSize(textAttribs)); } /** * Sets the text Font Weigth for the activator text. * Valid weights are *
        *
      • plain *
      • bold *
      */ public void setTextFontWeight(String weight) { boolean isBold=false; if (weight.compareTo("bold") == 0) { isBold = true; } else { isBold = false; } textAttribs.removeAttribute(StyleConstants.Bold); textAttribs.addAttribute(StyleConstants.Bold, new Boolean(isBold)); setFont(getAttributeSetFont(textAttribs)); Font font = getFont(); } /** * Returns the text Font weight of the activator text */ public String getTextFontWeight() { if (StyleConstants.isBold(textAttribs)) { return "bold"; } return "plain"; } /** * Sets the text Font Style for the activator text. * Valid font styles are *
        *
      • plain *
      • italic *
      */ public void setTextFontStyle(String style) { boolean isItalic=false; if (style.compareTo("italic") == 0) { isItalic = true; } else { isItalic = false; } textAttribs.removeAttribute(StyleConstants.Italic); textAttribs.addAttribute(StyleConstants.Italic, new Boolean(isItalic)); setFont(getAttributeSetFont(textAttribs)); Font font = getFont(); } /** * Returns the text Font style of the activator text */ public String getTextFontStyle() { if (StyleConstants.isItalic(textAttribs)) { return "italic"; } return "plain"; } /** * Sets the text Color for the activator text. * The following is a list of supported Color names *
        *
      • black *
      • blue *
      • cyan *
      • darkGray *
      • gray *
      • green *
      • lightGray *
      • magenta *
      • orange *
      • pink *
      • red *
      • white *
      • yellow *
      */ public void setTextColor(String name) { Color color=null; if (name.compareTo("black") == 0) { color = Color.black; } else if (name.compareTo("blue") == 0) { color = Color.blue; } else if (name.compareTo("cyan") == 0) { color = Color.cyan; } else if (name.compareTo("darkGray") == 0) { color = Color.darkGray; } else if (name.compareTo("gray") == 0) { color = Color.gray; } else if (name.compareTo("green") == 0) { color = Color.green; } else if (name.compareTo("lightGray") == 0) { color = Color.lightGray; } else if (name.compareTo("magenta") == 0) { color = Color.magenta; } else if (name.compareTo("orange") == 0) { color = Color.orange; } else if (name.compareTo("pink") == 0) { color = Color.pink; } else if (name.compareTo("red") == 0) { color = Color.red; } else if (name.compareTo("white") == 0) { color = Color.white; } else if (name.compareTo("yellow") == 0) { color = Color.yellow; } if (color == null) { return; } textAttribs.removeAttribute(StyleConstants.Foreground); textAttribs.addAttribute(StyleConstants.Foreground, color); setForeground(color); } /** * Returns the text Color of the activator text */ public String getTextColor() { Color color = getForeground(); return color.toString(); } /** * Gets the font from an attribute set. This is * implemented to try and fetch a cached font * for the given AttributeSet, and if that fails * the font features are resolved and the * font is fetched from the low-level font cache. * Font's are cached in the StyleSheet of a document * * @param attr the attribute set * @return the font */ private Font getAttributeSetFont(AttributeSet attr) { // PENDING(prinz) add cache behavior int style = Font.PLAIN; if (StyleConstants.isBold(attr)) { style |= Font.BOLD; } if (StyleConstants.isItalic(attr)) { style |= Font.ITALIC; } String family = StyleConstants.getFontFamily(attr); int size = StyleConstants.getFontSize(attr); /** * if either superscript or subscript is * is set, we need to reduce the font size * by 2. */ if (StyleConstants.isSuperscript(attr) || StyleConstants.isSubscript(attr)) { size -= 2; } // fonts are cached in the StyleSheet so use that return doc.getStyleSheet().getFont(family, style, size); } /** * Displays the viewer according to the viewerType */ public void actionPerformed(ActionEvent e) { URL link; try { link = new URL(content); } catch (MalformedURLException exc) { //XXX log something to ide.log?? return; } HtmlBrowser.URLDisplayer.getDefault().showURL(link); } }
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.