From 95538f52825b21fa61565f4ad7b54ae913bbdb76 Mon Sep 17 00:00:00 2001 From: Denis Molony Date: Thu, 3 Jun 2021 14:59:13 +1000 Subject: [PATCH] Extracted AnimationWorker from DataPanel --- .../diskbrowser/gui/AnimationWorker.java | 62 +++++++++++++++++++ .../bytezone/diskbrowser/gui/DataPanel.java | 50 +-------------- 2 files changed, 64 insertions(+), 48 deletions(-) create mode 100644 src/com/bytezone/diskbrowser/gui/AnimationWorker.java diff --git a/src/com/bytezone/diskbrowser/gui/AnimationWorker.java b/src/com/bytezone/diskbrowser/gui/AnimationWorker.java new file mode 100644 index 0000000..a62d0bd --- /dev/null +++ b/src/com/bytezone/diskbrowser/gui/AnimationWorker.java @@ -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 +// -----------------------------------------------------------------------------------// +{ + 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 chunks) + // ---------------------------------------------------------------------------------// + { + image.nextFrame (); + owner.update (); + } +} \ No newline at end of file diff --git a/src/com/bytezone/diskbrowser/gui/DataPanel.java b/src/com/bytezone/diskbrowser/gui/DataPanel.java index d561a5b..296df6d 100755 --- a/src/com/bytezone/diskbrowser/gui/DataPanel.java +++ b/src/com/bytezone/diskbrowser/gui/DataPanel.java @@ -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 - // ---------------------------------------------------------------------------------// - { - 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 chunks) - { - image.nextFrame (); - update (); - } - } } \ No newline at end of file