eu.livotov.tpt.gui.widgets
Class TPTMultiView

java.lang.Object
  extended by com.vaadin.ui.AbstractComponent
      extended by com.vaadin.ui.AbstractComponentContainer
          extended by com.vaadin.ui.AbstractLayout
              extended by com.vaadin.ui.AbstractOrderedLayout
                  extended by com.vaadin.ui.VerticalLayout
                      extended by eu.livotov.tpt.gui.widgets.TPTMultiView
All Implemented Interfaces:
com.vaadin.event.MethodEventSource, com.vaadin.terminal.Paintable, com.vaadin.terminal.Sizeable, com.vaadin.terminal.VariableOwner, com.vaadin.ui.Component, com.vaadin.ui.ComponentContainer, com.vaadin.ui.Layout, com.vaadin.ui.Layout.AlignmentHandler, com.vaadin.ui.Layout.MarginHandler, com.vaadin.ui.Layout.SpacingHandler, com.vaadin.ui.UriFragmentUtility.FragmentChangedListener, java.io.Serializable, java.util.EventListener

public class TPTMultiView
extends com.vaadin.ui.VerticalLayout
implements com.vaadin.ui.UriFragmentUtility.FragmentChangedListener

Provides lightweight multi-view component, that can hold an unlimited number of actual UI components, each associated with it's unique name and only one component can be displayed at a time, providing also fast switching between views.

TPTMultiView also allow to use view names, combined with the view parameters, separated by a '/' (slash) chatacter. In combination with view component that implements TPTView interface this will cause the parameters to be passed to a view that is switched on. This feature spicifically done for work with the combination of URIFragmentUtility, allowing not only to control your views from an URL programmatically or by a end user but also pass custom parameters from an URL to a view each time it is become active.

See Also:
Serialized Form

Nested Class Summary
static interface TPTMultiView.TPTView
          An optional interface, your component representing a single view may implement.
 
Nested classes/interfaces inherited from class com.vaadin.ui.AbstractComponent
com.vaadin.ui.AbstractComponent.ComponentErrorEvent, com.vaadin.ui.AbstractComponent.ComponentErrorHandler
 
Nested classes/interfaces inherited from interface com.vaadin.ui.Layout
com.vaadin.ui.Layout.AlignmentHandler, com.vaadin.ui.Layout.MarginHandler, com.vaadin.ui.Layout.MarginInfo, com.vaadin.ui.Layout.SpacingHandler
 
Nested classes/interfaces inherited from interface com.vaadin.ui.ComponentContainer
com.vaadin.ui.ComponentContainer.ComponentAttachEvent, com.vaadin.ui.ComponentContainer.ComponentAttachListener, com.vaadin.ui.ComponentContainer.ComponentDetachEvent, com.vaadin.ui.ComponentContainer.ComponentDetachListener
 
Nested classes/interfaces inherited from interface com.vaadin.ui.Component
com.vaadin.ui.Component.ErrorEvent, com.vaadin.ui.Component.ErrorListener, com.vaadin.ui.Component.Event, com.vaadin.ui.Component.Focusable, com.vaadin.ui.Component.Listener
 
Nested classes/interfaces inherited from interface com.vaadin.terminal.Paintable
com.vaadin.terminal.Paintable.RepaintRequestEvent, com.vaadin.terminal.Paintable.RepaintRequestListener
 
Field Summary
private  java.lang.String currentView
          Name of currently displayed view
private  java.util.Map<java.lang.String,java.lang.Class<? extends com.vaadin.ui.Component>> delayedViews
          Map to store lazy-loading views.
private  java.lang.String failsafeView
          Contains a failsafe view, to which view manager should switch in case actual view name will be invalid or view will not exists.
private  java.lang.String lastChangedFragment
          Last fragment, which was set by an URI Fragment Utility.
private  com.vaadin.ui.UriFragmentUtility uriManager
          URI manager, that tracks browser address bar changes and automatically switches the view, if enabled.
private  boolean uriManagerEnabled
          URI manager activation flag.
private  java.util.Map<java.lang.String,com.vaadin.ui.Component> views
          Internal map, where we'll store all registered views
 
Fields inherited from class com.vaadin.ui.AbstractOrderedLayout
components
 
Fields inherited from class com.vaadin.ui.AbstractLayout
margins
 
Fields inherited from interface com.vaadin.ui.Layout.AlignmentHandler
ALIGNMENT_BOTTOM, ALIGNMENT_HORIZONTAL_CENTER, ALIGNMENT_LEFT, ALIGNMENT_RIGHT, ALIGNMENT_TOP, ALIGNMENT_VERTICAL_CENTER
 
Fields inherited from interface com.vaadin.terminal.Sizeable
SIZE_UNDEFINED, UNIT_SYMBOLS, UNITS_CM, UNITS_EM, UNITS_EX, UNITS_INCH, UNITS_MM, UNITS_PERCENTAGE, UNITS_PICAS, UNITS_PIXELS, UNITS_POINTS
 
