edu.northwestern.at.wordhoard.swing.text
Class WrappedTextComponent

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by edu.northwestern.at.wordhoard.swing.text.WrappedTextComponent
All Implemented Interfaces:
PrintableContents, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, javax.swing.Scrollable
Direct Known Subclasses:
PartTextComponent

public class WrappedTextComponent
extends javax.swing.JComponent
implements javax.swing.Scrollable, PrintableContents

A wrapped text component.

A wrapped text component displays word-wrapped multilingual styled text with support for optional text selection and scrolling.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
 
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
WrappedTextComponent()
          Creates a new empty wrapped text component.
WrappedTextComponent(java.lang.String str, java.awt.Font font, int rightMargin)
          Creates a new plain (unstyled) wrapped text component.
WrappedTextComponent(Text text, int rightMargin)
          Creates a new wrapped text component.
 
Method Summary
 void addSelectionObserver(java.util.Observer o)
          Adds a selection observer.
 void clear()
          Clears the text.
 void copy()
          Copies the selected text to the system clipboard.
 TextLocation getLocationOfPoint(java.awt.Point p, boolean round)
          Gets the location of a point.
 java.awt.Dimension getMaximumSize()
          Gets the maximum size of the component.
 java.awt.Dimension getMinimumSize()
          Gets the minimum size of the component.
 java.awt.Dimension getPreferredScrollableViewportSize()
          Gets the preferred size of the viewport.
 java.awt.Dimension getPreferredSize()
          Gets the preferred size of the component.
 PrintableComponent getPrintableComponent(java.lang.String title, java.awt.print.PageFormat pageFormat)
          Return printable component.
 int getScrollableBlockIncrement(java.awt.Rectangle visibleRect, int orientation, int direction)
          Gets the block increment for scrolling.
 boolean getScrollableTracksViewportHeight()
          Returns true if a viewport should always force the height of this component to match the height of the viewport.
 boolean getScrollableTracksViewportWidth()
          Returns true if a viewport should always force the width of this component to match the width of the viewport.
 int getScrollableUnitIncrement(java.awt.Rectangle visibleRect, int orientation, int direction)
          Gets the unit increment for scrolling.
 TextRange getSelection()
          Gets the selection.
 javax.swing.JViewport getViewport()
          Gets the parent viewport.
 void handleMarkerClick(TextRange range)
          Handles a marker click.
 void paint(java.awt.Graphics graphics)
          Paints the component.
protected  void paintComponent(java.awt.Graphics graphics)
          Paints the component.
 void printContents(java.lang.String title, java.awt.print.PrinterJob printerJob, java.awt.print.PageFormat pageFormat)
          Prints the text,
 void scrollTo(TextRange range, boolean center)
          Scrolls to and selects a range of text.
 void selectAll()
          Selects all the text.
 void setLineNumberInterval(int lineNumberInterval)
          Sets the line number interval.
 void setMarkers(TextLocation[] markerLocations)
          Sets or clears markers.
 void setSelectable(boolean selectable)
          Sets the selectable option.
 void setSelection(TextRange selection)
          Sets the selection.
 void setText(Text text, DrawingContext context)
          Sets the text.
 void setText(Text text, DrawingContext context, TextLocation[] markerLocations)
          Sets the text.
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getAccessibleContext, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getNextFocusableComponent, getPopupLocation, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paintBorder, paintChildren, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, 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, updateUI
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, 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, addInputMethodListener, 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, getInputMethodRequests, 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, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, 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
 

Constructor Detail

WrappedTextComponent

public WrappedTextComponent()
Creates a new empty wrapped text component.


WrappedTextComponent

public WrappedTextComponent(Text text,
                            int rightMargin)
Creates a new wrapped text component.

Font information for the runs and line widths must be set on entry for the unwrapped text.

Parameters:
text - Unwrapped text.
rightMargin - Right margin in pixels.

WrappedTextComponent

public WrappedTextComponent(java.lang.String str,
                            java.awt.Font font,
                            int rightMargin)
Creates a new plain (unstyled) wrapped text component.

Parameters:
str - Text.
font - Font.
rightMargin - Right margin.
Method Detail

setText

public void setText(Text text,
                    DrawingContext context,
                    TextLocation[] markerLocations)
Sets the text.

Font information for the runs and line widths must be set on entry for the unwrapped text.

Parameters:
text - Unwrapped text.
context - Drawing context.
markerLocations - Marker locations in base coordinates, or null if none.

setText

public void setText(Text text,
                    DrawingContext context)
Sets the text.

Font information for the runs and line widths must be set on entry for the unwrapped text.

No lines are marked.

Parameters:
text - Unwrapped text.
context - Drawing context.

setSelectable

public void setSelectable(boolean selectable)
Sets the selectable option.

Parameters:
selectable - True if text selection is permitted. The default value is false.

clear

public void clear()
Clears the text.


getSelection

public TextRange getSelection()
Gets the selection.

Returns:
The selection in base coordinates, or null if none.

setSelection

public void setSelection(TextRange selection)
Sets the selection.

Parameters:
selection - The selection in base coordinates, or null if none.

selectAll

public void selectAll()
Selects all the text.

Throws:
java.lang.Exception

copy

public void copy()
Copies the selected text to the system clipboard.


