miSim

misim.editor
Class TextPanel

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--java.awt.Panel
                    |
                    +--misim.editor.TextPanel
All Implemented Interfaces:
javax.accessibility.Accessible, java.awt.event.AdjustmentListener, java.util.EventListener, java.awt.event.FocusListener, java.awt.image.ImageObserver, java.awt.ItemSelectable, java.awt.event.KeyListener, java.awt.MenuContainer, java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.lang.Runnable, java.io.Serializable
Direct Known Subclasses:
EditorPanel

public class TextPanel
extends java.awt.Panel
implements java.awt.event.AdjustmentListener, java.awt.event.FocusListener, java.awt.event.KeyListener, java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.awt.ItemSelectable, java.lang.Runnable

The text panel implements an uneditable display of text from a file. It uses a parser to break each line into tokens which are drawn in appropriate styles.

See Also:
Serialized Form

Inner classes inherited from class java.awt.Panel
java.awt.Panel.AccessibleAWTPanel
 
Inner classes inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Inner classes inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent
 
Field Summary
protected  LogListener logging
          The log listener for informing the user of events
 
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
TextPanel(TextFile file, EditorPrefs prefs, LogListener logging)
          Note that the creator must not use the loglistener to notify error events, as this text panel may be the view onto the log itself.
 
Method Summary
 void addItemListener(java.awt.event.ItemListener itemListener)
          Add an item listener to this panel, which will receive select events if the selectLine flag is set and a new line is selected
 void adjustmentValueChanged(java.awt.event.AdjustmentEvent aev)
          Respond to any scrollbar adjustment events.
 void append(java.lang.String text)
          Append text to the file
 boolean checkBoundaries(CursorPos newCursor)
          Make sure that the given cursor position is onscreen.
 void copySelection()
          Copy the currently selected text to the clipboard.
 void drawCursor(boolean show)
          Draw the cursor, or remove it as appropriate.
 void drawLine(int lineNum, java.awt.Graphics gr)
          Draw the givwen line number onscreen in the appropriate position.
 void focusGained(java.awt.event.FocusEvent evt)
           
 void focusLost(java.awt.event.FocusEvent evt)
           
 TextFile getFile()
          Return the file that this text panel refers to.
 java.lang.String getPanelName()
          Return the panelname
 int getSelectedLine()
          Return the currently selected line.
 java.lang.Object[] getSelectedObjects()
          Return the currently selected line, if selectLine is true and a line is selected
 java.lang.String getTitle()
          Return the title for this editor panel
 void gotoFirstError(boolean getFocus)
          Goto the first error in the file.
 void gotoLine(int lineNumber, boolean highlight)
          Move the cursor to the given line and display it.
 boolean hasSelection()
          Check to see if there is currently a selection that may be copied to the clipboard.
 void keyPressed(java.awt.event.KeyEvent ke)
           
 void keyReleased(java.awt.event.KeyEvent ke)
           
 void keyTyped(java.awt.event.KeyEvent ke)
           
 void mouseClicked(java.awt.event.MouseEvent e)
           
 void mouseDragged(java.awt.event.MouseEvent e)
          Respond to a mouse dragged event.
 void mouseEntered(java.awt.event.MouseEvent e)
           
 void mouseExited(java.awt.event.MouseEvent e)
           
 void mouseMoved(java.awt.event.MouseEvent e)
           
 void mousePressed(java.awt.event.MouseEvent e)
          Respond to a mouse click.
 void mouseReleased(java.awt.event.MouseEvent e)
           
 void moveCursor(CursorPos newCursor, boolean isShifted, boolean redraw)
          Process a new position for the cursor.
 void paint(java.awt.Graphics g)
           
protected  void processControlKey(java.awt.event.KeyEvent ke)
          Handle a control key press - specifically, the copy event.
protected  boolean processKey(int keyCode, java.awt.event.KeyEvent ke, CursorPos newCursor, boolean redraw)
           
 void redrawText(java.awt.Graphics g)
           
 void refresh()
          Redraw the text panel
 void removeItemListener(java.awt.event.ItemListener itemListener)
          Remove an item listener from this panel.
 void requestFocus()
          When the panel requests focus, pass it onto the canvas
 void run()
          ========================================================================= Thread handler for displaying cursor
 void selectLine(boolean selectLine)
          Set whether the current cursor position selects the entire line (and extended selections are therefore not supported), or not.
 void setCursor(boolean enabled)
          Set the cursor to be hidden or displayed
 void setTitle(java.lang.String title)
          Set the title for this editor panel
 void showErrors(boolean showErrors)
          Set whether errors should be shown or not
 void updateScrollbars()
          Update the scrollbars to reflect the current file size and the position of the view on that file.
 
