Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for specifying packages to import in library.properties (updated version of PR #2178) #3010

Merged
merged 8 commits into from
Jan 19, 2015
2 changes: 1 addition & 1 deletion app/src/processing/app/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ public void menuCanceled(MenuEvent event) {
}


abstract public void handleImportLibrary(String jarPath);
abstract public void handleImportLibrary(String name);


public JMenu getToolMenu() {
Expand Down
28 changes: 26 additions & 2 deletions app/src/processing/app/Mode.java
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,10 @@ public void actionPerformed(ActionEvent e) {
for (Library library : coreLibraries) {
JMenuItem item = new JMenuItem(library.getName());
item.addActionListener(listener);
item.setActionCommand(library.getJarPath());

// changed to library-name to facilitate specification of imports from properties file
item.setActionCommand(library.getName());

importMenu.add(item);
}
}
Expand All @@ -503,7 +506,9 @@ public void actionPerformed(ActionEvent e) {
for (Library library : contribLibraries) {
JMenuItem item = new JMenuItem(library.getName());
item.addActionListener(listener);
item.setActionCommand(library.getJarPath());

// changed to library-name to facilitate specification if imports from properties file
item.setActionCommand(library.getName());

String group = library.getGroup();
if (group != null) {
Expand Down Expand Up @@ -1364,6 +1369,25 @@ public String getModuleExtension() {

// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

/**
* Checks coreLibraries and contribLibraries for a library with the specified name
* @param libName the name of the library to find
* @return the Library or null if not found
*/
public Library findLibraryByName(String libName) {

for (Library lib : this.coreLibraries) {
if (libName.equals(lib.getName()))
return lib;
}

for (Library lib : this.contribLibraries) {
if (libName.equals(lib.getName()))
return lib;
}

return null;
}

/**
* Create a fresh applet/application folder if the 'delete target folder'
Expand Down
40 changes: 38 additions & 2 deletions app/src/processing/app/contrib/LocalContribution.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public abstract class LocalContribution extends Contribution {
protected File folder;
protected HashMap<String, String> properties;
protected ClassLoader loader;

protected List<String> specifiedImports; // mylib,mylib.util;

public LocalContribution(File folder) {
this.folder = folder;
Expand All @@ -61,6 +61,7 @@ public LocalContribution(File folder) {
name = properties.get("name");
id = properties.get("id");
categories = parseCategories(properties.get("category"));
specifiedImports = parseImports(properties.get("imports"));
if (name == null) {
name = folder.getName();
}
Expand Down Expand Up @@ -542,7 +543,42 @@ static protected String findClassInZipFileList(String base, File[] fileList) {
return null;
}
*/

/**
* Returns the imports (package-names) for a library, as specified in its library.properties
* (e.g., imports=libname.*,libname.support.*)
*
* @return String[] packageNames (without wildcards) or null if none are specified
*/
public String[] getSpecifiedImports() {

return specifiedImports != null ? specifiedImports.toArray(new String[0]) : null;
}

/**
* @return the list of Java imports to be added to the sketch when the library is imported
* or null if none are specified
*/
protected static List<String> parseImports(String importsStr) {

List<String> outgoing = new ArrayList<String>();

if (importsStr != null) {

String[] listing = PApplet.trim(PApplet.split(importsStr, ','));
for (String imp : listing) {

// In case the wildcard is specified, strip it, as it gets added later)
if (imp.endsWith(".*")) {

imp = imp.substring(0, imp.length() - 2);
}

outgoing.add(imp);
}
}

return (outgoing.size() > 0) ? outgoing : null;
}

// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Expand Down
17 changes: 15 additions & 2 deletions app/src/processing/mode/java/JavaEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,8 @@ public boolean handleSaveAs() {
* Add import statements to the current tab for all of packages inside
* the specified jar file.
*/
public void handleImportLibrary(String jarPath) {
public void handleImportLibrary(String libraryName) {

// make sure the user didn't hide the sketch folder
sketch.ensureExistence();

Expand All @@ -960,11 +961,23 @@ public void handleImportLibrary(String jarPath) {
if (mode.isDefaultExtension(sketch.getCurrentCode())) {
sketch.setCurrentCode(0);
}

Library lib = mode.findLibraryByName(libraryName);
if (lib == null) {
statusError("Unable to locate library: "+libraryName);
return;
}

// could also scan the text in the file to see if each import
// statement is already in there, but if the user has the import
// commented out, then this will be a problem.
String[] list = Base.packageListFromClassPath(jarPath);
String[] list = lib.getSpecifiedImports(); // ask the library for its imports
if (list == null) {

// Default to old behavior and load each package in the primary jar
list = Base.packageListFromClassPath(lib.getJarPath());
}

StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.length; i++) {
sb.append("import ");
Expand Down