org.artoflife.gui.population
Class PopulationView

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--javax.swing.JComponent
                    |
                    +--org.artoflife.gui.population.PopulationView
All Implemented Interfaces:
java.util.EventListener, java.awt.image.ImageObserver, java.awt.ItemSelectable, java.awt.MenuContainer, java.awt.event.MouseListener, java.awt.event.MouseMotionListener, PopulationListener, java.io.Serializable
Direct Known Subclasses:
PopulationPanel

public class PopulationView
extends javax.swing.JComponent
implements PopulationListener, java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.awt.ItemSelectable

Base class for JComponent-based views on a Population containing Individuals that have org.artoflife.gp.paintable.PaintablePhenotypes. Individuals are represented in the view by the image painted by their phenotype.

Fires ItemEvents when an Individual is clicked, and ActionEvents when an Individual is double-clicked. Maintains a list of selected Individuals, painting a border around when selected.

Manages addition, deletion, and changes of Individuals in the Population, repainting areas as necessary.

Dragging of Individuals within the view is also supported via org.artoflife.gui.paintable.view.PopulationViewTransferHandler and the JDK1.4 TransferHandler mechanism.

The positioning and painting of Individuals is delegated to IndividualRecord helpers that are used by an org.artoflife.gui.paintable.view.IndividualRecordPositionPolicy to manage pluggable positioning of Individuals within the view. The default implementation uses VerticalFitnessPolicy and IndividualCachingRecord.

Author:
Steve Meyfroidt
See Also:
Serialized Form

Field Summary
protected  java.awt.event.ActionListener actionListener
          Support firing ActionEvents when double-click an Individual.
static org.artoflife.gui.population.IndividualRecord currentDraggedIndividualRecord
          Per VM reference to currently dragged IndividualRecord.
static PopulationView currentDragSource
          Per VM record of the current PopulationView that initiated a drag.
static java.awt.Point dragOffset
          Per VM record of the last offset of mouse press into Individual's image for drag tracking.
protected  java.awt.event.ItemListener itemListener
          Support firing ItemEvents when click an Individual.
protected  Population population
          View onto this population, may be null.
protected  java.util.ArrayList populationRecords
          Ordered list of IndividualRecords to describe the Individuals in the population.
protected  RecordPositionPolicy positionPolicy
          Helper to maintain IndividualRecord positions within the view.
protected  Individual[] selection
          Array of length 1 if an Individual selected (only support single selection), or of length 0 if none selected, never null.
 
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
PopulationView()
          Subclasses must call setPositionPolicy(RecordPositionPolicy) to set a RecordPositionPolicy before letting this view run.
 
Method Summary
 void addActionListener(java.awt.event.ActionListener l)
           
 void addItemListener(java.awt.event.ItemListener l)
           
 void addNewIndividual(Individual individual, java.awt.Point location)
          Used by data transfer stuff to bring a new Individual into the population that this view manages.
static void clearDragState()
          Called at the end of a drag to reset the global (static) PopulationView drag state.
protected  org.artoflife.gui.population.IndividualRecord doIndividualAdded(Individual individual)
          Add a new record for an Individual, working out its position and size using the position policy.
protected  org.artoflife.gui.population.IndividualRecord doIndividualChanged(Individual individual)
          An individual changed so repaint over old position, update to new position etc.
protected  org.artoflife.gui.population.IndividualRecord doIndividualRemoved(Individual individual)
          Remove the record for an Individual and clear the selection if it was just removed.
protected  int findEmptyRecordIndex(java.util.ArrayList records)
          Utility for doIndividualAdded(Individual).
protected  org.artoflife.gui.population.IndividualRecord findRecord(Individual individual)
           
protected  int findRecordIndex(java.util.ArrayList records, Individual individual)
          Utility for doIndividualChanged(Individual) and doIndividualRemoved(Individual)
 java.awt.Dimension getIndividualDimension(Individual individual)
          For dragging images of individuals.
 Population getPopulation()
           
 java.lang.Object[] getSelectedObjects()
           
 void handleDragEnter()
          Called when a drag enters the source PopulationView so that it can add back the selected Individual temporarily.
 void handleDragExit()
          Called when a drag exits the source PopulationView so that it can remove the selected Individual temporarily.
protected  org.artoflife.gui.population.IndividualRecord individualRecordAt(java.util.ArrayList records, int x, int y)
           
protected  org.artoflife.gui.population.IndividualRecord individualRecordAt(int x, int y)
           