Constructor Summary
TPTMultiView()
          Creates a multiview with uri manager disabled.
TPTMultiView(boolean uriManagerEnabled)
          Creates a multiview, optionaly activating or deactivating the uri manager.
 
Method Summary
 void addView(java.lang.String viewName, java.lang.Class<? extends com.vaadin.ui.Component> viewClass)
          Adds a lazy loading view.
 TPTMultiView addView(java.lang.String viewName, com.vaadin.ui.Component view)
          Registers a new view.
protected  void fireViewActivatedMessage(com.vaadin.ui.Component view, java.lang.String previousViewId, java.lang.String parameters)
          Checks if view component implements the TPTView interface and if so, invokes the appropriate method to notify a view on event happened
protected  void fireViewAttachedMessage(com.vaadin.ui.Component view)
          Checks if view component implements the TPTView interface and if so, invokes the appropriate method to notify a view on event happened
protected  void fireViewDeactivatedMessage(com.vaadin.ui.Component view, java.lang.String newViewId)
          Checks if view component implements the TPTView interface and if so, invokes the appropriate method to notify a view on event happened
protected  void fireViewRemovedMessage(com.vaadin.ui.Component view)
          Checks if view component implements the TPTView interface and if so, invokes the appropriate method to notify a view on event happened
 void fragmentChanged(com.vaadin.ui.UriFragmentUtility.FragmentChangedEvent fragmentChangedEvent)
           
 com.vaadin.ui.Component getCurrentView()
          Provides the actual component of the current view
 java.lang.String getCurrentViewName()
          Provides name of the currently active view
 java.lang.String getFailsafeViewName()
          Provides the current failsafe view name (null by default) set by a setFailsafeViewName
protected  java.lang.String getPureViewName(java.lang.String viewName)
          Extracts the pure name from the view name, combined with the parameters.
 com.vaadin.ui.Component getView(java.lang.String viewName)
          Get the actual view component by the view name
 java.lang.String getViewName(com.vaadin.ui.Component view)
          Resolves view name by the actual view component instance.
protected  java.lang.String getViewParameters(java.lang.String viewName)
          Extracts the parameters from a view name if it was combined with such parameters.
 boolean isViewActive(java.lang.String viewName)
          Checks, if the specified view available and active
 boolean isViewAvailable(java.lang.String viewName)
          Checks if the specified view exists in the component
 void removeAllComponents()
          Removes all components but uri manager.
 TPTMultiView removeView(java.lang.String viewName)
          Removes view.
 TPTMultiView replaceView(java.lang.String viewName, com.vaadin.ui.Component view)
          Replaces an old view with the new one.
 void setFailsafeViewName(java.lang.String name)
          Sets the failsafe view name.
 void switchView(java.lang.String viewId)
          Switches the active view to a new one, causing display to update.
 
Methods inherited from class com.vaadin.ui.AbstractOrderedLayout
addComponent, addComponent, addComponentAsFirst, addListener, changeVariables, getComponentAlignment, getComponentIterator, getExpandRatio, isSpacing, isSpacingEnabled, paintContent, removeComponent, removeListener, replaceComponent, setComponentAlignment, setComponentAlignment, setComponentAlignment, setExpandRatio, setSpacing
 
Methods inherited from class com.vaadin.ui.AbstractLayout
getMargin, setMargin, setMargin, setMargin
 
Methods inherited from class com.vaadin.ui.AbstractComponentContainer
addListener, addListener, attach, detach, fireComponentAttachEvent, fireComponentDetachEvent, moveComponentsFrom, removeListener, removeListener, requestRepaintAll, setEnabled, setHeight, setWidth
 
Methods inherited from class com.vaadin.ui.AbstractComponent
addListener, addListener, addListener, addListener, addListener, addStyleName, childRequestedRepaint, fireComponentErrorEvent, fireComponentEvent, fireEvent, focus, getApplication, getCaption, getComponentError, getData, getDebugId, getDescription, getErrorHandler, getErrorMessage, getHeight, getHeightUnits, getIcon, getLocale, getParent, getStyle, getStyleName, getTag, getWidth, getWidthUnits, getWindow, handleError, isEnabled, isImmediate, isReadOnly, isVisible, paint, removeListener, removeListener, removeListener, removeListener, removeListener, removeListener, removeStyleName, requestRepaint, requestRepaintRequests, setCaption, setComponentError, setData, setDebugId, setDescription, setErrorHandler, setHeight, setHeight, setHeightUnits, setIcon, setImmediate, setLocale, setParent, setReadOnly, setSizeFull, setSizeUndefined, setStyle, setStyleName, setVisible, setWidth, setWidth, setWidthUnits
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.vaadin.ui.ComponentContainer
addListener, addListener, moveComponentsFrom, removeListener, removeListener, requestRepaintAll
 
