From aae3fc4b3b2a307d22f65939c65cd8a875f44c65 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Fri, 11 Mar 2022 22:09:15 -0500 Subject: [PATCH] remove extra ContribProgressMonitor class --- app/src/processing/app/Base.java | 2 +- .../app/contrib/ContribProgressBar.java | 65 +++++++++++++--- .../app/contrib/ContribProgressMonitor.java | 78 ------------------- .../app/contrib/ContributionListing.java | 2 +- .../app/contrib/ContributionManager.java | 4 +- .../app/contrib/LocalContribution.java | 6 +- .../processing/app/contrib/ManagerFrame.java | 2 +- .../app/contrib/StatusPanelDetail.java | 12 +-- todo.txt | 2 + 9 files changed, 66 insertions(+), 107 deletions(-) delete mode 100644 app/src/processing/app/contrib/ContribProgressMonitor.java diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index c81e3f0ed..1c7bdd1f4 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -480,7 +480,7 @@ public class Base { new UpdateCheck(this); ContributionListing cl = ContributionListing.getInstance(); - cl.downloadAvailableList(this, new ContribProgressMonitor() { }); + cl.downloadAvailableList(this, new ContribProgressBar(null)); long t9 = System.currentTimeMillis(); // System.out.println("base took " + (t2-t1) + " " + (t3-t2) + " " + (t4-t3) + // " " + (t5-t4) + " t6-t5=" + (t6-t5) + " " + (t7-t6) + " handleNew=" + (t8-t7) + " " + (t9-t8) + " ms"); diff --git a/app/src/processing/app/contrib/ContribProgressBar.java b/app/src/processing/app/contrib/ContribProgressBar.java index f083b965d..e4d0658b4 100644 --- a/app/src/processing/app/contrib/ContribProgressBar.java +++ b/app/src/processing/app/contrib/ContribProgressBar.java @@ -3,7 +3,7 @@ /* Part of the Processing project - http://processing.org - Copyright (c) 2013-20 The Processing Foundation + Copyright (c) 2013-22 The Processing Foundation Copyright (c) 2011-12 Ben Fry and Casey Reas This program is free software; you can redistribute it and/or modify @@ -31,28 +31,51 @@ import javax.swing.JProgressBar; // This code seems like it's adapted from old example code found on the web. // https://github.com/processing/processing4/issues/351 -abstract class ContribProgressBar extends ContribProgressMonitor { +public class ContribProgressBar { + static private final int UNKNOWN = -1; + JProgressBar progressBar; + int progress = 0; + int max; + + boolean finished = false; + boolean canceled = false; + boolean error = false; + Exception exception; + + public ContribProgressBar(JProgressBar progressBar) { this.progressBar = progressBar; } + + public void startTask(String name) { + startTask(name, UNKNOWN); + } + + public void startTask(String name, int maxValue) { finished = false; - progressBar.setString(name); - progressBar.setIndeterminate(maxValue == UNKNOWN); - progressBar.setMaximum(maxValue); + + if (progressBar != null) { + progressBar.setString(name); + progressBar.setIndeterminate(maxValue == UNKNOWN); + progressBar.setMaximum(maxValue); + } } + public void setProgress(int value) { - super.setProgress(value); - progressBar.setValue(value); + progress = value; + if (progressBar != null) { + progressBar.setValue(value); + } } - @Override + public final void finished() { - super.finished(); + finished = true; try { EventQueue.invokeAndWait(this::finishedAction); } catch (InterruptedException e) { @@ -67,11 +90,17 @@ abstract class ContribProgressBar extends ContribProgressMonitor { } } - public abstract void finishedAction(); - @Override + public void finishedAction() { } + + + public boolean isCanceled() { + return canceled; + } + + public final void cancel() { - super.cancel(); + canceled = true; try { EventQueue.invokeAndWait(this::cancelAction); } catch (InterruptedException e) { @@ -86,5 +115,17 @@ abstract class ContribProgressBar extends ContribProgressMonitor { } } + public void cancelAction() { } + + + public boolean isError() { + return error; + } + + + public void error(Exception e) { + error = true; + exception = e; + } } diff --git a/app/src/processing/app/contrib/ContribProgressMonitor.java b/app/src/processing/app/contrib/ContribProgressMonitor.java deleted file mode 100644 index c78ed41bc..000000000 --- a/app/src/processing/app/contrib/ContribProgressMonitor.java +++ /dev/null @@ -1,78 +0,0 @@ -/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */ - -/* - Part of the Processing project - http://processing.org - - Copyright (c) 2013-15 The Processing Foundation - Copyright (c) 2011-12 Ben Fry and Casey Reas - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License version 2 - as published by the Free Software Foundation. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License along - with this program; if not, write to the Free Software Foundation, Inc. - 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -*/ -package processing.app.contrib; - - -// I suspect this code can mostly be replaced with built-in Swing functions. -// This code seems like it's adapted from old example code found on the web. -// https://github.com/processing/processing4/issues/351 - -public abstract class ContribProgressMonitor { - static final int UNKNOWN = -1; - boolean canceled = false; - boolean error = false; - boolean finished = false; - Exception exception; - int max; - int progress = 0; - - public void startTask(String name, int maxValue) { - } - - public void setProgress(int value) { - progress = value; - } - - public int getProgress() { - return progress; - } - - public boolean isCanceled() { - return canceled; - } - - public void cancel() { - canceled = true; - } - - public boolean isError() { - return error; - } - - public Exception getException() { - return exception; - } - - public void error(Exception e) { - error = true; - exception = e; - } - - public boolean isFinished() { - return finished; - } - - public void finished() { - finished = true; - } -} - diff --git a/app/src/processing/app/contrib/ContributionListing.java b/app/src/processing/app/contrib/ContributionListing.java index 662743969..acf52fd4e 100644 --- a/app/src/processing/app/contrib/ContributionListing.java +++ b/app/src/processing/app/contrib/ContributionListing.java @@ -344,7 +344,7 @@ public class ContributionListing { * Only one instance will run at a time. */ public void downloadAvailableList(final Base base, - final ContribProgressMonitor progress) { + final ContribProgressBar progress) { // TODO: replace with SwingWorker [jv] new Thread(() -> { diff --git a/app/src/processing/app/contrib/ContributionManager.java b/app/src/processing/app/contrib/ContributionManager.java index ede915d60..7f9a5d50d 100644 --- a/app/src/processing/app/contrib/ContributionManager.java +++ b/app/src/processing/app/contrib/ContributionManager.java @@ -56,7 +56,7 @@ public class ContributionManager { * @return true if the file was successfully downloaded, false otherwise. */ static boolean download(URL source, byte[] post, - File dest, ContribProgressMonitor progress) { + File dest, ContribProgressBar progress) { boolean success = false; try { HttpURLConnection conn = (HttpURLConnection) source.openConnection(); @@ -154,7 +154,7 @@ public class ContributionManager { download(url, null, contribZip, downloadProgress); if (!downloadProgress.isCanceled() && !downloadProgress.isError()) { - installProgress.startTask(Language.text("contrib.progress.installing"), ContribProgressMonitor.UNKNOWN); + installProgress.startTask(Language.text("contrib.progress.installing")); final LocalContribution contribution = ad.install(base, contribZip, false, status); diff --git a/app/src/processing/app/contrib/LocalContribution.java b/app/src/processing/app/contrib/LocalContribution.java index 1e77e5dc0..80e7fb353 100644 --- a/app/src/processing/app/contrib/LocalContribution.java +++ b/app/src/processing/app/contrib/LocalContribution.java @@ -384,7 +384,7 @@ public abstract class LocalContribution extends Contribution { * Non-blocking call to remove a contribution in a new thread. */ void removeContribution(final Base base, - final ContribProgressMonitor pm, + final ContribProgressBar pm, final StatusPanel status) { // TODO: replace with SwingWorker [jv] new Thread(() -> remove(base, pm, status, ContributionListing.getInstance()), "Contribution Uninstaller").start(); @@ -392,10 +392,10 @@ public abstract class LocalContribution extends Contribution { void remove(final Base base, - final ContribProgressMonitor pm, + final ContribProgressBar pm, final StatusPanel status, final ContributionListing contribListing) { - pm.startTask("Removing", ContribProgressMonitor.UNKNOWN); + pm.startTask("Removing"); boolean doBackup = Preferences.getBoolean("contribution.backup.on_remove"); if (getType() == ContributionType.MODE) { diff --git a/app/src/processing/app/contrib/ManagerFrame.java b/app/src/processing/app/contrib/ManagerFrame.java index 6510009a3..040998f5a 100644 --- a/app/src/processing/app/contrib/ManagerFrame.java +++ b/app/src/processing/app/contrib/ManagerFrame.java @@ -183,7 +183,7 @@ public class ManagerFrame { //as there is only one instance of contribListing and it should be present in this class final ContributionTab activeTab = getActiveTab(); - ContribProgressMonitor progress = + ContribProgressBar progress = new ContribProgressBar(activeTab.progressBar) { @Override diff --git a/app/src/processing/app/contrib/StatusPanelDetail.java b/app/src/processing/app/contrib/StatusPanelDetail.java index b9453c498..517e59536 100644 --- a/app/src/processing/app/contrib/StatusPanelDetail.java +++ b/app/src/processing/app/contrib/StatusPanelDetail.java @@ -159,11 +159,6 @@ class StatusPanelDetail { } -// private boolean isSelected() { -// return listPanel.getSelectedPanel() == this; -// } - - protected void install() { clearStatusMessage(); installInProgress = true; @@ -198,8 +193,8 @@ class StatusPanelDetail { progressBar.setVisible(true); progressBar.setIndeterminate(true); - ContribProgressBar monitor = new RemoveProgressBar(progressBar); - getLocalContrib().removeContribution(getBase(), monitor, getStatusPanel()); + ContribProgressBar progress = new RemoveProgressBar(progressBar); + getLocalContrib().removeContribution(getBase(), progress, getStatusPanel()); } } @@ -216,8 +211,7 @@ class StatusPanelDetail { resetProgressBar(); AvailableContribution ad = contribListing.getAvailableContribution(contrib); - String url = ad.link; - installContribution(ad, url); + installContribution(ad, ad.link); } @Override diff --git a/todo.txt b/todo.txt index 75b0b8508..cc6095276 100755 --- a/todo.txt +++ b/todo.txt @@ -18,6 +18,8 @@ X implement updateTheme() X remove the extra 2-pixel line at the top o currently uses prepareGraphics(), do we need to remove that? X looks like nope, that was sorted out separately +X remove extra ContribProgressMonitor class +_ removing the current Mode will cause an exception when opening a sketch _ allow update of the current Mode _ an incompatible Mode prevents the PDE from quitting after last window is closed