Skip to content

Commit 72a6452

Browse files
author
Federico Fissore
committed
import library from URL
1 parent 1247b23 commit 72a6452

11 files changed

+256
-21
lines changed

app/build.xml

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
<?xml version="1.0"?>
22
<project name="Arduino PDE" default="build">
33

4+
<path id="class.path">
5+
<pathelement location="../core/core.jar"/>
6+
<pathelement location="${env.JAVA_HOME}/lib/tools.jar"/>
7+
<pathelement location="lib/apple.jar"/>
8+
<pathelement location="lib/ecj.jar"/>
9+
<pathelement location="lib/jna.jar"/>
10+
<pathelement location="lib/RXTXcomm.jar"/>
11+
<pathelement location="lib/commons-codec-1.2.jar"/>
12+
<pathelement location="lib/commons-httpclient-3.0.jar"/>
13+
<pathelement location="lib/commons-logging-1.0.4.jar"/>
14+
<pathelement location="lib/swing-layout-1.0.4.jar"/>
15+
<pathelement location="test-lib/junit-4.11.jar"/>
16+
</path>
17+
418
<target name="clean" description="Clean the build directories">
519
<delete dir="bin" />
620
<delete dir="test-bin" />
@@ -44,7 +58,7 @@
4458
encoding="UTF-8"
4559
includeAntRuntime="false"
4660
debug="true"
47-
classpath="../core/core.jar; ${env.JAVA_HOME}/lib/tools.jar; lib/apple.jar; lib/ecj.jar; lib/jna.jar; lib/RXTXcomm.jar" />
61+
classpathref="class.path" />
4862
<copy todir="bin" overwrite="true" verbose="true">
4963
<fileset dir="src" includes="**/*.properties" />
5064
</copy>
@@ -61,13 +75,7 @@
6175
debug="true">
6276
<classpath>
6377
<pathelement location="bin"/>
64-
<pathelement location="../core/core.jar"/>
65-
<pathelement location="${env.JAVA_HOME}/lib/tools.jar"/>
66-
<pathelement location="lib/apple.jar"/>
67-
<pathelement location="lib/ecj.jar"/>
68-
<pathelement location="lib/jna.jar"/>
69-
<pathelement location="lib/RXTXcomm.jar"/>
70-
<pathelement location="test-lib/junit-4.11.jar"/>
78+
<path refid="class.path"/>
7179
</classpath>
7280
</javac>
7381

@@ -80,13 +88,7 @@
8088
<classpath>
8189
<pathelement location="bin"/>
8290
<pathelement location="test-bin"/>
83-
<pathelement location="../core/core.jar"/>
84-
<pathelement location="${env.JAVA_HOME}/lib/tools.jar"/>
85-
<pathelement location="lib/apple.jar"/>
86-
<pathelement location="lib/ecj.jar"/>
87-
<pathelement location="lib/jna.jar"/>
88-
<pathelement location="lib/RXTXcomm.jar"/>
89-
<pathelement location="test-lib/junit-4.11.jar"/>
91+
<path refid="class.path"/>
9092
</classpath>
9193

9294
<formatter type="xml"/>

app/lib/commons-codec-1.2.jar

28.3 KB
Binary file not shown.

app/lib/commons-httpclient-3.0.jar

273 KB
Binary file not shown.

app/lib/commons-logging-1.0.4.jar

37.1 KB
Binary file not shown.

app/lib/swing-layout-1.0.4.jar

141 KB
Binary file not shown.

app/src/processing/app/Base.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,15 @@
3232

3333
import processing.app.debug.TargetPackage;
3434
import processing.app.debug.TargetPlatform;
35+
import processing.app.forms.ImportLibraryFromURL;
3536
import processing.app.helpers.FileUtils;
3637
import processing.app.helpers.Maps;
3738
import processing.app.helpers.PreferencesMap;
3839
import processing.app.helpers.filefilters.OnlyDirs;
3940
import processing.app.helpers.filefilters.OnlyFilesWithExtension;
40-
import processing.app.javax.swing.filechooser.FileNameExtensionFilter;import processing.app.tools.MapWithSubkeys;
41+
import processing.app.javax.swing.filechooser.FileNameExtensionFilter;
42+
import processing.app.tools.FromURLLibraryImporter;
43+
import processing.app.tools.MapWithSubkeys;
4144
import processing.app.tools.ZipDeflater;
4245
import processing.core.*;
4346
import static processing.app.I18n._;
@@ -1001,6 +1004,46 @@ public void actionPerformed(ActionEvent e) {
10011004
});
10021005
importMenu.add(addLibraryMenuItem);
10031006

