[8780cbec] | 1 | package com.algomagic.atn; |
---|
| 2 | |
---|
| 3 | import java.util.*; |
---|
| 4 | import java.awt.*; |
---|
| 5 | import java.awt.event.*; |
---|
| 6 | import java.awt.geom.*; |
---|
| 7 | import javax.swing.*; |
---|
| 8 | import javax.swing.border.*; |
---|
| 9 | import javax.swing.event.*; |
---|
| 10 | |
---|
| 11 | |
---|
| 12 | public class ControlPanel |
---|
| 13 | extends JPanel |
---|
| 14 | implements ActionListener, VisualizationConstants |
---|
| 15 | { |
---|
| 16 | |
---|
| 17 | JButton _play, _pause, _stepForward, _stepBack, _rewind, _fastForward, _stop; |
---|
| 18 | InternalObservable _observable; |
---|
| 19 | Visualization _vis; |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | public ControlPanel( Properties properties, Visualization vis ) { |
---|
| 23 | super( ); |
---|
| 24 | |
---|
| 25 | _vis = vis; |
---|
| 26 | _observable = new InternalObservable( ); |
---|
| 27 | Class clazz = this.getClass( ); |
---|
| 28 | |
---|
| 29 | _play = new JButton( new ImageIcon( clazz.getResource( "/resources/Play24.gif" ), "play" ) ); |
---|
| 30 | _play.setActionCommand( "play" ); |
---|
| 31 | _play.addActionListener( this ); |
---|
| 32 | _play.setEnabled( true ); |
---|
| 33 | |
---|
| 34 | _pause = new JButton( new ImageIcon( clazz.getResource( "/resources/Pause24.gif" ), "pause" ) ); |
---|
| 35 | _pause.setActionCommand( "pause" ); |
---|
| 36 | _pause.addActionListener( this ); |
---|
| 37 | _pause.setEnabled( false ); |
---|
| 38 | |
---|
| 39 | _stepForward = new JButton( new ImageIcon( clazz.getResource( "/resources/StepForward24.gif" ), "step forward" ) ); |
---|
| 40 | _stepForward.setActionCommand( "stepf" ); |
---|
| 41 | _stepForward.addActionListener( this ); |
---|
| 42 | _stepForward.setEnabled( true ); |
---|
| 43 | _stepForward.setMnemonic( 70 ); |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | _stepBack = new JButton( new ImageIcon( clazz.getResource( "/resources/StepBack24.gif" ), "step back" ) ); |
---|
| 47 | _stepBack.setActionCommand( "stepb" ); |
---|
| 48 | _stepBack.addActionListener( this ); |
---|
| 49 | _stepBack.setEnabled( true ); |
---|
| 50 | _stepBack.setMnemonic( 66 ); |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | _rewind = new JButton( new ImageIcon( clazz.getResource( "/resources/Rewind24.gif" ), "rewind" ) ); |
---|
| 54 | _rewind.setActionCommand( "rewind" ); |
---|
| 55 | _rewind.addActionListener( this ); |
---|
| 56 | _rewind.setEnabled( true ); |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | _fastForward = new JButton( new ImageIcon( clazz.getResource( "/resources/FastForward24.gif" ), "fast forward" ) ); |
---|
| 61 | _fastForward.setActionCommand( "ff" ); |
---|
| 62 | _fastForward.addActionListener( this ); |
---|
| 63 | _fastForward.setEnabled( true ); |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | setStyle( _play ); |
---|
| 67 | setStyle( _pause ); |
---|
| 68 | setStyle( _stepForward ); |
---|
| 69 | setStyle( _stepBack ); |
---|
| 70 | setStyle( _rewind ); |
---|
| 71 | setStyle( _fastForward ); |
---|
| 72 | |
---|
| 73 | setLayout( new BoxLayout( this, BoxLayout.X_AXIS ) ); |
---|
| 74 | add( _rewind ); |
---|
| 75 | add( _stepBack ); |
---|
| 76 | add( _play ); |
---|
| 77 | add( _pause ); |
---|
| 78 | add( _stepForward ); |
---|
| 79 | add( _fastForward ); |
---|
| 80 | |
---|
| 81 | JCheckBox loop = |
---|
| 82 | new JCheckBox( "loop", (new Boolean( properties.getProperty( LOOP_PROP, LOOP_DEFAULT ) )).booleanValue( ) ); |
---|
| 83 | |
---|
| 84 | loop.addItemListener( new ItemListener( ) { |
---|
| 85 | public void itemStateChanged( ItemEvent e ) { |
---|
| 86 | int s = e.getStateChange( ); |
---|
| 87 | Boolean state = Boolean.TRUE; |
---|
| 88 | |
---|
| 89 | if( s == ItemEvent.DESELECTED ) { |
---|
| 90 | state = Boolean.FALSE; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | _observable.notify( state ); |
---|
| 94 | |
---|
| 95 | } |
---|
| 96 | } ); |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | add( Box.createHorizontalStrut( 10 ) ); |
---|
| 100 | add( loop ); |
---|
| 101 | |
---|
| 102 | |
---|
| 103 | JCheckBox raise = |
---|
| 104 | new JCheckBox( "auto raise", true ); |
---|
| 105 | raise.addItemListener( new ItemListener( ) { |
---|
| 106 | |
---|
| 107 | public void itemStateChanged( ItemEvent e ) { |
---|
| 108 | int s = e.getStateChange( ); |
---|
| 109 | boolean state = true; |
---|
| 110 | |
---|
| 111 | if( s == ItemEvent.DESELECTED ) { |
---|
| 112 | state = false; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | _vis.setAutoRaise( state ); |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | } ); |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | add( Box.createHorizontalStrut( 10 ) ); |
---|
| 124 | add( raise ); |
---|
| 125 | |
---|
| 126 | |
---|
| 127 | |
---|
| 128 | JSpinner spinner = |
---|
| 129 | new JSpinner( new SpinnerNumberModel( Integer.parseInt( properties.getProperty( DELAY_PROP, DELAY_DEFAULT ) ), |
---|
| 130 | 0, |
---|
| 131 | 5000, |
---|
| 132 | 100 ) ); |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | JLabel spinnerLabel = new JLabel( "delay (msec)" ); |
---|
| 136 | |
---|
| 137 | spinner.addChangeListener( new ChangeListener( ) { |
---|
| 138 | public void stateChanged( ChangeEvent e ) { |
---|
| 139 | _observable.notify( e.getSource( ) ); |
---|
| 140 | } |
---|
| 141 | } ); |
---|
| 142 | |
---|
| 143 | |
---|
| 144 | add( spinner ); |
---|
| 145 | add( Box.createHorizontalStrut( 5 ) ); |
---|
| 146 | add( spinnerLabel ); |
---|
| 147 | |
---|
| 148 | |
---|
| 149 | PanelHelper.setBorder( this, "Controls" ); |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | public void addObserver( Observer o ) { |
---|
| 154 | _observable.addObserver( o ); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | |
---|
| 158 | private void setStyle( JButton b ) { |
---|
| 159 | b.setMinimumSize( new Dimension( 24, 24 ) ); |
---|
| 160 | b.setMaximumSize( new Dimension( 24, 24 ) ); |
---|
| 161 | b.setPreferredSize( new Dimension( 24, 24 ) ); |
---|
| 162 | |
---|
| 163 | b.setBorderPainted( false ); |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | public void actionPerformed( ActionEvent e ) { |
---|
| 167 | String cmd = e.getActionCommand( ); |
---|
| 168 | if( cmd.equals( "play" ) ) { |
---|
| 169 | _stepForward.setEnabled( false ); |
---|
| 170 | _stepBack.setEnabled( false ); |
---|
| 171 | _rewind.setEnabled( false ); |
---|
| 172 | _fastForward.setEnabled( false ); |
---|
| 173 | _play.setEnabled( false ); |
---|
| 174 | _pause.setEnabled( true ); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | else if( cmd.equals( "pause" ) ) { |
---|
| 178 | _stepForward.setEnabled( true ); |
---|
| 179 | _stepBack.setEnabled( true ); |
---|
| 180 | _rewind.setEnabled( true ); |
---|
| 181 | _fastForward.setEnabled( true ); |
---|
| 182 | _play.setEnabled( true ); |
---|
| 183 | _pause.setEnabled( false ); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | |
---|
| 187 | _observable.notify( e.getActionCommand( ) ); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | |
---|
| 191 | private class InternalObservable extends Observable { |
---|
| 192 | public void notify( Object arg ) { |
---|
| 193 | setChanged( ); |
---|
| 194 | notifyObservers( arg ); |
---|
| 195 | } |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | } |
---|
| 199 | |
---|