ch.epfl.craft.maglib
Class MagnetView

java.lang.Object
  extended byjava.awt.Component
      extended byjava.awt.Container
          extended byjavax.swing.JComponent
              extended byjavax.swing.JPanel
                  extended bych.epfl.craft.maglib.MagnetView
All Implemented Interfaces:
javax.accessibility.Accessible, java.awt.event.ActionListener, javax.swing.event.ChangeListener, java.util.EventListener, java.awt.image.ImageObserver, java.awt.event.KeyListener, MagnetLayoutListener, MagnetModelListener, java.awt.MenuContainer, java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.io.Serializable
Direct Known Subclasses:
MagMapMagnetView

public class MagnetView
extends javax.swing.JPanel
implements java.awt.event.MouseListener, java.awt.event.MouseMotionListener, MagnetLayoutListener, MagnetModelListener, java.awt.event.ActionListener, javax.swing.event.ChangeListener, java.awt.event.KeyListener

A component that displays a set of data nodes that can be organized through the use of virtual magnets. This component is only a visualization (+ some interaction) tool, it does not allow to edit its content

The view handles nodes moving, mouse over nodes, near mouse over and selection.

It can be used in a similar way to JTree, JList & co, as it is based on the same MVC architecture

Version:
1.0
Author:
Jerome Caffaro
See Also:
JTree, How to use JTree, Understanding JTree, Serialized Form

Nested Class Summary
 
Nested classes inherited from class javax.swing.JPanel
javax.swing.JPanel.AccessibleJPanel
 
Nested classes inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Nested classes inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
protected  MagnetNode currentNode
          The node being dragged/clicked/popuped
protected  MagnetNode currentSelectedNode
          The current selected node
protected  javax.swing.JMenuItem demagnetAllItem
          The menu item that demagnetize all magnets
protected  java.lang.String DEMAGNETIZE
          The alternate string label attached to makeMagnetItem
protected  java.lang.String DEMAGNETIZE_ALL
          The string label attached to demagnetAllItem
protected  java.awt.Point drawingOffset
          The offset for drawing.
protected  javax.swing.JSlider forceSlider
          The horizontal slider that will vary the field force of magnets.
protected  MagnetCellRenderer magnetCellRenderer
           
protected  java.lang.String MAGNETIZE
          The string label attached to makeMagnetItem
protected  MagnetLayout magnetLayout
           
protected  MagnetModel magnetModel
           
protected  javax.swing.JMenuItem makeMagnetItem
          The menu item that (de)magnetize a magnet node
protected  java.awt.Rectangle mouseOverRectangle
          The rectangle (around the mouse cursor) in which nodes are considered near the mouse
protected  java.awt.Point mousePosition
          The last know mouse position on the components
protected  java.awt.Point mousePressedPoint
          The position of the click in the view when mouse pressed
protected  java.util.HashSet nodesNearMouse
          The last known components near the mouse
protected  MagnetNode nodeUnderMouse
          The last known component under the mouse
protected  java.awt.Point offsetFromNodeOrigin
          The position of the click in the node being dragged/clicked
protected  javax.swing.JPopupMenu popup
          The menu called when right-click on a mouse
 
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
MagnetView()
          Returns a MagnetView with a sample model.
MagnetView(MagnetModel newModel)
          Returns an instance of MagnetView -- the view is created using the specified data model.
MagnetView(MagnetNode[] nodes)
          Returns an instance of MagnetView -- the view is created using the specified nodes, with DefaultMagnetModel.
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent e)
          Invoked when an action occurs on this instance.
