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

  1. package Jcd; // -- Listing-4 -- Display.java //  1
  2.                                              //  2
  3. import java.util.*;                          //  3
  4. import java.awt.*;                           //  4
  5. import java.awt.event.*;                     //  5
  6. import Jcd.SmartDrive;                       //  6
  7. import Jcd.Monitor;                          //  7
  8.                                              //  8
  9. class Display extends Panel implements Observer {
  10.                                              // 10
  11.   // Independent status display thread.      // 11
  12.                                              // 12
  13.   private TextField trackField = new TextField(2);
  14.   private TextField indexField = new TextField(8);
  15.   private TextField timeField = new TextField(22);
  16.                                              // 16
  17.   private SmartDrive cdPlayer;               // 17
  18.   private String prevText = "";              // 18
  19.                                              // 19
  20.   public Display(SmartDrive drive)           // 20
  21.   {                                          // 21
  22.     setLayout(
  23.         new FlowLayout(FlowLayout.LEFT,5,5));// 22
  24.     indexField.setEditable(false);           // 23
  25.     timeField.setEditable(false);            // 24
  26.                                              // 25
  27.     cdPlayer = drive;                        // 26
  28.     add(trackField);                         // 27
  29.     add(indexField);                         // 28
  30.     add(timeField);                          // 29
  31.                                              // 30
  32.     trackField.addFocusListener(
  33.         new TrackFocusLost());               // 31
  34.     trackField.addKeyListener(
  35.         new TrackKeyPress());                // 32
  36.                                              // 33
  37.     cdPlayer.monitor.addObserver(this);      // 34
  38.   }                                          // 35
  39.                                              // 36
  40.   public void update(Observable obj, Object arg) {
  41.     Monitor mon = (Monitor) obj;             // 38
  42.     if (mon.status != Drive.STATUS_INVALID) {
  43.       int time = (                           // 40
  44.           mon.trackEndAddress[mon.currentTrack] -
  45.           mon.currentAddress) /
  46.             Drive.FRAMES_PER_SECOND;         // 41
  47.       String newTrackText =
  48.         Integer.toString(mon.currentTrack);  // 42
  49.       // Prevent excessive updates - so that
  50.       // the user can edit the field.        // 43
  51.       if (prevText.compareTo(newTrackText) != 0) {
  52.         trackField.setText(newTrackText);    // 45
  53.         prevText = newTrackText;             // 46
  54.       }                                      // 47
  55.       indexField.setText("Index: " +         // 48 
  56.                          mon.currentIndex);  
  57.       timeField.setText("Trk Rem: " +        // 49
  58.                         time/60 + " min " +
  59.                         time%60 + " sec");
  60.     }                                        // 50
  61.   }                                          // 51
  62.                                              // 52
  63.   private class TrackFocusLost               // 53
  64.     extends FocusAdapter {                   // 54
  65.        // removes text entered on focus out.
  66.     public void focusLost(FocusEvent e) {    // 55
  67.        trackField.setText(prevText);
  68.     }
  69.   }                                          // 56
  70.                                              // 57
  71.   private class TrackKeyPress extends KeyAdapter {
  72.                                              // 59
  73.     public void keyPressed(KeyEvent e)       // 60
  74.     {                                        // 61
  75.       if (e.getKeyChar() == '\n') {          // 62
  76.         try {                                // 63
  77.           cdPlayer.play(                     // 64
  78.           Integer.parseInt(trackField.getText()));
  79.         }                                    // 65
  80.         catch (NumberFormatException nx) {   // 66
  81.           System.out.println(
  82.             "Invalid track number: " + nx);  // 67
  83.         }                                    // 68
  84.         catch (DriveException dx) {          // 69
  85.           System.out.println("Exception: " + dx);
  86.         }                                    // 71
  87.       }                                      // 72
  88.     }                                        // 73
  89.   }                                          // 74
  90.                                              // 75
  91. }                                            // 76
  92.