diff --git a/src/main/java/listfix/controller/Task.java b/src/main/java/listfix/controller/Task.java deleted file mode 100644 index 3fce9792..00000000 --- a/src/main/java/listfix/controller/Task.java +++ /dev/null @@ -1,57 +0,0 @@ - - -package listfix.controller; - -import listfix.view.support.IProgressObserver; - -import java.util.ArrayList; -import java.util.List; - - -public abstract class Task extends Thread -{ - private final List> observers; - private int progress = 0; - - - public Task() - { - observers = new ArrayList<>(); - } - - @Override - public abstract void run(); - - public final void addProgressObserver(IProgressObserver observer) - { - observers.add(observer); - } - - public final void removeProgressObserver(IProgressObserver observer) - { - observers.remove(observer); - } - - public final void notifyObservers(int percent) - { - if (percent < 0) - { - percent = 0; - } - else if (percent > 100) - { - percent = 100; - } - progress = percent; - - for (IProgressObserver observer : observers) - { - observer.reportProgress(percent); - } - } - - public int getProgress() - { - return progress; - } -} diff --git a/src/main/java/listfix/controller/tasks/WriteMediaLibraryIniTask.java b/src/main/java/listfix/controller/tasks/WriteMediaLibraryIniTask.java index 1538cf66..fd1ecc3f 100644 --- a/src/main/java/listfix/controller/tasks/WriteMediaLibraryIniTask.java +++ b/src/main/java/listfix/controller/tasks/WriteMediaLibraryIniTask.java @@ -8,10 +8,7 @@ import java.io.IOException; -/** - * @author jcaron - */ -public class WriteMediaLibraryIniTask extends listfix.controller.Task +public class WriteMediaLibraryIniTask extends Thread { private static final Logger _logger = LogManager.getLogger(WriteMediaLibraryIniTask.class); diff --git a/src/main/java/listfix/io/WinampHelper.java b/src/main/java/listfix/io/WinampHelper.java index 3b15792a..c4c3b335 100644 --- a/src/main/java/listfix/io/WinampHelper.java +++ b/src/main/java/listfix/io/WinampHelper.java @@ -20,6 +20,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; +import java.util.Collections; import java.util.List; /** @@ -56,15 +57,15 @@ public class WinampHelper /** * Generates an exact match batch repair for the cryptically named playlists in Winamp. * - * @param mediaFiles + * @param mediaLibrary Media-library * @return A BatchRepair instance * @see BatchRepair */ - public static BatchRepair getWinampBatchRepair(IMediaLibrary mediaFiles, IPlaylistOptions filePathOptions) + public static BatchRepair getWinampBatchRepair(IMediaLibrary mediaLibrary, IPlaylistOptions filePathOptions) { try { - final BatchRepair br = new BatchRepair(mediaFiles, new File(WINAMP_PATH)); + final BatchRepair br = new BatchRepair(mediaLibrary, new File(WINAMP_PATH)); br.setDescription("Batch Repair: Winamp Playlists"); List winLists = getWinampPlaylists(); for (listfix.model.playlists.winamp.generated.Playlist list : winLists) @@ -83,19 +84,17 @@ public static BatchRepair getWinampBatchRepair(IMediaLibrary mediaFiles, IPlayli public static void extractPlaylists(File destDir, IProgressObserver observer) throws JAXBException, IOException { // avoid resetting total if part of batch operation - boolean hasTotal = observer instanceof ProgressAdapter; - ProgressAdapter progress = ProgressAdapter.wrap(observer); + ProgressAdapter progress = ProgressAdapter.make(observer); if (!destDir.exists()) { - destDir.mkdir(); + if(!destDir.mkdir()) { + throw new IOException(String.format("Failed to create directory \"%s\"", destDir)); + } } List winLists = getWinampPlaylists(); - if (!hasTotal) - { - progress.setTotal(winLists.size()); - } + progress.setTotal(winLists.size()); for (Playlist list : winLists) { Path sourceFile = Path.of(WINAMP_PATH, list.getFilename()); @@ -106,9 +105,6 @@ public static void extractPlaylists(File destDir, IProgressObserver observ } } - /** - * @return - */ public static boolean isWinampInstalled() { return OperatingSystem.isWindows() && !WINAMP_PATH.isEmpty(); @@ -118,14 +114,13 @@ private static List getWinamp { String playlistPath = WINAMP_PATH + "playlists.xml"; File listsFile = new File(playlistPath); - if (!listsFile.canRead()) + if (listsFile.canRead()) { - return null; + JAXBContext context = JAXBContext.newInstance("listfix.model.playlists.winamp.generated"); + Unmarshaller unmarshaller = context.createUnmarshaller(); + Playlists lists = (Playlists) unmarshaller.unmarshal(listsFile); + return lists.getPlaylist(); } - - JAXBContext context = JAXBContext.newInstance("listfix.model.playlists.winamp.generated"); - Unmarshaller unmarshaller = context.createUnmarshaller(); - Playlists lists = (Playlists) unmarshaller.unmarshal(listsFile); - return lists.getPlaylist(); + return Collections.emptyList(); } } diff --git a/src/main/java/listfix/io/playlists/PlaylistWriter.java b/src/main/java/listfix/io/playlists/PlaylistWriter.java index bd59148f..6a16b5f6 100644 --- a/src/main/java/listfix/io/playlists/PlaylistWriter.java +++ b/src/main/java/listfix/io/playlists/PlaylistWriter.java @@ -87,7 +87,7 @@ public void save(Playlist playlist, boolean saveRelative, @Nullable ProgressAdap { if (adapter == null) { // Create a dummy progress-adapter - adapter = ProgressAdapter.wrap(null); + adapter = ProgressAdapter.make(null); } final int totalSteps = playlist.getEntries().size() + 3; diff --git a/src/main/java/listfix/io/playlists/m3u/M3UReader.java b/src/main/java/listfix/io/playlists/m3u/M3UReader.java index 078f51eb..75c38fde 100644 --- a/src/main/java/listfix/io/playlists/m3u/M3UReader.java +++ b/src/main/java/listfix/io/playlists/m3u/M3UReader.java @@ -53,7 +53,7 @@ public List readPlaylist(IProgressObserver observer) thro // Line1 holds the metadata about the file that we just hang on to, line2 represents the file reference. //Initialize the progress adapter if we're given an observer. - ProgressAdapter progress = ProgressAdapter.wrap(observer); + ProgressAdapter progress = ProgressAdapter.make(observer); progress.setTotal(fileLength); String line1 = buffer.readLine(); diff --git a/src/main/java/listfix/io/playlists/pls/PLSReader.java b/src/main/java/listfix/io/playlists/pls/PLSReader.java index 5f0b7b28..251011be 100644 --- a/src/main/java/listfix/io/playlists/pls/PLSReader.java +++ b/src/main/java/listfix/io/playlists/pls/PLSReader.java @@ -56,7 +56,7 @@ public List readPlaylist(IProgressObserver observer) thro // Definition of the PLS format can be found @ http://gonze.com/playlists/playlist-format-survey.html#PLS // Init a progress adapter if we have a progress observer. - ProgressAdapter progress = ProgressAdapter.wrap(observer); + ProgressAdapter progress = ProgressAdapter.make(observer); // Load the PLS file into memory (it's basically a glorified INI | java properties file). PLSProperties propBag = new PLSProperties(); diff --git a/src/main/java/listfix/io/playlists/wpl/WPLReader.java b/src/main/java/listfix/io/playlists/wpl/WPLReader.java index 344298dd..a4811c19 100644 --- a/src/main/java/listfix/io/playlists/wpl/WPLReader.java +++ b/src/main/java/listfix/io/playlists/wpl/WPLReader.java @@ -56,7 +56,7 @@ public PlaylistType getPlaylistType() @Override public List readPlaylist(IProgressObserver observer) throws IOException { - ProgressAdapter progress = ProgressAdapter.wrap(observer); + ProgressAdapter progress = ProgressAdapter.make(observer); progress.setTotal((int) fileLength); _cache = new StringBuilder(); diff --git a/src/main/java/listfix/io/playlists/xspf/XSPFReader.java b/src/main/java/listfix/io/playlists/xspf/XSPFReader.java index 34aaa048..ce3180cb 100644 --- a/src/main/java/listfix/io/playlists/xspf/XSPFReader.java +++ b/src/main/java/listfix/io/playlists/xspf/XSPFReader.java @@ -55,7 +55,7 @@ public PlaylistType getPlaylistType() public List readPlaylist(IProgressObserver observer) throws IOException { // Init a progress adapter if we have a progress observer. - ProgressAdapter progress = ProgressAdapter.wrap(observer); + ProgressAdapter progress = ProgressAdapter.make(observer); List entriesList = new ArrayList<>(); SpecificPlaylist loadedList = SpecificPlaylistFactory.getInstance().readFrom(playlistPath.toFile()); diff --git a/src/main/java/listfix/model/BatchRepair.java b/src/main/java/listfix/model/BatchRepair.java index 5240176b..a9ba24c2 100644 --- a/src/main/java/listfix/model/BatchRepair.java +++ b/src/main/java/listfix/model/BatchRepair.java @@ -102,6 +102,7 @@ public void add(BatchRepairItem item) /** * Performs an exact matches search on all entries in multiple playlists. + * * @param observer The progress observer for this operation. */ public void performExactMatchRepair(IDualProgressObserver observer, IPlaylistOptions filePathOptions) @@ -113,19 +114,21 @@ public void performExactMatchRepair(IDualProgressObserver observer, IPla /** * Performs a closest matches search on all entries in multiple playlists. + * * @param observer The progress observer for this operation. */ public void performClosestMatchRepair(IDualProgressObserver observer, IPlaylistOptions filePathOptions) { - this.performRepair(observer, filePathOptions, (item, list, task) -> { - item.setClosestMatches(list.findClosestMatches(this.mediaLibrary.getNestedMediaFiles(), task)); + this.performRepair(observer, filePathOptions, (item, list, progressObserver) -> { + item.setClosestMatches(list.findClosestMatches(this.mediaLibrary.getNestedMediaFiles(), progressObserver)); }); } public void performRepair(IDualProgressObserver observer, IPlaylistOptions filePathOptions, IRepairItem repairItem) { - DualProgressAdapter progress = DualProgressAdapter.wrap(observer); - progress.getOverall().setTotal(_items.size() * 2L); + final DualProgressAdapter progress = new DualProgressAdapter<>(observer); + final ProgressAdapter overallProgress = progress.getOverall(); + overallProgress.setTotal(_items.size() * 2L); List toRemoveFromBatch = new ArrayList<>(); for (BatchRepairItem item : _items) @@ -133,7 +136,6 @@ public void performRepair(IDualProgressObserver observer, IPlaylistOptio if (!observer.getCancelled()) { // load - progress.getOverall().stepCompleted(); progress.getTask().reportProgress(0, "Loading \"" + item.getDisplayName() + "\""); if (item.getPlaylist() == null) { @@ -148,11 +150,11 @@ public void performRepair(IDualProgressObserver observer, IPlaylistOptio toRemoveFromBatch.add(item); } } + overallProgress.stepCompleted(); // repair if (item.getPlaylist() != null && item.getPlaylist().getMissingCount() > 0) { - progress.getOverall().stepCompleted(); progress.getTask().reportProgress(0, "Repairing \"" + item.getDisplayName() + "\""); Playlist list = item.getPlaylist(); repairItem.repair(item, list, progress.getTask()); @@ -162,6 +164,7 @@ public void performRepair(IDualProgressObserver observer, IPlaylistOptio // Don't fix playlists that have nothing to fix... instead remove them from the result set. toRemoveFromBatch.add(item); } + overallProgress.stepCompleted(); } else { @@ -198,7 +201,7 @@ public String getDefaultBackupName() */ public void save(IPlaylistOptions filePathOptions, boolean isClosestMatchesSave, boolean backup, String destination, IProgressObserver observer) throws Exception { - ProgressAdapter progress = ProgressAdapter.wrap(observer); + ProgressAdapter progress = ProgressAdapter.make(observer); // get included items int stepCount = 0; @@ -258,7 +261,7 @@ public void save(IPlaylistOptions filePathOptions, boolean isClosestMatchesSave, { item.getPlaylist().applyClosestMatchSelections(item.getClosestMatches()); } - item.getPlaylist().save(filePathOptions.getSavePlaylistsWithRelativePaths(), progress); + item.getPlaylist().save(filePathOptions.getSavePlaylistsWithRelativePaths(), null); // ToDo nest observer } } } diff --git a/src/main/java/listfix/model/IRepairItem.java b/src/main/java/listfix/model/IRepairItem.java index 6b6b836a..5c5159fe 100644 --- a/src/main/java/listfix/model/IRepairItem.java +++ b/src/main/java/listfix/model/IRepairItem.java @@ -1,10 +1,9 @@ package listfix.model; import listfix.model.playlists.Playlist; -import listfix.view.support.ProgressAdapter; +import listfix.view.support.IProgressObserver; public interface IRepairItem { - - void repair(BatchRepairItem item, Playlist list, ProgressAdapter task); + void repair(BatchRepairItem item, Playlist list, IProgressObserver progressObserver); } diff --git a/src/main/java/listfix/model/playlists/Playlist.java b/src/main/java/listfix/model/playlists/Playlist.java index 36e1dc5d..1e61e193 100644 --- a/src/main/java/listfix/model/playlists/Playlist.java +++ b/src/main/java/listfix/model/playlists/Playlist.java @@ -185,7 +185,7 @@ public List getEntries() public void copySelectedEntries(List entryIndexList, File destinationDirectory, IProgressObserver observer) { - ProgressAdapter progress = ProgressAdapter.wrap(observer); + ProgressAdapter progress = ProgressAdapter.make(observer); progress.setTotal(entryIndexList.size()); PlaylistEntry tempEntry; Path fileToCopy; @@ -553,7 +553,7 @@ public int addAt(int ix, Collection files, IProgressObserver obser private List getEntriesForFiles(Collection files, IProgressObserver observer) throws IOException { - ProgressAdapter progress = ProgressAdapter.wrap(observer); + ProgressAdapter progress = ProgressAdapter.make(observer); progress.setTotal(files.size()); List ents = new ArrayList<>(); @@ -565,7 +565,7 @@ private List getEntriesForFiles(Collection files, IProgress { // playlist file IPlaylistReader reader = PlaylistReaderFactory.getPlaylistReader(file, this.playListOptions); - ents.addAll(reader.readPlaylist(progress)); + ents.addAll(reader.readPlaylist(null)); // ToDo: nest this progress in overall progress } else { @@ -604,7 +604,7 @@ public void changeEntryFileName(int ix, String newName) */ public List repair(IMediaLibrary mediaLibrary, IProgressObserver observer) { - ProgressAdapter progress = ProgressAdapter.wrap(observer); + ProgressAdapter progress = ProgressAdapter.make(observer); progress.setTotal(this._entries.size()); List fixed = new ArrayList<>(); @@ -669,7 +669,7 @@ public void batchRepair(IMediaLibrary dirLists, IProgressObserver observ */ public void batchRepair(Collection fileList, IMediaLibrary dirLists, IProgressObserver observer) { - ProgressAdapter progress = ProgressAdapter.wrap(observer); + ProgressAdapter progress = ProgressAdapter.make(observer); progress.setTotal(_entries.size()); final boolean caseInsensitive = this.playListOptions.getCaseInsensitiveExactMatching(); @@ -714,7 +714,7 @@ public List findClosestMatches(Collection libraryFiles, public List findClosestMatches(List entries, Collection libraryFiles, IProgressObserver observer) { - ProgressAdapter progress = ProgressAdapter.wrap(observer); + ProgressAdapter progress = ProgressAdapter.make(observer); progress.setTotal(entries.size()); List fixed = new LinkedList<>(); @@ -936,12 +936,8 @@ public void saveAs(File destination, IProgressObserver observer) throws public final void save(boolean saveRelative, IProgressObserver observer) throws Exception { // avoid resetting total if part of batch operation - boolean hasTotal = observer instanceof ProgressAdapter; - ProgressAdapter progress = ProgressAdapter.wrap(observer); - if (!hasTotal) - { - progress.setTotal(_entries.size()); - } + ProgressAdapter progress = ProgressAdapter.make(observer); + progress.setTotal(_entries.size()); IPlaylistWriter writer = PlaylistWriterFactory.getPlaylistWriter(_file, this.playListOptions); writer.save(this, saveRelative, progress); diff --git a/src/main/java/listfix/model/playlists/PlaylistEntry.java b/src/main/java/listfix/model/playlists/PlaylistEntry.java index 4de70e40..77bd5b2b 100644 --- a/src/main/java/listfix/model/playlists/PlaylistEntry.java +++ b/src/main/java/listfix/model/playlists/PlaylistEntry.java @@ -150,7 +150,7 @@ public void play(IPlaylistOptions playListOptions) throws Exception public List findClosestMatches(Collection mediaFiles, IProgressObserver observer, IPlaylistOptions playListOptions) { List matches = new ArrayList<>(); - ProgressAdapter progress = ProgressAdapter.wrap(observer); + ProgressAdapter progress = new ProgressAdapter<>(observer); progress.setTotal(mediaFiles.size()); matches.clear(); diff --git a/src/main/java/listfix/view/dialogs/BatchClosestMatchResultsDialog.form b/src/main/java/listfix/view/dialogs/BatchClosestMatchResultsDialog.form deleted file mode 100644 index 92fb79d2..00000000 --- a/src/main/java/listfix/view/dialogs/BatchClosestMatchResultsDialog.form +++ /dev/null @@ -1,67 +0,0 @@ - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/main/java/listfix/view/dialogs/BatchExactMatchesResultsDialog.form b/src/main/java/listfix/view/dialogs/BatchExactMatchesResultsDialog.form deleted file mode 100644 index 50383e72..00000000 --- a/src/main/java/listfix/view/dialogs/BatchExactMatchesResultsDialog.form +++ /dev/null @@ -1,179 +0,0 @@ - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/main/java/listfix/view/dialogs/BatchExactMatchesResultsDialog.java b/src/main/java/listfix/view/dialogs/BatchExactMatchesResultsDialog.java index d6e07fd6..90506b09 100644 --- a/src/main/java/listfix/view/dialogs/BatchExactMatchesResultsDialog.java +++ b/src/main/java/listfix/view/dialogs/BatchExactMatchesResultsDialog.java @@ -15,20 +15,28 @@ import org.apache.logging.log4j.Logger; import javax.swing.*; +import javax.swing.border.BevelBorder; +import java.awt.event.*; + import java.io.File; import java.util.List; import java.util.concurrent.ExecutionException; /** * This is the dialog we display for an exact matches search on multiple playlists. - * @author jcaron */ -public class BatchExactMatchesResultsDialog extends javax.swing.JDialog +public class BatchExactMatchesResultsDialog extends JDialog { + private JButton _btnBrowse; + private JButton _btnSave; + private JCheckBox _chkBackup; + private PlaylistsList _pnlList; + private JTextField _txtBackup; + private listfix.view.controls.PlaylistEditCtrl playlistEditCtrl1; private boolean _userCancelled = false; private static final Logger _logger = LogManager.getLogger(BatchExactMatchesResultsDialog.class); - private final IListFixGui listFixGui; + private final BatchRepair _batch; /** * Creates new form BatchExactMatchesResultsDialog @@ -43,14 +51,14 @@ public BatchExactMatchesResultsDialog(java.awt.Frame parent, boolean modal, Batc _txtBackup.setText(_batch.getDefaultBackupName()); // load and repair lists - final DualProgressDialog pd = new DualProgressDialog(parent, "Finding Exact Matches...", "Please wait...", "Overall Progress:"); + final DualProgressDialog> pd = new DualProgressDialog<>(parent, "Finding Exact Matches...", "Please wait...", "Overall Progress:"); DualProgressWorker dpw = new DualProgressWorker<>() { @Override protected void process(List> chunks) { - ProgressItem titem = new ProgressItem(true, -1, null); - ProgressItem oitem = new ProgressItem(false, -1, null); + ProgressItem titem = new ProgressItem<>(true, -1, null); + ProgressItem oitem = new ProgressItem<>(false, -1, null); getEffectiveItems(chunks, titem, oitem); if (titem.percentComplete >= 0) @@ -95,6 +103,7 @@ protected Void doInBackground() for (BatchRepairItem item : _batch.getItems()) { + IPlaylistModifiedListener listener = this::onPlaylistModified; item.getPlaylist().addModifiedListener(listener); } @@ -115,8 +124,6 @@ protected Void doInBackground() } } - private final IPlaylistModifiedListener listener = this :: onPlaylistModified; - private void onPlaylistModified(Playlist list) { _pnlList.playlistModified(list); @@ -134,110 +141,103 @@ private void updateSelectedPlaylist() } } - private final BatchRepair _batch; + private void initComponents() + { - /** - * This method is called from within the constructor to - * initialize the form. - * WARNING: Do NOT modify this code. The content of this method is - * always regenerated by the Form Editor. - */ - @SuppressWarnings("unchecked") - // //GEN-BEGIN:initComponents - private void initComponents() { - - _backupPanel = new javax.swing.JPanel(); - _chkBackup = new javax.swing.JCheckBox(); - _txtBackup = new javax.swing.JTextField(); - _btnCancel = new javax.swing.JButton(); - _btnSave = new javax.swing.JButton(); - _btnBrowse = new javax.swing.JButton(); - jSplitPane1 = new javax.swing.JSplitPane(); - jPanel1 = new javax.swing.JPanel(); - jPanel3 = new javax.swing.JPanel(); - jLabel1 = new javax.swing.JLabel(); - playlistEditCtrl1 = new listfix.view.controls.PlaylistEditCtrl(this.listFixGui); - _pnlList = new PlaylistsList(_batch); - - setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - addWindowListener(new java.awt.event.WindowAdapter() { - public void windowClosing(java.awt.event.WindowEvent evt) { - formWindowClosing(evt); - } - }); - - _chkBackup.setText("Backup original files to zip file:"); - _chkBackup.addItemListener(evt -> onChkBackupItemStateChanged(evt)); - - _txtBackup.setEnabled(false); - - _btnCancel.setText("Cancel"); - _btnCancel.addActionListener(evt -> onBtnCancelActionPerformed(evt)); - - _btnSave.setText("Save All Repairs"); - _btnSave.addActionListener(evt -> onBtnSaveActionPerformed(evt)); - - _btnBrowse.setText("..."); - _btnBrowse.setEnabled(false); - _btnBrowse.addActionListener(evt -> onBtnBrowseActionPerformed(evt)); - - javax.swing.GroupLayout _backupPanelLayout = new javax.swing.GroupLayout(_backupPanel); - _backupPanel.setLayout(_backupPanelLayout); - _backupPanelLayout.setHorizontalGroup( - _backupPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(_backupPanelLayout.createSequentialGroup() - .addContainerGap() - .addComponent(_chkBackup) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(_txtBackup, javax.swing.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(_btnBrowse) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 98, Short.MAX_VALUE) - .addComponent(_btnSave) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(_btnCancel) - .addContainerGap()) - ); - _backupPanelLayout.setVerticalGroup( - _backupPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(_backupPanelLayout.createSequentialGroup() - .addContainerGap() - .addGroup(_backupPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(_chkBackup) - .addComponent(_txtBackup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(_btnCancel) - .addComponent(_btnSave) - .addComponent(_btnBrowse)) - .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) - ); - - getContentPane().add(_backupPanel, java.awt.BorderLayout.PAGE_END); - - jSplitPane1.setDividerLocation(184); - jSplitPane1.setContinuousLayout(true); - - jPanel1.setLayout(new java.awt.BorderLayout()); - - jPanel3.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT)); - - jLabel1.setText("Playlist details"); - jPanel3.add(jLabel1); - - jPanel1.add(jPanel3, java.awt.BorderLayout.PAGE_START); - - playlistEditCtrl1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED)); - jPanel1.add(playlistEditCtrl1, java.awt.BorderLayout.CENTER); - - jSplitPane1.setRightComponent(jPanel1); - jSplitPane1.setLeftComponent(_pnlList); - - getContentPane().add(jSplitPane1, java.awt.BorderLayout.CENTER); - - pack(); - }// //GEN-END:initComponents - - private void onBtnBrowseActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_onBtnBrowseActionPerformed - {//GEN-HEADEREND:event_onBtnBrowseActionPerformed + JPanel _backupPanel = new JPanel(); + _chkBackup = new JCheckBox(); + _txtBackup = new JTextField(); + JButton _btnCancel = new JButton(); + _btnSave = new JButton(); + _btnBrowse = new JButton(); + JSplitPane jSplitPane1 = new JSplitPane(); + JPanel jPanel1 = new JPanel(); + JPanel jPanel3 = new JPanel(); + JLabel jLabel1 = new JLabel(); + playlistEditCtrl1 = new listfix.view.controls.PlaylistEditCtrl(this.listFixGui); + _pnlList = new PlaylistsList(_batch); + + setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); + addWindowListener(new WindowAdapter() + { + public void windowClosing(WindowEvent evt) + { + formWindowClosing(evt); + } + }); + + _chkBackup.setText("Backup original files to zip file:"); + _chkBackup.addItemListener(this::onChkBackupItemStateChanged); + + _txtBackup.setEnabled(false); + + _btnCancel.setText("Cancel"); + _btnCancel.addActionListener(this::onBtnCancelActionPerformed); + + _btnSave.setText("Save All Repairs"); + _btnSave.addActionListener(this::onBtnSaveActionPerformed); + + _btnBrowse.setText("..."); + _btnBrowse.setEnabled(false); + _btnBrowse.addActionListener(this::onBtnBrowseActionPerformed); + + GroupLayout _backupPanelLayout = new GroupLayout(_backupPanel); + _backupPanel.setLayout(_backupPanelLayout); + _backupPanelLayout.setHorizontalGroup( + _backupPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING) + .addGroup(_backupPanelLayout.createSequentialGroup() + .addContainerGap() + .addComponent(_chkBackup) + .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) + .addComponent(_txtBackup, GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE) + .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) + .addComponent(_btnBrowse) + .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 98, Short.MAX_VALUE) + .addComponent(_btnSave) + .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) + .addComponent(_btnCancel) + .addContainerGap()) + ); + _backupPanelLayout.setVerticalGroup( + _backupPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING) + .addGroup(_backupPanelLayout.createSequentialGroup() + .addContainerGap() + .addGroup(_backupPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE) + .addComponent(_chkBackup) + .addComponent(_txtBackup, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) + .addComponent(_btnCancel) + .addComponent(_btnSave) + .addComponent(_btnBrowse)) + .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + ); + + getContentPane().add(_backupPanel, java.awt.BorderLayout.PAGE_END); + + jSplitPane1.setDividerLocation(184); + jSplitPane1.setContinuousLayout(true); + + jPanel1.setLayout(new java.awt.BorderLayout()); + + jPanel3.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT)); + + jLabel1.setText("Playlist details"); + jPanel3.add(jLabel1); + + jPanel1.add(jPanel3, java.awt.BorderLayout.PAGE_START); + + playlistEditCtrl1.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED)); + jPanel1.add(playlistEditCtrl1, java.awt.BorderLayout.CENTER); + + jSplitPane1.setRightComponent(jPanel1); + jSplitPane1.setLeftComponent(_pnlList); + + getContentPane().add(jSplitPane1, java.awt.BorderLayout.CENTER); + + pack(); + } + + private void onBtnBrowseActionPerformed(ActionEvent evt) + { JFileChooser dlg = new JFileChooser(); if (!_txtBackup.getText().isEmpty()) { @@ -247,10 +247,10 @@ private void onBtnBrowseActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIR { _txtBackup.setText(dlg.getSelectedFile().getAbsolutePath()); } - }//GEN-LAST:event_onBtnBrowseActionPerformed + } - private void onBtnSaveActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_onBtnSaveActionPerformed - {//GEN-HEADEREND:event_onBtnSaveActionPerformed + private void onBtnSaveActionPerformed(ActionEvent evt)//GEN-FIRST:event_onBtnSaveActionPerformed + {//GEN-HEADEREND:event_onBtnSaveActionPerformed ProgressWorker worker = new ProgressWorker<>() { @Override @@ -267,7 +267,7 @@ protected Void doInBackground() throws Exception { worker.get(); } - catch (InterruptedException ex) + catch (InterruptedException ignore) { // ignore, these happen when people cancel - should not be logged either. } @@ -281,16 +281,16 @@ protected Void doInBackground() throws Exception } setVisible(false); - }//GEN-LAST:event_onBtnSaveActionPerformed + } - private void onBtnCancelActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_onBtnCancelActionPerformed - {//GEN-HEADEREND:event_onBtnCancelActionPerformed + private void onBtnCancelActionPerformed(ActionEvent evt) + { _userCancelled = true; setVisible(false); - }//GEN-LAST:event_onBtnCancelActionPerformed + } - private void onChkBackupItemStateChanged(java.awt.event.ItemEvent evt)//GEN-FIRST:event_onChkBackupItemStateChanged - {//GEN-HEADEREND:event_onChkBackupItemStateChanged + private void onChkBackupItemStateChanged(ItemEvent evt) + { boolean isChecked = _chkBackup.isSelected(); _txtBackup.setEnabled(isChecked); _btnBrowse.setEnabled(isChecked); @@ -299,42 +299,15 @@ private void onChkBackupItemStateChanged(java.awt.event.ItemEvent evt)//GEN-FIRS _txtBackup.selectAll(); _txtBackup.requestFocusInWindow(); } - }//GEN-LAST:event_onChkBackupItemStateChanged - - private void formWindowClosing(java.awt.event.WindowEvent evt)//GEN-FIRST:event_formWindowClosing - {//GEN-HEADEREND:event_formWindowClosing - _userCancelled = true; - }//GEN-LAST:event_formWindowClosing - - public boolean isUserCancelled() - { - return getUserCancelled(); } - /** - * @param userCancelled the _userCancelled to set - */ - public void setUserCancelled(boolean userCancelled) + private void formWindowClosing(WindowEvent ignore) { - this._userCancelled = userCancelled; + _userCancelled = true; } - public boolean getUserCancelled() + public boolean getUserCancelled() { return _userCancelled; } - // Variables declaration - do not modify//GEN-BEGIN:variables - private javax.swing.JPanel _backupPanel; - private javax.swing.JButton _btnBrowse; - private javax.swing.JButton _btnCancel; - private javax.swing.JButton _btnSave; - private javax.swing.JCheckBox _chkBackup; - private listfix.view.controls.PlaylistsList _pnlList; - private javax.swing.JTextField _txtBackup; - private javax.swing.JLabel jLabel1; - private javax.swing.JPanel jPanel1; - private javax.swing.JPanel jPanel3; - private javax.swing.JSplitPane jSplitPane1; - private listfix.view.controls.PlaylistEditCtrl playlistEditCtrl1; - // End of variables declaration//GEN-END:variables } diff --git a/src/main/java/listfix/view/dialogs/DualProgressDialog.form b/src/main/java/listfix/view/dialogs/DualProgressDialog.form deleted file mode 100644 index d29bd614..00000000 --- a/src/main/java/listfix/view/dialogs/DualProgressDialog.form +++ /dev/null @@ -1,106 +0,0 @@ - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/main/java/listfix/view/dialogs/DualProgressDialog.java b/src/main/java/listfix/view/dialogs/DualProgressDialog.java index 7631848f..7ea8e66b 100644 --- a/src/main/java/listfix/view/dialogs/DualProgressDialog.java +++ b/src/main/java/listfix/view/dialogs/DualProgressDialog.java @@ -1,18 +1,26 @@ - - package listfix.view.dialogs; import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; -import java.beans.PropertyChangeEvent; -import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; -public class DualProgressDialog extends javax.swing.JDialog +/** + * Dialog displaying double progressbar to show progress of nested background tasks. + * + * @param the result type returned by this SwingWorker's doInBackground and get methods + * @param the type used for carrying out intermediate results by this SwingWorker's publish and process methods + */ +public class DualProgressDialog extends javax.swing.JDialog { - /** Creates new form DualProgressDialog - */ + private SwingWorker _worker; + private JLabel _overallLabel; + private JProgressBar _overallProgress; + private JLabel _taskLabel; + private JProgressBar _taskProgress; + public DualProgressDialog(java.awt.Frame parent, String title, boolean modal) { super(parent, title, modal); @@ -22,16 +30,11 @@ public DualProgressDialog(java.awt.Frame parent, String title, boolean modal) public DualProgressDialog(java.awt.Frame parent, String title, String taskMsg, String overallMsg) { this(parent, title, true); - _taskLabel.setText(taskMsg); - _overallLabel.setText(overallMsg); - pack(); - setSize(400, getHeight()); setLocationRelativeTo(parent); - addWindowListener(new WindowAdapter() { @Override @@ -42,134 +45,119 @@ public void windowOpened(WindowEvent e) }); } - /** - * - * @param worker - */ - public void show(SwingWorker worker) + public void show(SwingWorker worker) { _worker = worker; PropertyChangeSupport pcs = _worker.getPropertyChangeSupport(); // close dialog when state changes to done - pcs.addPropertyChangeListener("state", new PropertyChangeListener() - { - public void propertyChange(PropertyChangeEvent evt) + pcs.addPropertyChangeListener("state", evt -> { + if (_worker.isDone()) { - if (_worker.isDone()) - { - setVisible(false); - } + setVisible(false); } }); - setVisible(true); } /** * This method is called from within the constructor to * initialize the form. - * WARNING: Do NOT modify this code. The content of this method is - * always regenerated by the Form Editor. */ - @SuppressWarnings("unchecked") - // //GEN-BEGIN:initComponents - private void initComponents() { - java.awt.GridBagConstraints gridBagConstraints; - - _middlePanel = new javax.swing.JPanel(); - _taskLabel = new javax.swing.JLabel(); - _taskProgress = new javax.swing.JProgressBar(); - _overallLabel = new javax.swing.JLabel(); - _overallProgress = new javax.swing.JProgressBar(); - _bottomPanel = new javax.swing.JPanel(); - _cancelButton = new javax.swing.JButton(); - - setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - addWindowListener(new java.awt.event.WindowAdapter() { - public void windowClosing(java.awt.event.WindowEvent evt) { - formWindowClosing(evt); - } - }); - - _middlePanel.setLayout(new java.awt.GridBagLayout()); - - _taskLabel.setText("Task Progress"); - gridBagConstraints = new java.awt.GridBagConstraints(); - gridBagConstraints.gridy = 0; - gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; - gridBagConstraints.weightx = 1.0; - gridBagConstraints.insets = new java.awt.Insets(10, 10, 4, 10); - _middlePanel.add(_taskLabel, gridBagConstraints); - - _taskProgress.setMinimumSize(new java.awt.Dimension(250, 14)); - gridBagConstraints = new java.awt.GridBagConstraints(); - gridBagConstraints.gridy = 1; - gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; - gridBagConstraints.insets = new java.awt.Insets(0, 10, 10, 10); - _middlePanel.add(_taskProgress, gridBagConstraints); - - _overallLabel.setText("Overall Progress"); - gridBagConstraints = new java.awt.GridBagConstraints(); - gridBagConstraints.gridy = 2; - gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; - gridBagConstraints.weightx = 1.0; - gridBagConstraints.insets = new java.awt.Insets(10, 10, 4, 10); - _middlePanel.add(_overallLabel, gridBagConstraints); - - _overallProgress.setMinimumSize(new java.awt.Dimension(250, 14)); - gridBagConstraints = new java.awt.GridBagConstraints(); - gridBagConstraints.gridy = 3; - gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; - gridBagConstraints.insets = new java.awt.Insets(0, 10, 10, 10); - _middlePanel.add(_overallProgress, gridBagConstraints); - - getContentPane().add(_middlePanel, java.awt.BorderLayout.CENTER); - - _bottomPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT)); - - _cancelButton.setText("Cancel"); - _cancelButton.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - _cancelButtonActionPerformed(evt); - } - }); - _cancelButton.addKeyListener(new java.awt.event.KeyAdapter() { - public void keyPressed(java.awt.event.KeyEvent evt) { - _cancelButtonKeyPressed(evt); - } - }); - _bottomPanel.add(_cancelButton); - - getContentPane().add(_bottomPanel, java.awt.BorderLayout.SOUTH); - - pack(); - }// //GEN-END:initComponents - - private void _cancelButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event__cancelButtonActionPerformed - {//GEN-HEADEREND:event__cancelButtonActionPerformed + private void initComponents() + { + java.awt.GridBagConstraints gridBagConstraints; + + JPanel _middlePanel = new JPanel(); + _taskLabel = new javax.swing.JLabel(); + _taskProgress = new javax.swing.JProgressBar(); + _overallLabel = new javax.swing.JLabel(); + _overallProgress = new javax.swing.JProgressBar(); + // Variables declaration - do not modify//GEN-BEGIN:variables + JPanel _bottomPanel = new JPanel(); + JButton _cancelButton = new JButton(); + + setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); + addWindowListener(new WindowAdapter() + { + public void windowClosing(java.awt.event.WindowEvent evt) + { + formWindowClosing(evt); + } + }); + + _middlePanel.setLayout(new java.awt.GridBagLayout()); + + _taskLabel.setText("Task Progress"); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridy = 0; + gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; + gridBagConstraints.weightx = 1.0; + gridBagConstraints.insets = new java.awt.Insets(10, 10, 4, 10); + _middlePanel.add(_taskLabel, gridBagConstraints); + + _taskProgress.setMinimumSize(new java.awt.Dimension(250, 14)); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridy = 1; + gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; + gridBagConstraints.insets = new java.awt.Insets(0, 10, 10, 10); + _middlePanel.add(_taskProgress, gridBagConstraints); + + _overallLabel.setText("Overall Progress"); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridy = 2; + gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; + gridBagConstraints.weightx = 1.0; + gridBagConstraints.insets = new java.awt.Insets(10, 10, 4, 10); + _middlePanel.add(_overallLabel, gridBagConstraints); + + _overallProgress.setMinimumSize(new java.awt.Dimension(250, 14)); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridy = 3; + gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; + gridBagConstraints.insets = new java.awt.Insets(0, 10, 10, 10); + _middlePanel.add(_overallProgress, gridBagConstraints); + + getContentPane().add(_middlePanel, java.awt.BorderLayout.CENTER); + + _bottomPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT)); + + _cancelButton.setText("Cancel"); + _cancelButton.addActionListener(this::_cancelButtonActionPerformed); + _cancelButton.addKeyListener(new java.awt.event.KeyAdapter() + { + public void keyPressed(java.awt.event.KeyEvent evt) + { + _cancelButtonKeyPressed(evt); + } + }); + _bottomPanel.add(_cancelButton); + + getContentPane().add(_bottomPanel, java.awt.BorderLayout.SOUTH); + + pack(); + } + + private void _cancelButtonActionPerformed(ActionEvent ignore) + { _worker.cancel(true); - }//GEN-LAST:event__cancelButtonActionPerformed + } - private void formWindowClosing(java.awt.event.WindowEvent evt)//GEN-FIRST:event_formWindowClosing - {//GEN-HEADEREND:event_formWindowClosing + private void formWindowClosing(java.awt.event.WindowEvent ignore) + { _worker.cancel(true); - }//GEN-LAST:event_formWindowClosing + } - private void _cancelButtonKeyPressed(java.awt.event.KeyEvent evt)//GEN-FIRST:event__cancelButtonKeyPressed - {//GEN-HEADEREND:event__cancelButtonKeyPressed - if (evt.getKeyCode() == evt.VK_ESCAPE) + private void _cancelButtonKeyPressed(java.awt.event.KeyEvent evt) + { + if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) { _worker.cancel(true); } - }//GEN-LAST:event__cancelButtonKeyPressed + } - /** - * - * @return - */ public JLabel getTaskLabel() { return _taskLabel; @@ -189,15 +177,4 @@ public JProgressBar getOverallProgressBar() { return _overallProgress; } - - SwingWorker _worker; - // Variables declaration - do not modify//GEN-BEGIN:variables - private javax.swing.JPanel _bottomPanel; - private javax.swing.JButton _cancelButton; - private javax.swing.JPanel _middlePanel; - private javax.swing.JLabel _overallLabel; - private javax.swing.JProgressBar _overallProgress; - private javax.swing.JLabel _taskLabel; - private javax.swing.JProgressBar _taskProgress; - // End of variables declaration//GEN-END:variables } diff --git a/src/main/java/listfix/view/dialogs/MultiListBatchClosestMatchResultsDialog.java b/src/main/java/listfix/view/dialogs/MultiListBatchClosestMatchResultsDialog.java index 3f9e20d9..1142568d 100644 --- a/src/main/java/listfix/view/dialogs/MultiListBatchClosestMatchResultsDialog.java +++ b/src/main/java/listfix/view/dialogs/MultiListBatchClosestMatchResultsDialog.java @@ -31,19 +31,10 @@ public class MultiListBatchClosestMatchResultsDialog extends javax.swing.JDialog private IPlaylistOptions filePathOptions; - /** - * Creates new form MultiListBatchClosestMatchResultsDialog - */ - public MultiListBatchClosestMatchResultsDialog(java.awt.Frame parent, boolean modal, IPlaylistOptions filePathOptions) - { - super(parent, modal); - this.filePathOptions = filePathOptions; - initComponents(); - } - public MultiListBatchClosestMatchResultsDialog(java.awt.Frame parent, boolean modal, BatchRepair br, IPlaylistOptions filePathOptions) { super(parent, br.getDescription(), modal); + this.filePathOptions = filePathOptions; _batch = br; initComponents(); diff --git a/src/main/java/listfix/view/support/DualProgressAdapter.java b/src/main/java/listfix/view/support/DualProgressAdapter.java index 18239056..2ef0389a 100644 --- a/src/main/java/listfix/view/support/DualProgressAdapter.java +++ b/src/main/java/listfix/view/support/DualProgressAdapter.java @@ -1,94 +1,77 @@ - - package listfix.view.support; -public final class DualProgressAdapter implements IDualProgressObserver +public final class DualProgressAdapter { - public static DualProgressAdapter wrap(IDualProgressObserver observer) - { - if (observer instanceof DualProgressAdapter) - return (DualProgressAdapter) observer; - else - return new DualProgressAdapter<>(observer); - } + private final IDualProgressObserver observer; + private final ProgressAdapter overall; + private final IProgressObserver task; + private int taskPercentageComplete = 0; - private DualProgressAdapter(IDualProgressObserver observer) + public DualProgressAdapter(IDualProgressObserver observer) { - _observer = observer; + this.observer = observer; - IProgressObserver taskObserver = new IProgressObserver() + this.task = new IProgressObserver() { + @Override public void reportProgress(int progress) { - _observer.reportTaskProgress(progress, null); + this.reportProgress(progress, null); } + @Override public void reportProgress(int progress, T state) { - _observer.reportTaskProgress(progress, state); + DualProgressAdapter.this.observer.reportTaskProgress(progress, state); + // Adjust overall progress + DualProgressAdapter.this.taskPercentageComplete = progress; + // Trigger total progress + DualProgressAdapter.this.overall.setCompleted(DualProgressAdapter.this.overall.getCompleted()); } @Override public boolean getCancelled() { - return _observer.getCancelled(); + return DualProgressAdapter.this.observer.getCancelled(); } }; - _task = ProgressAdapter.wrap(taskObserver); - IProgressObserver overallObserver = new IProgressObserver() + this.overall = new ProgressAdapter(new IProgressObserver() { + @Override public void reportProgress(int progress) { - _observer.reportOverallProgress(progress, null); + this.reportProgress(progress, null); } + @Override public void reportProgress(int progress, T state) { - _observer.reportOverallProgress(progress, state); + DualProgressAdapter.this.observer.reportOverallProgress(progress, state); } @Override public boolean getCancelled() { - return _observer.getCancelled(); + return DualProgressAdapter.this.observer.getCancelled(); + } + }){ + @Override + protected double calculateProgress(long completed, long total) { + double taskPortion = (double)DualProgressAdapter.this.taskPercentageComplete / total; + return super.calculateProgress(completed, total) + taskPortion; } }; - _overall = ProgressAdapter.wrap(overallObserver); - } - - public void reportTaskProgress(int percentComplete, T state) - { - if (_observer != null) - _observer.reportTaskProgress(percentComplete, state); - } - - public void reportOverallProgress(int percentComplete, T state) - { - if (_observer != null) - _observer.reportOverallProgress(percentComplete, state); } - IDualProgressObserver _observer; - - public ProgressAdapter getTask() + public IProgressObserver getTask() { - return _task; + return this.task; } - ProgressAdapter _task; - public ProgressAdapter getOverall() { - return _overall; - } - - ProgressAdapter _overall; - - @Override - public boolean getCancelled() - { - return _observer.getCancelled(); + return overall; } } diff --git a/src/main/java/listfix/view/support/DualProgressWorker.java b/src/main/java/listfix/view/support/DualProgressWorker.java index a1d74f85..a612b2a0 100644 --- a/src/main/java/listfix/view/support/DualProgressWorker.java +++ b/src/main/java/listfix/view/support/DualProgressWorker.java @@ -16,12 +16,12 @@ public abstract class DualProgressWorker extends SwingWorker(true, percentComplete, state)); } public void reportOverallProgress(int percentComplete, V state) { - publish(new ProgressItem(false, percentComplete, state)); + publish(new ProgressItem(false, percentComplete, state)); } public boolean getCancelled() @@ -29,7 +29,7 @@ public boolean getCancelled() return this.isCancelled(); } - protected static class ProgressItem + public static class ProgressItem { public ProgressItem(boolean isTask, int percentComplete, V state) { diff --git a/src/main/java/listfix/view/support/ProgressAdapter.java b/src/main/java/listfix/view/support/ProgressAdapter.java index ace2bc68..cf1b28cf 100644 --- a/src/main/java/listfix/view/support/ProgressAdapter.java +++ b/src/main/java/listfix/view/support/ProgressAdapter.java @@ -1,58 +1,61 @@ - - package listfix.view.support; -public final class ProgressAdapter implements IProgressObserver +public class ProgressAdapter { - public static ProgressAdapter wrap(IProgressObserver observer) - { - if (observer instanceof ProgressAdapter) - return (ProgressAdapter) observer; - else - return new ProgressAdapter(observer); - } + private IProgressObserver observer; + private long _total; + private long completed; + private int percentComplete; - private ProgressAdapter(IProgressObserver observer) + private static final IProgressObserver dummyObserver = new IProgressObserver<>() { - _observer = observer; - } + @Override + public void reportProgress(int progress) + { + } + + @Override + public void reportProgress(int progress, Object state) + { + } - public void reportProgress(int progress) + @Override + public boolean getCancelled() + { + return false; + } + }; + + public static ProgressAdapter make(IProgressObserver observer) { - if (_observer != null) - _observer.reportProgress(progress); + return new ProgressAdapter<>(observer == null ? (IProgressObserver)dummyObserver : observer); } - public void reportProgress(int progress, T state) + public ProgressAdapter(IProgressObserver observer) { - if (_observer != null) - _observer.reportProgress(progress, state); + this.observer = observer; } - private IProgressObserver _observer; - public long getCompleted() { - return _completed; + return this.completed; } public void setCompleted(long completed) { - _completed = completed; + this.completed = completed; refreshPercentComplete(); } - private long _completed; - public void stepCompleted() { - _completed += 1; + this.completed += 1; refreshPercentComplete(); } public void stepCompleted(long done) { - _completed += done; + this.completed += done; refreshPercentComplete(); } @@ -63,40 +66,47 @@ public long getTotal() public void setTotal(long total) { - boolean report = isValid() && _percentComplete != 0 && total != 0; - _total = total; - _completed = 0; - _percentComplete = 0; + boolean report = isValid() && percentComplete != 0 && total != 0; + this._total = total; + this.completed = 0; + this.percentComplete = 0; if (report) - reportProgress(_percentComplete); + this.observer.reportProgress(percentComplete); } - private long _total; - - private void refreshPercentComplete() { if (isValid()) { - double pct = Math.round(((double)_completed * 100.0) / (double)_total); - if (pct != _percentComplete) + double pct = Math.round(this.calculateProgress(this.completed, this._total)); + if (pct != this.percentComplete) { - _percentComplete = (int) pct; - reportProgress(_percentComplete); + this.percentComplete = (int) pct; + this.observer.reportProgress(this.percentComplete); } } } + protected double calculateProgress(long completed, long total) { + return (completed * 100.0) / (double) total; + } + private boolean isValid() { - return _total > 0 && _observer != null; + return this._total > 0 && this.observer != null; } - private int _percentComplete; - public boolean getCancelled() { - return _observer != null ? _observer.getCancelled() : false; + return this.observer != null && this.observer.getCancelled(); + } + +// public IProgressObserver getObserver() { +// return this.observer; +// } + + public int getPercentComplete() { + return this.percentComplete; } }