Skip to content

Commit a657582

Browse files
committed
Merge branch 'lib-1.5-newformat' into ide-1.5.x
2 parents 849af97 + 14308c6 commit a657582

File tree

12 files changed

+487
-156
lines changed

12 files changed

+487
-156
lines changed

app/src/processing/app/Base.java

Lines changed: 74 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
import processing.app.helpers.PreferencesMap;
3838
import processing.app.helpers.filefilters.OnlyDirs;
3939
import processing.app.helpers.filefilters.OnlyFilesWithExtension;
40-
import processing.app.javax.swing.filechooser.FileNameExtensionFilter;import processing.app.tools.MapWithSubkeys;
40+
import processing.app.javax.swing.filechooser.FileNameExtensionFilter;import processing.app.packages.Library;
41+
import processing.app.packages.LibraryList;
42+
import processing.app.tools.MapWithSubkeys;
4143
import processing.app.tools.ZipDeflater;
4244
import processing.core.*;
4345
import static processing.app.I18n._;
@@ -89,10 +91,10 @@ public class Base {
8991
static private List<File> librariesFolders;
9092

9193
// maps library name to their library folder
92-
static private Map<String, File> libraries;
94+
static private LibraryList libraries;
9395

9496
// maps #included files to their library folder
95-
static Map<String, File> importToLibraryTable;
97+
static Map<String, Library> importToLibraryTable;
9698

9799
// classpath for all known libraries for p5
98100
// (both those in the p5/libs folder and those with lib subfolders
@@ -1041,26 +1043,18 @@ protected void rebuildSketchbookMenu(JMenu menu) {
10411043
}
10421044
}
10431045

