Groovy Documentation

com.wordpress.tipsforjava.swing
[Java] Class TextLineNumber

java.lang.Object
  java.awt.Component
      java.awt.Container
          javax.swing.JComponent
              javax.swing.JPanel
                  com.wordpress.tipsforjava.swing.TextLineNumber
All Implemented Interfaces:
CaretListener, DocumentListener, PropertyChangeListener

public class TextLineNumber
extends JPanel

This class will display line numbers for a related text component. The text component must use the same line height for each line. TextLineNumber supports wrapped lines and will highlight the line number of the current line in the text component. This class was designed to be used as a component added to the row header of a JScrollPane.

Authors:
Rob Camick
Darryl Burke


Field Summary
static float CENTER

private static int HEIGHT

static float LEFT

private static Border OUTER

static float RIGHT

private int borderGap

private JTextComponent component

private Color currentLineForeground

private float digitAlignment

private HashMap fonts

private int lastDigits

private int lastHeight

private int lastLine

private int minimumDisplayDigits

private boolean updateFont

 
Fields inherited from class JPanel
WHEN_FOCUSED, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_IN_FOCUSED_WINDOW, UNDEFINED_CONDITION, TOOL_TIP_TEXT_KEY, TOP_ALIGNMENT, CENTER_ALIGNMENT, BOTTOM_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, WIDTH, HEIGHT, PROPERTIES, SOMEBITS, FRAMEBITS, ALLBITS, ERROR, ABORT
 
Fields inherited from class JComponent
WHEN_FOCUSED, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_IN_FOCUSED_WINDOW, UNDEFINED_CONDITION, TOOL_TIP_TEXT_KEY, TOP_ALIGNMENT, CENTER_ALIGNMENT, BOTTOM_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, WIDTH, HEIGHT, PROPERTIES, SOMEBITS, FRAMEBITS, ALLBITS, ERROR, ABORT
 
Constructor Summary
TextLineNumber(JTextComponent component)

Create a line number component for a text component.

TextLineNumber(JTextComponent component, int minimumDisplayDigits)

Create a line number component for a text component.

 
Method Summary
void caretUpdate(CaretEvent e)

void changedUpdate(DocumentEvent e)

private void documentChanged()

int getBorderGap()

Gets the border gap

Color getCurrentLineForeground()

Gets the current line rendering Color

float getDigitAlignment()

Gets the digit alignment

int getMinimumDisplayDigits()

Gets the minimum display digits

private int getOffsetX(int availableWidth, int stringWidth)

private int getOffsetY(int rowStartOffset, FontMetrics fontMetrics)

protected String getTextLineNumber(int rowStartOffset)

boolean getUpdateFont()

Gets the update font property

void insertUpdate(DocumentEvent e)

private boolean isCurrentLine(int rowStartOffset)

void paintComponent(Graphics g)

Draw the line numbers

void propertyChange(PropertyChangeEvent evt)

void removeUpdate(DocumentEvent e)

void setBorderGap(int borderGap)

The border gap is used in calculating the left and right insets of the border.

void setCurrentLineForeground(Color currentLineForeground)

The Color used to render the current line digits.

void setDigitAlignment(float digitAlignment)

Specify the horizontal alignment of the digits within the component.

void setMinimumDisplayDigits(int minimumDisplayDigits)

Specify the mimimum number of digits used to calculate the preferred width of the component.

private void setPreferredWidth()

Calculate the width needed to display the maximum line number

void setUpdateFont(boolean updateFont)

