FileCleaner.java



import java.io.File;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;

/** ディレクトリ内のファイルを一覧表示し、
    マウス操作よってファイルを消去する FileCleanerクラス */

public class FileCleaner extends DirViewer {

   /** マウスのイベントを処理するアダプターのオブジェクト */

       public Deleter deleter;    // ファイルの消去を実行する

   /** コンストラクタ */

       FileCleaner( File dir ){

               super( dir );

               deleter = new Deleter( this, icons );

               for( int i=0; i<icons.length; i++ ) {

                    icons[i].canvas.addMouseListener( deleter );
               }
       }

   /** 最初に呼び出されるメソッド */
 
       public static void main( String argv[] ) {
 
              String dirName;  // ディレクトリ名を指定
 
              if( argv.length >= 1 ) {
 
                  dirName = argv[0];
              }
              else {
 
                  dirName = ".";  // デフォルトはカレントディレクトリ
              }
 
           // 自分自身のオブジェクトを生成して処理を開始
 
              try {
                   File dir = new File( dirName ); // この dir はローカル変数
                   if( dir.isDirectory() ) {
                       FileCleaner fc = new FileCleaner( dir );
                   }
                   else {
                        System.err.println( dirName + " is not directory");
                        System.exit(0);
                   }
              }
              catch( NullPointerException e ) {
 
                   System.err.println( "Directory name string is null");
                   System.exit(0);
              }
       } 
} // クラス FileCleaner の定義の終わり


/** マウスのイベントを処理し、ファイルの消去を実行する */

class Deleter extends MouseAdapter {

   /** 親の FileCleaerオブジェクト */

       public FileCleaner parent;

   /** 操作の対象となるアイコン */

       protected FileIcon icons[];

   /** コンストラクタ */

       Deleter( FileCleaner parent, FileIcon icons[] ){

             this.parent = parent;
             this.icons = icons;
       }


   /** マウスのクリックを処理するメソッド */

       public void mouseClicked( MouseEvent evt ) {
 
System.out.println("Clicked method called");  //debug

              if( evt.getClickCount() == 2 ) { //ダブル・クリックの場合

System.out.println("Double Click");  //debug
                  for( int i=0; i<icons.length; i++ ) {

                     if( icons[i].canvas == evt.getComponent() ) {

System.out.println("target is found");  //debug
                        try{
                           if( icons[i].file.delete() == true ){

                               parent.remove( icons[i] );
                           }
                        }
                        catch( SecurityException e ){
                        }
                     }
                  }
              }
       }
}