cz.psika.numerist
Class DiagramCanvas

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by cz.psika.numerist.DiagramCanvas
All Implemented Interfaces:
History, Item, ItemObservable, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, java.lang.Cloneable
Direct Known Subclasses:
Diagram

public class DiagramCanvas
extends javax.swing.JComponent
implements Item, java.lang.Cloneable

Diagram drawing component. Diagram is created in connection with the Entry object. It is observable as every object implementing Item interface. Implicit constructor is used to create empty diagrams. Drawing diagrams here is quite fast and should be usable on very slow machines (<200 MHz) too. Nevertheless many graphic optimizations could be done. Drawing diagrams for entries with quite large source strings could evoke disappearing of diagram(s) inside small window. Please resize window in this case, diagram should show up.

Author:
Tomas Psika
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
protected  int activeRectangle
          Rectangle over which mouse pointer is situated.
protected static double ARROW_TRIANGLE_ANGLE
          Default arrow angle.
protected  java.awt.geom.GeneralPath arrowhead
          Path for drawing arrowheads.
protected  java.awt.BasicStroke arrowLineStroke
          Line strokes.
protected static java.awt.Color BACKGROUND_COLOR
           
protected static java.awt.Color BORDER_COLOR
           
protected static java.awt.Color CIRCLE_COLOR
           
protected static java.awt.Color[] circleColors
          Blue circle colors, outer circles are going to be brighter.
protected  java.awt.BasicStroke circleLineStroke
          Line strokes.
protected  java.awt.geom.Rectangle2D[] circleRects
          Dynamic rectangles which bound circles.
protected static int colLines
           
protected  java.awt.Dimension currentSize
          Current dimension.
static java.awt.Dimension DEFAULT_DIMENSION
          Default dimension of canvas.
protected  java.awt.BasicStroke diagonalLineStroke
          Line strokes.
protected  java.awt.Image dImage
          Caching image.
protected  boolean empty
          Empty diagram flag.
protected  Entry entry
          Source entry.
protected static java.awt.Color HIGHLIGHT_GRID_CELL_COLOR
           
protected static java.awt.Color HIGHLIGHT_LINE_COLOR
           
protected static int innerDiagonalLines
           
protected static java.awt.Color INVISIBLE_COLOR
           
protected  boolean isThumbnail
          Flag to distinguish thumbnail and full diagram.
protected static int MAX_NUMBER_OF_CIRCLE_COLORS
           
static int NO_RECTANGLE_ACTIVE
          Mouse pointer is not over some rectangle.
protected static boolean NO_TOGGLE_BORDER
          Suppress border toggle.
protected  java.awt.Color[] numberColors
          Colors for numbers loaded from style or default colors.
protected  java.util.Set<ItemObserver> observers
          Diagram observers.
protected static int outerDiagonalLine
           
protected  boolean paintError
          Painting error.
protected static java.awt.Color PLACE_BACKGROUND_COLOR
           
protected  java.awt.geom.Rectangle2D[] rects
          Rectangle parts of diagram.
protected static int rowLines
           
protected  Style style
          Cache for current entry's style.
protected  java.awt.geom.Line2D tmpLine
          Reusable graphic line.
protected static boolean TOGGLE_BORDER
          Change of border required.
 
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
protected DiagramCanvas()
          Implicit constructor used by child class to create empty diagrams.
  DiagramCanvas(Entry entry, java.awt.Dimension size, boolean setThumbnail)
          Constructor of diagram.
 
Method Summary
 void addObserver(ItemObserver observer)
          Observing diagrams not used for now.
 java.lang.Object clone()
          Cloning the diagram.
 Memento createMemento()
          Create history object.
 void deleteObserver(ItemObserver observer)
          Observing diagrams not used for now.
protected  void drawArrows(java.awt.Graphics2D g)
          Painting arrows.
protected  void drawArrowsIf(java.awt.Graphics2D g, int pos1, int pos2, int angle)
          Drawing lines with arrow(s).
protected  void drawCircles(java.awt.Graphics2D g)
          Drawing circles.
protected  void drawGrid(java.awt.Graphics2D g)
          Draws grid lines.
protected  void drawNumbers(java.awt.Graphics2D g)
          Draw all numbers/digits.
 boolean equals(java.lang.Object other)
          Diagrams are equal when their Entry instances are the same or their unique identifiers are the same.
 Entry fetchEntry()
          Fetch entry of this diagram.
 void forceRepaint()
          Force repainting of component by deleting offscreen image buffer.
 Entry getEntry()
          Get diagram entry.