protected static MagnetModel createMagnetModel(java.lang.Object value)
          Returns a MagnetModel wrapping the specified object.
 boolean getDisplayForceField()
          Returns true if the force field around magnet is displayed in the view, else false.
 MagnetCellRenderer getMagnetCellRenderer()
          Returns the MagnetCellRenderer that is used by the this MagnetView instance.
 MagnetLayout getMagnetLayout()
          Returns the MagnetLayout that is used by the this MagnetView.
 MagnetModel getMagnetModel()
          Returns the MagnetModel that is used by the this MagnetView instance.
 java.awt.Point getMousePosition()
          Returns the last known position of the mouse on this component.
 void keyPressed(java.awt.event.KeyEvent e)
           
 void keyReleased(java.awt.event.KeyEvent e)
           
 void keyTyped(java.awt.event.KeyEvent e)
          KeyListener IMPLEMENTATION
 void layoutHasChanged()
          Implementation of the MagnetLayoutListener interface.
 void modelHasChanged()
          MAGNETMODELLISTENER IMPLEMENTATION
 void mouseClicked(java.awt.event.MouseEvent e)
          Handle mouse click on this component.
 void mouseDragged(java.awt.event.MouseEvent e)
          Handle mouse drag on this component.
 void mouseEntered(java.awt.event.MouseEvent e)
          Handle mouse enter on this component.
 void mouseExited(java.awt.event.MouseEvent e)
          Handle mouse exit on this component.
 void mouseMoved(java.awt.event.MouseEvent e)
          Handle mouse move on this component.
 void mousePressed(java.awt.event.MouseEvent e)
          Handle pression of mouse button on this component.
 void mouseReleased(java.awt.event.MouseEvent e)
          Handle release of mouse button on this component.
protected  java.util.HashSet nodeNearPoint(java.awt.Point p)
          Returns the MagnetNodes near location p.
protected  MagnetNode nodeUnderPoint(java.awt.Point p)
          Returns the MagnetNode at location p.
 void paintComponent(java.awt.Graphics g)
          Invoked by Swing to draw components.
 void setDisplayForceField(boolean bool)
          Toggles the display of the force field around magnet boolean bool true if the force field around magnet is displayed, else false
 void setMagnetCellRenderer(MagnetCellRenderer newMagnetCellRenderer)
          Sets the cell renderer that will be used by the this MagnetView to display the nodes in the view.
 void setMagnetLayout(MagnetLayout newMagnetLayout)
          Sets the layout algorithm that will be used by the this MagnetView.
 void setMagnetModel(MagnetModel newMagnetModel)
          Sets the model that will be used by the this MagnetView.
 void setMouseOverArea(java.awt.Dimension dim)
          Sets the size of the virtual rectangle area around the mouse cursor such that nodes will be considered near the mouse.
 void stateChanged(javax.swing.event.ChangeEvent e)
          Invoked when the target of the listener has changed its state.
 
Methods inherited from class javax.swing.JPanel
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI
 
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, 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, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, 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
 
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, 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

magnetLayout

protected transient MagnetLayout magnetLayout

magnetCellRenderer

protected transient MagnetCellRenderer magnetCellRenderer

magnetModel

protected transient MagnetModel magnetModel

mousePosition

protected java.awt.Point mousePosition
The last know mouse position on the components


nodeUnderMouse

protected MagnetNode nodeUnderMouse
The last known component under the mouse


nodesNearMouse

protected java.util.HashSet nodesNearMouse
The last known components near the mouse


currentNode

protected MagnetNode currentNode
The node being dragged/clicked/popuped


currentSelectedNode

protected MagnetNode currentSelectedNode
The current selected node


offsetFromNodeOrigin

protected java.awt.Point offsetFromNodeOrigin
The position of the click in the node being dragged/clicked


mousePressedPoint

protected java.awt.Point mousePressedPoint
The position of the click in the view when mouse pressed


mouseOverRectangle

protected java.awt.Rectangle mouseOverRectangle
The rectangle (around the mouse cursor) in which nodes are considered near the mouse


popup

protected javax.swing.JPopupMenu popup
The menu called when right-click on a mouse


makeMagnetItem

protected javax.swing.JMenuItem makeMagnetItem
The menu item that (de)magnetize a magnet node


MAGNETIZE

protected java.lang.String MAGNETIZE
The string label attached to makeMagnetItem


DEMAGNETIZE

protected java.lang.String DEMAGNETIZE
The alternate string label attached to makeMagnetItem


demagnetAllItem

protected javax.swing.JMenuItem demagnetAllItem
The menu item that demagnetize all magnets


DEMAGNETIZE_ALL

protected java.lang.String DEMAGNETIZE_ALL
The string label attached to demagnetAllItem


drawingOffset

protected java.awt.Point drawingOffset
The offset for drawing. Used to translate all the nodes


forceSlider

protected javax.swing.JSlider forceSlider
The horizontal slider that will vary the field force of magnets. Values: Min 0, Max 100 and default 50

Constructor Detail

MagnetView

public MagnetView()
Returns a MagnetView with a sample model.


MagnetView

public MagnetView(MagnetModel newModel)
Returns an instance of MagnetView -- the view is created using the specified data model.

Parameters:
newModel - the MagnetModel to use as the data model

MagnetView

public MagnetView(MagnetNode[] nodes)
Returns an instance of MagnetView -- the view is created using the specified nodes, with DefaultMagnetModel.

Parameters:
nodes - the nodes to use as the data
Method Detail

createMagnetModel

protected static MagnetModel createMagnetModel(java.lang.Object value)
Returns a MagnetModel wrapping the specified object.

To do :

Parameters:
value - the Object used as the foundation for the TreeModel
Returns:
a MagnetModel wrapping the specified object

setMagnetLayout

public void setMagnetLayout(MagnetLayout newMagnetLayout)
Sets the layout algorithm that will be used by the this MagnetView. If none has been set or if the given parameter is null, a default layout (DefaultMagnetLayout) is used.

Parameters:
newMagnetLayout - the layouter to be used by this MagnetView instance
See Also:
DefaultMagnetLayout

getMagnetLayout

public MagnetLayout getMagnetLayout()
Returns the MagnetLayout that is used by the this MagnetView.

Returns:
the layouter currently used by this MagnetView instance
See Also:
DefaultMagnetLayout

setMagnetModel

public void setMagnetModel(MagnetModel newMagnetModel)
Sets the model that will be used by the this MagnetView. If the given parameter is null, the previous or a default one (DefaultMagnetCellRenderer) is used.

Parameters:
newMagnetModel - the model to be used by this MagnetView instance.
See Also:
DefaultMagnetModel

getMagnetModel

public MagnetModel getMagnetModel()
Returns the MagnetModel that is used by the this MagnetView instance.

Returns:
the model to be used by this MagnetView object

setMagnetCellRenderer

public void setMagnetCellRenderer(MagnetCellRenderer newMagnetCellRenderer)
Sets the cell renderer that will be used by the this MagnetView to display the nodes in the view. If none has been set or if the given parameter is null, a default renderer (DefaultMagnetCellRenderer) is used.

Parameters:
newMagnetCellRenderer - the cell renderer to be used by this MagnetView instance.
See Also:
DefaultMagnetCellRenderer

getMagnetCellRenderer

public MagnetCellRenderer getMagnetCellRenderer()
Returns the MagnetCellRenderer that is used by the this MagnetView instance.

Returns:
the cell renderer to be used by this MagnetView object
See Also:
DefaultMagnetCellRenderer

getMousePosition

public java.awt.Point getMousePosition()
Returns the last known position of the mouse on this component.

Returns:
the position of the mouse in the view

setDisplayForceField

public void setDisplayForceField(boolean bool)
Toggles the display of the force field around magnet boolean bool true if the force field around magnet is displayed, else false


getDisplayForceField

public boolean getDisplayForceField()
Returns true if the force field around magnet is displayed in the view, else false.

Returns:
true if the force field around magnet is displayed, else false.

paintComponent

public void paintComponent(java.awt.Graphics g)
Invoked by Swing to draw components. Uses the user defined MagnetCellRenderer. If none was specified, it uses DefaultMagnetCellRenderer.

Parameters:
g - the Graphics context in which to paint
See Also:
DefaultMagnetCellRenderer

nodeUnderPoint

protected MagnetNode nodeUnderPoint(java.awt.Point p)
Returns the MagnetNode at location p. If there is no node at p, returns null. The point coordinates must be relative to this MagnetView.

Parameters:
p - the point to test
Returns:
the node under p

nodeNearPoint

protected java.util.HashSet nodeNearPoint(java.awt.Point p)
Returns the MagnetNodes near location p. If there is no node near p, returns emptyHashSet. The point coordinates must be relative to this MagnetView. The near factor can be set with setMouseOverArea(java.awt.Dimension).

Parameters:
p - the point to test
Returns:
the nodes near p

setMouseOverArea

public void setMouseOverArea(java.awt.Dimension dim)
Sets the size of the virtual rectangle area around the mouse cursor such that nodes will be considered near the mouse.

Parameters:
dim - the dimension of the rectangle area

mousePressed

public void mousePressed(java.awt.event.MouseEvent e)
Handle pression of mouse button on this component.

Specified by:
mousePressed in interface java.awt.event.MouseListener
Parameters:
e - the event associated with the action

mouseReleased

public void mouseReleased(java.awt.event.MouseEvent e)
Handle release of mouse button on this component.

Specified by:
mouseReleased in interface java.awt.event.MouseListener
Parameters:
e - the event associated with the action

mouseEntered

public void mouseEntered(java.awt.event.MouseEvent e)
Handle mouse enter on this component.

Specified by:
mouseEntered in interface java.awt.event.MouseListener
Parameters:
e - the event associated with the action

mouseExited

public void mouseExited(java.awt.event.MouseEvent e)
Handle mouse exit on this component.

Specified by:
mouseExited in interface java.awt.event.MouseListener
Parameters:
e - the event associated with the action

mouseClicked

public void mouseClicked(java.awt.event.MouseEvent e)
Handle mouse click on this component.

Specified by:
mouseClicked in interface java.awt.event.MouseListener
Parameters:
e - the event associated with the action

mouseMoved

public void mouseMoved(java.awt.event.MouseEvent e)
Handle mouse move on this component.

Specified by:
mouseMoved in interface java.awt.event.MouseMotionListener
Parameters:
e - the event associated with the action

mouseDragged

public void mouseDragged(java.awt.event.MouseEvent e)
Handle mouse drag on this component.

Specified by:
mouseDragged in interface java.awt.event.MouseMotionListener
Parameters:
e - the event associated with the action

layoutHasChanged

public void layoutHasChanged()
Implementation of the MagnetLayoutListener interface. Called when the model has changed

Specified by:
layoutHasChanged in interface MagnetLayoutListener

modelHasChanged

public void modelHasChanged()
MAGNETMODELLISTENER IMPLEMENTATION

Specified by:
modelHasChanged in interface MagnetModelListener

actionPerformed

public void actionPerformed(java.awt.event.ActionEvent e)
Invoked when an action occurs on this instance. Only actions from the popup menu on the nodes

Specified by:
actionPerformed in interface java.awt.event.ActionListener
Parameters:
e - the associated event

stateChanged

public void stateChanged(javax.swing.event.ChangeEvent e)
Invoked when the target of the listener has changed its state.

Specified by:
stateChanged in interface javax.swing.event.ChangeListener
Parameters:
e - a ChangeEvent object

keyTyped

public void keyTyped(java.awt.event.KeyEvent e)
KeyListener IMPLEMENTATION

Specified by:
keyTyped in interface java.awt.event.KeyListener

keyPressed

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

keyReleased

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


Copyright © CRAFT EPFL. All Rights Reserved.