Set the update font property.

 
Methods inherited from class JPanel
getAccessibleContext, setUI, updateUI, getUIClassID, getUI, contains, getLocation, print, getSize, isOpaque, enable, disable, update, getY, getX, getInsets, getInsets, setFont, getPreferredSize, getMinimumSize, getMaximumSize, getAlignmentX, getAlignmentY, paint, getListeners, addNotify, removeNotify, setFocusTraversalKeys, requestFocus, requestFocus, firePropertyChange, firePropertyChange, firePropertyChange, setEnabled, isDoubleBuffered, setVisible, setForeground, setBackground, getBounds, reshape, getWidth, getHeight, setPreferredSize, setMinimumSize, setMaximumSize, getBaseline, getBaselineResizeBehavior, getGraphics, getFontMetrics, repaint, repaint, printAll, requestFocusInWindow, addAncestorListener, setInheritsPopupMenu, getInheritsPopupMenu, setComponentPopupMenu, getComponentPopupMenu, isPaintingTile, isPaintingForPrint, isManagingFocus, setNextFocusableComponent, getNextFocusableComponent, setRequestFocusEnabled, isRequestFocusEnabled, grabFocus, setVerifyInputWhenFocusTarget, getVerifyInputWhenFocusTarget, setBorder, getBorder, setAlignmentY, setAlignmentX, setInputVerifier, getInputVerifier, setDebugGraphicsOptions, getDebugGraphicsOptions, registerKeyboardAction, registerKeyboardAction, unregisterKeyboardAction, getRegisteredKeyStrokes, getConditionForKeyStroke, getActionForKeyStroke, resetKeyboardActions, setInputMap, getInputMap, getInputMap, setActionMap, getActionMap, requestDefaultFocus, getDefaultLocale, setDefaultLocale, setToolTipText, getToolTipText, getToolTipText, getToolTipLocation, getPopupLocation, createToolTip, scrollRectToVisible, setAutoscrolls, getAutoscrolls, setTransferHandler, getTransferHandler, getClientProperty, putClientProperty, isLightweightComponent, setOpaque, computeVisibleRect, getVisibleRect, addVetoableChangeListener, removeVetoableChangeListener, getVetoableChangeListeners, getTopLevelAncestor, removeAncestorListener, getAncestorListeners, revalidate, isValidateRoot, isOptimizedDrawingEnabled, paintImmediately, paintImmediately, setDoubleBuffered, getRootPane, add, add, add, add, add, remove, remove, list, list, removeAll, getComponent, invalidate, validate, getComponentCount, countComponents, getComponents, insets, setComponentZOrder, getComponentZOrder, getLayout, setLayout, doLayout, layout, preferredSize, minimumSize, paintComponents, printComponents, addContainerListener, removeContainerListener, getContainerListeners, deliverEvent, getComponentAt, getComponentAt, locate, getMousePosition, findComponentAt, findComponentAt, isAncestorOf, getFocusTraversalKeys, areFocusTraversalKeysSet, isFocusCycleRoot, isFocusCycleRoot, transferFocusBackward, setFocusTraversalPolicy, getFocusTraversalPolicy, isFocusTraversalPolicySet, setFocusCycleRoot, setFocusTraversalPolicyProvider, isFocusTraversalPolicyProvider, transferFocusDownCycle, applyComponentOrientation, addPropertyChangeListener, addPropertyChangeListener, add, toString, getName, contains, size, getParent, getLocation, remove, setName, list, list, list, getSize, resize, resize, setSize, setSize, enable, location, isValid, setLocale, action, getMousePosition, getGraphicsConfiguration, dispatchEvent, isVisible, isEnabled, isDisplayable, isFocusable, getTreeLock, isLightweight, getPeer, isFocusOwner, transferFocus, getFont, isPreferredSizeSet, isMinimumSizeSet, isMaximumSizeSet, isShowing, handleEvent, postEvent, getFocusCycleRootAncestor, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, setDropTarget, getDropTarget, getToolkit, enableInputMethods, show, show, hide, getForeground, isForegroundSet, getBackground, isBackgroundSet, isFontSet, getLocale, getColorModel, getLocationOnScreen, setLocation, setLocation, move, getBounds, bounds, setBounds, setBounds, setCursor, getCursor, isCursorSet, paintAll, repaint, repaint, repaint, imageUpdate, createImage, createImage, createVolatileImage, createVolatileImage, prepareImage, prepareImage, checkImage, checkImage, setIgnoreRepaint, getIgnoreRepaint, inside, addComponentListener, removeComponentListener, getComponentListeners, addFocusListener, removeFocusListener, getFocusListeners, addHierarchyListener, removeHierarchyListener, getHierarchyListeners, addHierarchyBoundsListener, removeHierarchyBoundsListener, getHierarchyBoundsListeners, addKeyListener, removeKeyListener, getKeyListeners, addMouseListener, removeMouseListener, getMouseListeners, addMouseMotionListener, removeMouseMotionListener, getMouseMotionListeners, addMouseWheelListener, removeMouseWheelListener, getMouseWheelListeners, addInputMethodListener, removeInputMethodListener, getInputMethodListeners, getInputMethodRequests, getInputContext, mouseDown, mouseDrag, mouseUp, mouseMove, mouseEnter, mouseExit, keyDown, keyUp, gotFocus, lostFocus, isFocusTraversable, setFocusable, setFocusTraversalKeysEnabled, getFocusTraversalKeysEnabled, nextFocus, transferFocusUpCycle, hasFocus, removePropertyChangeListener, removePropertyChangeListener, getPropertyChangeListeners, getPropertyChangeListeners, setComponentOrientation, getComponentOrientation, wait, wait, wait, equals, hashCode, getClass, notify, notifyAll
 
