001    package net.sourceforge.mp3dings.utils;
002    
003    /* Mp3dings - manage mp3 meta-information
004    * Copyright (C) 2003 Moritz Ringler
005    * $Id: TimeIntensiveTask.java,v 1.3 2006/08/12 08:20:02 ringler Exp $
006    *
007    * This program is free software; you can redistribute it and/or
008    * modify it under the terms of the GNU General Public License
009    * as published by the Free Software Foundation; either version 2
010    * of the License, or (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, write to the Free Software
019    * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
020    */
021    import java.awt.Frame;
022    import javax.swing.SwingUtilities;
023    import de.mospace.swing.SimpleProgressMonitor;
024    
025    public class TimeIntensiveTask{
026        protected Frame owner;
027        private SimpleProgressMonitor progress;
028    
029        /**
030        * @param owner parent window
031        * @param title title of the dialog
032        * @param note initial text to display
033        **/
034        public TimeIntensiveTask(Frame owner, String title, String note){
035            progress = new SimpleProgressMonitor(owner, title, note){
036                public void close(){
037                    TimeIntensiveTask.this.owner.setEnabled(true);
038                    super.close();
039                }
040            };
041            this.owner = owner;
042        }
043    
044        public SimpleProgressMonitor getMonitor(){
045            return progress;
046        }
047    
048        public void run(){
049        }
050    
051        public void start(){
052            Runnable job = new Runnable(){
053                public void run(){
054                    progress.setVisible(true);
055                    owner.setEnabled(false);
056                    try{
057                        TimeIntensiveTask.this.run();
058                    } finally {
059                        /* Will also reenable owner */
060                        progress.close();
061                    }
062                    SwingUtilities.invokeLater(
063                    new Runnable(){
064                        public void run(){
065                            owner.requestFocus();
066                            finished();
067                        }
068                    });
069                }
070            };
071            (new Thread(job)).start();
072        }
073    
074        public void finished(){
075        }
076    
077    }