Methods inherited from interface com.vaadin.ui.Component
addListener, addStyleName, attach, childRequestedRepaint, detach, getApplication, getCaption, getIcon, getLocale, getParent, getStyleName, getWindow, isEnabled, isReadOnly, isVisible, removeListener, removeStyleName, setCaption, setEnabled, setIcon, setParent, setReadOnly, setStyleName, setVisible
 
Methods inherited from interface com.vaadin.terminal.Paintable
addListener, getDebugId, paint, removeListener, requestRepaint, requestRepaintRequests, setDebugId
 
Methods inherited from interface com.vaadin.terminal.VariableOwner
isImmediate
 
Methods inherited from interface com.vaadin.terminal.Sizeable
getHeight, getHeightUnits, getWidth, getWidthUnits, setHeight, setHeight, setHeight, setHeightUnits, setSizeFull, setSizeUndefined, setWidth, setWidth, setWidth, setWidthUnits
 

Field Detail

views

private java.util.Map<java.lang.String,com.vaadin.ui.Component> views
Internal map, where we'll store all registered views


delayedViews

private java.util.Map<java.lang.String,java.lang.Class<? extends com.vaadin.ui.Component>> delayedViews
Map to store lazy-loading views. If actual view instance is not present in the "views" map, TPTView will try to find and instantiate it on activation from delayedViews map.


currentView

private java.lang.String currentView
Name of currently displayed view


uriManager

private com.vaadin.ui.UriFragmentUtility uriManager
URI manager, that tracks browser address bar changes and automatically switches the view, if enabled.


uriManagerEnabled

private boolean uriManagerEnabled
URI manager activation flag. When set to false, uri changes will not cause the current view to switch.


lastChangedFragment

private java.lang.String lastChangedFragment
Last fragment, which was set by an URI Fragment Utility. Used to track and eliminate duplicate view activation events


failsafeView

private java.lang.String failsafeView
Contains a failsafe view, to which view manager should switch in case actual view name will be invalid or view will not exists. If failsafe view is null - an IllegalArgumentException will be thrown on attempt to switch to non existing view. When a failsafe view is activated, the non-existed view name is passed there as a view parameter. Original parameter (if were present) will be passed slash-separated after the view name: nonexview/param

Constructor Detail

TPTMultiView

public TPTMultiView()
Creates a multiview with uri manager disabled. View is based on VerticalLayout, sets it's witdh and height to 100% and does not contain any margins and borders.


TPTMultiView

public TPTMultiView(boolean uriManagerEnabled)
Creates a multiview, optionaly activating or deactivating the uri manager. View is based on VerticalLayout, sets it's witdh and height to 100% and does not contain any margins and borders.

Parameters:
uriManagerEnabled - enables or disabled the URI manager. Enabling the URI manager will cause this component to automatically switch the current view if URI text in the browser is changed.
Method Detail

setFailsafeViewName

public void setFailsafeViewName(java.lang.String name)
Sets the failsafe view name. When you try to switch to a non-existing view and failsafe view name is defined, view manager will switch to it (keeping your view parameters as well). If no filesafe view name is set - an IllegalArgumentException is will be thrown in attempt to go to non existing view. If failsafe view name will also not exists, then IllegalArgumentException will be thrown.

Parameters:
name - view name to switch to in case actual view is not found.

getFailsafeViewName

public java.lang.String getFailsafeViewName()
Provides the current failsafe view name (null by default) set by a setFailsafeViewName

Returns:
current failsafe view name

addView

public TPTMultiView addView(java.lang.String viewName,
                            com.vaadin.ui.Component view)
Registers a new view. If this is a first view, it also becomes an active, e.g. it will be displayed. If this is not a first view - no active view will be changed, you'll need to call switchView(...) to show this view.

If you'll try to add a view with duplicate name, an IllegalArgumentException will be thrown.

Parameters:
viewName - unique name of this view
view - actual component of a view
Returns:
self instance in order to simplify adding multiple views at one line.

addView

public void addView(java.lang.String viewName,
                    java.lang.Class<? extends com.vaadin.ui.Component> viewClass)
Adds a lazy loading view. When this view will be called for activation for the first time, TPTView will instantiate the given class by calling default constructor. Please note, that in event you add a lazy view, you'll not receive 'viewAttached' event until view is activated. Once activated, you'll receive viewAttached event first and then immideately receive a viewActivated event as a second one.

Parameters:
viewName - view name
viewClass - class, that represnets the view. Class must have a default constructor.

replaceView

public TPTMultiView replaceView(java.lang.String viewName,
                                com.vaadin.ui.Component view)
Replaces an old view with the new one. If you replaces an active view, it will be updated on the display as well.

