com.cburch.editor
Class LineNumbers

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JViewport
                  extended by com.cburch.editor.LineNumbers
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, javax.accessibility.Accessible

public class LineNumbers
extends javax.swing.JViewport

Tracks the lines within a document, and enables them to be displayed along with the document. Even when line numbers aren't displayed, objects of this class are still useful for converting from line numbers to offsets and vice versa.

Line numbers are by default numbered starting with 1, although this can be configured via the setFirstLine method. The toOffset and fromOffset methods take this into account; the line numbers they work with are not zero-indexed unless setFirstLine has been configured thus.

This implementation has a small known bug: It does not identify when the text component's document changes. If this should happen, it will maintain the line numbers of the previous document. You can get it to reset to the proper document using its setJTextComponent method.

Version:
0.1 2005-05-31
Author:
Carl Burch
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Field Summary
 
Fields inherited from class javax.swing.JViewport
BACKINGSTORE_SCROLL_MODE, BLIT_SCROLL_MODE, SIMPLE_SCROLL_MODE
 
Fields inherited from class javax.swing.JComponent
TOOL_TIP_TEXT_KEY, 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
LineNumbers(javax.swing.text.JTextComponent textPane)
          Constructs a line-numbering object for the given text component.
 
Method Summary
 int fromOffset(int offset)
          The line number where the given offset is located within the document.
 int getFirstLine()
          Returns the number currently associated with the first line of the document.
 void paintComponent(java.awt.Graphics g)
          Overrides JViewport's paintComponent method so that we can draw the line numbers appropriately.
 void setFirstLine(int value)
          Changes the number associated with the document's first line.
 void setJTextComponent(javax.swing.text.JTextComponent value)
          Configures which text component's line numbers we should track.
 void setViewPosition(java.awt.Point p)
          Overrides JViewport's setViewPosition method so that we can track the top of the viewport.
 int toOffset(int lineNumber)
          The offset of the first character in the given line.
 
Methods inherited from class javax.swing.JViewport
addChangeListener, getAccessibleContext, getChangeListeners, getExtentSize, getInsets, getInsets, getScrollMode, getUI, getUIClassID, getView, getViewPosition, getViewRect, getViewSize, isBackingStoreEnabled, isOptimizedDrawingEnabled, paint, remove, removeChangeListener, repaint, reshape, scrollRectToVisible, setBackingStoreEnabled, setBorder, setExtentSize, setScrollMode, setUI, setView, setViewSize, toViewCoordinates, toViewCoordinates, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paintImmediately, paintImmediately, print, printAll, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, resetKeyboardActions, revalidate, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, 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, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, contains, createImage, createImage, createVolatileImage, createVolatileImage, dispatchEvent, enable, enableInputMethods, 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, 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
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LineNumbers

public LineNumbers(javax.swing.text.JTextComponent textPane)
Constructs a line-numbering object for the given text component.

Parameters:
textPane - the text component whose line numbers we should track.
Method Detail

setJTextComponent

public void setJTextComponent(javax.swing.text.JTextComponent value)
Configures which text component's line numbers we should track.

Parameters:
value - the text component whom we should track.

getFirstLine

public int getFirstLine()
Returns the number currently associated with the first line of the document.

Returns:
the first line's number.

setFirstLine

public void setFirstLine(int value)
Changes the number associated with the document's first line. This is 1 by default.

Parameters:
value - the first line's number.

fromOffset

public int fromOffset(int offset)
The line number where the given offset is located within the document. Note that line numbers are indexed starting at 1 by default.

Parameters:
offset - the offset whose line number is desired.
Returns:
the number of the corresponding line, offset as indicated with setFirstLine.

toOffset

public int toOffset(int lineNumber)
The offset of the first character in the given line. Note that lines are not zero-indexed by default.

Parameters:
lineNumber - the line index in question, offset as configured with setFirstLine.
Returns:
the offset of the first character within the line.

setViewPosition

public void setViewPosition(java.awt.Point p)
Overrides JViewport's setViewPosition method so that we can track the top of the viewport.

Overrides:
setViewPosition in class javax.swing.JViewport
Parameters:
p - the point that is now the top of the viewport.

paintComponent

public void paintComponent(java.awt.Graphics g)
Overrides JViewport's paintComponent method so that we can draw the line numbers appropriately.

Overrides:
paintComponent in class javax.swing.JComponent
Parameters:
g - the object we should use for painting line numbers.