home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / Javacup / IN4WJCXU.TAR / internet / IN4WJCXU / src / como / sys / RemoteFileDialog.java < prev    next >
Encoding:
Java Source  |  1996-05-21  |  4.3 KB  |  177 lines

  1. /*
  2.  * RemoteFileDialog    (c) 1996 Jan Kautz & Ulrich Gall
  3.  *
  4.  * Reads from a given file a list of files, which may
  5.  * be loaded, if those files are pictures/sounds then
  6.  * they may be displayed!
  7.  */
  8.  
  9. package como.sys;
  10.  
  11. import como.io.*;
  12. import como.awt.*;
  13. import como.util.*;
  14. import java.io.*;
  15. import java.awt.*;
  16. import java.util.*;
  17. import java.applet.*;
  18.  
  19.  
  20. public class RemoteFileDialog extends Panel {
  21.     public static final int PICTURE = 0;
  22.     public static final int SOUND = 1;
  23.     public static final int DATA = 99;
  24.  
  25.     ComObj comobj;
  26.     List list = null;
  27.     Button okbutton;
  28.     Button cancelbutton;
  29.     Button playsound;
  30.     ImageButton imagebutton;
  31.     Label imagename;
  32.     Component notify;
  33.     Vector files = null;
  34.     int selected = -1;
  35.     int type;
  36.  
  37.     public RemoteFileDialog() {
  38.         super();
  39.     }
  40.  
  41.     /**
  42.      * The RemoteFileDialog is a helper-class, that opens a file on the server.
  43.      * (same pathes as the ComObj.openInputStream()). This files contains filenames
  44.      * that may be loaded. The RemoteFileDialog displays these filenames. You may
  45.      * select one of these. If it is a known filetype (picture/sound must be
  46.      * indicated in the first line of the filename-file), you can play/view
  47.      * with a double-click on the filename. The syntax of the file:
  48.      * first line: picture | sound | data.
  49.      * next lines: filename : description.
  50.      * @param comobj the ComObj.
  51.      * @param filename the name of the file that contains the filenames
  52.      * @param notify a Component that will be notified with an ACTION-Event,
  53.      *  where the target is set to the RemoteFileDialog and the arg to
  54.      *  the filename (null if cancel was pressed).
  55.      */
  56.     public RemoteFileDialog( ComObj comobj, String filename, Component notify ) {
  57.         super();
  58.  
  59.         this.notify = notify;
  60.         this.comobj = comobj;
  61.  
  62.         setLayout( new BorderLayout( 10, 10 ) );
  63.         list = new List( 8, false );
  64.  
  65.         DataInputStream dis = new DataInputStream( comobj.openInputStream( filename ) );
  66.         files = StreamLine.loadLines( dis );
  67.  
  68.         if( files == null || files.size() < 1 )
  69.         {
  70.             type = DATA;
  71.             add( "Center", new Label( "Error loading "+filename ) );
  72.             add( "South", cancelbutton = new Button( "OK" ) );
  73.  
  74.             layout();
  75.             repaint();
  76.  
  77.             return;
  78.         }
  79.  
  80.         Enumeration e = files.elements();
  81.         String tp = (String)e.nextElement();
  82.         if( tp.equalsIgnoreCase( "picture" ) ) type = PICTURE;
  83.         else if( tp.equalsIgnoreCase( "sound" ) ) type = SOUND;
  84.         else type = DATA;
  85.  
  86.         while( e.hasMoreElements() ) {
  87.             list.addItem( (String)e.nextElement() );
  88.         }
  89.  
  90.         add( "Center", list );
  91.  
  92.         Panel buttonpanel = new Panel();
  93.         buttonpanel.setLayout( new FlowLayout( FlowLayout.CENTER ) );
  94.         buttonpanel.add( okbutton = new Button( "OK" ) );
  95.         buttonpanel.add( cancelbutton = new Button( "Cancel" ) );
  96.         add( "South", buttonpanel );
  97.  
  98.         switch( type ) {
  99.             case SOUND:
  100.                 add( "North", new Label( "Double click for sound." ) );
  101.                 break;
  102.  
  103.             case PICTURE:
  104.                 add( "North", new Label( "Double click to preview picture." ) );
  105.  
  106.                 Panel imagepanel = new Panel();
  107.                 imagepanel.setLayout( new BorderLayout( 10, 10 ) );
  108.                 imagepanel.add( "Center", imagebutton = new ImageButton( 70, 70, " pic" ) );
  109.                 imagebutton.setNoPush( true );
  110.                 imagepanel.add( "North", imagename = new Label( "(pic name)" ) );
  111.                 add( "East", imagepanel );
  112.                 break;
  113.  
  114.             default:
  115.                 break;
  116.         }
  117.  
  118.         layout();
  119.         repaint();
  120.     }
  121.  
  122.     public boolean handleEvent( Event evt ) {
  123.         if( evt.target == list ) {
  124.             selected = list.getSelectedIndex();
  125.         }
  126.  
  127.         return super.handleEvent( evt );
  128.     }
  129.  
  130.     public String getName( String all ) {
  131.         String name = "unknown";
  132.         int idx;
  133.  
  134.         idx = all.indexOf( ':' );
  135.  
  136.         if( idx < 0 ) name = all;
  137.         else name = all.substring( 0, idx-1 );
  138.  
  139.         return name.trim();
  140.     }
  141.  
  142.     public boolean action( Event evt, Object what ) {
  143.         if( evt.target == okbutton && selected != -1 ) {
  144.             String name = getName( (String)files.elementAt( selected+1 ) );
  145.  
  146.             notify.postEvent( new Event( this, Event.ACTION_EVENT, name ) );
  147.             return true;
  148.         }
  149.  
  150.         if( evt.target == cancelbutton ) {
  151.             notify.postEvent( new Event( this, Event.ACTION_EVENT, null ) );
  152.  
  153.             return true;
  154.         }
  155.  
  156.         if( evt.target == list ) {
  157.             String file = getName( (String)evt.arg );
  158.  
  159.             switch( type ) {
  160.                 case SOUND:
  161.                     AudioClip clip = comobj.loadAudioClip( file );
  162.                     clip.play();
  163.                     break;
  164.  
  165.                 case PICTURE:
  166.                     // TODO: show name of the image somewhere
  167.                     Image img = comobj.loadImage( file );
  168.                     imagebutton.setImages( img, img );
  169.                     imagename.setText( file );
  170.                     break;
  171.             }
  172.         }
  173.  
  174.         return false;
  175.     }
  176. }
  177.