protected  org.artoflife.gui.population.IndividualRecord makeRecordFor(Individual individual)
          Default implementation returns an IndividualRecord but override to change this.
 void mouseClicked(java.awt.event.MouseEvent e)
          Fire ActionEvent when double-clicked an Individual.
 void mouseDragged(java.awt.event.MouseEvent e)
          Recognise drags by looking for a MousePressed followed by MouseDrag for a certain time after the press.
 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)
          Change selected and fire ItemEvent when pressed on an Individual.
 void mouseReleased(java.awt.event.MouseEvent e)
           
 void paint(java.awt.Graphics graphics)
           
protected  void paintIndividual(java.awt.Graphics2D g, org.artoflife.gui.population.IndividualRecord record)
          Paint the IndividualRecord if it falls within the graphics' clip region.
 void populationChanged(PopulationEvent event)
          Repaint to reflect a change in population being viewed.
protected  void processActionEvent(java.awt.event.ActionEvent e)
           
protected  void processItemEvent(java.awt.event.ItemEvent e)
           
 void removeActionListener(java.awt.event.ActionListener l)
           
 void removeItemListener(java.awt.event.ItemListener l)
           
protected  void repositionForNewBounds(java.util.ArrayList records, int x, int y, int width, int height)
          Utility.
 void select(Individual individual)
           
 void setBounds(int x, int y, int width, int height)
          Override to calculate internal dimensions and reposition Individuals.
 void setPopulation(Population population)
           
 void setPositionPolicy(RecordPositionPolicy policy)
           
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getAccessibleContext, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, isValidateRoot, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, 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, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, 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, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, 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, processMouseEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, repaint, repaint, repaint, resize, resize, 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
 

Field Detail

population

protected Population population
View onto this population, may be null.


populationRecords

protected java.util.ArrayList populationRecords
Ordered list of IndividualRecords to describe the Individuals in the population. Never null: if population is null this will be an empty list.


selection

protected Individual[] selection
Array of length 1 if an Individual selected (only support single selection), or of length 0 if none selected, never null.


itemListener

protected transient java.awt.event.ItemListener itemListener
Support firing ItemEvents when click an Individual.


actionListener

protected transient java.awt.event.ActionListener actionListener
Support firing ActionEvents when double-click an Individual.


positionPolicy

protected RecordPositionPolicy positionPolicy
Helper to maintain IndividualRecord positions within the view. Must be set in subclass constructor.


currentDragSource

public static PopulationView currentDragSource
Per VM record of the current PopulationView that initiated a drag. This is a hack to get round limitations in Sun's DnD impl.


dragOffset

public static java.awt.Point dragOffset
Per VM record of the last offset of mouse press into Individual's image for drag tracking. This is ugly but exists to get round difficulties with the JDK1.4 DnD implementation.


currentDraggedIndividualRecord

public static org.artoflife.gui.population.IndividualRecord currentDraggedIndividualRecord
Per VM reference to currently dragged IndividualRecord. This is ugly but exists to get round difficulties with the JDK1.4 DnD implementation.

Constructor Detail

PopulationView

public PopulationView()
Subclasses must call setPositionPolicy(RecordPositionPolicy) to set a RecordPositionPolicy before letting this view run.

Method Detail

setPositionPolicy

public void setPositionPolicy(RecordPositionPolicy policy)
Parameters:
policy - a RecordPositionPolicy for this view to use when positioning indiviuduals.

setPopulation

public void setPopulation(Population population)
Parameters:
population - that this view presents, may be null

getPopulation

public Population getPopulation()
Returns:
population that this view presents, may be null

populationChanged

public void populationChanged(PopulationEvent event)
Repaint to reflect a change in population being viewed.

Specified by:
populationChanged in interface PopulationListener
Parameters:
event - to respond to

doIndividualAdded

protected org.artoflife.gui.population.IndividualRecord doIndividualAdded(Individual individual)
Add a new record for an Individual, working out its position and size using the position policy.

This calls makeRecordFor(Individual) to create the record for flexibility: the default uses an IndividualCachingRecord.

Parameters:
individual - whose record to be created and added
Returns:
the new record added, never null

makeRecordFor

protected org.artoflife.gui.population.IndividualRecord makeRecordFor(Individual individual)
Default implementation returns an IndividualRecord but override to change this.

Parameters:
individual - Individual to create a record for * @return IndividualRecord a record for the Individual

doIndividualRemoved

protected org.artoflife.gui.population.IndividualRecord doIndividualRemoved(Individual individual)
Remove the record for an Individual and clear the selection if it was just removed.

Parameters:
individual - whose record to be removed
Returns:
the record removed or null if none removed

doIndividualChanged

protected org.artoflife.gui.population.IndividualRecord doIndividualChanged(Individual individual)
An individual changed so repaint over old position, update to new position etc.