1007+
JMenuItem addRemoteLibraryMenuItem = new JMenuItem(_("Add Library from URL..."));
1008+
addRemoteLibraryMenuItem.addActionListener(new ActionListener() {
1009+
public void actionPerformed(ActionEvent e) {
1010+
final ImportLibraryFromURL urlDialog = new ImportLibraryFromURL(editor);
1011+
urlDialog.addActionListener(new ActionListener() {
1012+
public void actionPerformed(ActionEvent event) {
1013+
final String url = urlDialog.getUrl();
1014+
if (url == null || url.trim().length() == 0) {
1015+
editor.statusError("Empty URL");
1016+
return;
1017+
}
1018+
1019+
urlDialog.hideDownloadingLabel();
1020+
EventQueue.invokeLater(new Runnable() {
1021+
public void run() {
1022+
FromURLLibraryImporter importer = new FromURLLibraryImporter();
1023+
try {
1024+
Base.this.importZipLibrary(editor, importer.fetchLibraryFrom(url));
1025+
Base.this.onBoardOrPortChange();
1026+
Base.this.rebuildImportMenu(Editor.importMenu, editor);
1027+
Base.this.rebuildExamplesMenu(Editor.examplesMenu);
1028+
urlDialog.dispose();
1029+
} catch (IllegalArgumentException e) {
1030+
editor.statusError("Invalid URL");
1031+
} catch (IOException e) {
1032+
editor.statusError(e);
1033+
} finally {
1034+
urlDialog.showDownloadingLabel();
1035+
}
1036+
}
1037+
});
1038+
}
1039+
});
1040+
1041+
urlDialog.setLocationRelativeTo(editor);
1042+
urlDialog.setVisible(true);
1043+
}
1044+
});
1045+
importMenu.add(addRemoteLibraryMenuItem);
1046+
10041047
// Split between user supplied libraries and IDE libraries
10051048
Map<String, File> ideLibs = getIDELibs();
10061049
Map<String, File> userLibs = getUserLibs();
@@ -2682,6 +2725,10 @@ public void handleAddLibrary(Editor editor) {
26822725
}
26832726

