split library and user defined circuits to different tree nodes

This commit is contained in:
hneemann 2017-07-17 21:21:25 +02:00
parent f38f39c6c0
commit 2ec6a90b70
5 changed files with 134 additions and 52 deletions

View File

@ -66,7 +66,8 @@ public class ElementLibrary implements Iterable<ElementLibrary.ElementContainer>
private final ArrayList<LibraryListener> listeners = new ArrayList<>();
private final LibraryNode root;
private ShapeFactory shapeFactory;
private LibraryNode customNode;
private ElementLibraryFolder custom;
private ElementLibraryFolder library;
private File rootLibraryPath;
/**
@ -159,6 +160,9 @@ public class ElementLibrary implements Iterable<ElementLibrary.ElementContainer>
populateNodeMap();
custom = new ElementLibraryFolder(root, Lang.get("menu_custom"));
library = new ElementLibraryFolder(root, Lang.get("menu_library"));
isProgrammable.clear();
root.traverse(libraryNode -> {
ElementTypeDescription d = libraryNode.getDescriptionOrNull();
@ -190,7 +194,7 @@ public class ElementLibrary implements Iterable<ElementLibrary.ElementContainer>
* @return the node with the custom elements
*/
public LibraryNode getCustomNode() {
return customNode;
return custom.getNode();
}
private void populateNodeMap() {
@ -295,32 +299,19 @@ public class ElementLibrary implements Iterable<ElementLibrary.ElementContainer>
File libPath = Settings.getInstance().get(Keys.SETTINGS_LIBRARY_PATH);
if (libPath != null && !libPath.exists()) libPath = null;
LibraryNode changedNode = null;
if (rootLibraryPath != null || libPath != null) {
if (customNode == null) {
customNode = new LibraryNode(Lang.get("menu_custom"));
root.add(customNode);
changedNode = root;
} else {
customNode.removeAll();
changedNode = customNode;
}
int num = 0;
if (libPath != null)
num += scanFolder(libPath, customNode);
if (rootLibraryPath != null)
num += scanFolder(rootLibraryPath, customNode);
LOGGER.debug("found " + num + " files");
} else if (customNode != null) {
root.remove(customNode);
customNode = null;
changedNode = root;
}
LibraryNode cn1 = library.scanFolder(libPath);
LibraryNode cn2 = custom.scanFolder(rootLibraryPath);
populateNodeMap();
if (changedNode != null)
fireLibraryChanged(changedNode);
if (cn1 == root || cn2 == root) {
fireLibraryChanged(root);
} else {
if (cn1 != null)
fireLibraryChanged(cn1);
if (cn2 != null)
fireLibraryChanged(cn2);
}
}
/**
@ -333,31 +324,6 @@ public class ElementLibrary implements Iterable<ElementLibrary.ElementContainer>
l.libraryChanged(node);
}
private static int scanFolder(File path, LibraryNode node) {
int num = 0;
File[] list = path.listFiles();
if (list != null) {
ArrayList<File> orderedList = new ArrayList<>(Arrays.asList(list));
orderedList.sort((f1, f2) -> NumStringComparator.compareStr(f1.getName(), f2.getName()));
for (File f : orderedList) {
if (f.isDirectory()) {
LibraryNode n = new LibraryNode(f.getName());
num += scanFolder(f, n);
if (!n.isEmpty())
node.add(n);
}
}
for (File f : orderedList) {
final String name = f.getName();
if (f.isFile() && name.endsWith(".dig")) {
node.add(new LibraryNode(f));
num++;
}
}
}
return num;
}
/**
* Adds a listener to this library
*
@ -598,8 +564,12 @@ public class ElementLibrary implements Iterable<ElementLibrary.ElementContainer>
map.put(name, libraryNode);
libraryNode.setUnique(true);
} else {
presentNode.setUnique(false);
libraryNode.setUnique(false);
if (presentNode.getFile().equals(libraryNode.getFile()))
libraryNode.setUnique(true);
else {
presentNode.setUnique(false); // ToDo does not work if there are more than two duplicates and
libraryNode.setUnique(false); // some of the duplicates point to the same file
}
}
}
}

View File

@ -0,0 +1,103 @@
package de.neemann.digital.draw.library;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
/**
* Handles a single folder
* Created by hneemann on 17.07.17.
*/
public class ElementLibraryFolder {
private static final Logger LOGGER = LoggerFactory.getLogger(ElementLibraryFolder.class);
private final LibraryNode root;
private final String menuTitle;
private LibraryNode node;
private File lastPath;
/**
* create a new folder manager
*
* @param root the root node
* @param menuTitle string to show in menu
*/
public ElementLibraryFolder(LibraryNode root, String menuTitle) {
this.root = root;
this.menuTitle = menuTitle;
}
/**
* @return the managed node
*/
public LibraryNode getNode() {
return node;
}
private boolean hasChanged(File path) {
File lp = lastPath;
lastPath = path;
if (lp == path) return false;
return lp == null || !lp.equals(path);
}
/**
* scans the given folder
*
* @param path the path to scan
* @return the node which has changed
*/
public LibraryNode scanFolder(File path) {
LibraryNode changedNode = null;
if (hasChanged(path)) {
if (path != null) {
if (node == null) {
node = new LibraryNode(menuTitle);
root.add(node);
changedNode = root;
} else {
node.removeAll();
changedNode = node;
}
int num = scanFolder(path, node);
LOGGER.debug("found " + num + " files in " + path);
} else if (node != null) {
root.remove(node);
node = null;
changedNode = root;
}
}
return changedNode;
}
private static int scanFolder(File path, LibraryNode node) {
int num = 0;
File[] list = path.listFiles();
if (list != null) {
ArrayList<File> orderedList = new ArrayList<>(Arrays.asList(list));
orderedList.sort((f1, f2) -> NumStringComparator.compareStr(f1.getName(), f2.getName()));
for (File f : orderedList) {
if (f.isDirectory()) {
LibraryNode n = new LibraryNode(f.getName());
num += scanFolder(f, n);
if (!n.isEmpty())
node.add(n);
}
}
for (File f : orderedList) {
final String name = f.getName();
if (f.isFile() && name.endsWith(".dig")) {
node.add(new LibraryNode(f));
num++;
}
}
}
return num;
}
}

View File

@ -359,4 +359,11 @@ public class LibraryNode implements Iterable<LibraryNode> {
public boolean isUnique() {
return unique;
}
/**
* @return the file containing this circuit
*/
public File getFile() {
return file;
}
}

View File

@ -747,6 +747,7 @@ Sind evtl. die Namen der Variablen nicht eindeutig?</string>
<string name="menu_cut">Ausschneiden</string>
<string name="menu_copy">Kopieren</string>
<string name="menu_custom">Benutzerdefiniert</string>
<string name="menu_library">Bibliothek</string>
<string name="menu_delete">Löschen</string>
<string name="menu_delete_tt">Löscht ausgewählte Elemente</string>
<string name="menu_edit">Bearbeiten</string>

View File

@ -734,6 +734,7 @@ The names of the variables may not be unique.</string>
<string name="menu_cut">Cut</string>
<string name="menu_copy">Copy</string>
<string name="menu_custom">Custom</string>
<string name="menu_library">Library</string>
<string name="menu_delete">Delete components</string>
<string name="menu_delete_tt">Delete selected single component or group of components.</string>
<string name="menu_edit">Edit</string>