001    /**
002     * iFish - An iRiver iHP jukebox database creation tool
003     *
004     * Copyright (C) 2009 Richard "Shred" Körber
005     *   http://ifish.shredzone.org
006     *
007     * This program is free software: you can redistribute it and/or modify
008     * it under the terms of the GNU General Public License as published by
009     * the Free Software Foundation, either version 3 of the License, or
010     * (at your option) any later version.
011     *
012     * This program is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015     * GNU General Public License for more details.
016     *
017     * You should have received a copy of the GNU General Public License
018     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
019     */
020    package net.shredzone.ifish.actions;
021    
022    import net.shredzone.ifish.*;
023    import net.shredzone.jshred.swing.SwingUtils;
024    import java.awt.*;
025    import javax.swing.*;
026    
027    /**
028     * The basic class for asynchronous IFish actions. The action itself
029     * will be executed in a separate thread, keeping the GUI thread
030     * running. The GUI will be blocked meanwhile, though. This kind is
031     * meant for actions which take a rather long time to execute, but
032     * do not open a modal dialog.
033     *
034     * @author  Richard Körber &lt;dev@shredzone.de&gt;
035     * @version $Id: AsyncIFishAction.java 291 2009-04-28 21:29:27Z shred $
036     */
037    public abstract class AsyncIFishAction extends IFishAction {
038      private static final long serialVersionUID = 795973077089301966L;
039    
040      /**
041       * Create a new, asynchronous Action.
042       *
043       * @param   fish        IFishPane this action belongs to
044       * @param   name        Action Name
045       * @param   icon        Action Icon or null
046       * @param   tip         Action Tooltip or null
047       * @param   accel       Accelerator Key or null
048       */
049      public AsyncIFishAction( IFishPane fish, String name, Icon icon, String tip, KeyStroke accel ) {
050        super( fish, name, icon, tip, accel );
051      }
052      
053      /**
054       * Invoke the action directly from within the application.
055       */
056      @Override
057      public void perform() {
058        //--- Create background thread ---
059        Thread thread = new Thread( new Runnable() {
060          @Override
061          public void run() {
062            //--- Lock the frame ---
063            final Frame frame = SwingUtils.getComponentFrame( fish );
064            if( frame!=null ) frame.setEnabled( false );
065            //--- Invoke the action ---
066            try {
067              action();
068            }finally {
069              //--- In any case, unlock the frame ---
070              if( frame!=null ) frame.setEnabled( true );
071            }
072          }
073        });
074        
075        //--- Run it ---
076        thread.start();
077      }
078    
079      /**
080       * Invoke the action synchronously, e.g. from batch scripts.
081       */
082      public void performSync() {
083        action();
084      }
085      
086    }
087