Extracted AnimationWorker from DataPanel

This commit is contained in:
Denis Molony 2021-06-03 14:59:13 +10:00
parent 9d706d62ef
commit 95538f5282
2 changed files with 64 additions and 48 deletions

View File

@ -0,0 +1,62 @@
package com.bytezone.diskbrowser.gui;
import java.util.List;
import javax.swing.SwingWorker;
import com.bytezone.diskbrowser.applefile.SHRPictureFile2;
// -----------------------------------------------------------------------------------//
public class AnimationWorker extends SwingWorker<Void, Integer>
// -----------------------------------------------------------------------------------//
{
volatile boolean running;
SHRPictureFile2 image;
DataPanel owner;
// ---------------------------------------------------------------------------------//
public AnimationWorker (DataPanel owner, SHRPictureFile2 image)
// ---------------------------------------------------------------------------------//
{
assert image.isAnimation ();
this.image = image;
this.owner = owner;
}
// ---------------------------------------------------------------------------------//
public void cancel ()
// ---------------------------------------------------------------------------------//
{
running = false;
}
// ---------------------------------------------------------------------------------//
@Override
protected Void doInBackground () throws Exception
// ---------------------------------------------------------------------------------//
{
running = true;
try
{
while (running)
{
Thread.sleep (image.getDelay ());
publish (0);
}
}
catch (InterruptedException e)
{
e.printStackTrace ();
}
return null;
}
// ---------------------------------------------------------------------------------//
@Override
protected void process (List<Integer> chunks)
// ---------------------------------------------------------------------------------//
{
image.nextFrame ();
owner.update ();
}
}

View File

@ -16,7 +16,6 @@ import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.SwingWorker;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
@ -62,7 +61,7 @@ public class DataPanel extends JTabbedPane
boolean assemblerTextValid;
DataSource currentDataSource;
private Worker animation;
private AnimationWorker animation;
final MenuHandler menuHandler;
@ -381,7 +380,7 @@ public class DataPanel extends JTabbedPane
{
if (animation != null)
animation.cancel ();
animation = new Worker ((SHRPictureFile2) dataSource);
animation = new AnimationWorker (this, (SHRPictureFile2) dataSource);
animation.execute ();
}
}
@ -515,49 +514,4 @@ public class DataPanel extends JTabbedPane
if (currentDataSource instanceof BasicTextFile)
setDataSource (currentDataSource);
}
// ---------------------------------------------------------------------------------//
class Worker extends SwingWorker<Void, Integer>
// ---------------------------------------------------------------------------------//
{
volatile boolean running;
SHRPictureFile2 image;
public Worker (SHRPictureFile2 image)
{
assert image.isAnimation ();
this.image = image;
}
public void cancel ()
{
running = false;
}
@Override
protected Void doInBackground () throws Exception
{
running = true;
try
{
while (running)
{
Thread.sleep (image.getDelay ());
publish (0);
}
}
catch (InterruptedException e)
{
e.printStackTrace ();
}
return null;
}
@Override
protected void process (List<Integer> chunks)
{
image.nextFrame ();
update ();
}
}
}