verinec.gui.core
Class NwComponent

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.JLabel
                  extended by verinec.gui.core.NwComponent
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible, SwingConstants
Direct Known Subclasses:
NwBinding, NwInterface, NwNode

public abstract class NwComponent
extends JLabel

Base class for every network component of the GUI.

The class itself provides several functionalities. Common methodes are directly implemented in this class: moving and scaling the component as well as creating the icon.

Moving is implemented as follows: all setBounds, setLocation and the moveComponent methods are redirected to setBounds(int, int, int, int). This method prevents moving a component out of the window area. Components having attached children should overwrite that method to move their children with them.

Author:
Renato Loeffel
See Also:
Serialized Form

Nested Class Summary
(package private)  class NwComponent.Mouse
          Handling of mouse clicks on nodes: select/unselect and context menue with right click.
 
Nested classes/interfaces inherited from class javax.swing.JLabel
JLabel.AccessibleJLabel
 
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
protected  ImageIcon componentIcon
          Gui element
protected  ImageIcon componentIconNeg
          Gui element
protected  Element config
          The XML element defining this component.
private static int defaultSize
          Default size in pixel of a component when the view is 100%.
protected  DrawPanel drawPanel
          The panel this component is located in.
protected  VerinecStudio gui
          Reference to the main application.
private  MouseListener mouse
           
private  JPopupMenu popup
          Gui element
protected  Point pos
          The position can get negative, although we want the component to always apear on screen.
private static Random random
          Random number generator for id's.
protected  boolean selection
          Gui element
protected  int type
          The network type of this component.
 
Fields inherited from class javax.swing.JLabel
labelFor
 
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 javax.swing.SwingConstants
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
protected NwComponent(Element config, Point position, float factor, String iconPath, VerinecStudio gui, int type)
          Initializes the size, position and the coordination of the component.
 
Method Summary
 void addContextMenu(Action a)
          Add an action to the context menu.
 void addContextMenuSeparator()
          Adds a separator to the context menu.
 boolean changeIcon(String iconPath)
          Changes the icon of the component.
 void delete()
          Deletes the node and removes the subtree in the parents XML-Element.
 int getAbsX()
          Return the real x position, not the screen position.
 int getAbsY()
          Return the real y position, not the screen position.
 Element getConfig()
          Returns the XML element of the object.
static int getDefaultSize()
          Returns the default size of the component.
protected abstract  Element getLayoutChild(Element config)
          Extract the node gui information child from the configuration, add a default if necessary.
protected static long getRandomId()
          Get a positive random number to be used as id.
 int getType()
          Get the network type of this component.
 void initialize()
          Reset the component to initial state, that is remove all foreign listeners and menue entries.
 boolean isSelected()
          Returns the selection state of the object.
private  boolean loadIcon(String iconPath)
          Loads an icon for this component.
 void moveComponent(int dx, int dy)
          Moves the component to the new coordinates.
 void resizeComponent(float factor)
          Resizes the component to the new size.
static void resizeDefaultSize(float factor)
          Scale the default size of a component.
 void setBounds(int x, int y, int w, int h)
          Set the position and size of the component on the panel.
 void setBounds(Rectangle r)
          Set the position of the component.
 void setLocation(int x, int y)
          Set the position of the component.
 void setLocation(Point p)
          Set the position of the component.
 void setSelected(boolean value)
          Changes whether the node is currently selected or not.
protected  void showIcon(boolean inverted)
          Load the icon if there is one.
 
Methods inherited from class javax.swing.JLabel
checkHorizontalKey, checkVerticalKey, getAccessibleContext, getDisabledIcon, getDisplayedMnemonic, getDisplayedMnemonicIndex, getHorizontalAlignment, getHorizontalTextPosition, getIcon, getIconTextGap, getLabelFor, getText, getUI, getUIClassID, getVerticalAlignment, getVerticalTextPosition, imageUpdate, paramString, setDisabledIcon, setDisplayedMnemonic, setDisplayedMnemonic, setDisplayedMnemonicIndex, setHorizontalAlignment, setHorizontalTextPosition, setIcon, setIconTextGap, setLabelFor, setText, setUI, setVerticalAlignment, setVerticalTextPosition, updateUI
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, 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, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, 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
 
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, 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, 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, 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, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, 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

mouse

private MouseListener mouse

config

protected Element config
The XML element defining this component.


drawPanel

protected DrawPanel drawPanel
The panel this component is located in.


gui

protected VerinecStudio gui
Reference to the main application.


type

protected int type
The network type of this component. Only valid vor interfaces, bindings, wires and hubs. The allowed values can be found in NetworkTypes.


pos

protected Point pos
The position can get negative, although we want the component to always apear on screen. Here we save the "real" position.


popup

private JPopupMenu popup
Gui element


componentIcon

protected ImageIcon componentIcon
Gui element


componentIconNeg

protected ImageIcon componentIconNeg
Gui element


random

private static Random random
Random number generator for id's.


selection

protected boolean selection
Gui element


defaultSize

private static int defaultSize
Default size in pixel of a component when the view is 100%.