If you'll try to replace a view with that is not yet exists, an IllegalArgumentException will be thrown.

Parameters:
viewName - view name to replace
view - new view component
Returns:
self instance to simplify replacing multiple views at one line

removeView

public TPTMultiView removeView(java.lang.String viewName)
Removes view. If you remove an active view, the display become blank and no active view will exists anymore, so you'll have to call switchView to display a new one.

If you'll try to remove a view that is not exists, an IllegalArgumentException will be thrown.

Parameters:
viewName - view name to remove.
Returns:
self instance in order to simplify multiple removes at one line.

switchView

public void switchView(java.lang.String viewId)
Switches the active view to a new one, causing display to update. You may also switch to a null view in order to clear the display.

If you'll try to switch to a view that does not exists, an IllegalArgumentException will be thrown in casd URI manager is not enabled or noting will happen otherwise.

Parameters:
viewId - new active view name or name, combined with the view parameters, separated by a slash character, for instance: myview equals myview/12345 but in second case, string 12345 will be passed to a view as viewActivated method parameter, in case the view object being swithced to implements TPTView interface. If not - custom parameter will be simply ignored.

isViewAvailable

public boolean isViewAvailable(java.lang.String viewName)
Checks if the specified view exists in the component

Parameters:
viewName - view name to check
Returns:
TRUE, if the specified view name exists

isViewActive

public boolean isViewActive(java.lang.String viewName)
Checks, if the specified view available and active

Parameters:
viewName - view nameto chexk
Returns:
TRUE, if the specified view name exists and a view, associated with it is active, e.g. displayed ont he screen.

getView

public com.vaadin.ui.Component getView(java.lang.String viewName)
Get the actual view component by the view name

Parameters:
viewName - view name.
Returns:
view component, associated with the specified name. If non-existing name is given, an IllegalArgumentException will be thrown.

getCurrentViewName

public java.lang.String getCurrentViewName()
Provides name of the currently active view

Returns:
name of the view being active

getCurrentView

public com.vaadin.ui.Component getCurrentView()
Provides the actual component of the current view

Returns:
component of the current view

removeAllComponents

public void removeAllComponents()
Removes all components but uri manager. This is done in order to protect uri manager from accidental removals from a multiview as the removal will breal uri change detection. If you'll need to remove the uri manager for some reason, please use explicit removal.

Specified by:
removeAllComponents in interface com.vaadin.ui.ComponentContainer
Overrides:
removeAllComponents in class com.vaadin.ui.AbstractComponentContainer

getViewName

public java.lang.String getViewName(com.vaadin.ui.Component view)
Resolves view name by the actual view component instance.

Parameters:
view - view component instance to resolve name of
Returns:
view name of the given component or null, if no such component found in the list of registered views.

fragmentChanged

public void fragmentChanged(com.vaadin.ui.UriFragmentUtility.FragmentChangedEvent fragmentChangedEvent)
Specified by:
fragmentChanged in interface com.vaadin.ui.UriFragmentUtility.FragmentChangedListener

fireViewActivatedMessage

protected void fireViewActivatedMessage(com.vaadin.ui.Component view,
                                        java.lang.String previousViewId,
                                        java.lang.String parameters)
Checks if view component implements the TPTView interface and if so, invokes the appropriate method to notify a view on event happened

Parameters:
view - view being activated (showing)
parameters - parameters, passed to a view (if any)

fireViewDeactivatedMessage

protected void fireViewDeactivatedMessage(com.vaadin.ui.Component view,
                                          java.lang.String newViewId)
Checks if view component implements the TPTView interface and if so, invokes the appropriate method to notify a view on event happened

Parameters:
view - view being deactivated (hidden)

fireViewAttachedMessage

protected void fireViewAttachedMessage(com.vaadin.ui.Component view)
Checks if view component implements the TPTView interface and if so, invokes the appropriate method to notify a view on event happened

Parameters:
view - new view being added to a multiview component (added)

fireViewRemovedMessage

protected void fireViewRemovedMessage(com.vaadin.ui.Component view)
Checks if view component implements the TPTView interface and if so, invokes the appropriate method to notify a view on event happened

Parameters:
view - view being removed from a multiview component (removed)

getPureViewName

protected java.lang.String getPureViewName(java.lang.String viewName)
Extracts the pure name from the view name, combined with the parameters. Parameters are separated from a view name by a slash character.

Parameters:
viewName - view name or view name,combined with the view parameters
Returns:
always the pure view name without parameters

getViewParameters

protected java.lang.String getViewParameters(java.lang.String viewName)
Extracts the parameters from a view name if it was combined with such parameters.

Parameters:
viewName - view name or view name, combined with the view parameters
Returns:
parameters , extracted from the view name if it was combined with the parameters. If no parameters present, an empty string will be returned.