TaskWindow now shows progress of each task.

This commit is contained in:
huanghongxun 2015-07-28 13:50:14 +08:00
parent 538688c8f1
commit 369fe5e164
14 changed files with 117 additions and 66 deletions

View File

@ -20,6 +20,7 @@ import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;
import org.jackhuang.hellominecraft.C;
import org.jackhuang.hellominecraft.HMCLog;
import org.jackhuang.hellominecraft.launcher.settings.Settings;

View File

@ -124,6 +124,7 @@ public abstract class IAssetsHandler {
progress = 0;
max = assetsDownloadURLs.size();
al = new ArrayList<>();
int hasDownloaded = 0;
for (int i = 0; i < max; i++) {
String mark = assetsDownloadURLs.get(i);
String url = u + mark;
@ -137,7 +138,10 @@ public abstract class IAssetsHandler {
String sha = DigestUtils.sha1Hex(NetUtils.getBytesFromStream(fis));
IOUtils.closeQuietly(fis);
if (contents.get(i).eTag.equals(sha)) {
hasDownloaded++;
HMCLog.log("File " + assetsLocalNames.get(i) + " has downloaded successfully, skipped downloading.");
if (ppl != null)
ppl.setProgress(this, hasDownloaded, max);
continue;
}
}

View File

@ -1,31 +0,0 @@
/*
* Copyright 2013 huangyuhui <huanghongxun2008@126.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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.
*/
package org.jackhuang.hellominecraft.tasks;
/**
*
* @author huangyuhui
*/
public abstract class ProgressProvider {
protected ProgressProviderListener ppl;
public ProgressProvider setProgressProviderListener(ProgressProviderListener p) {
ppl = p;
return this;
}
}

View File

@ -22,9 +22,9 @@ package org.jackhuang.hellominecraft.tasks;
*/
public interface ProgressProviderListener {
void setProgress(int prog, int max);
void setProgress(Task task, int prog, int max);
void setStatus(String sta);
void setStatus(Task task, String sta);
void onProgressProviderDone();
void onProgressProviderDone(Task task);
}

View File

@ -22,7 +22,7 @@ import java.util.Collection;
*
* @author huangyuhui
*/
public abstract class Task extends ProgressProvider {
public abstract class Task {
/**
* Run in a new thread(packed in TaskList).
@ -75,4 +75,11 @@ public abstract class Task extends ProgressProvider {
public Collection<Task> getAfterTasks() {
return null;
}
protected ProgressProviderListener ppl;
public Task setProgressProviderListener(ProgressProviderListener p) {
ppl = p;
return this;
}
}

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Form version="1.3" maxVersion="1.8" type="org.netbeans.modules.form.forminfo.JDialogFormInfo">
<Form version="1.6" maxVersion="1.8" type="org.netbeans.modules.form.forminfo.JDialogFormInfo">
<Properties>
<Property name="defaultCloseOperation" type="int" value="2"/>
<Property name="title" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
@ -53,7 +53,7 @@
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<Component id="srlDownload" pref="241" max="32767" attributes="0"/>
<Component id="srlDownload" pref="232" max="32767" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="btnCancel" alignment="1" min="-2" max="-2" attributes="0"/>
@ -114,10 +114,14 @@
<Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
<SubComponents>
<Component class="javax.swing.JList" name="lstDownload">
<Component class="javax.swing.JTable" name="lstDownload">
<Properties>
<Property name="model" type="javax.swing.ListModel" editor="org.netbeans.modules.form.editors2.ListModelEditor">
<StringArray count="0"/>
<Property name="model" type="javax.swing.table.TableModel" editor="org.netbeans.modules.form.RADConnectionPropertyEditor">
<Connection code="SwingUtils.makeDefaultTableModel(new String[]{C.i18n(&quot;taskwindow.file_name&quot;), C.i18n(&quot;taskwindow.download_progress&quot;)}, new Class[]{String.class, String.class}, new boolean[]{false,false})&#xd;&#xa;" type="code"/>
</Property>
<Property name="rowSelectionAllowed" type="boolean" value="false"/>
<Property name="selectionModel" type="javax.swing.ListSelectionModel" editor="org.netbeans.modules.form.editors2.JTableSelectionModelEditor">
<JTableSelectionModel selectionMode="0"/>
</Property>
</Properties>
</Component>

View File

@ -18,6 +18,7 @@ package org.jackhuang.hellominecraft.tasks;
import java.awt.EventQueue;
import java.util.ArrayList;
import javax.swing.SwingUtilities;
import org.jackhuang.hellominecraft.C;
import org.jackhuang.hellominecraft.utils.functions.NonConsumer;
import org.jackhuang.hellominecraft.HMCLog;
@ -72,8 +73,9 @@ public class TaskWindow extends javax.swing.JDialog
pgsSingle.setValue(0);
pgsTotal.setValue(0);
suc = false;
SwingUtils.clear(lstDownload);
SwingUtils.clearDefaultTable(lstDownload);
failReasons.clear();
tasks.clear();
try {
taskList.start();
} catch (Exception e) {
@ -100,7 +102,7 @@ public class TaskWindow extends javax.swing.JDialog
lblTotalProgress = new javax.swing.JLabel();
pgsTotal = new javax.swing.JProgressBar();
srlDownload = new javax.swing.JScrollPane();
lstDownload = new javax.swing.JList();
lstDownload = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/jackhuang/hellominecraft/launcher/I18N"); // NOI18N
@ -126,6 +128,10 @@ public class TaskWindow extends javax.swing.JDialog
pgsTotal.setStringPainted(true);
lstDownload.setModel(SwingUtils.makeDefaultTableModel(new String[]{C.i18n("taskwindow.file_name"), C.i18n("taskwindow.download_progress")}, new Class[]{String.class, String.class}, new boolean[]{false,false})
);
lstDownload.setRowSelectionAllowed(false);
lstDownload.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
srlDownload.setViewportView(lstDownload);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
@ -151,7 +157,7 @@ public class TaskWindow extends javax.swing.JDialog
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(srlDownload, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
.addComponent(srlDownload, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnCancel, javax.swing.GroupLayout.Alignment.TRAILING)
@ -175,6 +181,8 @@ public class TaskWindow extends javax.swing.JDialog
}//GEN-LAST:event_btnCancelActionPerformed
private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
tasks.clear();
if (!this.failReasons.isEmpty()) {
MessageBox.Show(StrUtils.parseParams("", failReasons.toArray(), "\n"), C.i18n("message.error"), MessageBox.ERROR_MESSAGE);
failReasons.clear();
@ -190,16 +198,29 @@ public class TaskWindow extends javax.swing.JDialog
private javax.swing.JButton btnCancel;
private javax.swing.JLabel lblSingleProgress;
private javax.swing.JLabel lblTotalProgress;
private javax.swing.JList lstDownload;
private javax.swing.JTable lstDownload;
private javax.swing.JProgressBar pgsSingle;
private javax.swing.JProgressBar pgsTotal;
private javax.swing.JScrollPane srlDownload;
// End of variables declaration//GEN-END:variables
ArrayList<Task> tasks = new ArrayList<>();
ArrayList<Integer> progresses = new ArrayList<>();
@Override
public void setProgress(int progress, int max) {
pgsSingle.setMaximum(max);
pgsSingle.setValue(progress);
public void setProgress(Task task, int progress, int max) {
SwingUtilities.invokeLater(() -> {
int idx = tasks.indexOf(task);
if(idx == -1) return;
int pgs = progress * 100 / max;
if(progresses.get(idx) != pgs) {
SwingUtils.setValueAt(lstDownload, pgs + "%", idx, 1);
progresses.set(idx, pgs);
}
if(task.isParallelExecuting()) return;
pgsSingle.setMaximum(max);
pgsSingle.setValue(progress);
});
}
@Override
@ -211,11 +232,14 @@ public class TaskWindow extends javax.swing.JDialog
@Override
public void onDoing(Task task) {
if (!task.isParallelExecuting())
task.setProgressProviderListener(this);
task.setProgressProviderListener(this);
SwingUtils.appendLast(lstDownload, task.getInfo());
SwingUtils.moveEnd(srlDownload);
SwingUtilities.invokeLater(() -> {
tasks.add(task);
progresses.add(0);
SwingUtils.appendLast(lstDownload, task.getInfo(), "0%");
SwingUtils.moveEnd(srlDownload);
});
}
public boolean areTasksFinished() {
@ -224,26 +248,39 @@ public class TaskWindow extends javax.swing.JDialog
@Override
public void onDone(Task task) {
pgsTotal.setMaximum(taskList.taskCount());
pgsTotal.setValue(pgsTotal.getValue() + 1);
SwingUtilities.invokeLater(() -> {
pgsTotal.setMaximum(taskList.taskCount());
pgsTotal.setValue(pgsTotal.getValue() + 1);
int idx = tasks.indexOf(task);
if (idx == -1) return;
tasks.remove(idx);
progresses.remove(idx);
SwingUtils.removeRow(lstDownload, idx);
});
}
@Override
public void onFailed(Task task) {
failReasons.add(task.getInfo() + ": " + (null == task.getFailReason() ? "No exception" : task.getFailReason().getLocalizedMessage()));
pgsTotal.setMaximum(taskList.taskCount());
pgsTotal.setValue(pgsTotal.getValue() + 1);
SwingUtils.replaceLast(lstDownload, task.getFailReason());
SwingUtils.moveEnd(srlDownload);
SwingUtilities.invokeLater(() -> {
failReasons.add(task.getInfo() + ": " + (null == task.getFailReason() ? "No exception" : task.getFailReason().getLocalizedMessage()));
pgsTotal.setMaximum(taskList.taskCount());
pgsTotal.setValue(pgsTotal.getValue() + 1);
int idx = tasks.indexOf(task);
SwingUtils.setValueAt(lstDownload, task.getFailReason(), idx, 0);
SwingUtils.setValueAt(lstDownload, "0%", idx, 1);
SwingUtils.moveEnd(srlDownload);
});
}
@Override
public void onProgressProviderDone() {
public void onProgressProviderDone(Task task) {
}
@Override
public void setStatus(String sta) {
SwingUtils.replaceLast(lstDownload, sta);
public void setStatus(Task task, String sta) {
SwingUtilities.invokeLater(() -> {
SwingUtils.setValueAt(lstDownload, sta, lstDownload.getRowCount(), 0);
});
}
}

View File

@ -204,13 +204,13 @@ public class FileDownloadTask extends Task implements PreviousResult<File>, Prev
downloaded += read;
if (ppl != null) {
ppl.setProgress(downloaded, size);
ppl.setProgress(this, downloaded, size);
}
}
closeFiles();
tempFile.renameTo(filePath);
if (ppl != null) {
ppl.onProgressProviderDone();
ppl.onProgressProviderDone(this);
}
return true;
} catch (Exception e) {

View File

@ -62,7 +62,7 @@ public class HTTPGetTask extends TaskInfo implements PreviousResult<String> {
while ((i = is.read()) != -1) {
baos.write(i);
if (ppl != null) {
ppl.setProgress(++read, size);
ppl.setProgress(this, ++read, size);
}
if (!shouldContinue) {
return true;

View File

@ -126,5 +126,20 @@ public class SwingUtils {
model.removeRow(0);
table.updateUI();
}
public static void appendLast(JTable table, Object... elements) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addRow(elements);
}
public static void setValueAt(JTable table, Object element, int row, int col) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setValueAt(element, row, col);
}
public static void removeRow(JTable table, int row) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.removeRow(row);
}
}

View File

@ -208,6 +208,8 @@ advancedsettings.game_dir.default=\u9ed8\u8ba4(.minecraft/)
advancedsettings.game_dir.independent=\u5404\u7248\u672c\u72ec\u7acb(.minecraft/versions/<\u7248\u672c\u540d>/,\u9664assets,libraries)
advancedsettings.no_jvm_args=\u4e0d\u6dfb\u52a0JVM\u53c2\u6570(\u4f7f\u7528Java9\u65f6\u5fc5\u52fe)
advancedsettings.java_args_default=\u542f\u52a8\u5668\u9ed8\u8ba4\u6dfb\u52a0\u7684\u53c2\u6570\uff08\u8bf7\u4e0d\u8981\u91cd\u590d\u6dfb\u52a0\uff09\uff1a-XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode -XX:-UseAdaptiveSizePolicy -XX:MaxPermSize=???m -Xmx???m -Dfml.ignoreInvalidMinecraftCertificates=true -Dfml.ignorePatchDiscrepancies=true
advancedsettings.wrapper_launcher=\u524d\u7f6e\u542f\u52a8\u6307\u4ee4(\u4e0d\u5fc5\u586b\u5199\uff0c\u5185\u5bb9\u5c06\u52a0\u5728\u542f\u52a8\u811a\u672c\u6700\u524d\uff0c\u5982optirun...)
advancedsettings.server_ip=\u76f4\u5165\u670d\u52a1\u5668ip\u5730\u5740(\u4e0d\u5fc5\u586b\u5199\uff0c\u542f\u52a8\u6e38\u620f\u540e\u76f4\u63a5\u8fdb\u5165\u5bf9\u5e94\u670d\u52a1\u5668)
mainwindow.show_log=\u67e5\u770b\u65e5\u5fd7
mainwindow.make_launch_script=\u751f\u6210\u542f\u52a8\u811a\u672c
@ -266,6 +268,8 @@ taskwindow.single_progress=\u5355\u9879\u8fdb\u5ea6
taskwindow.total_progress=\u603b\u8fdb\u5ea6
taskwindow.cancel=\u53d6\u6d88
taskwindow.no_more_instance=\u53ef\u80fd\u540c\u65f6\u6253\u5f00\u4e86\u591a\u4e2a\u4efb\u52a1\u7a97\u53e3\uff0c\u8bf7\u4e0d\u8981\u591a\u6b21\u6253\u5f00\uff01
taskwindow.file_name=\u4efb\u52a1
taskwindow.download_progress=\u8fdb\u5ea6
setupwindow.include_minecraft=\u5bfc\u5165\u6e38\u620f\u6587\u4ef6\u5939
setupwindow.find_in_configurations=\u5bfc\u5165\u5b8c\u6210\uff0c\u5feb\u5230\u914d\u7f6e\u4e0b\u62c9\u6846\u4e2d\u627e\u65b0\u6e38\u620f\u8def\u5f84\u5427\uff01
@ -298,5 +302,3 @@ color.green=\u7eff\u8272
color.orange=\u6a59\u8272
color.dark_blue=\u6df1\u84dd\u8272
color.purple=\u7d2b\u8272
advancedsettings.wrapper_launcher=\u524d\u7f6e\u542f\u52a8\u6307\u4ee4(\u4e0d\u5fc5\u586b\u5199\uff0c\u5185\u5bb9\u5c06\u52a0\u5728\u542f\u52a8\u811a\u672c\u6700\u524d\uff0c\u5982optirun...)
advancedsettings.server_ip=\u76f4\u5165\u670d\u52a1\u5668ip\u5730\u5740(\u4e0d\u5fc5\u586b\u5199\uff0c\u542f\u52a8\u6e38\u620f\u540e\u76f4\u63a5\u8fdb\u5165\u5bf9\u5e94\u670d\u52a1\u5668)

View File

@ -203,6 +203,8 @@ advancedsettings.game_dir.default=Default (.minecraft/)
advancedsettings.game_dir.independent=Independent (.minecraft/versions/<version name>/,\u9664assets,libraries)
advancedsettings.no_jvm_args=No JVM Args
advancedsettings.java_args_default=Default java args: -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode -XX:-UseAdaptiveSizePolicy -XX:MaxPermSize=???m -Xmx???m -Dfml.ignoreInvalidMinecraftCertificates=true -Dfml.ignorePatchDiscrepancies=true
advancedsettings.wrapper_launcher=Wrapper Launcher(like optirun...)
advancedsettings.server_ip=Server Host
mainwindow.show_log=Show Logs
mainwindow.make_launch_script=Make Launching Script.
@ -258,6 +260,8 @@ taskwindow.single_progress=Single progress
taskwindow.total_progress=Total progress
taskwindow.cancel=Cancel
taskwindow.no_more_instance=Maybe you opened more than one task window, dont open it again!
taskwindow.file_name=Task
taskwindow.download_progress=Progress
setupwindow.include_minecraft=Import game
setupwindow.find_in_configurations=Finished importing. You can find it in the configuration selection bar.

View File

@ -203,6 +203,8 @@ advancedsettings.game_dir.default=\u9ed8\u8ba4(.minecraft/)
advancedsettings.game_dir.independent=\u5404\u7248\u672c\u72ec\u7acb(.minecraft/versions/<\u7248\u672c\u540d>/,\u9664assets,libraries)
advancedsettings.no_jvm_args=\u4e0d\u6dfb\u52a0JVM\u53c2\u6570(\u4f7f\u7528Java9\u65f6\u5fc5\u52fe)
advancedsettings.java_args_default=\u542f\u52a8\u5668\u9ed8\u8ba4\u6dfb\u52a0\u7684\u53c2\u6570\uff08\u8bf7\u4e0d\u8981\u91cd\u590d\u6dfb\u52a0\uff09\uff1a-XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode -XX:-UseAdaptiveSizePolicy -XX:MaxPermSize=???m -Xmx???m -Dfml.ignoreInvalidMinecraftCertificates=true -Dfml.ignorePatchDiscrepancies=true
advancedsettings.wrapper_launcher=\u524d\u7f6e\u542f\u52a8\u6307\u4ee4(\u4e0d\u5fc5\u586b\u5199\uff0c\u5185\u5bb9\u5c06\u52a0\u5728\u542f\u52a8\u811a\u672c\u6700\u524d\uff0c\u5982optirun...)
advancedsettings.server_ip=\u76f4\u5165\u670d\u52a1\u5668ip\u5730\u5740(\u4e0d\u5fc5\u586b\u5199\uff0c\u542f\u52a8\u6e38\u620f\u540e\u76f4\u63a5\u8fdb\u5165\u5bf9\u5e94\u670d\u52a1\u5668)
mainwindow.show_log=\u67e5\u770b\u65e5\u5fd7
mainwindow.make_launch_script=\u751f\u6210\u542f\u52a8\u811a\u672c
@ -260,6 +262,8 @@ taskwindow.single_progress=\u5355\u9879\u8fdb\u5ea6
taskwindow.total_progress=\u603b\u8fdb\u5ea6
taskwindow.cancel=\u53d6\u6d88
taskwindow.no_more_instance=\u53ef\u80fd\u540c\u65f6\u6253\u5f00\u4e86\u591a\u4e2a\u4efb\u52a1\u7a97\u53e3\uff0c\u8bf7\u4e0d\u8981\u591a\u6b21\u6253\u5f00\uff01
taskwindow.file_name=\u4efb\u52a1
taskwindow.download_progress=\u8fdb\u5ea6
setupwindow.include_minecraft=\u5bfc\u5165\u6e38\u620f\u6587\u4ef6\u5939
setupwindow.find_in_configurations=\u5bfc\u5165\u5b8c\u6210\uff0c\u5feb\u5230\u914d\u7f6e\u4e0b\u62c9\u6846\u4e2d\u627e\u65b0\u6e38\u620f\u8def\u5f84\u5427\uff01

View File

@ -203,6 +203,8 @@ advancedsettings.game_dir.default=\u9ed8\u8a8d(.minecraft/)
advancedsettings.game_dir.independent=\u5404\u7248\u672c\u7368\u7acb(.minecraft/versions/<\u7248\u672c\u540d>/,\u9664assets,libraries)
advancedsettings.no_jvm_args=\u4e0d\u6dfb\u52a0JVM\u53c3\u6578(\u4f7f\u7528Java9\u6642\u5fc5\u52fe)
advancedsettings.java_args_default=\u555f\u52d5\u5668\u9ed8\u8a8d\u6dfb\u52a0\u7684\u53c3\u6578\uff08\u8acb\u4e0d\u8981\u91cd\u8907\u6dfb\u52a0\uff09\uff1a-XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode -XX:-UseAdaptiveSizePolicy -XX:MaxPermSize=???m -Xmx???m -Dfml. ignoreInvalidMinecraftCertificates=true -Dfml.ignorePatchDiscrepancies=true
advancedsettings.wrapper_launcher=\u524d\u7f6e\u555f\u52d5\u6307\u4ee4(\u4e0d\u5fc5\u586b\u5beb\uff0c\u5167\u5bb9\u5c07\u52a0\u5728\u555f\u52d5\u8173\u672c\u6700\u524d\uff0c\u5982optirun...)
advancedsettings.server_ip=\u76f4\u5165\u670d\u52d9\u5668ip\u5730\u5740(\u4e0d\u5fc5\u586b\u5beb\uff0c\u555f\u52d5\u904a\u6232\u5f8c\u76f4\u63a5\u9032\u5165\u5c0d\u61c9\u670d\u52d9\u5668)
mainwindow.show_log=\u67e5\u770b\u65e5\u8a8c
mainwindow.make_launch_script=\u751f\u6210\u555f\u52d5\u8173\u672c
@ -258,6 +260,8 @@ taskwindow.single_progress=\u55ae\u9805\u9032\u5ea6
taskwindow.total_progress=\u7e3d\u9032\u5ea6
taskwindow.cancel=\u53d6\u6d88
taskwindow.no_more_instance=\u53ef\u80fd\u540c\u6642\u6253\u958b\u4e86\u591a\u500b\u4efb\u52d9\u7a97\u53e3\uff0c\u8acb\u4e0d\u8981\u591a\u6b21\u6253\u958b\uff01
taskwindow.file_name=\u4efb\u52d9
taskwindow.download_progress=\u9032\u5ea6
setupwindow.include_minecraft=\u5c0e\u5165\u904a\u6232\u6587\u4ef6\u593e
setupwindow.find_in_configurations=\u5c0e\u5165\u5b8c\u6210\uff0c\u5feb\u5230\u914d\u7f6e\u4e0b\u62c9\u6846\u4e2d\u627e\u65b0\u904a\u6232\u8def\u5f91\u5427\uff01