Constructor Detail

NwComponent

protected NwComponent(Element config,
                      Point position,
                      float factor,
                      String iconPath,
                      VerinecStudio gui,
                      int type)
Initializes the size, position and the coordination of the component.

Parameters:
config - The config element of the component.
position - The top left corner point of the component. Will be ignored if the configuration contains layout information.
factor - A ratio to the default size.
iconPath - The location where the icon can be found or null for no icon.
gui - The main application.
type - The netowrk type id or 0 if node component.
Method Detail

initialize

public void initialize()
Reset the component to initial state, that is remove all foreign listeners and menue entries.


addContextMenu

public void addContextMenu(Action a)
Add an action to the context menu.

Parameters:
a - The action to add.

addContextMenuSeparator

public void addContextMenuSeparator()
Adds a separator to the context menu.


getType

public int getType()
Get the network type of this component. Not valid on node components (PCNode, Hub).

Returns:
Network type id.
See Also:
NetworkTypes

resizeDefaultSize

public static void resizeDefaultSize(float factor)
Scale the default size of a component. This is needed when the zoom level is changed.

Parameters:
factor - the resize factor.

getDefaultSize

public static int getDefaultSize()
Returns the default size of the component.

Returns:
the current default size.

setLocation

public void setLocation(int x,
                        int y)
Set the position of the component. Prevents placing anything into the negative area of the pane, which would make it unreachable.

Overrides:
setLocation in class Component
Parameters:
x - Top left corner x coordinate
y - Top left corner y coordinate

setLocation

public void setLocation(Point p)
Set the position of the component. Redirected to setBounds(int, int, int, int).

Overrides:
setLocation in class Component
Parameters:
p - Top left corner.

setBounds

public void setBounds(int x,
                      int y,
                      int w,
                      int h)
Set the position and size of the component on the panel. Does not update the information saved to XML. All setLocation/setBounds/moveComponent methods are redirected to this method.

Prevents placing anything into the negative area of the pane, which would make it unreachable.
Takes care of scaling the icon if width or height changes.

If a component needs to do anything when its moved, overwrite this method, but do not forget to call super.setBounds at some place.

Overrides:
setBounds in class Component
Parameters:
x - Top left corner x coordinate
y - Top left corner y coordinate
w - With of component
h - Height of component

setBounds

public void setBounds(Rectangle r)
Set the position of the component. Redirected to setBounds(int, int, int, int).

Overrides:
setBounds in class Component
Parameters:
r - Area of the component.

getAbsX

public int getAbsX()
Return the real x position, not the screen position. This can also get negative and is used to preserve the original information when nodes are moved towards a border. Use getX to get the real screen coordinate.

Returns:
Real position of component.

getAbsY

public int getAbsY()
Return the real y position, not the screen position. This can also get negative and is used to preserve the original information when nodes are moved towards a border. Use getY to get the real screen coordinate.

Returns:
Real position of component.

getLayoutChild

protected abstract Element getLayoutChild(Element config)
Extract the node gui information child from the configuration, add a default if necessary. This should have the side effect of setting the element to a specified location if it is specified in the layout element.

Parameters:
config - The network xml object.
Returns:
A gui element from configuration namespace.

moveComponent

public void moveComponent(int dx,
                          int dy)
Moves the component to the new coordinates. Updates the XML to save the new coordinates. Uses setBounds(int, int, int, int) internally to actually move the icon on the panel.

Parameters:
dx - horizontal translation.
dy - vertical translation.

resizeComponent

public void resizeComponent(float factor)
Resizes the component to the new size.

Parameters:
factor - the resize factor for the icon.

setSelected

public final void setSelected(boolean value)
Changes whether the node is currently selected or not. Tells VerinecStudio that the component state changed, which will trigger the editor for this component to be displayed. (For performance reasons, the operation is only performed if the new status is different from the old.)

Parameters:
value - the new selection state.

isSelected

public final boolean isSelected()
Returns the selection state of the object.

Returns:
True if component is selected, false otherwise.

getConfig

public final Element getConfig()
Returns the XML element of the object.

Returns:
the config element of the component.

delete

public void delete()
Deletes the node and removes the subtree in the parents XML-Element.


loadIcon

private boolean loadIcon(String iconPath)
Loads an icon for this component. (Without changing/creating any imgPath attribute in nodeinfo)

Parameters:
iconPath - the path of the new icon.
Returns:
True if the operation succeeded, false otherwise.

changeIcon

public boolean changeIcon(String iconPath)
Changes the icon of the component. The path to the icon is stored in the imgPath attribute of the component.

Parameters:
iconPath - the path of the new icon.
Returns:
True if the operation succeeded, false otherwise.

showIcon

protected final void showIcon(boolean inverted)
Load the icon if there is one. If inverted is true, uses the inverted icon componentIconNeg, otherwise the normal one componentIcon.

Parameters:
inverted -

getRandomId

protected static long getRandomId()
Get a positive random number to be used as id.

Returns:
A positive random number for usage in an id string.

Copyright © 2005 Verinec, DIUF