scrollTo

public void scrollTo(TextRange range,
                     boolean center)
Scrolls to and selects a range of text.

Parameters:
range - The text range in base coordinates.
center - If true, center the range in the view, even if it is already visible. If false, center the range only if it is not already visible.

addSelectionObserver

public void addSelectionObserver(java.util.Observer o)
Adds a selection observer.

The observer is notified whenever the selection changes.

Parameters:
o - Observer.

setLineNumberInterval

public void setLineNumberInterval(int lineNumberInterval)
Sets the line number interval.

The line number interval controls the display of line numbers. 0 = no line numbers, 1 = every line numbered, n = every n'th line numbered, -1 = stanza numbers.

Parameters:
lineNumberInterval - The new line number interval.

setMarkers

public void setMarkers(TextLocation[] markerLocations)
Sets or clears markers.

Parameters:
markerLocations - Marker locations in base coordinates, or null to clear markers.

getLocationOfPoint

public TextLocation getLocationOfPoint(java.awt.Point p,
                                       boolean round)
Gets the location of a point.

Parameters:
p - Point.
round - If true, location is rounded to nearest location in the text.
Returns:
Location in base coordinates.

getPreferredSize

public java.awt.Dimension getPreferredSize()
Gets the preferred size of the component.

Overrides:
getPreferredSize in class javax.swing.JComponent
Returns:
Preferred size of the component.

getMinimumSize

public java.awt.Dimension getMinimumSize()
Gets the minimum size of the component.

Overrides:
getMinimumSize in class javax.swing.JComponent
Returns:
Minimum size of the component.

getMaximumSize

public java.awt.Dimension getMaximumSize()
Gets the maximum size of the component.

Overrides:
getMaximumSize in class javax.swing.JComponent
Returns:
Maximum size of the component.

paintComponent

protected void paintComponent(java.awt.Graphics graphics)
Paints the component.

Overrides:
paintComponent in class javax.swing.JComponent
Parameters:
graphics - Graphics environment.

paint

public void paint(java.awt.Graphics graphics)
Paints the component.

Overrides:
paint in class javax.swing.JComponent
Parameters:
graphics - Graphics environment.

handleMarkerClick

public void handleMarkerClick(TextRange range)
                       throws java.lang.Exception
Handles a marker click.

Subclasses may override this method to handle clicks on markers. The default action is to do nothing.

Parameters:
range - Text range of line containing marker clicked, in base coordinates.
Throws:
java.lang.Exception

getViewport

public javax.swing.JViewport getViewport()
Gets the parent viewport.

Returns:
The parent viewport, or null if none.

getPreferredScrollableViewportSize

public java.awt.Dimension getPreferredScrollableViewportSize()
Gets the preferred size of the viewport.

Specified by:
getPreferredScrollableViewportSize in interface javax.swing.Scrollable
Returns:
Preferred size of the viewport.

getScrollableUnitIncrement

public int getScrollableUnitIncrement(java.awt.Rectangle visibleRect,
                                      int orientation,
                                      int direction)
Gets the unit increment for scrolling.

Specified by:
getScrollableUnitIncrement in interface javax.swing.Scrollable
Parameters:
visibleRect - The view area visible within the viewport.
orientation - SwingConstants.VERTICAL or SwingConstants.HORIZONTAL.
direction - Less than zero to scroll up/left, greater than zero for down/right.
Returns:
The unit increment = the height of one line of nominal font size text (for vertical scrolling), or 0 (for horizontal scrolling, which is unused).

getScrollableBlockIncrement

public int getScrollableBlockIncrement(java.awt.Rectangle visibleRect,
                                       int orientation,
                                       int direction)
Gets the block increment for scrolling.

Specified by:
getScrollableBlockIncrement in interface javax.swing.Scrollable
Parameters:
visibleRect - The view area visible within the viewport.
orientation - SwingConstants.VERTICAL or SwingConstants.HORIZONTAL.
direction - Less than zero to scroll up/left, greater than zero for down/right.
Returns:
The block increment = the height of the visible rect minus the height of one line of nominal font size text (for vertical scrolling), or 0 (for horizontal scrolling, which is unused).

getScrollableTracksViewportWidth

public boolean getScrollableTracksViewportWidth()
Returns true if a viewport should always force the width of this component to match the width of the viewport.

Specified by:
getScrollableTracksViewportWidth in interface javax.swing.Scrollable
Returns:
false.

getScrollableTracksViewportHeight

public boolean getScrollableTracksViewportHeight()
Returns true if a viewport should always force the height of this component to match the height of the viewport.

Specified by:
getScrollableTracksViewportHeight in interface javax.swing.Scrollable
Returns:
false.

printContents

public void printContents(java.lang.String title,
                          java.awt.print.PrinterJob printerJob,
                          java.awt.print.PageFormat pageFormat)
Prints the text,

Parameters:
title - Title for output.
printerJob - The printer job.
pageFormat - The printer page format.

getPrintableComponent

public PrintableComponent getPrintableComponent(java.lang.String title,
                                                java.awt.print.PageFormat pageFormat)
Return printable component.

Specified by:
getPrintableComponent in interface PrintableContents
Parameters:
title - Title for printing.
pageFormat - Page format for printing.
Returns:
Printable component for XList.