Skip to content

Commit fe2d087

Browse files
cmaglieFederico Fissore
authored and
Federico Fissore
committed
Implemented "Update List" button of Boards Installer.
1 parent 5d1e4e3 commit fe2d087

File tree

5 files changed

+100
-13
lines changed

5 files changed

+100
-13
lines changed

app/src/cc/arduino/packages/contributions/ui/ContributionManagerUI.java

+26-6
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public class ContributionManagerUI extends JDialog {
9191
private String category;
9292
private String[] filters;
9393

94-
public ContributionManagerUI(Frame parent, ContributionsIndexer indexer) {
94+
public ContributionManagerUI(Frame parent) {
9595
super(parent, "Boards Manager", Dialog.ModalityType.APPLICATION_MODAL);
9696

9797
setResizable(true);
@@ -238,7 +238,9 @@ public void run() {
238238
});
239239
}
240240
});
241+
}
241242

243+
public void setIndexer(ContributionsIndexer indexer) {
242244
contribModel.setIndex(indexer.getIndex());
243245
setCategories(indexer.getIndex().getCategories());
244246

@@ -305,11 +307,6 @@ public void setProgress(int progress, String text) {
305307
progressBar.setString(text);
306308
}
307309

308-
public void onUpdatePressed() {
309-
// TODO Auto-generated method stub
310-
System.out.println("Update pressed");
311-
}
312-
313310
/*
314311
* Installer methods follows
315312
*/
@@ -322,6 +319,25 @@ public void onCancelPressed() {
322319
installerThread.interrupt();
323320
}
324321

322+
public void onUpdatePressed() {
323+
installerThread = new Thread(new Runnable() {
324+
@Override
325+
public void run() {
326+
try {
327+
setProgressVisible(true);
328+
installer.updateIndex();
329+
onIndexesUpdated();
330+
} catch (Exception e) {
331+
// TODO Show ERROR
332+
e.printStackTrace();
333+
} finally {
334+
setProgressVisible(false);
335+
}
336+
}
337+
});
338+
installerThread.start();
339+
}
340+
325341
public void onInstallPressed(final ContributedPlatform platform) {
326342
installerThread = new Thread(new Runnable() {
327343
@Override
@@ -358,4 +374,8 @@ public void run() {
358374
installerThread.start();
359375
}
360376

377+
protected void onIndexesUpdated() throws Exception {
378+
// Empty
379+
}
380+
361381
}

app/src/processing/app/Base.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,15 @@ public void onBoardOrPortChange() {
11101110

11111111
private void openInstallBoardDialog() {
11121112
// Create dialog for contribution manager
1113-
ContributionManagerUI managerUI = new ContributionManagerUI(activeEditor, BaseNoGui.indexer);
1113+
@SuppressWarnings("serial")
1114+
ContributionManagerUI managerUI = new ContributionManagerUI(activeEditor) {
1115+
@Override
1116+
protected void onIndexesUpdated() throws Exception {
1117+
BaseNoGui.reloadAllHardware();
1118+
setIndexer(BaseNoGui.indexer);
1119+
}
1120+
};
1121+
managerUI.setIndexer(BaseNoGui.indexer);
11141122
managerUI.setVisible(true);
11151123
}
11161124

app/src/processing/app/Editor.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -2769,12 +2769,14 @@ public void statusEmpty() {
27692769

27702770
protected void onBoardOrPortChange() {
27712771
Map<String, String> boardPreferences = Base.getBoardPreferences();
2772-
lineStatus.setBoardName(boardPreferences.get("name"));
2772+
if (boardPreferences != null)
2773+
lineStatus.setBoardName(boardPreferences.get("name"));
2774+
else
2775+
lineStatus.setBoardName("-");
27732776
lineStatus.setSerialPort(Preferences.get("serial.port"));
27742777
lineStatus.repaint();
27752778
}
27762779

2777-
27782780
/**
27792781
* Returns the edit popup menu.
27802782
*/

arduino-core/src/cc/arduino/packages/contributions/ContributionInstaller.java

+35
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,39 @@ public void remove(ContributedPlatform platform) {
234234
}
235235
}
236236
}
237+
238+
public void updateIndex() throws Exception {
239+
final String statusText = _("Downloading platforms index...");
240+
updateProgress(0, statusText);
241+
242+
URL url = new URL("http://arduino.cc/package_index.json");
243+
File tmpFile = File.createTempFile("package_index", ".json");
244+
FileDownloader downloader = new FileDownloader(url, tmpFile);
245+
downloader.addObserver(new Observer() {
246+
@Override
247+
public void update(Observable o, Object arg) {
248+
FileDownloader me = (FileDownloader) o;
249+
String msg = "";
250+
if (me.getDownloadSize() != null) {
251+
long downloaded = me.getInitialSize() + me.getDownloaded() / 1000;
252+
long total = me.getInitialSize() + me.getDownloadSize() / 1000;
253+
msg = format(_("Downloaded {0}kb of {1}kb."), downloaded, total);
254+
}
255+
updateProgress((int) progress + progressStepsDelta * me.getProgress() /
256+
100.0, statusText + " " + msg);
257+
}
258+
});
259+
downloader.download();
260+
if (!downloader.isCompleted())
261+
throw new Exception("Error dowloading " + url, downloader.getError());
262+
263+
// TODO: Check downloaded index
264+
265+
// Replace old index with the updated one
266+
File outputFile = indexer.getIndexFile();
267+
if (outputFile.exists())
268+
outputFile.delete();
269+
if (!tmpFile.renameTo(outputFile))
270+
throw new Exception("An error occurred while updating platforms index!");
271+
}
237272
}

arduino-core/src/processing/app/BaseNoGui.java

+26-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.File;
66
import java.io.FileInputStream;
77
import java.io.FileWriter;
8+
import java.io.FileOutputStream;
89
import java.io.IOException;
910
import java.io.InputStream;
1011
import java.net.URISyntaxException;
@@ -19,6 +20,7 @@
1920
import cc.arduino.packages.Uploader;
2021
import processing.app.debug.Compiler;
2122
import cc.arduino.packages.contributions.ContributionsIndexer;
23+
import cc.arduino.utils.ArchiveExtractor;
2224
import processing.app.debug.TargetBoard;
2325
import processing.app.debug.LegacyTargetPackage;
2426
import processing.app.debug.TargetPackage;
@@ -585,14 +587,34 @@ static public void initLogger() {
585587
}
586588

587589
static public void initPackages() throws Exception {
590+
reloadAllHardware();
591+
}
592+
593+
static public void reloadAllHardware() throws Exception {
588594
indexer = new ContributionsIndexer(BaseNoGui.getSettingsFolder());
589-
if (!indexer.getIndexFile().isFile())
590-
// TODO: run first setup
591-
;
595+
File indexFile = indexer.getIndexFile();
596+
if (!indexFile.isFile()) {
597+
try {
598+
File distFile = getContentFile("dist/default_package.tar.bz2");
599+
if (distFile.isFile()) {
600+
// If present, unpack distribution file into preferences folder
601+
ArchiveExtractor.extract(distFile, BaseNoGui.getSettingsFolder(), 1);
602+
603+
// TODO: The first distribution file may be removed after extraction?
604+
} else {
605+
// Otherwise create an empty packages index
606+
FileOutputStream out = new FileOutputStream(indexFile);
607+
out.write("{ \"packages\" : [ ] }".getBytes());
608+
out.close();
609+
}
610+
} catch (IOException e) {
611+
e.printStackTrace();
612+
}
613+
}
592614
indexer.parseIndex();
593615
indexer.syncWithFilesystem();
594616
System.out.println(indexer);
595-
617+
596618
packages = new HashMap<String, TargetPackage>();
597619
loadHardware(getHardwareFolder());
598620
loadHardware(getSketchbookHardwareFolder());

0 commit comments

Comments
 (0)