protected  void initRects()
          Initialize all rectangles used for rendering numbers, circles and arrowheads.
 boolean isCopy()
          Some diagram could be made as copy of the other one.
 boolean isEmpty()
          Find whether this diagram is the empty diagram.
 boolean isThumbnail()
          Check type of diagram.
 void notifyObservers(Constants.ItemEvents event)
          Observing diagrams not used for now.
 void paintComponent(java.awt.Graphics g)
          Painting diagram.
 void refresh()
          Refresh diagram, hide some dynamic effects temporarily.
 void setBorder(boolean toggle)
          Set border for component or toggle border type.
 void setEntry(Entry newEntry)
          Set entry.
 void setMemento(Memento memento)
          Set state of this object to previously saved status.
 void setNewSize(java.awt.Dimension newSize)
          Resizing of diagram.
 
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, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, 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, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printComponent, printChildren, 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, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, 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, checkImage, checkImage, 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, transferFocusBackward, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

currentSize

protected java.awt.Dimension currentSize
Current dimension.


entry

protected Entry entry
Source entry.


style

protected Style style
Cache for current entry's style.


ARROW_TRIANGLE_ANGLE

protected static final double ARROW_TRIANGLE_ANGLE
Default arrow angle.

See Also:
Constant Field Values

BACKGROUND_COLOR

protected static final java.awt.Color BACKGROUND_COLOR

CIRCLE_COLOR

protected static final java.awt.Color CIRCLE_COLOR

PLACE_BACKGROUND_COLOR

protected static final java.awt.Color PLACE_BACKGROUND_COLOR

MAX_NUMBER_OF_CIRCLE_COLORS

protected static final int MAX_NUMBER_OF_CIRCLE_COLORS
See Also:
Constant Field Values

BORDER_COLOR

protected static final java.awt.Color BORDER_COLOR

HIGHLIGHT_GRID_CELL_COLOR

protected static final java.awt.Color HIGHLIGHT_GRID_CELL_COLOR

HIGHLIGHT_LINE_COLOR

protected static final java.awt.Color HIGHLIGHT_LINE_COLOR

INVISIBLE_COLOR

protected static final java.awt.Color INVISIBLE_COLOR

DEFAULT_DIMENSION

public static final java.awt.Dimension DEFAULT_DIMENSION
Default dimension of canvas. //@see Item#fetchDiagram(boolean wantThumbnail)


rowLines

protected static final int rowLines
See Also:
Constant Field Values

colLines

protected static final int colLines
See Also:
Constant Field Values

innerDiagonalLines

protected static final int innerDiagonalLines
See Also:
Constant Field Values

outerDiagonalLine

protected static final int outerDiagonalLine
See Also:
Constant Field Values

numberColors

protected java.awt.Color[] numberColors
Colors for numbers loaded from style or default colors.


circleColors

protected static final java.awt.Color[] circleColors
Blue circle colors, outer circles are going to be brighter.


circleLineStroke

protected java.awt.BasicStroke circleLineStroke
Line strokes.


arrowLineStroke

protected java.awt.BasicStroke arrowLineStroke
Line strokes.


diagonalLineStroke

protected java.awt.BasicStroke diagonalLineStroke
Line strokes.


rects

protected java.awt.geom.Rectangle2D[] rects
Rectangle parts of diagram.


circleRects

protected java.awt.geom.Rectangle2D[] circleRects
Dynamic rectangles which bound circles.


arrowhead

protected java.awt.geom.GeneralPath arrowhead
Path for drawing arrowheads.


tmpLine

protected java.awt.geom.Line2D tmpLine
Reusable graphic line.


dImage

protected java.awt.Image dImage
Caching image.


paintError

protected boolean paintError
Painting error.


isThumbnail

protected final boolean isThumbnail
Flag to distinguish thumbnail and full diagram.


TOGGLE_BORDER

protected static final boolean TOGGLE_BORDER
Change of border required.

See Also:
Constant Field Values

NO_TOGGLE_BORDER

protected static final boolean NO_TOGGLE_BORDER
Suppress border toggle.

See Also:
Constant Field Values

observers

protected java.util.Set<ItemObserver> observers
Diagram observers.


NO_RECTANGLE_ACTIVE

public static final int NO_RECTANGLE_ACTIVE
Mouse pointer is not over some rectangle.

See Also:
Constant Field Values

activeRectangle

protected int activeRectangle
Rectangle over which mouse pointer is situated. These diagram rectangles invoke repainting of part of diagram, hightlighting lines and circles etc.


empty

protected final boolean empty
Empty diagram flag.

Constructor Detail

DiagramCanvas

protected DiagramCanvas()
Implicit constructor used by child class to create empty diagrams. Empty diagram has no observers and no attached entry.


DiagramCanvas

public DiagramCanvas(Entry entry,
                     java.awt.Dimension size,
                     boolean setThumbnail)
Constructor of diagram.

Parameters:
entry - source entry
size - specified dimension of requested diagram
setThumbnail - thumbnail of full diagram behaviour
Method Detail

setEntry

public void setEntry(Entry newEntry)
Set entry. Sets entry and causes diagram repaint.