26842727
File sourceFile = fileChooser.getSelectedFile();
2728+
importZipLibrary(editor, sourceFile);
2729+
}
2730+
2731+
private void importZipLibrary(Editor editor, File sourceFile) {
26852732
File tmpFolder = null;
26862733

26872734
try {

app/src/processing/app/I18n.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ public class I18n {
2323

2424
// prompt text stuff
2525

26-
static String PROMPT_YES;
27-
static String PROMPT_NO;
28-
static String PROMPT_CANCEL;
29-
static String PROMPT_OK;
30-
static String PROMPT_BROWSE;
26+
public static String PROMPT_YES;
27+
public static String PROMPT_NO;
28+
public static String PROMPT_CANCEL;
29+
public static String PROMPT_OK;
30+
public static String PROMPT_BROWSE;
3131

3232
static protected void init (String language) {
3333
// there might be a null pointer exception ... most likely will never happen but the jvm gets mad
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package processing.app.forms;
2+
3+
import processing.app.I18n;
4+
5+
import java.awt.*;
6+
import java.awt.event.ActionListener;
7+
8+
import static processing.app.I18n._;
9+
10+
public class ImportLibraryFromURL extends ImportLibraryFromURLDialog {
11+
12+
public ImportLibraryFromURL(Frame parent) {
13+
super(parent, true);
14+
15+
setTitle(_("Type the URL of the library ZIP file"));
16+
typeURLLabel.setText(getTitle() + ":");
17+
downloadingLabel.setText(_("Downloading... please wait..."));
18+
downloadingLabel.setVisible(false);
19+
urlTextField.setText("");
20+
okButton.setText(I18n.PROMPT_OK);
21+
22+
}
23+
24+
public void hideDownloadingLabel() {
25+
downloadingLabel.setVisible(true);
26+
}
27+
28+
public void showDownloadingLabel() {
29+
downloadingLabel.setVisible(false);
30+
}
31+
32+
public void addActionListener(ActionListener listener) {
33+
okButton.addActionListener(listener);
34+
}
35+
36+
public String getUrl() {
37+
return urlTextField.getText();
38+
}
39+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package processing.app.forms;
2+
3+
import org.jdesktop.layout.GroupLayout;
4+
import org.jdesktop.layout.LayoutStyle;
5+
6+
import javax.swing.*;
7+
import java.awt.*;
8+
9+
public class ImportLibraryFromURLDialog extends JDialog {
10+
11+
protected JButton okButton;
12+
protected JLabel typeURLLabel;
13+
protected JLabel downloadingLabel;
14+
protected JTextField urlTextField;
15+
16+
public ImportLibraryFromURLDialog(Frame parent, boolean modal) {
17+
super(parent, modal);
18+
initComponents();
19+
}
20+
21+
@SuppressWarnings("unchecked")
22+
private void initComponents() {
23+
24+
typeURLLabel = new JLabel();
25+
urlTextField = new JTextField();
26+
okButton = new JButton();
27+
downloadingLabel = new JLabel();
28+
29+
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
30+
31+
typeURLLabel.setText("typeURLLabel");
32+
33+
urlTextField.setText("urlTextField");
34+
35+
okButton.setText("okButton");
36+
37+
downloadingLabel.setText("downloadingLabel");
38+
39+
GroupLayout layout = new GroupLayout(getContentPane());
40+
getContentPane().setLayout(layout);
41+
layout.setHorizontalGroup(
42+
layout.createParallelGroup(GroupLayout.LEADING)
43+
.add(layout.createSequentialGroup()
44+
.addContainerGap()
45+
.add(layout.createParallelGroup(GroupLayout.LEADING)
46+
.add(urlTextField)
47+
.add(layout.createSequentialGroup()
48+
.add(layout.createParallelGroup(GroupLayout.LEADING)
49+
.add(typeURLLabel)
50+
.add(layout.createSequentialGroup()
51+
.add(okButton)
52+
.addPreferredGap(LayoutStyle.RELATED)
53+
.add(downloadingLabel)))
54+
.add(0, 279, Short.MAX_VALUE)))
55+
.addContainerGap())
56+
);
57+
layout.setVerticalGroup(
58+
layout.createParallelGroup(GroupLayout.LEADING)
59+
.add(layout.createSequentialGroup()
60+
.addContainerGap()
61+
.add(typeURLLabel)
62+
.addPreferredGap(LayoutStyle.RELATED)
63+
.add(urlTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
64+
.addPreferredGap(LayoutStyle.RELATED)
65+
.add(layout.createParallelGroup(GroupLayout.BASELINE)
66+
.add(okButton)
67+
.add(downloadingLabel))
68+
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
69+
);
70+
71+
pack();
72+
}
73+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package processing.app.tools;
2+
3+
import org.apache.commons.httpclient.Header;
4+
import org.apache.commons.httpclient.HttpClient;
5+
import org.apache.commons.httpclient.HttpMethod;
6+
import org.apache.commons.httpclient.methods.GetMethod;
7+
import processing.app.Sketch;
8+
9+
import java.io.File;
10+
import java.io.FileOutputStream;
11+
import java.io.IOException;
12+
13+
public class FromURLLibraryImporter {
14+
15+
private final HttpClient client;
16+
17+
public FromURLLibraryImporter() {
18+
client = new HttpClient();
19+
}
20+
21+
public File fetchLibraryFrom(String url) throws IOException {
22+
HttpMethod get = new GetMethod(url);
23+
get.setFollowRedirects(true);
24+
try {
25+
client.executeMethod(get);
26+
27+
if (get.getStatusCode() != 200) {
28+
throw new IOException("Server returned error " + get.getStatusCode() + ": " + get.getStatusText());
29+
}
30+
31+
String filename = fileNameFrom(get);
32+
33+
return saveLocalFile(get, filename);
34+
} finally {
35+
get.releaseConnection();
36+
}
37+
38+
}
39+
40+
private File saveLocalFile(HttpMethod get, String filename) throws IOException {
41+
File tempFile = File.createTempFile("arduinoLibrary", "tmp");
42+
FileOutputStream fos = null;
43+
try {
44+
fos = new FileOutputStream(tempFile);
45+
fos.write(get.getResponseBody());
46+
} finally {
47+
if (fos != null) {
48+
fos.close();
49+
}
50+
}
51+
52+
filename = Sketch.sanitizeName(filename);
53+
54+
File localFile = new File(tempFile.getParent(), filename);
55+
if (tempFile.renameTo(localFile)) {
56+
return localFile;
57+
}
58+
59+
return tempFile;
60+
}
61+
62+
private String fileNameFrom(HttpMethod get) {
63+
String filename = get.getPath().substring(get.getPath().lastIndexOf("/"));
64+
Header contentDisposition = get.getResponseHeader("Content-Disposition");
65+
if (contentDisposition != null) {
66+
filename = contentDisposition.getElements()[0].getParameters()[0].getValue();
67+
}
68+
return filename;
69+
}
70+
}

build/build.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
<include name="app/lib/RXTXcomm.jar" />
3030
<include name="app/lib/ant.jar" />
3131
<include name="app/lib/ant-launcher.jar" />
32+
<include name="app/lib/commons-codec-1.2.jar" />
33+
<include name="app/lib/commons-httpclient-3.0.jar" />
34+
<include name="app/lib/commons-logging-1.0.4.jar" />
35+
<include name="app/lib/swing-layout-1.0.4.jar" />
3236
</fileset>
3337

3438
<target name="build" description="Build Arduino.">

0 commit comments

Comments
 (0)