1044-
public Map<String, File> getIDELibs() {
1046+
public LibraryList getIDELibs() {
10451047
if (libraries == null)
1046-
return new HashMap<String, File>();
1047-
Map<String, File> ideLibs = new HashMap<String, File>(libraries);
1048-
for (String lib : libraries.keySet()) {
1049-
if (FileUtils.isSubDirectory(getSketchbookFolder(), libraries.get(lib)))
1050-
ideLibs.remove(lib);
1051-
}
1052-
return ideLibs;
1048+
return new LibraryList();
1049+
LibraryList res = new LibraryList(libraries);
1050+
res.removeAll(getUserLibs());
1051+
return res;
10531052
}
10541053

1055-
public Map<String, File> getUserLibs() {
1054+
public LibraryList getUserLibs() {
10561055
if (libraries == null)
1057-
return new HashMap<String, File>();
1058-
Map<String, File> userLibs = new HashMap<String, File>(libraries);
1059-
for (String lib : libraries.keySet()) {
1060-
if (!FileUtils.isSubDirectory(getSketchbookFolder(), libraries.get(lib)))
1061-
userLibs.remove(lib);
1062-
}
1063-
return userLibs;
1056+
return new LibraryList();
1057+
return libraries.filterLibrariesInSubfolder(getSketchbookFolder());
10641058
}
10651059

10661060
public void rebuildImportMenu(JMenu importMenu, final Editor editor) {
@@ -1080,8 +1074,8 @@ public void actionPerformed(ActionEvent e) {
10801074
// Split between user supplied libraries and IDE libraries
10811075
TargetPlatform targetPlatform = getTargetPlatform();
10821076
if (targetPlatform != null) {
1083-
Map<String, File> ideLibs = getIDELibs();
1084-
Map<String, File> userLibs = getUserLibs();
1077+
LibraryList ideLibs = getIDELibs();
1078+
LibraryList userLibs = getUserLibs();
10851079
try {
10861080
// Find the current target. Get the platform, and then select the
10871081
// correct name and core path.
@@ -1121,44 +1115,33 @@ public void rebuildExamplesMenu(JMenu menu) {
11211115
if (found) menu.addSeparator();
11221116

11231117
// Add examples from libraries
1124-
Map<String, File> ideLibs = getIDELibs();
1125-
List<String> names = new ArrayList<String>(ideLibs.keySet());
1126-
Collections.sort(names, String.CASE_INSENSITIVE_ORDER);
1127-
for (String name : names) {
1128-
File folder = ideLibs.get(name);
1129-
addSketchesSubmenu(menu, name, folder, false);
1130-
// Allows "fat" libraries to have examples in the root folder
1131-
if (folder.getName().equals(Base.getTargetPlatform().getName()))
1132-
addSketchesSubmenu(menu, name, folder.getParentFile(), false);
1133-
}
1134-
1135-
Map<String, File> userLibs = getUserLibs();
1118+
LibraryList ideLibs = getIDELibs();
1119+
ideLibs.sort();
1120+
for (Library lib : ideLibs)
1121+
addSketchesSubmenu(menu, lib, false);
1122+
1123+
LibraryList userLibs = getUserLibs();
11361124
if (userLibs.size()>0) {
11371125
menu.addSeparator();
1138-
names = new ArrayList<String>(userLibs.keySet());
1139-
Collections.sort(names, String.CASE_INSENSITIVE_ORDER);
1140-
for (String name : names) {
1141-
File folder = userLibs.get(name);
1142-
addSketchesSubmenu(menu, name, folder, false);
1143-
// Allows "fat" libraries to have examples in the root folder
1144-
if (folder.getName().equals(Base.getTargetPlatform().getName()))
1145-
addSketchesSubmenu(menu, name, folder.getParentFile(), false);
1146-
}
1126+
userLibs.sort();
1127+
for (Library lib : userLibs)
1128+
addSketchesSubmenu(menu, lib, false);
11471129
}
11481130
} catch (IOException e) {
11491131
e.printStackTrace();
11501132
}
11511133
}
11521134

1153-
public Map<String, File> scanLibraries(List<File> folders) {
1154-
Map<String, File> res = new HashMap<String, File>();
1135+
public LibraryList scanLibraries(List<File> folders) throws IOException {
1136+
LibraryList res = new LibraryList();
11551137
for (File folder : folders)
1156-
res.putAll(scanLibraries(folder));
1138+
res.addOrReplaceAll(scanLibraries(folder));
11571139
return res;
11581140
}
11591141

1160-
public Map<String, File> scanLibraries(File folder) {
1161-
Map<String, File> res = new HashMap<String, File>();
1142+
public LibraryList scanLibraries(File folder) throws IOException {
1143+
LibraryList res = new LibraryList();
1144+
11621145
String list[] = folder.list(new OnlyDirs());
11631146
// if a bad folder or something like that, this might come back null
11641147
if (list == null)
@@ -1175,41 +1158,14 @@ public Map<String, File> scanLibraries(File folder) {
11751158
continue;
11761159
}
11771160

1178-
subfolder = scanFatLibrary(subfolder);
1179-
1161+
Library lib = Library.create(subfolder);
11801162
// (also replace previously found libs with the same name)
1181-
if (subfolder != null)
1182-
res.put(libName, subfolder);
1163+
if (lib != null)
1164+
res.addOrReplace(lib);
11831165
}
11841166
return res;
11851167
}
11861168

1187-
/**
1188-
* Scans inside a "FAT" (multi-platform) library folder to see if it contains
1189-
* a version suitable for the actual selected architecture. If a suitable
1190-
* version is found the folder containing that version is returned, otherwise
1191-
* <b>null</b> is returned.<br />
1192-
* <br />
1193-
* If a non-"FAT" library is detected, we assume that the library is suitable
1194-
* for the current architecture and the libFolder parameter is returned.<br />
1195-
*
1196-
* @param libFolder
1197-
* @return
1198-
*/
1199-
public File scanFatLibrary(File libFolder) {
1200-
// A library is considered "fat" if it contains a file called
1201-
// "library.properties"
1202-
File libraryPropFile = new File(libFolder, "library.properties");
1203-
if (!libraryPropFile.exists() || !libraryPropFile.isFile())
1204-
return libFolder;
1205-
1206-
// Search for a subfolder for actual architecture, return null if not found
1207-
File archSubfolder = new File(libFolder, Base.getTargetPlatform().getName());
1208-
if (!archSubfolder.exists() || !archSubfolder.isDirectory())
1209-
return null;
1210-
return archSubfolder;
1211-
}
1212-
12131169
public void onBoardOrPortChange() {
12141170
TargetPlatform targetPlatform = getTargetPlatform();
12151171
if (targetPlatform == null)
@@ -1228,18 +1184,25 @@ public void onBoardOrPortChange() {
12281184
// Scan for libraries in each library folder.
12291185
// Libraries located in the latest folders on the list can override
12301186
// other libraries with the same name.
1231-
libraries = scanLibraries(librariesFolders);
1232-
1187+
try {
1188+
libraries = scanLibraries(librariesFolders);
1189+
} catch (IOException e) {
1190+
showWarning(_("Error"), _("Error reading preferences"), e);
1191+
}
1192+
String currentArch = Base.getTargetPlatform().getName();
1193+
libraries = libraries.filterByArchitecture(currentArch);
1194+
12331195
// Populate importToLibraryTable
1234-
importToLibraryTable = new HashMap<String, File>();
1235-
for (File subfolder : libraries.values()) {
1196+
importToLibraryTable = new HashMap<String, Library>();
1197+
for (Library lib : libraries) {
12361198
try {
1237-
String packages[] = headerListFromIncludePath(subfolder);
1238-
for (String pkg : packages) {
1239-
importToLibraryTable.put(pkg, subfolder);
1199+
String headers[] = headerListFromIncludePath(lib.getSrcFolder());
1200+
for (String header : headers) {
1201+
importToLibraryTable.put(header, lib);
12401202
}
12411203
} catch (IOException e) {
1242-
showWarning(_("Error"), I18n.format("Unable to list header files in {0}", subfolder), e);
1204+
showWarning(_("Error"), I18n
1205+
.format("Unable to list header files in {0}", lib.getSrcFolder()), e);
12431206
}
12441207
}
12451208

@@ -1517,6 +1480,13 @@ protected boolean addSketches(JMenu menu, File folder,
15171480
return ifound; // actually ignored, but..
15181481
}
15191482

1483+
private boolean addSketchesSubmenu(JMenu menu, Library lib,
1484+
boolean replaceExisting)
1485+
throws IOException {
1486+
return addSketchesSubmenu(menu, lib.getName(), lib.getFolder(),
1487+
replaceExisting);
1488+
}
1489+
15201490
private boolean addSketchesSubmenu(JMenu menu, String name, File folder,
15211491
final boolean replaceExisting) throws IOException {
15221492

@@ -1583,29 +1553,28 @@ public void actionPerformed(ActionEvent e) {
15831553
return found;
15841554
}
15851555

1586-
protected void addLibraries(JMenu menu, Map<String, File> libs) throws IOException {
1556+
protected void addLibraries(JMenu menu, LibraryList libs) throws IOException {
15871557

1588-
List<String> list = new ArrayList<String>(libs.keySet());
1589-
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
1558+
LibraryList list = new LibraryList(libs);
1559+
list.sort();
15901560

1591-
ActionListener listener = new ActionListener() {
1592-
public void actionPerformed(ActionEvent event) {
1593-
String jarPath = event.getActionCommand();
1594-
try {
1595-
activeEditor.getSketch().importLibrary(jarPath);
1596-
} catch (IOException e) {
1597-
showWarning(_("Error"), I18n.format("Unable to list header files in {0}", jarPath), e);
1561+
for (Library lib : list) {
1562+
@SuppressWarnings("serial")
1563+
AbstractAction action = new AbstractAction(lib.getName()) {
1564+
public void actionPerformed(ActionEvent event) {
1565+
Library l = (Library) getValue("library");
1566+
try {
1567+
activeEditor.getSketch().importLibrary(l);
1568+
} catch (IOException e) {
1569+
showWarning(_("Error"), I18n.format("Unable to list header files in {0}", l.getSrcFolder()), e);
1570+
}
15981571
}
1599-
}
1600-
};
1601-
1602-
for (String name : list) {
1603-
File folder = libs.get(name);
1604-
1572+
};
1573+
action.putValue("library", lib);
1574+
16051575
// Add new element at the bottom
1606-
JMenuItem item = new JMenuItem(name);
1607-
item.addActionListener(listener);
1608-
item.setActionCommand(folder.getAbsolutePath());
1576+
JMenuItem item = new JMenuItem(action);
1577+
item.putClientProperty("library", lib);
16091578
menu.add(item);
16101579

16111580
// XXX: DAM: should recurse here so that library folders can be nested
@@ -1877,7 +1846,7 @@ static public File createTempFolder(String name) {
18771846
}
18781847

18791848

1880-
static public Map<String, File> getLibraries() {
1849+
static public LibraryList getLibraries() {
18811850
return libraries;
18821851
}
18831852

app/src/processing/app/Sketch.java

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import processing.app.debug.Sizer;
3030
import processing.app.debug.Uploader;
3131
import processing.app.helpers.PreferencesMap;
32+
import processing.app.packages.Library;
33+
import processing.app.packages.LibraryList;
3234
import processing.app.preproc.*;
3335
import processing.core.*;
3436
import static processing.app.I18n._;
@@ -87,16 +89,10 @@ public class Sketch {
8789
/** Class path determined during build. */
8890
private String classPath;
8991

90-
/**
91-
* This is *not* the "Processing" libraries path, this is the Java libraries
92-
* path, as in java.library.path=BlahBlah, which identifies search paths for
93-
* DLLs or JNILIBs.
94-
*/
95-
private String libraryPath;
9692
/**
9793
* List of library folders.
9894
*/
99-
private ArrayList<File> importedLibraries;
95+
private LibraryList importedLibraries;
10096

10197
/**
10298
* path is location of the main .pde file, because this is also
@@ -1120,15 +1116,19 @@ public boolean addFile(File sourceFile) {
11201116
}
11211117

11221118

1119+
public void importLibrary(Library lib) throws IOException {
1120+
importLibrary(lib.getSrcFolder());
1121+
}
1122+
11231123
/**
11241124
* Add import statements to the current tab for all of packages inside
11251125
* the specified jar file.
11261126
*/
1127-
public void importLibrary(String jarPath) throws IOException {
1127+
public void importLibrary(File jarPath) throws IOException {
11281128
// make sure the user didn't hide the sketch folder
11291129
ensureExistence();
11301130

1131-
String list[] = Base.headerListFromIncludePath(new File(jarPath));
1131+
String list[] = Base.headerListFromIncludePath(jarPath);
11321132

11331133
// import statements into the main sketch file (code[0])
11341134
// if the current code is a .java file, insert into current
@@ -1421,18 +1421,14 @@ public String preprocess(String buildPath, PdePreprocessor preprocessor) throws
14211421

14221422
// grab the imports from the code just preproc'd
14231423

1424-
importedLibraries = new ArrayList<File>();
1425-
//Remember to clear library path before building it.
1426-
libraryPath = "";
1424+
importedLibraries = new LibraryList();
14271425
for (String item : preprocessor.getExtraImports()) {
14281426

1429-
File libFolder = (File) Base.importToLibraryTable.get(item);
1430-
//If needed can Debug libraryPath here
1427+
Library lib = Base.importToLibraryTable.get(item);
1428+
//If needed can Debug libraryPath here
14311429

1432-
if (libFolder != null && !importedLibraries.contains(libFolder)) {
1433-
importedLibraries.add(libFolder);
1434-
//classPath += Compiler.contentsToClassPath(libFolder);
1435-
libraryPath += File.pathSeparator + libFolder.getAbsolutePath();
1430+
if (lib != null && !importedLibraries.contains(lib)) {
1431+
importedLibraries.add(lib);
14361432
}
14371433
}
14381434

@@ -1462,7 +1458,7 @@ public String preprocess(String buildPath, PdePreprocessor preprocessor) throws
14621458
}
14631459

14641460

1465-
public ArrayList<File> getImportedLibraries() {
1461+
public LibraryList getImportedLibraries() {
14661462
return importedLibraries;
14671463
}
14681464

@@ -1887,11 +1883,6 @@ public String getClassPath() {
18871883
}
18881884

18891885

1890-
public String getLibraryPath() {
1891-
return libraryPath;
1892-
}
1893-
1894-
18951886
public SketchCode[] getCode() {
18961887
return code;
18971888
}

0 commit comments

Comments
 (0)