Parameters:
newEntry - entry to set
Throws:
java.lang.IllegalArgumentException - when called for empty diagrams

getEntry

public Entry getEntry()
Get diagram entry.

Returns:
entry associated with this diagram

setNewSize

public void setNewSize(java.awt.Dimension newSize)
Resizing of diagram.

Parameters:
newSize - new diagram dimension

isCopy

public boolean isCopy()
Some diagram could be made as copy of the other one. This functionality is used for making copy in dual view mode. Original diagram and it's copy could be edited side by side. Diagram is considered as copy of another one if it's entry is different, i.e. has not the same internal identifier.

Returns:
true if this diagram is copy of another one
See Also:
Entry.isCopy()

paintComponent

public void paintComponent(java.awt.Graphics g)
Painting diagram. Firstly we configure graphics and then find out whether we have previously painted and fresh copy of diagram in offscreen image buffer. If not, painting starts into newly created offscreen image. This mechanism saves resources and runs quickly.

Overrides:
paintComponent in class javax.swing.JComponent
Parameters:
g - Graphics object

forceRepaint

public void forceRepaint()
Force repainting of component by deleting offscreen image buffer.


drawGrid

protected void drawGrid(java.awt.Graphics2D g)
Draws grid lines. Some rectangle could be hightlighted with different background color when mouse pointer over it.

Parameters:
g - Graphics2D object

drawNumbers

protected void drawNumbers(java.awt.Graphics2D g)
Draw all numbers/digits. Each number could have it's specific color set.

Parameters:
g - Graphics2D object

drawCircles

protected void drawCircles(java.awt.Graphics2D g)
Drawing circles.

Parameters:
g - Graphics2D object
Throws:
java.lang.IllegalStateException - if entry not valid

drawArrows

protected void drawArrows(java.awt.Graphics2D g)
Painting arrows. These arrows are painted as simple lines and arrowheads are positioned and rotated.

Parameters:
g - graphic context
See Also:
drawArrowsIf(java.awt.Graphics2D, int, int, int)

drawArrowsIf

protected void drawArrowsIf(java.awt.Graphics2D g,
                            int pos1,
                            int pos2,
                            int angle)
Drawing lines with arrow(s). This method and/or drawArrows() method should be rewritten to be more readable and maybe a bit quicker. There are some things which should be removed and made more clear. But I dont have much motivation to do it, because it runs quite fast for now.

Parameters:
g - graphic context
pos1 - position of first rectangle
pos2 - position of second rectangle
angle - angle of arrowhead and line from first rectangle's point of view

setBorder

public void setBorder(boolean toggle)
Set border for component or toggle border type.

Parameters:
toggle - change border style

refresh

public void refresh()
Refresh diagram, hide some dynamic effects temporarily.


isEmpty

public boolean isEmpty()
Find whether this diagram is the empty diagram.

Returns:
EmptyDiagramCanvas instance must return true here

isThumbnail

public boolean isThumbnail()
Check type of diagram.

Returns:
true when this diagram is thumbnail

fetchEntry

public Entry fetchEntry()
Fetch entry of this diagram.

Specified by:
fetchEntry in interface Item
Returns:
diagram's entry

createMemento

public Memento createMemento()
Create history object.

Specified by:
createMemento in interface History
Returns:
DiagramCanvasMemento object representing history status of this instance
See Also:
DiagramCanvasMemento.DiagramCanvasMemento(Entry entry)

setMemento

public void setMemento(Memento memento)
Set state of this object to previously saved status. We just set previous entry. Repainting of diagram canvas will be done automatically.

Specified by:
setMemento in interface History
Parameters:
memento - DiagramCanvasMemento object representing history status of this instance
See Also:
DiagramCanvasMemento.fetchEntry()

equals

public boolean equals(java.lang.Object other)
Diagrams are equal when their Entry instances are the same or their unique identifiers are the same.

Overrides:
equals in class java.lang.Object
Parameters:
other - other DiagramCanvas instance
Returns:
true if diagrams are the same

clone

public java.lang.Object clone()
Cloning the diagram.

Overrides:
clone in class java.lang.Object
Returns:
new cloned DiagramCanvas instance

initRects

protected void initRects()
Initialize all rectangles used for rendering numbers, circles and arrowheads. Method called when diagram is being resized or when diagram construction started, not necessary to call this within paintComponent repeatedly.


addObserver

public void addObserver(ItemObserver observer)
Observing diagrams not used for now.

Specified by:
addObserver in interface ItemObservable
Parameters:
observer - an observer to attach

deleteObserver

public void deleteObserver(ItemObserver observer)
Observing diagrams not used for now.

Specified by:
deleteObserver in interface ItemObservable
Parameters:
observer - to remove

notifyObservers

public void notifyObservers(Constants.ItemEvents event)
Observing diagrams not used for now.

Specified by:
notifyObservers in interface ItemObservable
Parameters:
event - type of event to notify all registered observers