Methods inherited from class java.awt.Panel
addNotify, getAccessibleContext
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getInsets, getLayout, getListeners, getMaximumSize, getMinimumSize, getPreferredSize, insets, invalidate, isAncestorOf, layout, list, list, locate, minimumSize, paintComponents, paramString, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, removeNotify, setFont, setLayout, update, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addPropertyChangeListener, addPropertyChangeListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, getBackground, getBounds, getBounds, getColorModel, getComponentOrientation, getCursor, getDropTarget, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getInputContext, getInputMethodRequests, getLocale, getLocation, getLocation, getLocationOnScreen, getName, getParent, getPeer, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isDisplayable, isDoubleBuffered, isEnabled, isFocusTraversable, isLightweight, isOpaque, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setForeground, setLocale, setLocation, setLocation, setName, setSize, setSize, setVisible, show, show, size, toString, transferFocus
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

logging

protected LogListener logging
The log listener for informing the user of events
Constructor Detail

TextPanel

public TextPanel(TextFile file,
                 EditorPrefs prefs,
                 LogListener logging)
          throws java.io.IOException
Note that the creator must not use the loglistener to notify error events, as this text panel may be the view onto the log itself. Once the TextPanel is created, the LogListener may be used as usual.
Method Detail

getPanelName

public java.lang.String getPanelName()
Return the panelname
Returns:
The editor panel's name

getTitle

public java.lang.String getTitle()
Return the title for this editor panel
Returns:
The editor panel's title

setTitle

public void setTitle(java.lang.String title)
Set the title for this editor panel
Parameters:
title - The new title for this editor panel

addItemListener

public void addItemListener(java.awt.event.ItemListener itemListener)
Add an item listener to this panel, which will receive select events if the selectLine flag is set and a new line is selected
Specified by:
addItemListener in interface java.awt.ItemSelectable
Parameters:
itemListener - The ItemListener to add

removeItemListener

public void removeItemListener(java.awt.event.ItemListener itemListener)
Remove an item listener from this panel.
Specified by:
removeItemListener in interface java.awt.ItemSelectable
Parameters:
itemListener - The ItemListener to remove

getSelectedObjects

public java.lang.Object[] getSelectedObjects()
Return the currently selected line, if selectLine is true and a line is selected
Specified by:
getSelectedObjects in interface java.awt.ItemSelectable
Returns:
The current Line object, or null

setCursor

public void setCursor(boolean enabled)
Set the cursor to be hidden or displayed
Parameters:
enabled - If true, the cursor is displayed

showErrors

public void showErrors(boolean showErrors)
Set whether errors should be shown or not
Parameters:
showErrors - If true, errors are indicated

selectLine

public void selectLine(boolean selectLine)
Set whether the current cursor position selects the entire line (and extended selections are therefore not supported), or not.
Parameters:
selectLine - If true, select the entire line

getFile

public TextFile getFile()
Return the file that this text panel refers to.
Returns:
The current TextFile object

refresh

public void refresh()
Redraw the text panel

hasSelection

public boolean hasSelection()
Check to see if there is currently a selection that may be copied to the clipboard.
Returns:
True if some text is selected

append

public void append(java.lang.String text)
Append text to the file
Parameters:
text - The String to append to the end of this file

gotoLine

public void gotoLine(int lineNumber,
                     boolean highlight)
Move the cursor to the given line and display it. Note that this clears the current selection. This is used by the assembler to display lines with errors.
Parameters:
lineNumber - The line to display
highlight - If true, highlight the line

gotoFirstError

public void gotoFirstError(boolean getFocus)
Goto the first error in the file. If there are no errors, go to the first line in the file

getSelectedLine

public int getSelectedLine()
Return the currently selected line. If a range of lines are selected, return the last (highest numerically) line in the range. If no lines are selected, return -1.
Returns:
The currently selected line

paint

public void paint(java.awt.Graphics g)
Overrides:
paint in class java.awt.Container

redrawText

public void redrawText(java.awt.Graphics g)

drawLine

public void drawLine(int lineNum,
                     java.awt.Graphics gr)
Draw the givwen line number onscreen in the appropriate position. This includes any highlighting and selections, but does not draw the cursor (see drawCursor for that!).
Parameters:
lineNum - The line number to be drawn

