Created
          October 27, 2013 20:10 
        
      - 
      
- 
        Save ulmangt/7187336 to your computer and use it in GitHub Desktop. 
    An example demonstrating addition of a second time cursor to 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 javax.media.opengl.GL2; | |
| import com.metsci.glimpse.axis.Axis1D; | |
| import com.metsci.glimpse.axis.tagged.Tag; | |
| import com.metsci.glimpse.axis.tagged.TaggedAxis1D; | |
| import com.metsci.glimpse.context.GlimpseBounds; | |
| import com.metsci.glimpse.event.mouse.GlimpseMouseAdapter; | |
| import com.metsci.glimpse.event.mouse.GlimpseMouseEvent; | |
| import com.metsci.glimpse.event.mouse.ModifierKey; | |
| import com.metsci.glimpse.examples.Example; | |
| import com.metsci.glimpse.painter.base.GlimpseDataPainter1D; | |
| import com.metsci.glimpse.plot.stacked.StackedPlot2D.Orientation; | |
| import com.metsci.glimpse.plot.timeline.StackedTimePlot2D; | |
| import com.metsci.glimpse.support.color.GlimpseColor; | |
| import com.metsci.glimpse.support.settings.OceanLookAndFeel; | |
| /** | |
| * An example demonstrating addition of a second time cursor to a StackedTimePlot2D | |
| * @author ulman | |
| */ | |
| public class ExtraTimeCursorExample extends CollapsibleTimelinePlotExample | |
| { | |
| public static void main( String[] args ) throws Exception | |
| { | |
| Example example = Example.showWithSwing( new ExtraTimeCursorExample( ) ); | |
| // use the ocean look and feel | |
| example.getCanvas( ).setLookAndFeel( new OceanLookAndFeel( ) ); | |
| } | |
| @Override | |
| public StackedTimePlot2D getLayout( ) | |
| { | |
| StackedTimePlot2D plot = super.getLayout( ); | |
| // choose a name for the tag which will define the time for the new time cursor | |
| final String customTag = "MyCustomTagName"; | |
| // add a painter to the StackedTimePlot2D overlay layout to paint the new time cursor | |
| // adding the painter to the overlay layout ensures it will appear about everything painter in the plots | |
| plot.getOverlayLayout( ).addPainter( new SimpleTimeCursorPainter( customTag, plot.getOrientation( ) ) ); | |
| // add a mouse listener which will reposition the custom time cusor when | |
| // the timeline is clicked while holding down the Ctrl key | |
| plot.getOverlayLayout( ).addGlimpseMouseListener( new GlimpseMouseAdapter( ) | |
| { | |
| @Override | |
| public void mousePressed( GlimpseMouseEvent event ) | |
| { | |
| // check whether or not the Ctrl key is down | |
| if ( event.isKeyDown( ModifierKey.Ctrl ) ) | |
| { | |
| // get the time axis (we know it will be a TaggedAxis1D) | |
| TaggedAxis1D timeAxis = ( TaggedAxis1D ) event.getAxis1D( ); | |
| // get the x axis value for the click position | |
| double value = event.getAxisCoordinatesX( ); | |
| // create our custom axis tag if it does not exist | |
| Tag tag = timeAxis.getTag( customTag ); | |
| if ( tag == null ) | |
| { | |
| tag = timeAxis.addTag( customTag, value ); | |
| } | |
| // set the value of the tag to the click position | |
| tag.setValue( value ); | |
| // update the axis | |
| timeAxis.validate( ); | |
| } | |
| } | |
| } ); | |
| return plot; | |
| } | |
| /** | |
| * A simple painter which draws a line a the position of a specified TaggedAxis1D Tag. | |
| * @author ulma | |
| */ | |
| public static class SimpleTimeCursorPainter extends GlimpseDataPainter1D | |
| { | |
| protected float[] color = GlimpseColor.fromColorRgba( 120, 51, 155, 255 ); | |
| protected float width = 3.0f; | |
| protected String tagName; | |
| Orientation orientation; | |
| public SimpleTimeCursorPainter( String tagName, Orientation orientation ) | |
| { | |
| this.orientation = orientation; | |
| this.tagName = tagName; | |
| } | |
| @Override | |
| public void paintTo( GL2 gl, GlimpseBounds bounds, Axis1D axis ) | |
| { | |
| TaggedAxis1D taggedAxis = ( TaggedAxis1D ) axis; | |
| Tag tag = taggedAxis.getTag( tagName ); | |
| if ( tag == null ) return; | |
| float value = ( float ) tag.getValue( ); | |
| GlimpseColor.glColor( gl, color ); | |
| gl.glLineWidth( width ); | |
| gl.glBegin( GL2.GL_LINES ); | |
| try | |
| { | |
| if ( orientation == Orientation.VERTICAL ) | |
| { | |
| gl.glVertex2f( value, 0 ); | |
| gl.glVertex2f( value, bounds.getHeight( ) ); | |
| } | |
| else | |
| { | |
| gl.glVertex2f( 0, value ); | |
| gl.glVertex2f( bounds.getWidth( ), value ); | |
| } | |
| } | |
| finally | |
| { | |
| gl.glEnd( ); | |
| } | |
| } | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment