net.sourceforge.sillyview
Class HTMLPane

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.text.JTextComponent
                  extended by javax.swing.JEditorPane
                      extended by net.sourceforge.sillyview.HTMLPane
All Implemented Interfaces:
javax.accessibility.Accessible, java.awt.image.ImageObserver, java.awt.MenuContainer, javax.swing.Scrollable, java.io.Serializable
Direct Known Subclasses:
HTMLPaneView

public class HTMLPane
extends javax.swing.JEditorPane

This class is a convenience class for JEditorPanes used to show HTML content that are un-editable (so as to enable listening for hyperlink events) and handle UTF-8 characters gracefully. It wouldn't be strictly necessary were it not for some bugs in the Java 1.4.2 StyledDocument api (getlength()=0). This class keeps its own copy of the EditorPane's text to work around the getLength, and subsequent getText problems. Important to note is that a HTMLPane is aware of and responsive to Swing Pluggable LookAndFeel colors. JEditorPanes are ignorant of changes to LookAndFeel. You can plug in your own LookAndFeel to StyleSheet converter by subclassing LookAndFeelToCSS and using HTMLPane's setConverter() method. The default BasicLookAndFeelToCSS converter may well not handle your favorite LookAndFeel (Swing's default Metal LookAndFeel is supported) , and you may be forced to write a LookAndFeelToCSS converter for it. A "theme" stylesheet (the result of a conversion) does not override styles implemented directly in the HTML document or included in the HTML document. It does override all other styles.

Version:
1.0
Author:
T.J. Willis
See Also:
Serialized Form

Nested Class Summary
 
Nested classes inherited from class javax.swing.JEditorPane
javax.swing.JEditorPane.AccessibleJEditorPane, javax.swing.JEditorPane.AccessibleJEditorPaneHTML, javax.swing.JEditorPane.JEditorPaneAccessibleHypertextSupport
 
Nested classes inherited from class javax.swing.text.JTextComponent
javax.swing.text.JTextComponent.AccessibleJTextComponent, javax.swing.text.JTextComponent.KeyBinding
 
Nested classes inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Nested classes inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
private  boolean autodump
           
private  Category cat
           
private  LookAndFeelToCSS converter
           
private  java.lang.String dString
           
private  boolean initialized
           
private  javax.swing.LookAndFeel lastLookAndFeel
           
private  javax.swing.plaf.TextUI lastUI
           
 
Fields inherited from class javax.swing.text.JTextComponent
DEFAULT_KEYMAP, FOCUS_ACCELERATOR_KEY
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
HTMLPane()
          Creates a new HTMLPane instance with no text.
HTMLPane(java.lang.String text)
          Creates a new HTMLPane instance with the given text.
HTMLPane(java.lang.String text, javax.swing.text.html.StyleSheet theme)
          Creates a new HTMLPane instance with the given text and stylesheet.
HTMLPane(javax.swing.text.html.StyleSheet theme)
          Creates a new HTMLPane instance with the given stylesheet.
HTMLPane(java.net.URL initialPage)
          Creates a new HTMLPane instance showing the HTML page at the given URL and with no accessory stylesheet.
HTMLPane(java.net.URL initialPage, javax.swing.text.html.StyleSheet theme)
          Creates a new HTMLPane instance showing the HTML page at the given URL and with the given accesory stylesheet.
 
Method Summary
 void addHyperlinkListener(javax.swing.event.HyperlinkListener l)
          Add a HyperlinkListener to this pane.
private  void bugFixSetText(java.lang.String text)
          Sets the text to the given value, tearing down the JEditorPane's backing model, which prevents peculiarities.
 void doLayout()
          Tries to do super.doLayout() and warns if this causes an exception.
 void dump()
          Dumps the current stylesheet for debugging.
 void dump(javax.swing.text.html.StyleSheet styles)
          Sends all style rules to be printed out to System.out.
 LookAndFeelToCSS getConverter()
          Describe getConverter method here.
private static java.lang.String getDocument(java.lang.String documentURL)
          Returns the HTML document at the given URL as a String.
protected static java.lang.String getDocument(java.net.URL u)
          Returns the HTML document at the given URL as a String.
protected  javax.swing.text.html.StyleSheet getDocumentStyleSheet()
          Returns the stylesheet for the current document.
private  javax.swing.text.html.HTMLEditorKit getHTMLEditorKit()
          Returns the editorkit if it is a HTMLEditorKit, or null otherwise.
private  javax.swing.text.html.StyleSheet getStyleSheet(javax.swing.LookAndFeel mlf)
           
private  void init()
          Sets the pane to non-editable, the content type to "text/html; UTF-8" and deactivates "autoformsubmission" so that HyperlinkListeners can handle form submission events.
 void setAutoDump(boolean b)
          Setting autoDump to true causes the stylesheet to be printed every time it is changed.
 void setConverter(LookAndFeelToCSS conv)
          Allows you to plug in a LookAndFeel to CSS converter of your choice.
 void setText(java.lang.String text)
          Sets the text and clears and clears any applied "theme" stylesheets.
private  void setText(java.net.URL textURL)
          Sets the text to the HTML document at the given URL.
private  void setTextUrl(java.lang.String textUrl)
          Sets the text to the HTML document at the given URL.
protected  void setTheme(java.lang.String styleSheetUrl)
          Adds the stylesheet at the given url as a "theme," i.e.
private  void setTheme(javax.swing.text.html.StyleSheet s2)
          Adds the stylesheet at the given url as a "theme," i.e.
private  void setTheme(java.net.URL styleSheetUrl)
          Adds the stylesheet at the given url as a "theme," i.e.
 void setUI(javax.swing.plaf.TextUI newui)
          Describe setUI method here.
 
Methods inherited from class javax.swing.JEditorPane
createDefaultEditorKit, createEditorKitForContentType, fireHyperlinkUpdate, getAccessibleContext, getContentType, getEditorKit, getEditorKitClassNameForContentType, getEditorKitForContentType, getHyperlinkListeners, getPage, getPreferredSize, getScrollableTracksViewportHeight, getScrollableTracksViewportWidth, getStream, getText, getUIClassID, paramString, read, registerEditorKitForContentType, registerEditorKitForContentType, removeHyperlinkListener, replaceSelection, scrollToReference, setContentType, setEditorKit, setEditorKitForContentType, setPage, setPage
 
Methods inherited from class javax.swing.text.JTextComponent
addCaretListener, addInputMethodListener, addKeymap, copy, cut, fireCaretUpdate, getActions, getCaret, getCaretColor, getCaretListeners, getCaretPosition, getDisabledTextColor, getDocument, getDragEnabled, getFocusAccelerator, getHighlighter, getInputMethodRequests, getKeymap, getKeymap, getMargin, getNavigationFilter, getPreferredScrollableViewportSize, getScrollableBlockIncrement, getScrollableUnitIncrement, getSelectedText, getSelectedTextColor, getSelectionColor, getSelectionEnd, getSelectionStart, getText, getToolTipText, getUI, isEditable, loadKeymap, modelToView, moveCaretPosition, paste, processInputMethodEvent, read, removeCaretListener, removeKeymap, removeNotify, select, selectAll, setCaret, setCaretColor, setCaretPosition, setComponentOrientation, setDisabledTextColor, setDocument, setDragEnabled, setEditable, setFocusAccelerator, setHighlighter, setKeymap, setMargin, setNavigationFilter, setSelectedTextColor, setSelectionColor, setSelectionEnd, setSelectionStart, updateUI, viewToModel, write
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

dString

private java.lang.String dString

converter

private LookAndFeelToCSS converter

initialized

private volatile boolean initialized

cat

private Category cat

autodump

private boolean autodump

lastLookAndFeel

private volatile javax.swing.LookAndFeel lastLookAndFeel

lastUI

private volatile javax.swing.plaf.TextUI lastUI
Constructor Detail

HTMLPane

public HTMLPane()
Creates a new HTMLPane instance with no text.


HTMLPane

public HTMLPane(java.lang.String text)
Creates a new HTMLPane instance with the given text. Default text encoding is "text/html; UTF-8".

Parameters:
text - a String value

HTMLPane

public HTMLPane(javax.swing.text.html.StyleSheet theme)
Creates a new HTMLPane instance with the given stylesheet. Default text encoding is "text/html; UTF-8".

Parameters:
theme - a StyleSheet value

HTMLPane

public HTMLPane(java.lang.String text,
                javax.swing.text.html.StyleSheet theme)
Creates a new HTMLPane instance with the given text and stylesheet. Default text encoding is "text/html; UTF-8".

Parameters:
text - a String value
theme - a StyleSheet value

HTMLPane

public HTMLPane(java.net.URL initialPage)
         throws java.io.IOException
Creates a new HTMLPane instance showing the HTML page at the given URL and with no accessory stylesheet. Default text encoding is "text/html; UTF-8".

Parameters:
initialPage - an URL value
Throws:
java.io.IOException - if an error occurs

HTMLPane

public HTMLPane(java.net.URL initialPage,
                javax.swing.text.html.StyleSheet theme)
         throws java.io.IOException