drawCursor

public void drawCursor(boolean show)
Draw the cursor, or remove it as appropriate.
Parameters:
show - If true, draw the cursor, else remove it

checkBoundaries

public boolean checkBoundaries(CursorPos newCursor)
Make sure that the given cursor position is onscreen. If the screen limits are changed, neither the scrollbars nor the image itself is updated. However, the display offset variables are updated, and the routine returns a value of true. If the position is onscreen the routine returns false
Parameters:
newCursor - The cursor position to check
Returns:
true If the position was offscreen

updateScrollbars

public void updateScrollbars()
Update the scrollbars to reflect the current file size and the position of the view on that file.

adjustmentValueChanged

public void adjustmentValueChanged(java.awt.event.AdjustmentEvent aev)
Respond to any scrollbar adjustment events. This will update the display offset variables as appropriate and trigger a redraw.
Specified by:
adjustmentValueChanged in interface java.awt.event.AdjustmentListener
Parameters:
aev - The Adjustment Event that occurred.

requestFocus

public void requestFocus()
When the panel requests focus, pass it onto the canvas
Overrides:
requestFocus in class java.awt.Component

focusGained

public void focusGained(java.awt.event.FocusEvent evt)
Specified by:
focusGained in interface java.awt.event.FocusListener

focusLost

public void focusLost(java.awt.event.FocusEvent evt)
Specified by:
focusLost in interface java.awt.event.FocusListener

keyPressed

public void keyPressed(java.awt.event.KeyEvent ke)
Specified by:
keyPressed in interface java.awt.event.KeyListener

keyReleased

public void keyReleased(java.awt.event.KeyEvent ke)
Specified by:
keyReleased in interface java.awt.event.KeyListener

keyTyped

public void keyTyped(java.awt.event.KeyEvent ke)
Specified by:
keyTyped in interface java.awt.event.KeyListener

mousePressed

public void mousePressed(java.awt.event.MouseEvent e)
Respond to a mouse click. This confirms that the panel has focus, checks that the cursor thread is alive and then responds to the click itself. If it is a shift click, this extends the current selection, otherwise it clears the selection. The new cursor position is then calculated from the mouse co-ordinates.
Specified by:
mousePressed in interface java.awt.event.MouseListener
Parameters:
e - The mouse event to respond to

mouseDragged

public void mouseDragged(java.awt.event.MouseEvent e)
Respond to a mouse dragged event. If the drag did not start in the window (ie. we received no preceeding mousePressed event) and we do not have focus, ignore the event. Otherwise calculate the new position for the cursor and process it as a shifted cursor movement - i.e. extend the current selection (if any) with the moveCursor method.
Specified by:
mouseDragged in interface java.awt.event.MouseMotionListener
Parameters:
e - The MouseEvent that we must respond to

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent e)
Specified by:
mouseReleased in interface java.awt.event.MouseListener

mouseMoved

public void mouseMoved(java.awt.event.MouseEvent e)
Specified by:
mouseMoved in interface java.awt.event.MouseMotionListener

mouseClicked

public void mouseClicked(java.awt.event.MouseEvent e)
Specified by:
mouseClicked in interface java.awt.event.MouseListener

mouseEntered

public void mouseEntered(java.awt.event.MouseEvent e)
Specified by:
mouseEntered in interface java.awt.event.MouseListener

mouseExited

public void mouseExited(java.awt.event.MouseEvent e)
Specified by:
mouseExited in interface java.awt.event.MouseListener

copySelection

public void copySelection()
Copy the currently selected text to the clipboard. If no text is selected, return without doing anything

processKey

protected boolean processKey(int keyCode,
                             java.awt.event.KeyEvent ke,
                             CursorPos newCursor,
                             boolean redraw)

processControlKey

protected void processControlKey(java.awt.event.KeyEvent ke)
Handle a control key press - specifically, the copy event.
Parameters:
aChar - - the character that has been pressed

moveCursor

public void moveCursor(CursorPos newCursor,
                       boolean isShifted,
                       boolean redraw)
Process a new position for the cursor.
Parameters:
newRow - The new row for the cursor
newCol - The new column for the cursor
isShifted - If this is a 'shifted' movement - ie. it extends the current selection
redraw - If true, always redraw the entire panel, otherwise on draw the necessary areas

run

public void run()
========================================================================= Thread handler for displaying cursor
Specified by:
run in interface java.lang.Runnable

miSim