Methods inherited from class JComponent
contains, getLocation, print, getSize, isOpaque, enable, disable, update, getY, getX, getInsets, getInsets, setFont, getPreferredSize, getMinimumSize, getMaximumSize, getAlignmentX, getAlignmentY, paint, getListeners, addNotify, removeNotify, setFocusTraversalKeys, requestFocus, requestFocus, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleContext, setEnabled, isDoubleBuffered, setVisible, setForeground, setBackground, getBounds, reshape, getWidth, getHeight, setPreferredSize, setMinimumSize, setMaximumSize, getBaseline, getBaselineResizeBehavior, getGraphics, getFontMetrics, repaint, repaint, printAll, requestFocusInWindow, updateUI, getUIClassID, addAncestorListener, setInheritsPopupMenu, getInheritsPopupMenu, setComponentPopupMenu, getComponentPopupMenu, isPaintingTile, isPaintingForPrint, isManagingFocus, setNextFocusableComponent, getNextFocusableComponent, setRequestFocusEnabled, isRequestFocusEnabled, grabFocus, setVerifyInputWhenFocusTarget, getVerifyInputWhenFocusTarget, setBorder, getBorder, setAlignmentY, setAlignmentX, setInputVerifier, getInputVerifier, setDebugGraphicsOptions, getDebugGraphicsOptions, registerKeyboardAction, registerKeyboardAction, unregisterKeyboardAction, getRegisteredKeyStrokes, getConditionForKeyStroke, getActionForKeyStroke, resetKeyboardActions, setInputMap, getInputMap, getInputMap, setActionMap, getActionMap, requestDefaultFocus, getDefaultLocale, setDefaultLocale, setToolTipText, getToolTipText, getToolTipText, getToolTipLocation, getPopupLocation, createToolTip, scrollRectToVisible, setAutoscrolls, getAutoscrolls, setTransferHandler, getTransferHandler, getClientProperty, putClientProperty, isLightweightComponent, setOpaque, computeVisibleRect, getVisibleRect, addVetoableChangeListener, removeVetoableChangeListener, getVetoableChangeListeners, getTopLevelAncestor, removeAncestorListener, getAncestorListeners, revalidate, isValidateRoot, isOptimizedDrawingEnabled, paintImmediately, paintImmediately, setDoubleBuffered, getRootPane, add, add, add, add, add, remove, remove, list, list, removeAll, getComponent, invalidate, validate, getComponentCount, countComponents, getComponents, insets, setComponentZOrder, getComponentZOrder, getLayout, setLayout, doLayout, layout, preferredSize, minimumSize, paintComponents, printComponents, addContainerListener, removeContainerListener, getContainerListeners, deliverEvent, getComponentAt, getComponentAt, locate, getMousePosition, findComponentAt, findComponentAt, isAncestorOf, getFocusTraversalKeys, areFocusTraversalKeysSet, isFocusCycleRoot, isFocusCycleRoot, transferFocusBackward, setFocusTraversalPolicy, getFocusTraversalPolicy, isFocusTraversalPolicySet, setFocusCycleRoot, setFocusTraversalPolicyProvider, isFocusTraversalPolicyProvider, transferFocusDownCycle, applyComponentOrientation, addPropertyChangeListener, addPropertyChangeListener, add, toString, getName, contains, size, getParent, getLocation, remove, setName, list, list, list, getSize, resize, resize, setSize, setSize, enable, location, isValid, setLocale, action, getMousePosition, getGraphicsConfiguration, dispatchEvent, isVisible, isEnabled, isDisplayable, isFocusable, getTreeLock, isLightweight, getPeer, isFocusOwner, transferFocus, getFont, isPreferredSizeSet, isMinimumSizeSet, isMaximumSizeSet, isShowing, handleEvent, postEvent, getFocusCycleRootAncestor, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, setDropTarget, getDropTarget, getToolkit, enableInputMethods, show, show, hide, getForeground, isForegroundSet, getBackground, isBackgroundSet, isFontSet, getLocale, getColorModel, getLocationOnScreen, setLocation, setLocation, move, getBounds, bounds, setBounds, setBounds, setCursor, getCursor, isCursorSet, paintAll, repaint, repaint, repaint, imageUpdate, createImage, createImage, createVolatileImage, createVolatileImage, prepareImage, prepareImage, checkImage, checkImage, setIgnoreRepaint, getIgnoreRepaint, inside, addComponentListener, removeComponentListener, getComponentListeners, addFocusListener, removeFocusListener, getFocusListeners, addHierarchyListener, removeHierarchyListener, getHierarchyListeners, addHierarchyBoundsListener, removeHierarchyBoundsListener, getHierarchyBoundsListeners, addKeyListener, removeKeyListener, getKeyListeners, addMouseListener, removeMouseListener, getMouseListeners, addMouseMotionListener, removeMouseMotionListener, getMouseMotionListeners, addMouseWheelListener, removeMouseWheelListener, getMouseWheelListeners, addInputMethodListener, removeInputMethodListener, getInputMethodListeners, getInputMethodRequests, getInputContext, mouseDown, mouseDrag, mouseUp, mouseMove, mouseEnter, mouseExit, keyDown, keyUp, gotFocus, lostFocus, isFocusTraversable, setFocusable, setFocusTraversalKeysEnabled, getFocusTraversalKeysEnabled, nextFocus, transferFocusUpCycle, hasFocus, removePropertyChangeListener, removePropertyChangeListener, getPropertyChangeListeners, getPropertyChangeListeners, setComponentOrientation, getComponentOrientation, wait, wait, wait, equals, hashCode, getClass, notify, notifyAll
 