Parameters:
individual - that changed
Returns:
the record for the individual that changed, updated as appropriate, or null if couldn't find

paint

public void paint(java.awt.Graphics graphics)
Overrides:
paint in class javax.swing.JComponent
See Also:
JComponent.paint(java.awt.Graphics)

paintIndividual

protected void paintIndividual(java.awt.Graphics2D g,
                               org.artoflife.gui.population.IndividualRecord record)
Paint the IndividualRecord if it falls within the graphics' clip region.


setBounds

public void setBounds(int x,
                      int y,
                      int width,
                      int height)
Override to calculate internal dimensions and reposition Individuals.

Overrides:
setBounds in class java.awt.Component

repositionForNewBounds

protected void repositionForNewBounds(java.util.ArrayList records,
                                      int x,
                                      int y,
                                      int width,
                                      int height)
Utility. If the view changes bounds, this is called to recalculate the positions of individuals. The new bounds of the view are passed.


findEmptyRecordIndex

protected int findEmptyRecordIndex(java.util.ArrayList records)
Utility for doIndividualAdded(Individual).

Returns:
index of first empty slot in passed array, or -1 if none found

findRecordIndex

protected int findRecordIndex(java.util.ArrayList records,
                              Individual individual)
Utility for doIndividualChanged(Individual) and doIndividualRemoved(Individual)

Parameters:
individual - the one to find (can be null)
Returns:
index of record for passed Individual, or -1 if not found

findRecord

protected org.artoflife.gui.population.IndividualRecord findRecord(Individual individual)

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

mousePressed

public void mousePressed(java.awt.event.MouseEvent e)
Change selected and fire ItemEvent when pressed on an Individual. Also record the timestamp of this event which might signal the start of a drag.

Specified by:
mousePressed in interface java.awt.event.MouseListener

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

mouseDragged

public void mouseDragged(java.awt.event.MouseEvent e)
Recognise drags by looking for a MousePressed followed by MouseDrag for a certain time after the press.

Specified by:
mouseDragged in interface java.awt.event.MouseMotionListener

mouseClicked

public void mouseClicked(java.awt.event.MouseEvent e)
Fire ActionEvent when double-clicked an Individual. Single-clicks are implicitly handled during mousePressed in order to ensure that there is a selection if dragging is initiated after mousePressed.

Specified by:
mouseClicked in interface java.awt.event.MouseListener

individualRecordAt

protected org.artoflife.gui.population.IndividualRecord individualRecordAt(int x,
                                                                           int y)

individualRecordAt

protected org.artoflife.gui.population.IndividualRecord individualRecordAt(java.util.ArrayList records,
                                                                           int x,
                                                                           int y)

addItemListener

public void addItemListener(java.awt.event.ItemListener l)
Specified by:
addItemListener in interface java.awt.ItemSelectable

removeItemListener

public void removeItemListener(java.awt.event.ItemListener l)
Specified by:
removeItemListener in interface java.awt.ItemSelectable

processItemEvent

protected void processItemEvent(java.awt.event.ItemEvent e)

getSelectedObjects

public java.lang.Object[] getSelectedObjects()
Specified by:
getSelectedObjects in interface java.awt.ItemSelectable
Returns:
array of length 1 containing currently selected Individual, or array of length 0 if none selected, never null
See Also:
ItemSelectable.getSelectedObjects()

select

public void select(Individual individual)

addActionListener

public void addActionListener(java.awt.event.ActionListener l)

removeActionListener

public void removeActionListener(java.awt.event.ActionListener l)

processActionEvent

protected void processActionEvent(java.awt.event.ActionEvent e)

clearDragState

public static void clearDragState()
Called at the end of a drag to reset the global (static) PopulationView drag state.


handleDragExit

public void handleDragExit()
Called when a drag exits the source PopulationView so that it can remove the selected Individual temporarily.


handleDragEnter

public void handleDragEnter()
Called when a drag enters the source PopulationView so that it can add back the selected Individual temporarily.


addNewIndividual

public void addNewIndividual(Individual individual,
                             java.awt.Point location)
                      throws IncompatibleGenotypeException
Used by data transfer stuff to bring a new Individual into the population that this view manages.

Parameters:
individual - the new individual to bring into the population
IncompatibleGenotypeException

getIndividualDimension

public java.awt.Dimension getIndividualDimension(Individual individual)
For dragging images of individuals.

To do:
move this into PopulationViewTransferHandler which is the only user

artoflife pre0.1.0-dev

Copyright © 2002 the Art Of Life team. All Rights Reserved.