use of com.cburch.logisim.tools.Library in project logisim-evolution by reds-heig.
the class Loader method loadJarFile.
Library loadJarFile(File request, String className) throws LoadFailedException {
File actual = getSubstitution(request);
// Up until 2.1.8, this was written to use a URLClassLoader, which
// worked pretty well, except that the class never releases its file
// handles. For this reason, with 2.2.0, it's been switched to use
// a custom-written class ZipClassLoader instead. The ZipClassLoader
// is based on something downloaded off a forum, and I'm not as sure
// that it works as well. It certainly does more file accesses.
// Anyway, here's the line for this new version:
ZipClassLoader loader = new ZipClassLoader(actual);
// And here's the code that was present up until 2.1.8, and which I
// know to work well except for the closing-files bit. If necessary, we
// can revert by deleting the above declaration and reinstating the
// below.
/*
* URL url; try { url = new URL("file", "localhost",
* file.getCanonicalPath()); } catch (MalformedURLException e1) { throw
* new LoadFailedException("Internal error: Malformed URL"); } catch
* (IOException e1) { throw new
* LoadFailedException(Strings.get("jarNotOpenedError")); }
* URLClassLoader loader = new URLClassLoader(new URL[] { url });
*/
// load library class from loader
Class<?> retClass;
try {
retClass = loader.loadClass(className);
} catch (ClassNotFoundException e) {
throw new LoadFailedException(StringUtil.format(Strings.get("jarClassNotFoundError"), className));
}
if (!(Library.class.isAssignableFrom(retClass))) {
throw new LoadFailedException(StringUtil.format(Strings.get("jarClassNotLibraryError"), className));
}
// instantiate library
Library ret;
try {
ret = (Library) retClass.newInstance();
} catch (Exception e) {
throw new LoadFailedException(StringUtil.format(Strings.get("jarLibraryNotCreatedError"), className));
}
return ret;
}
use of com.cburch.logisim.tools.Library in project logisim-evolution by reds-heig.
the class ProjectActions method checkValidFilename.
/**
* Returns true if the filename contains valid characters only, that is,
* alphanumeric characters and underscores.
*/
private static boolean checkValidFilename(String filename, Project proj, HashMap<String, String> Errors) {
boolean IsOk = true;
HashMap<String, Library> TempSet = new HashMap<String, Library>();
HashSet<String> ForbiddenNames = new HashSet<String>();
LibraryTools.BuildLibraryList(proj.getLogisimFile(), TempSet);
LibraryTools.BuildToolList(proj.getLogisimFile(), ForbiddenNames);
ForbiddenNames.addAll(TempSet.keySet());
Pattern p = Pattern.compile("[^a-z0-9_.]", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(filename);
if (m.find()) {
IsOk = false;
Errors.put(FILE_NAME_FORMAT_ERROR, "InvalidFileFormatError");
}
if (ForbiddenNames.contains(filename.toUpperCase())) {
IsOk = false;
Errors.put(FILE_NAME_KEYWORD_ERROR, "UsedLibraryToolnameError");
}
return IsOk;
}
use of com.cburch.logisim.tools.Library in project logisim-evolution by reds-heig.
the class TtyInterface method displayStatistics.
private static void displayStatistics(LogisimFile file) {
FileStatistics stats = FileStatistics.compute(file, file.getMainCircuit());
FileStatistics.Count total = stats.getTotalWithSubcircuits();
int maxName = 0;
for (FileStatistics.Count count : stats.getCounts()) {
int nameLength = count.getFactory().getDisplayName().length();
if (nameLength > maxName)
maxName = nameLength;
}
String fmt = "%" + countDigits(total.getUniqueCount()) + "d\t" + "%" + countDigits(total.getRecursiveCount()) + "d\t";
String fmtNormal = fmt + "%-" + maxName + "s\t%s\n";
for (FileStatistics.Count count : stats.getCounts()) {
Library lib = count.getLibrary();
String libName = lib == null ? "-" : lib.getDisplayName();
// OK
System.out.printf(// OK
fmtNormal, Integer.valueOf(count.getUniqueCount()), Integer.valueOf(count.getRecursiveCount()), count.getFactory().getDisplayName(), libName);
}
FileStatistics.Count totalWithout = stats.getTotalWithoutSubcircuits();
System.out.printf(// OK
fmt + "%s\n", Integer.valueOf(totalWithout.getUniqueCount()), Integer.valueOf(totalWithout.getRecursiveCount()), Strings.get("statsTotalWithout"));
System.out.printf(// OK
fmt + "%s\n", Integer.valueOf(total.getUniqueCount()), Integer.valueOf(total.getRecursiveCount()), Strings.get("statsTotalWith"));
}
use of com.cburch.logisim.tools.Library in project logisim-evolution by reds-heig.
the class ToolboxManip method doubleClicked.
public void doubleClicked(ProjectExplorerEvent event) {
Object clicked = event.getTarget();
if (clicked instanceof ProjectExplorerToolNode) {
Tool baseTool = ((ProjectExplorerToolNode) clicked).getValue();
if (baseTool instanceof AddTool) {
AddTool tool = (AddTool) baseTool;
ComponentFactory source = tool.getFactory();
if (source instanceof SubcircuitFactory) {
SubcircuitFactory circFact = (SubcircuitFactory) source;
proj.setCurrentCircuit(circFact.getSubcircuit());
proj.getFrame().setEditorView(Frame.EDIT_LAYOUT);
if (lastSelected != null) {
proj.setTool(lastSelected);
} else {
Library base = proj.getLogisimFile().getLibrary("Base");
if (base != null)
proj.setTool(base.getTool("Edit Tool"));
}
}
}
}
}
use of com.cburch.logisim.tools.Library in project logisim-evolution by reds-heig.
the class ProjectLibraryActions method doLoadJarLibrary.
public static void doLoadJarLibrary(Project proj) {
Loader loader = proj.getLogisimFile().getLoader();
JFileChooser chooser = loader.createChooser();
chooser.setDialogTitle(Strings.get("loadJarDialogTitle"));
chooser.setFileFilter(Loader.JAR_FILTER);
int check = chooser.showOpenDialog(proj.getFrame());
if (check == JFileChooser.APPROVE_OPTION) {
File f = chooser.getSelectedFile();
String className = null;
// try to retrieve the class name from the "Library-Class"
// attribute in the manifest. This section of code was contributed
// by Christophe Jacquet (Request Tracker #2024431).
JarFile jarFile = null;
try {
jarFile = new JarFile(f);
Manifest manifest = jarFile.getManifest();
className = manifest.getMainAttributes().getValue("Library-Class");
} catch (IOException e) {
// if opening the JAR file failed, do nothing
} finally {
if (jarFile != null) {
try {
jarFile.close();
} catch (IOException e) {
}
}
}
// if the class name was not found, go back to the good old dialog
if (className == null) {
className = JOptionPane.showInputDialog(proj.getFrame(), Strings.get("jarClassNamePrompt"), Strings.get("jarClassNameTitle"), JOptionPane.QUESTION_MESSAGE);
// if user canceled selection, abort
if (className == null)
return;
}
Library lib = loader.loadJarLibrary(f, className);
if (lib != null) {
proj.doAction(LogisimFileActions.loadLibrary(lib, proj.getLogisimFile()));
}
}
}
Aggregations