Created
          October 25, 2013 19:01 
        
      - 
      
- 
        Save ulmangt/7160032 to your computer and use it in GitHub Desktop. 
    Example demonstrating animating of the selected time of a StackedTimePlot2D
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import java.util.concurrent.locks.Condition; | |
| import java.util.concurrent.locks.ReentrantLock; | |
| import com.metsci.glimpse.axis.tagged.Tag; | |
| import com.metsci.glimpse.axis.tagged.TaggedAxis1D; | |
| import com.metsci.glimpse.examples.Example; | |
| import com.metsci.glimpse.plot.timeline.StackedTimePlot2D; | |
| import com.metsci.glimpse.support.settings.OceanLookAndFeel; | |
| public class ReplayLoopTimelineExample extends HorizontalTimelinePlotExample | |
| { | |
| protected static final long sleepMillis = 20; | |
| protected static final int stepsPerCycle = 200; | |
| public static void main( String[] args ) throws Exception | |
| { | |
| Example example = Example.showWithSwing( new ReplayLoopTimelineExample( ) ); | |
| // use the ocean look and feel | |
| example.getCanvas( ).setLookAndFeel( new OceanLookAndFeel( ) ); | |
| } | |
| @Override | |
| public StackedTimePlot2D getLayout( ) | |
| { | |
| StackedTimePlot2D plot = super.getLayout( ); | |
| ReplayManager maanger = new ReplayManager( plot ); | |
| maanger.start( ); | |
| return plot; | |
| } | |
| /** | |
| * A threaded manager class which handles animating of the selected time bar of a StackedTimePlot2D. | |
| */ | |
| public static class ReplayManager | |
| { | |
| // locks allowing ReplayManager to act in a threadsafe manner | |
| // (play and pause may be called safely from any thread) | |
| // these may not be necessary for simple use cases | |
| protected Condition playbackCondition; | |
| protected ReentrantLock playbackLock; | |
| // whether the animation is currently playing or paused | |
| protected boolean play = true; | |
| // a reference to the thread on which the playback is occurring | |
| protected Thread playbackThread; | |
| protected Tag min; | |
| protected Tag max; | |
| protected Tag selected; | |
| protected TaggedAxis1D axis; | |
| public ReplayManager( StackedTimePlot2D plot ) | |
| { | |
| this( plot.getTimeSelectionMinTag( ), plot.getTimeSelectionMaxTag( ), plot.getTimeSelectionTag( ), plot.getTimeAxis( ) ); | |
| } | |
| public ReplayManager( Tag min, Tag max, Tag selected, TaggedAxis1D axis ) | |
| { | |
| this.playbackLock = new ReentrantLock( ); | |
| this.playbackCondition = playbackLock.newCondition( ); | |
| this.min = min; | |
| this.max = max; | |
| this.selected = selected; | |
| this.axis = axis; | |
| } | |
| public void start( ) | |
| { | |
| playbackThread = new Thread( ) | |
| { | |
| @Override | |
| public void run( ) | |
| { | |
| while ( true ) | |
| { | |
| double end = 0; | |
| double start = 0; | |
| double selection = 0; | |
| playbackLock.lock( ); | |
| try | |
| { | |
| // pause playback if the pause() method has been called since the last loop iteration | |
| while ( !play ) | |
| { | |
| playbackCondition.await( ); | |
| } | |
| end = max.getValue( ); | |
| start = min.getValue( ); | |
| selection = selected.getValue( ); | |
| } | |
| catch ( InterruptedException e ) | |
| { | |
| } | |
| finally | |
| { | |
| playbackLock.unlock( ); | |
| } | |
| // calculate the amount to update the selected time by | |
| double step = Math.max( 1, ( end - start ) / stepsPerCycle ); | |
| selection += step; | |
| // wrap the selected time if it reaches the beginning or end of the time window | |
| if ( selection < start ) selection = start; | |
| if ( selection > end ) selection = start; | |
| // update the selected time and validate the axis (necessary whenever axis parameters | |
| // are changed programmatically) | |
| selected.setValue( selection ); | |
| axis.validate( ); | |
| // sleep the animation thread for a small amount of time | |
| try | |
| { | |
| Thread.sleep( sleepMillis ); | |
| } | |
| catch ( InterruptedException e ) | |
| { | |
| } | |
| } | |
| } | |
| }; | |
| playbackThread.setName( "time-manager" ); | |
| playbackThread.start( ); | |
| } | |
| public void play( ) | |
| { | |
| playbackLock.lock( ); | |
| try | |
| { | |
| play = true; | |
| playbackCondition.signalAll( ); | |
| } | |
| finally | |
| { | |
| playbackLock.unlock( ); | |
| } | |
| } | |
| public void pause( ) | |
| { | |
| playbackLock.lock( ); | |
| try | |
| { | |
| play = false; | |
| } | |
| finally | |
| { | |
| playbackLock.unlock( ); | |
| } | |
| selected.setValue( max.getValue( ) ); | |
| axis.validateTags( ); | |
| } | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment