001    package org.jaga.hooks;
002    
003    
004    
005    /**
006     * TODO: Complete these comments.
007     *
008     * <p><u>Project:</u> JAGA - Java API for Genetic Algorithms.</p>
009     *
010     * <p><u>Company:</u> University College London and JAGA.Org
011     *    (<a href="http://www.jaga.org" target="_blank">http://www.jaga.org</a>).
012     * </p>
013     *
014     * <p><u>Copyright:</u> (c) 2004 by G. Paperin.<br/>
015     *    This program is free software; you can redistribute it and/or modify
016     *    it under the terms of the GNU General Public License as published by
017     *    the Free Software Foundation, ONLY if you include a note of the original
018     *    author(s) in any redistributed/modified copy.<br/>
019     *    This program is distributed in the hope that it will be useful,
020     *    but WITHOUT ANY WARRANTY; without even the implied warranty of
021     *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
022     *    GNU General Public License for more details.<br/>
023     *    You should have received a copy of the GNU General Public License
024     *    along with this program; if not, write to the Free Software
025     *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026     *    or see http://www.gnu.org/licenses/gpl.html</p>
027     *
028     * @author Greg Paperin (greg@jaga.org)
029     *
030     * @version JAGA public release 1.0 beta
031     */
032    
033    class AnalysisFrameUpdateThread extends Thread {
034    
035            private long period = 1000;
036            AnalysisHook hook = null;
037            GAAnalysisFrame frame = null;
038            private boolean keepAlive = true;
039    
040            public AnalysisFrameUpdateThread(long period, GAAnalysisFrame frame, AnalysisHook hook) {
041                    super("Analysis frame update thread");
042                    if (10 > period)
043                            throw new IllegalArgumentException("Sleep period too small (" + period + ")");
044                    if (null == frame)
045                            throw new NullPointerException("Frame may not be null");
046                    if (null == hook)
047                            throw new NullPointerException("Hook may not be null");
048                    this.period = period;
049                    this.frame = frame;
050                    this.hook = hook;
051            }
052    
053            public void quit() {
054                    this.keepAlive = false;
055            }
056    
057            public void run() {
058                    while (keepAlive) {
059    
060                            if (hook.isViewUpdated())
061                                    frame.updateView();
062    
063                            try {
064                                    sleep(period);
065                            } catch (InterruptedException e) {
066                                    e.printStackTrace();
067                            }
068    
069                    }
070            }
071    }