[8780cbec] | 1 | package com.algomagic.atn; |
---|
| 2 | |
---|
| 3 | import java.util.*; |
---|
| 4 | import javax.swing.*; |
---|
| 5 | |
---|
| 6 | public class PlayBackController |
---|
| 7 | extends Observable |
---|
| 8 | implements Observer, VisualizationConstants |
---|
| 9 | |
---|
| 10 | { |
---|
| 11 | private int _delay; |
---|
| 12 | private boolean _loop; |
---|
| 13 | private List _updates; |
---|
| 14 | private int _index = 0; |
---|
| 15 | private boolean _clear = true; |
---|
| 16 | private boolean _playing = false; |
---|
| 17 | |
---|
| 18 | public PlayBackController( List updates, Properties properties ) { |
---|
| 19 | _delay = Integer.parseInt( properties.getProperty( DELAY_PROP, DELAY_DEFAULT ) ); |
---|
| 20 | _loop = (new Boolean( properties.getProperty( LOOP_PROP, LOOP_DEFAULT ) )).booleanValue( ); |
---|
| 21 | _updates = updates; |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | public void update( Observable o, Object arg ) { |
---|
| 25 | if( arg instanceof String ) { |
---|
| 26 | String cmd = (String)arg; |
---|
| 27 | |
---|
| 28 | if( cmd.equals( "play" ) ) { |
---|
| 29 | _playing = true; |
---|
| 30 | new Thread( new PlayThread( ) ).start( ); |
---|
| 31 | } else if( cmd.equals( "pause" ) ) { |
---|
| 32 | _playing = false; |
---|
| 33 | } else if( cmd.equals( "stepf" ) ) { |
---|
| 34 | stepf( ); |
---|
| 35 | } else if( cmd.equals( "stepb" ) ) { |
---|
| 36 | stepb( ); |
---|
| 37 | } else if( cmd.equals( "ff" ) ) { |
---|
| 38 | ff( ); |
---|
| 39 | } else if( cmd.equals( "rewind" ) ) { |
---|
| 40 | rewind( ); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | } else if( arg instanceof JSpinner ) { |
---|
| 44 | SpinnerNumberModel snm = (SpinnerNumberModel)((JSpinner)arg).getModel( ); |
---|
| 45 | _delay = snm.getNumber( ).intValue( ); |
---|
| 46 | } else if (arg instanceof Boolean ) { |
---|
| 47 | _loop = ((Boolean)arg).booleanValue( ); |
---|
| 48 | } |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | public int getIndex( ) { |
---|
| 53 | return _index; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | private synchronized void stepf( ) { |
---|
| 58 | if( _index == _updates.size( ) ) { |
---|
| 59 | _index = 0; |
---|
| 60 | if( _playing == true && _loop == false ) { |
---|
| 61 | _playing = false; |
---|
| 62 | } |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | if( _index == 0 && _clear == false ) { |
---|
| 67 | _clear = true; |
---|
| 68 | |
---|
| 69 | setChanged( ); |
---|
| 70 | notifyObservers( "CLEAR" ); |
---|
| 71 | |
---|
| 72 | return; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | _clear = false; |
---|
| 76 | |
---|
| 77 | PanelUpdate u = (PanelUpdate)_updates.get(_index); |
---|
| 78 | u.setUndo( false ); |
---|
| 79 | |
---|
| 80 | setChanged( ); |
---|
| 81 | notifyObservers( u ); |
---|
| 82 | _index++; |
---|
| 83 | |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | private synchronized void stepb( ) { |
---|
| 87 | // System.out.println( "steck back " + _index ); |
---|
| 88 | // System.out.print( "stepb: " + _index + " " ); |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | if( _index == 0 ) { |
---|
| 92 | ff( ); |
---|
| 93 | // System.out.println( _index ); |
---|
| 94 | |
---|
| 95 | return; |
---|
| 96 | } |
---|
| 97 | _index--; |
---|
| 98 | |
---|
| 99 | PanelUpdate u = (PanelUpdate)_updates.get( _index ); |
---|
| 100 | u.setUndo( true ); |
---|
| 101 | |
---|
| 102 | setChanged( ); |
---|
| 103 | notifyObservers( u ); |
---|
| 104 | // System.out.println( _index ); |
---|
| 105 | |
---|
| 106 | if( _index == 0 ) { |
---|
| 107 | _clear = true; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | private synchronized void ff( ) { |
---|
| 113 | int i; |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | // System.out.print( "ff: " + _index + " " ); |
---|
| 117 | |
---|
| 118 | for( i=_index; i < _updates.size( ); i++ ) { |
---|
| 119 | setChanged( ); |
---|
| 120 | PanelUpdate u = (PanelUpdate)_updates.get(i); |
---|
| 121 | u.setUndo( false ); |
---|
| 122 | |
---|
| 123 | _index=i; |
---|
| 124 | notifyObservers( u ); |
---|
| 125 | |
---|
| 126 | // System.out.println( "here" ); |
---|
| 127 | |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | _clear = false; |
---|
| 131 | _index=i; |
---|
| 132 | |
---|
| 133 | // System.out.println( _index ); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | |
---|
| 137 | private synchronized void rewind( ) { |
---|
| 138 | _index = 0; |
---|
| 139 | _clear = true; |
---|
| 140 | |
---|
| 141 | setChanged( ); |
---|
| 142 | notifyObservers( "CLEAR" ); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | |
---|
| 146 | private class PlayThread implements Runnable { |
---|
| 147 | public void run( ) { |
---|
| 148 | while( _playing ) { |
---|
| 149 | try { |
---|
| 150 | stepf( ); |
---|
| 151 | Thread.currentThread( ).sleep( _delay ); |
---|
| 152 | } catch( InterruptedException ie ) { |
---|
| 153 | throw new RuntimeException( ie ); |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | } |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | |
---|