Skip to content

Commit 0f4980d

Browse files
[Fix]Progressbar,UX Messages
1 parent 66c80a3 commit 0f4980d

File tree

1 file changed

+47
-24
lines changed

1 file changed

+47
-24
lines changed

mode/tools/SDKUpdater/src/processing/mode/android/tools/SDKUpdater.java

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,14 @@ class DownloadTask extends SwingWorker<Object, Object> {
328328
ProgressIndicator progress;
329329
JProgressBar progressBar;
330330
Boolean isPlatform;
331+
JLabel status;
331332

332-
DownloadTask(JProgressBar progressBar, Boolean isPlatform) {
333+
DownloadTask(JProgressBar progressBar, Boolean isPlatform,JLabel status) {
333334
super();
334335
progress = new ConsoleProgressIndicator();
335336
this.progressBar = progressBar;
336337
this.isPlatform = isPlatform;
338+
this.status = status;
337339
}
338340

339341
@Override
@@ -350,6 +352,7 @@ protected Object doInBackground() throws Exception {
350352
Downloader downloader = new LegacyDownloader(fop, settings);
351353

352354
RepoManager mRepoManager = mHandler.getSdkManager(progress);
355+
status.setText("Loading repository");
353356
mRepoManager.loadSynchronously(0, progress, downloader, settings);
354357

355358
List<RemotePackage> remotes = new ArrayList<>();
@@ -375,6 +378,7 @@ protected Object doInBackground() throws Exception {
375378
for (RemotePackage p : remotes) {
376379
Installer installer = SdkInstallerUtil.findBestInstallerFactory(p, mHandler)
377380
.createInstaller(p, mRepoManager, downloader, mHandler.getFileOp());
381+
status.setText("Downloading...");
378382
if (!(installer.prepare(progress) && installer.complete(progress))) {
379383
// there was an error, abort.
380384
throw new SdkManagerCli.CommandFailedException();
@@ -398,6 +402,12 @@ protected void done() {
398402
tabs.setEnabled(true);
399403
status.setText("Refreshing packages...");
400404
statusPlatform.setText("Refreshing packages...");
405+
406+
//reset the progress bar to indeterminate for querying
407+
progressBar.setIndeterminate(false);
408+
progressBarPlatform.setIndeterminate(false);
409+
410+
//start querying again
401411
queryTask = new QueryTask();
402412
queryTask.addPropertyChangeListener(SDKUpdater.this);
403413
queryTask.execute();
@@ -456,6 +466,21 @@ public java.util.List<String> getPaths(RepoManager mgr,Boolean isPlatform) {
456466
}
457467
}
458468

469+
private void startProgressThread(final JProgressBar bar){
470+
Thread update = new Thread() {
471+
@Override
472+
public void run() {
473+
while (downloadTaskRunning) {
474+
try {
475+
Thread.sleep(100);
476+
} catch (InterruptedException e) { }
477+
bar.setValue((int) (downloadTask.progress.getFraction() * 100));
478+
}
479+
}
480+
};
481+
update.start();
482+
}
483+
459484
private void createLayout(final boolean standalone) {
460485
setTitle(getMenuTitle());
461486

@@ -538,27 +563,16 @@ public String getColumnName(int column) {
538563
actionButton.addActionListener(new ActionListener() {
539564
public void actionPerformed(ActionEvent e) {
540565
if (downloadTaskRunning) { // i.e button state is Cancel
566+
status.setText("Update canceled");
567+
actionButton.setText("Update");
541568
cancelTasks();
542569
} else { // i.e button state is Update
543-
downloadTask = new DownloadTask(progressBar,false);
544-
progressBar.setIndeterminate(true);
570+
downloadTask = new DownloadTask(progressBar,false,status);
571+
progressBar.setIndeterminate(false);
572+
progressBar.setMaximum(100);
545573
downloadTask.execute();
546-
547-
// getFraction() always returns 0.0, needs to be set somewhere (??)
548-
// Thread update = new Thread() {
549-
// @Override
550-
// public void run() {
551-
// while (downloadTaskRunning) {
552-
// try {
553-
// Thread.sleep(100);
554-
// } catch (InterruptedException e) { }
555-
// System.out.println("Updating: " + downloadTask.progress.getFraction());
556-
// }
557-
// }
558-
// };
559-
// update.start();
560-
561-
status.setText("Downloading available updates...");
574+
startProgressThread(progressBar);
575+
status.setText("Downloading selected updates...");
562576
actionButton.setText("Cancel");
563577
tabs.setEnabled(false);
564578
}
@@ -661,11 +675,16 @@ public String getColumnName(int column) {
661675
@Override
662676
public void actionPerformed(ActionEvent e) {
663677
if (downloadTaskRunning) { // i.e button state is Cancel
678+
statusPlatform.setText("Download canceled");
679+
actionButtonPlatform.setText("Install");
664680
cancelTasks();
665681
} else { // i.e button state is Update
666-
downloadTask = new DownloadTask(progressBarPlatform,true);
667-
progressBarPlatform.setIndeterminate(true);
682+
downloadTask = new DownloadTask(progressBarPlatform,true,statusPlatform);
683+
progressBarPlatform.setIndeterminate(false);
684+
progressBarPlatform.setMaximum(100);
668685
downloadTask.execute();
686+
startProgressThread(progressBarPlatform);
687+
statusPlatform.setText("Downloading selected platforms..");
669688
actionButtonPlatform.setText("Cancel");
670689
tabs.setEnabled(false);
671690
}
@@ -719,11 +738,15 @@ public void cancelTasks() {
719738
queryTask.cancel(true);
720739
if (downloadTaskRunning) {
721740
downloadTask.cancel(true);
722-
status.setText("Download canceled");
741+
742+
//reset UI
743+
tabs.setEnabled(true);
723744
JOptionPane.showMessageDialog(null,
724745
"Download canceled", "Warning", JOptionPane.WARNING_MESSAGE);
725-
actionButton.setText("Update");
726-
actionButtonPlatform.setText("Install");
746+
747+
//reset progress bar
748+
progressBar.setValue(0);
749+
progressBarPlatform.setValue(0);
727750
}
728751
}
729752

0 commit comments

Comments
 (0)