Field Detail

CENTER

public static final float CENTER


HEIGHT

private static final int HEIGHT


LEFT

public static final float LEFT


OUTER

private static final Border OUTER


RIGHT

public static final float RIGHT


borderGap

private int borderGap


component

private JTextComponent component


currentLineForeground

private Color currentLineForeground


digitAlignment

private float digitAlignment


fonts

private HashMap fonts


lastDigits

private int lastDigits


lastHeight

private int lastHeight


lastLine

private int lastLine


minimumDisplayDigits

private int minimumDisplayDigits


updateFont

private boolean updateFont


 
Constructor Detail

TextLineNumber

public TextLineNumber(JTextComponent component)
Create a line number component for a text component. This minimum display width will be based on 3 digits.
Parameters:
component - the related text component


TextLineNumber

public TextLineNumber(JTextComponent component, int minimumDisplayDigits)
Create a line number component for a text component.
Parameters:
component - the related text component
minimumDisplayDigits - the number of digits used to calculate the minimum width of the component


 
Method Detail

caretUpdate

de
	public
public void caretUpdate(CaretEvent e)


changedUpdate

de
	public
public void changedUpdate(DocumentEvent e)


documentChanged

private void documentChanged()


getBorderGap

public int getBorderGap()
Gets the border gap
Returns:
the border gap in pixels


getCurrentLineForeground

public Color getCurrentLineForeground()
Gets the current line rendering Color
Returns:
the Color used to render the current line number


getDigitAlignment

public float getDigitAlignment()
Gets the digit alignment
Returns:
the alignment of the painted digits


getMinimumDisplayDigits

public int getMinimumDisplayDigits()
Gets the minimum display digits
Returns:
the minimum display digits


getOffsetX

private int getOffsetX(int availableWidth, int stringWidth)


getOffsetY

private int getOffsetY(int rowStartOffset, FontMetrics fontMetrics)


getTextLineNumber

protected String getTextLineNumber(int rowStartOffset)


getUpdateFont

public boolean getUpdateFont()
Gets the update font property
Returns:
the update font property


insertUpdate

de
	public
public void insertUpdate(DocumentEvent e)


isCurrentLine

private boolean isCurrentLine(int rowStartOffset)


paintComponent

de
	public
public void paintComponent(Graphics g)
Draw the line numbers


propertyChange

de
	public
public void propertyChange(PropertyChangeEvent evt)


removeUpdate

de
	public
public void removeUpdate(DocumentEvent e)


setBorderGap

public void setBorderGap(int borderGap)
The border gap is used in calculating the left and right insets of the border. Default value is 5.
Parameters:
borderGap - the gap in pixels


setCurrentLineForeground

public void setCurrentLineForeground(Color currentLineForeground)
The Color used to render the current line digits. Default is Coolor.RED.
Parameters:
currentLineForeground - the Color used to render the current line


setDigitAlignment

public void setDigitAlignment(float digitAlignment)
Specify the horizontal alignment of the digits within the component. Common values would be:
Parameters:
currentLineForeground - the Color used to render the current line


setMinimumDisplayDigits

public void setMinimumDisplayDigits(int minimumDisplayDigits)
Specify the mimimum number of digits used to calculate the preferred width of the component. Default is 3.
Parameters:
minimumDisplayDigits - the number digits used in the preferred width calculation


setPreferredWidth

private void setPreferredWidth()
Calculate the width needed to display the maximum line number


setUpdateFont

public void setUpdateFont(boolean updateFont)
Set the update font property. Indicates whether this Font should be updated automatically when the Font of the related text component is changed.
Parameters:
updateFont - when true update the Font and repaint the line numbers, otherwise just repaint the line numbers.


 

Groovy Documentation