Created
          November 6, 2013 00:28 
        
      - 
      
- 
        Save ulmangt/7328828 to your computer and use it in GitHub Desktop. 
    An (incomplete) GlimpseCanvas implementation backed by a GLJPanel
  
        
  
    
      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
    
  
  
    
  | package com.metsci.glimpse.canvas; | |
| import static com.metsci.glimpse.gl.util.GLPBufferUtils.*; | |
| import static com.metsci.glimpse.util.logging.LoggerUtils.*; | |
| import java.awt.BorderLayout; | |
| import java.awt.Color; | |
| import java.awt.Dimension; | |
| import java.awt.event.KeyListener; | |
| import java.awt.event.MouseListener; | |
| import java.awt.event.MouseMotionListener; | |
| import java.awt.event.MouseWheelListener; | |
| import java.util.List; | |
| import java.util.concurrent.CopyOnWriteArrayList; | |
| import java.util.logging.Logger; | |
| import javax.media.opengl.DefaultGLCapabilitiesChooser; | |
| import javax.media.opengl.GL; | |
| import javax.media.opengl.GLAutoDrawable; | |
| import javax.media.opengl.GLCapabilities; | |
| import javax.media.opengl.GLContext; | |
| import javax.media.opengl.GLDrawableFactory; | |
| import javax.media.opengl.GLEventListener; | |
| import javax.media.opengl.GLOffscreenAutoDrawable; | |
| import javax.media.opengl.GLProfile; | |
| import javax.media.opengl.awt.GLJPanel; | |
| import javax.swing.JPanel; | |
| import com.metsci.glimpse.context.GlimpseBounds; | |
| import com.metsci.glimpse.context.GlimpseContext; | |
| import com.metsci.glimpse.context.GlimpseContextImpl; | |
| import com.metsci.glimpse.context.GlimpseTarget; | |
| import com.metsci.glimpse.context.GlimpseTargetStack; | |
| import com.metsci.glimpse.event.mouse.swing.MouseWrapperSwing; | |
| import com.metsci.glimpse.gl.GLRunnable; | |
| import com.metsci.glimpse.layout.GlimpseLayout; | |
| import com.metsci.glimpse.support.settings.LookAndFeel; | |
| public class GLJPanelGlimpseCanvas extends JPanel implements GlimpseCanvas | |
| { | |
| private static final Logger logger = Logger.getLogger( GLJPanelGlimpseCanvas.class.getName( ) ); | |
| private static final long serialVersionUID = 1L; | |
| protected GLProfile glProfile; | |
| protected GLCapabilities glCapabilities; | |
| protected GLOffscreenAutoDrawable glDrawableParent; | |
| protected GLOffscreenAutoDrawable glDrawable; | |
| protected GLContext glContext; | |
| protected GLJPanel glPanel; | |
| protected LayoutManager layoutManager; | |
| protected MouseWrapperSwing mouseHelper; | |
| protected boolean isEventConsumer = true; | |
| protected boolean isEventGenerator = true; | |
| protected boolean isDisposed = false; | |
| protected List<GLRunnable> disposeListeners; | |
| public GLJPanelGlimpseCanvas( String profile, GLContext context ) | |
| { | |
| if ( context == null ) | |
| { | |
| this.glDrawableParent = createPixelBuffer( 1, 1 ); | |
| context = this.glDrawableParent.getContext( ); | |
| } | |
| this.glContext = context; | |
| this.glProfile = GLProfile.get( profile ); | |
| this.glCapabilities = new GLCapabilities( glProfile ); | |
| this.glDrawable = GLDrawableFactory.getFactory( glProfile ).createOffscreenAutoDrawable( null, glCapabilities, null, 1, 1, context ); | |
| this.glPanel = new GLJPanel( this.glCapabilities, new DefaultGLCapabilitiesChooser( ), this.glContext ); | |
| this.setLayout( new BorderLayout( ) ); | |
| this.add( this.glPanel, BorderLayout.CENTER ); | |
| this.setBackground( Color.green ); | |
| // workaround to enable the panel to shrink | |
| this.setMinimumSize( new Dimension( 0, 0 ) ); | |
| this.isDisposed = false; | |
| this.mouseHelper = new MouseWrapperSwing( this ); | |
| this.addMouseListener( this.mouseHelper ); | |
| this.addMouseMotionListener( this.mouseHelper ); | |
| this.addMouseWheelListener( this.mouseHelper ); | |
| this.layoutManager = new LayoutManager( ); | |
| this.addGLEventListener( this.glPanel ); | |
| this.disposeListeners = new CopyOnWriteArrayList<GLRunnable>( ); | |
| } | |
| @Override | |
| public GlimpseContext getGlimpseContext( ) | |
| { | |
| return new GlimpseContextImpl( this ); | |
| } | |
| @Override | |
| public void setLookAndFeel( LookAndFeel laf ) | |
| { | |
| for ( GlimpseTarget target : this.layoutManager.getLayoutList( ) ) | |
| { | |
| target.setLookAndFeel( laf ); | |
| } | |
| } | |
| @Override | |
| public void addLayout( GlimpseLayout layout ) | |
| { | |
| this.layoutManager.addLayout( layout ); | |
| } | |
| @Override | |
| public void addLayout( GlimpseLayout layout, int zOrder ) | |
| { | |
| this.layoutManager.addLayout( layout, zOrder ); | |
| } | |
| @Override | |
| public void setZOrder( GlimpseLayout layout, int zOrder ) | |
| { | |
| this.layoutManager.setZOrder( layout, zOrder ); | |
| } | |
| @Override | |
| public void removeLayout( GlimpseLayout layout ) | |
| { | |
| this.layoutManager.removeLayout( layout ); | |
| } | |
| @Override | |
| public void removeAllLayouts( ) | |
| { | |
| this.layoutManager.removeAllLayouts( ); | |
| } | |
| @SuppressWarnings( { "unchecked", "rawtypes" } ) | |
| @Override | |
| public List<GlimpseTarget> getTargetChildren( ) | |
| { | |
| // layoutManager returns an unmodifiable list, thus this cast is typesafe | |
| // (there is no way for the recipient of the List<GlimpseTarget> view to | |
| // add GlimpseTargets which are not GlimpseLayouts to the list) | |
| return ( List ) this.layoutManager.getLayoutList( ); | |
| } | |
| @Override | |
| // the glCanvas covers the entire underlying JPanel, so event listeners should be attached to the glCanvas, not this | |
| public void addMouseListener( MouseListener listener ) | |
| { | |
| this.glPanel.addMouseListener( listener ); | |
| } | |
| @Override | |
| // the glCanvas covers the entire underlying JPanel, so event listeners should be attached to the glCanvas, not this | |
| public void addMouseMotionListener( MouseMotionListener listener ) | |
| { | |
| this.glPanel.addMouseMotionListener( listener ); | |
| } | |
| @Override | |
| // the glCanvas covers the entire underlying JPanel, so event listeners should be attached to the glCanvas, not this | |
| public void addMouseWheelListener( MouseWheelListener listener ) | |
| { | |
| this.glPanel.addMouseWheelListener( listener ); | |
| } | |
| @Override | |
| // the glCanvas covers the entire underlying JPanel, so event listeners should be attached to the glCanvas, not this | |
| public void removeMouseListener( MouseListener listener ) | |
| { | |
| this.glPanel.removeMouseListener( listener ); | |
| } | |
| @Override | |
| // the glCanvas covers the entire underlying JPanel, so event listeners should be attached to the glCanvas, not this | |
| public void removeMouseMotionListener( MouseMotionListener listener ) | |
| { | |
| this.glPanel.removeMouseMotionListener( listener ); | |
| } | |
| @Override | |
| // the glCanvas covers the entire underlying JPanel, so event listeners should be attached to the glCanvas, not this | |
| public void removeMouseWheelListener( MouseWheelListener listener ) | |
| { | |
| this.glPanel.removeMouseWheelListener( listener ); | |
| } | |
| @Override | |
| // the glCanvas covers the entire underlying JPanel, so event listeners should be attached to the glCanvas, not this | |
| public void addKeyListener( KeyListener listener ) | |
| { | |
| this.glPanel.addKeyListener( listener ); | |
| } | |
| @Override | |
| // the glCanvas covers the entire underlying JPanel, so event listeners should be attached to the glCanvas, not this | |
| public void removeKeyListener( KeyListener listener ) | |
| { | |
| this.glPanel.removeKeyListener( listener ); | |
| } | |
| public Dimension getDimension( ) | |
| { | |
| return this.getSize( ); | |
| } | |
| @Override | |
| public GlimpseBounds getTargetBounds( GlimpseTargetStack stack ) | |
| { | |
| return new GlimpseBounds( getDimension( ) ); | |
| } | |
| @Override | |
| public GlimpseBounds getTargetBounds( ) | |
| { | |
| return getTargetBounds( null ); | |
| } | |
| @Override | |
| public void paint( ) | |
| { | |
| this.glPanel.repaint( ); | |
| } | |
| @Override | |
| public GLContext getGLContext( ) | |
| { | |
| return this.glPanel.getContext( ); | |
| } | |
| @Override | |
| public String toString( ) | |
| { | |
| return SwingGlimpseCanvas.class.getSimpleName( ); | |
| } | |
| @Override | |
| public boolean isEventConsumer( ) | |
| { | |
| return this.isEventConsumer; | |
| } | |
| @Override | |
| public void setEventConsumer( boolean consume ) | |
| { | |
| this.isEventConsumer = consume; | |
| } | |
| @Override | |
| public boolean isEventGenerator( ) | |
| { | |
| return this.isEventGenerator; | |
| } | |
| @Override | |
| public void setEventGenerator( boolean generate ) | |
| { | |
| this.isEventGenerator = generate; | |
| } | |
| @Override | |
| public boolean isDisposed( ) | |
| { | |
| return this.isDisposed; | |
| } | |
| @Override | |
| public void dispose( ) | |
| { | |
| this.glPanel.destroy( ); | |
| this.isDisposed = true; | |
| } | |
| @Override | |
| public void addDisposeListener( GLRunnable runnable ) | |
| { | |
| this.disposeListeners.add( runnable ); | |
| } | |
| private void addGLEventListener( GLAutoDrawable drawable ) | |
| { | |
| drawable.addGLEventListener( new GLEventListener( ) | |
| { | |
| @Override | |
| public void init( GLAutoDrawable drawable ) | |
| { | |
| try | |
| { | |
| GL gl = drawable.getGL( ); | |
| gl.setSwapInterval( 0 ); | |
| } | |
| catch ( Exception e ) | |
| { | |
| // without this, repaint rate is tied to screen refresh rate on some systems | |
| // this doesn't work on some machines (Mac OSX in particular) | |
| // but it's not a big deal if it fails | |
| logWarning( logger, "Trouble in init.", e ); | |
| } | |
| } | |
| @Override | |
| public void display( GLAutoDrawable drawable ) | |
| { | |
| for ( GlimpseLayout layout : layoutManager.getLayoutList( ) ) | |
| { | |
| layout.paintTo( getGlimpseContext( ) ); | |
| } | |
| } | |
| @Override | |
| public void reshape( GLAutoDrawable drawable, int x, int y, int width, int height ) | |
| { | |
| for ( GlimpseLayout layout : layoutManager.getLayoutList( ) ) | |
| { | |
| layout.layoutTo( getGlimpseContext( ) ); | |
| } | |
| } | |
| @Override | |
| public void dispose( GLAutoDrawable drawable ) | |
| { | |
| logInfo( logger, "Disposing SwingGlimpseCanvas..." ); | |
| for ( GlimpseLayout layout : layoutManager.getLayoutList( ) ) | |
| { | |
| layout.dispose( getGlimpseContext( ) ); | |
| } | |
| for ( GLRunnable runnable : disposeListeners ) | |
| { | |
| runnable.run( drawable.getContext( ) ); | |
| } | |
| } | |
| } ); | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment