/* * TilePlotQueue.java * * Created on June 9, 2006, 5:40 PM */ package Current.popups.Memory; import java.util.*; import Current.basic.*; import Current.plot.Plotters.*; import Current.manage.*; /** * * @author pat */ public class TilePlotQueue implements Runnable { protected Manager M; protected LinkedList queue; protected Thread T; protected Interrupter I; protected Interrupter tile_plot_interrupter; /** Creates a new instance of TilePlotQueue */ public TilePlotQueue(Manager M) { this.M=M; queue=new LinkedList(); } /** * Add some tiles to the plot queue. * */ public void add(LinkedList tiles, XPlotter plotter, boolean override) { synchronized (queue) { for (ListIterator it=tiles.listIterator(); it.hasNext(); ) { Tile t=(Tile)(it.next()); if ((override)||(t.getPlotter()==null)) { t=new McbTile(t.getStringWord(), t.getColor(), plotter); } queue.addLast(t); } } } // public boolean isEmpty() { // synchronized(queue) { // return (queue.size()==0); // } // } public int getSize() { synchronized(queue) { return queue.size(); } } public void start() { if (!running()) { T=new Thread(this); T.start(); M.mcbSend(this); } } public void stop() { if (running()) { I.interrupt(); tile_plot_interrupter.interrupt(); T=null; M.mcbSend(this); } } /** Clear the plot queue. * in order to call this, the plotter must be stopped */ public void clear() { if (!running()) { synchronized(queue) { queue=new LinkedList(); } M.mcbSend(this); } } // return true if running public boolean running() { return (T!=null); } public void run() { run2(); T=null; M.mcbSend(this); } protected void run2() { boolean b=false; synchronized (queue) { b=(queue.size()>0); } Tile t=null,tile=null; I=new Interrupter(); //System.out.println("Started"); while ((b)&&(! I.isInterrupted())) { synchronized (queue) { t=(Tile)(queue.getFirst()); } //System.out.println("Begun loop"); if ((t!=null)&&(t.getPlotter()!=null)) { //System.out.println("Trying to plot a tile"); tile_plot_interrupter=new Interrupter(); try { tile=t.getPlotter().plot(t.getStringWord(), t.getColor(), tile_plot_interrupter, M); } catch (Throwable T) { T.printStackTrace(); } if ((tile!=null)&&(! I.isInterrupted())) { if (tile.getColor()==null) { tile.setColor(M.getColor()); } M.plotTile(tile); } else { System.err.println("TilePlotQueue: failed to plot a tile"); } // if ((t==null) // && (! tile_plot_interrupter.isInterrupted()) // && (! I.isInterrupted())) { // System.err.println("TilePlotQueue: No tile found."); // System.err.println("TilePlotQueue: Setting current word to troublesome word."); // I.interrupt(); // M.setWord(t.getStringWord()); // synchronized (queue) { // queue.removeFirst(); // } // } //return; } synchronized (queue) { queue.removeFirst(); b=(queue.size()>0); } M.mcbSend(this); } } }