home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / linux / lg / issue29 / HAMILTON / SmartDrive.java < prev    next >
Encoding:
Java Source  |  1998-01-24  |  13.5 KB  |  280 lines

  1. package Jcd; // -- Listing-5 -- SmartDrive.java   1
  2.                                              //   2
  3. import java.io.*;                            //   3
  4. import java.util.*;                          //   4
  5. import java.awt.*;                           //   5
  6. import Jcd.Drive;                            //   6
  7. import Jcd.Monitor;                          //   7
  8.                                              //   8
  9. class SmartDrive 
  10.   extends Drive implements Observer {        //   9
  11.                                              //  10
  12.   public Monitor monitor;                    //  11
  13.   public TrackList tracksToPlay = null;      //  12
  14.   public int program_pos = 0;                //  13
  15.   public boolean singleMode = false;         //  14
  16.                                              //  15
  17.   public SmartDrive(String device,           //  16
  18.                     String module, int flags)
  19.   {                                          //  17
  20.     super(device, module, flags);            //  18
  21.     setUp();                                 //  19
  22.   }                                          //  20
  23.                                              //  21
  24.   protected void setUp()                     //  22
  25.   {                                          //  23
  26.     monitor = new Monitor(this);             //  24
  27.     monitor.addObserver(this);               //  25
  28.     tracksToPlay = new TrackList(30);        //  26
  29.   }                                          //  27
  30.                                              //  28
  31.   public void setSingleMode(boolean state)   //  29
  32.   {                                          //  30
  33.     singleMode = state;                      //  31
  34.   }                                          //  32
  35.                                              //  33
  36.   public void play(int track)                //  34
  37.     throws DriveException
  38.   {                                          //  35
  39.     if (singleMode) single(track);           //  36
  40.     else super.play(track);                  //  37
  41.   }                                          //  38
  42.                                              //  39
  43.   public boolean startPlaying()              //  40 
  44.     throws DriveException   
  45.   {                                          //  41
  46.     if (status() == Drive.STATUS_PAUSED)     //  42
  47.       resume();                              //  43
  48.     else if (!tracksToPlay.isEmpty()) {      //  44
  49.       if (tracksToPlay.atEnd())              //  45
  50.         tracksToPlay.reset();                //  46
  51.       play(tracksToPlay.nextTrack());        //  47
  52.     }                                        //  48
  53.     else play(1);                            //  49
  54.     return true;                             //  50
  55.   }                                          //  51
  56.                                              //  52
  57.   public boolean next() throws DriveException//  53
  58.   {                                          //  54
  59.     if (tracksToPlay.isEmpty()) {            //  55
  60.       int track = currentTrack();            //  56
  61.       int max = numberOfTracks();            //  57
  62.       if (track >= max) return false;        //  58
  63.       play(track + 1);                       //  59
  64.     }                                        //  60
  65.     else {                                   //  61
  66.       int next = tracksToPlay.nextTrack();   //  62
  67.       if (next <= 0)                         //  63
  68.         return false;                        //  64
  69.       play(next);                            //  65
  70.     }                                        //  66
  71.     return true;                             //  67
  72.   }                                          //  68
  73.                                              //  69
  74.   public boolean prev() throws DriveException//  70
  75.   {                                          //  71
  76.     if (tracksToPlay.isEmpty()) {            //  72
  77.       int track = currentTrack();            //  73
  78.       if (track <= 1) return false;          //  74
  79.       play(track - 1);                       //  75
  80.     }                                        //  76
  81.     else {                                   //  77
  82.       int next = tracksToPlay.prevTrack();   //  78
  83.       if (next <= 0) return false;           //  79
  84.       play(next);                            //  80
  85.     }                                        //  81
  86.     return true;                             //  82
  87.   }                                          //  83
  88.                                              //  84
  89.   public int remaining()                     //  85
  90.   {                                          //  86
  91.     int result = 0;                          //  87
  92.     if (tracksToPlay.isEmpty())              //  88
  93.       result = monitor.cdEndAddress -
  94.                  monitor.currentAddress;     //  89
  95.     else {                                   //  90
  96.       int index = 0;                         //  91
  97.       Enumeration iter = tracksToPlay.elements();
  98.       while (iter.hasMoreElements()) {       //  93
  99.         int track =                          //  94
  100.          ((Integer) iter.nextElement()).intValue();
  101.         if (index >= tracksToPlay.stepNumber() &&
  102.             track < monitor.trackLength.length) {
  103.           result += monitor.trackLength[track];
  104.         }                                    //  98
  105.         index++;                             //  99
  106.       }                                      // 100
  107.       if (!tracksToPlay.atStart() && 
  108.           !tracksToPlay.atEnd()) {           // 101
  109.         result -= (monitor.currentAddress -  // 102
  110.                    monitor.trackStartAddress);
  111.       }                                      // 103
  112.     }                                        // 104
  113.     return result;                           // 105
  114.   }                                          // 106
  115.                                              // 107
  116.   public int togglePause()                   // 108
  117.     throws DriveException
  118.   {                                          // 109
  119.     if (status() == Drive.STATUS_PAUSED) {   // 110
  120.       resume();                              // 111
  121.       return Drive.STATUS_PLAY;              // 112
  122.     }                                        // 113
  123.     pause();                                 // 114
  124.     return Drive.STATUS_PAUSED;              // 115
  125.   }                                          // 116
  126.                                              // 117
  127.   public synchronized void update(Observable o, 
  128.                                   Object arg)// 118
  129.   {                                          // 119
  130.     try {                                    // 120
  131.       if (monitor.status != Drive.STATUS_INVALID) {
  132.         if (monitor.cdChanged)               // 122
  133.           tracksToPlay.clear();              // 123
  134.         else if (monitor.status == 
  135.                  Drive.STATUS_PLAY) {        // 124
  136.           int track= monitor.currentTrack;   // 125
  137.           int tend= monitor.trackEndAddress[track];
  138.           if (monitor.currentAddress >= 
  139.               tend - 210) { // Near end of track?
  140.             // Poll frequently so we don't miss the
  141.             // event.                        // 128
  142.             while (currentAddress() < tend &&// 129
  143.               monitor.status == Drive.STATUS_PLAY
  144.               && currentAddress() != 0) {    // 131
  145.               try { Thread.sleep(100); // 100 msec
  146.               }                              // 133
  147.               catch (InterruptedException e) {
  148.               }                              // 135
  149.             }                                // 136
  150.             if (!tracksToPlay.atStart())     // 137
  151.               programEndOfTrack();           // 138
  152.             else            // Normal play   // 139
  153.               normalEndOfTrack(track);       // 140
  154.           }                                  // 141
  155.         }                                    // 142
  156.       }                                      // 143
  157.     }                                        // 144
  158.     catch (DriveException e) {               // 145
  159.       System.out.println("Exception: " + e); // 146
  160.     }                                        // 147
  161.   }                                          // 148
  162.                                              // 149
  163.   protected void programEndOfTrack()         // 150
  164.   {                                          // 151
  165.     try {                                    // 152
  166.       int next = tracksToPlay.nextTrack();   // 153
  167.       if (tracksToPlay.atEnd()) {            // 154
  168.         stop(); tracksToPlay.reset();       
  169.       }
  170.       else if (singleMode) stop();           // 155
  171.       else play(next);                       // 156
  172.     }                                        // 157
  173.     catch (DriveException e) {               // 158
  174.       System.out.println(                    // 159 
  175.        "Program end of track exception: " + e);
  176.     }                                        // 160
  177.   }                                          // 161
  178.                                              // 162
  179.   protected void normalEndOfTrack(int track) // 163
  180.   {  // If during the track we switched play // 164
  181.      // modes, ensure the correct behaviour occurs.
  182.     try {                                    // 165
  183.       if (singleMode) stop();                // 166
  184.       else if (track < monitor.numberOfTracks)
  185.         play(track + 1);                     // 167
  186.     }                                        // 168
  187.     catch (DriveException e) {               // 169
  188.       System.out.println(
  189.        "Normal-Play end of track exception: " + e);
  190.     }                                        // 171
  191.   }                                          // 172
  192. }                                            // 173
  193.                                              // 174
  194. class TrackList extends Vector {             // 175
  195.  
  196.    // Gone past last track.                  // 176
  197.   protected boolean at_end = false;
  198.   protected Integer current = null;          // 177
  199.                                              // 178
  200.   TrackList(int initial_n) {                 // 179
  201.     super(initial_n);
  202.   } 
  203.   public synchronized void addTrack(int t) { // 181
  204.     addElement(new Integer(t)); 
  205.   }
  206.                                              // 182
  207.   public synchronized void removeTrack(int t)// 183
  208.   {                                          // 184
  209.     for (int pos = size() - 1; pos >= 0; pos--) {
  210.       Integer elem = (Integer) (elementAt(pos));
  211.       if (elem.intValue() == t) { // Found   // 187
  212.         removeElement(elem);                 // 188
  213.         if (elem==current) // Select a new current
  214.           current = (Integer) ((pos>=size()) ? 
  215.             lastElement() : elementAt(pos)); // 190
  216.       }                                      // 191
  217.     }                                        // 192
  218.   }                                          // 193
  219.                                              // 194
  220.   public synchronized int stepNumber()       // 195
  221.   {         // Program step number           // 196
  222.     return (current != null) ? 
  223.            indexOf((Object) current) : 0 ;   // 197
  224.   }                                          // 198
  225.                                              // 199
  226.   public synchronized int currentTrack()     // 200
  227.   {                                          // 201
  228.     return 
  229.      (current != null) ? current.intValue() : 0 ;
  230.   }                                          // 203
  231.                                              // 204
  232.   public synchronized int nextTrack()        // 205
  233.   {    // Advance along the list by one.     // 206
  234.     if (size() == 0) return 0;               // 207
  235.     if (current == null)                     // 208
  236.       current = (Integer) firstElement();    // 209
  237.     else {                                   // 210
  238.       int pos= indexOf((Object) current) + 1;// 211
  239.       at_end= pos == size(); //Finished last track?
  240.       current = (Integer) (at_end ? 
  241.         lastElement() : elementAt(pos));     // 213
  242.     }                                        // 214
  243.     return current.intValue();               // 215
  244.   }                                          // 216
  245.                                              // 217
  246.   public synchronized int prevTrack()        // 218
  247.   {                                          // 219
  248.     if (current != null) {                   // 220
  249.       int pos= indexOf((Object) current) - 1;// 221
  250.       current = (Integer) ((pos<0) ?
  251.         firstElement() : elementAt(pos));    // 222
  252.       return current.intValue();             // 223
  253.     }                                        // 224
  254.     return 0;                                // 225
  255.   }                                          // 226
  256.                                              // 227
  257.   public boolean atStart()                   // 228
  258.     { return current == null ; }
  259.   public boolean atEnd() { return at_end; }  // 229
  260.                                              // 230
  261.   public void skipTo(int track)              // 231
  262.   {                                          // 232
  263.     if (size() > 0) {                        // 233
  264.       while (!atEnd())                       // 234
  265.         if (nextTrack() == track && !atEnd())
  266.           return;
  267.       reset();   // Wrap around to the start // 235
  268.       while (!atEnd())                       // 236
  269.         if (nextTrack() == track) 
  270.           return;
  271.       current = (Integer) lastElement();     // 237
  272.     }                                        // 238
  273.   }                                          // 239
  274.                                              // 240
  275.   public synchronized void clear()           // 241
  276.     { reset(); removeAllElements(); }  
  277.   public synchronized void reset()           // 242
  278.     { current = null; at_end = false; }
  279. }                                            // 243
  280.