37
37
import processing .app .helpers .PreferencesMap ;
38
38
import processing .app .helpers .filefilters .OnlyDirs ;
39
39
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 ;
41
43
import processing .app .tools .ZipDeflater ;
42
44
import processing .core .*;
43
45
import static processing .app .I18n ._ ;
@@ -89,10 +91,10 @@ public class Base {
89
91
static private List <File > librariesFolders ;
90
92
91
93
// maps library name to their library folder
92
- static private Map < String , File > libraries ;
94
+ static private LibraryList libraries ;
93
95
94
96
// maps #included files to their library folder
95
- static Map <String , File > importToLibraryTable ;
97
+ static Map <String , Library > importToLibraryTable ;
96
98
97
99
// classpath for all known libraries for p5
98
100
// (both those in the p5/libs folder and those with lib subfolders
@@ -1041,26 +1043,18 @@ protected void rebuildSketchbookMenu(JMenu menu) {
1041
1043
}
1042
1044
}
1043
1045
1044
- public Map < String , File > getIDELibs () {
1046
+ public LibraryList getIDELibs () {
1045
1047
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 ;
1053
1052
}
1054
1053
1055
- public Map < String , File > getUserLibs () {
1054
+ public LibraryList getUserLibs () {
1056
1055
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 ());
1064
1058
}
1065
1059
1066
1060
public void rebuildImportMenu (JMenu importMenu , final Editor editor ) {
@@ -1080,8 +1074,8 @@ public void actionPerformed(ActionEvent e) {
1080
1074
// Split between user supplied libraries and IDE libraries
1081
1075
TargetPlatform targetPlatform = getTargetPlatform ();
1082
1076
if (targetPlatform != null ) {
1083
- Map < String , File > ideLibs = getIDELibs ();
1084
- Map < String , File > userLibs = getUserLibs ();
1077
+ LibraryList ideLibs = getIDELibs ();
1078
+ LibraryList userLibs = getUserLibs ();
1085
1079
try {
1086
1080
// Find the current target. Get the platform, and then select the
1087
1081
// correct name and core path.
@@ -1121,44 +1115,33 @@ public void rebuildExamplesMenu(JMenu menu) {
1121
1115
if (found ) menu .addSeparator ();
1122
1116
1123
1117
// 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 ();
1136
1124
if (userLibs .size ()>0 ) {
1137
1125
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 );
1147
1129
}
1148
1130
} catch (IOException e ) {
1149
1131
e .printStackTrace ();
1150
1132
}
1151
1133
}
1152
1134
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 ();
1155
1137
for (File folder : folders )
1156
- res .putAll (scanLibraries (folder ));
1138
+ res .addOrReplaceAll (scanLibraries (folder ));
1157
1139
return res ;
1158
1140
}
1159
1141
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
+
1162
1145
String list [] = folder .list (new OnlyDirs ());
1163
1146
// if a bad folder or something like that, this might come back null
1164
1147
if (list == null )
@@ -1175,41 +1158,14 @@ public Map<String, File> scanLibraries(File folder) {
1175
1158
continue ;
1176
1159
}
1177
1160
1178
- subfolder = scanFatLibrary (subfolder );
1179
-
1161
+ Library lib = Library .create (subfolder );
1180
1162
// (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 );
1183
1165
}
1184
1166
return res ;
1185
1167
}
1186
1168
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
-
1213
1169
public void onBoardOrPortChange () {
1214
1170
TargetPlatform targetPlatform = getTargetPlatform ();
1215
1171
if (targetPlatform == null )
@@ -1228,18 +1184,25 @@ public void onBoardOrPortChange() {
1228
1184
// Scan for libraries in each library folder.
1229
1185
// Libraries located in the latest folders on the list can override
1230
1186
// 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
+
1233
1195
// 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 ) {
1236
1198
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 );
1240
1202
}
1241
1203
} 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 );
1243
1206
}
1244
1207
}
1245
1208
@@ -1517,6 +1480,13 @@ protected boolean addSketches(JMenu menu, File folder,
1517
1480
return ifound ; // actually ignored, but..
1518
1481
}
1519
1482
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
+
1520
1490
private boolean addSketchesSubmenu (JMenu menu , String name , File folder ,
1521
1491
final boolean replaceExisting ) throws IOException {
1522
1492
@@ -1583,29 +1553,28 @@ public void actionPerformed(ActionEvent e) {
1583
1553
return found ;
1584
1554
}
1585
1555
1586
- protected void addLibraries (JMenu menu , Map < String , File > libs ) throws IOException {
1556
+ protected void addLibraries (JMenu menu , LibraryList libs ) throws IOException {
1587
1557
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 ();
1590
1560
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
+ }
1598
1571
}
1599
- }
1600
- };
1601
-
1602
- for (String name : list ) {
1603
- File folder = libs .get (name );
1604
-
1572
+ };
1573
+ action .putValue ("library" , lib );
1574
+
1605
1575
// 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 );
1609
1578
menu .add (item );
1610
1579
1611
1580
// XXX: DAM: should recurse here so that library folders can be nested
@@ -1877,7 +1846,7 @@ static public File createTempFolder(String name) {
1877
1846
}
1878
1847
1879
1848
1880
- static public Map < String , File > getLibraries () {
1849
+ static public LibraryList getLibraries () {
1881
1850
return libraries ;
1882
1851
}
1883
1852
0 commit comments