Creates a new HTMLPane instance showing the HTML page at the given URL and with the given accesory stylesheet. Default text encoding is "text/html; UTF-8".

Parameters:
initialPage - an URL value
theme - a StyleSheet value
Throws:
java.io.IOException - if an error occurs
Method Detail

init

private final void init()
Sets the pane to non-editable, the content type to "text/html; UTF-8" and deactivates "autoformsubmission" so that HyperlinkListeners can handle form submission events.


getDocumentStyleSheet

protected javax.swing.text.html.StyleSheet getDocumentStyleSheet()
Returns the stylesheet for the current document.

Returns:
a StyleSheet value

getDocument

private static java.lang.String getDocument(java.lang.String documentURL)
                                     throws java.net.MalformedURLException,
                                            java.io.IOException
Returns the HTML document at the given URL as a String.

Parameters:
documentURL - a String value
Returns:
a String value
Throws:
java.net.MalformedURLException - if an error occurs
java.io.IOException - if an error occurs

getDocument

protected static java.lang.String getDocument(java.net.URL u)
                                       throws java.io.IOException
Returns the HTML document at the given URL as a String.

Parameters:
u - an URL value
Returns:
a String value
Throws:
java.io.IOException - if an error occurs

setTheme

protected void setTheme(java.lang.String styleSheetUrl)
                 throws java.net.MalformedURLException
Adds the stylesheet at the given url as a "theme," i.e. it will not override rules in the document's own stylesheet.

Parameters:
styleSheetUrl - a String value
Throws:
java.net.MalformedURLException - if an error occurs

setTheme

private void setTheme(java.net.URL styleSheetUrl)
Adds the stylesheet at the given url as a "theme," i.e. it will not override rules in the document's own stylesheet.

Parameters:
styleSheetUrl - an URL value

setTheme

private void setTheme(javax.swing.text.html.StyleSheet s2)
Adds the stylesheet at the given url as a "theme," i.e. it will not override rules in the document's own stylesheet.

Parameters:
s2 - a StyleSheet value

getHTMLEditorKit

private javax.swing.text.html.HTMLEditorKit getHTMLEditorKit()
Returns the editorkit if it is a HTMLEditorKit, or null otherwise.


bugFixSetText

private final void bugFixSetText(java.lang.String text)
Sets the text to the given value, tearing down the JEditorPane's backing model, which prevents peculiarities. See the documentation for JEditorPane.setText() for more information.


setText

public final void setText(java.lang.String text)
Sets the text and clears and clears any applied "theme" stylesheets.

Overrides:
setText in class javax.swing.JEditorPane
Parameters:
text - a String value

setTextUrl

private void setTextUrl(java.lang.String textUrl)
                 throws java.net.MalformedURLException,
                        java.io.IOException
Sets the text to the HTML document at the given URL.

Parameters:
textUrl - a String value
Throws:
java.net.MalformedURLException - if an error occurs
java.io.IOException - if an error occurs

setText

private void setText(java.net.URL textURL)
              throws java.io.IOException
Sets the text to the HTML document at the given URL.

Parameters:
textURL - an URL value
Throws:
java.io.IOException - if an error occurs

dump

public final void dump()
Dumps the current stylesheet for debugging.


setAutoDump

public void setAutoDump(boolean b)
Setting autoDump to true causes the stylesheet to be printed every time it is changed.

Parameters:
b - a boolean value

dump

public final void dump(javax.swing.text.html.StyleSheet styles)
Sends all style rules to be printed out to System.out.

Parameters:
styles - a StyleSheet value

setUI

public void setUI(javax.swing.plaf.TextUI newui)
Describe setUI method here.

Overrides:
setUI in class javax.swing.text.JTextComponent
Parameters:
newui - a javax.swing.plaf.TextUI value

getConverter

public final LookAndFeelToCSS getConverter()
Describe getConverter method here.

Returns:
a LookAndFeelToCSS value

setConverter

public final void setConverter(LookAndFeelToCSS conv)
Allows you to plug in a LookAndFeel to CSS converter of your choice.

Parameters:
conv - a LookAndFeelToCSS value

getStyleSheet

private javax.swing.text.html.StyleSheet getStyleSheet(javax.swing.LookAndFeel mlf)

addHyperlinkListener

public void addHyperlinkListener(javax.swing.event.HyperlinkListener l)
Add a HyperlinkListener to this pane. This allows you to handle hyperlink events as well as form submission events.

Overrides:
addHyperlinkListener in class javax.swing.JEditorPane
Parameters:
l - a HyperlinkListener value

doLayout

public void doLayout()
Tries to do super.doLayout() and warns if this causes an exception.

Overrides:
doLayout in class java.awt.Container