Added a refresh to library.

This commit is contained in:
hneemann 2016-03-24 19:38:05 +01:00
parent 6e947da74f
commit 194da3351e
3 changed files with 25 additions and 17 deletions

View File

@ -1,8 +1,5 @@
package de.neemann.digital.gui; package de.neemann.digital.gui;
import de.neemann.digital.core.element.Element;
import de.neemann.digital.core.element.ElementAttributes;
import de.neemann.digital.core.element.ElementFactory;
import de.neemann.digital.core.element.ElementTypeDescription; import de.neemann.digital.core.element.ElementTypeDescription;
import de.neemann.digital.gui.components.CircuitComponent; import de.neemann.digital.gui.components.CircuitComponent;
import de.neemann.digital.gui.draw.elements.Circuit; import de.neemann.digital.gui.draw.elements.Circuit;
@ -17,6 +14,7 @@ import de.process.utils.gui.ToolTipAction;
import javax.swing.*; import javax.swing.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.io.File; import java.io.File;
import java.util.ArrayList;
/** /**
* @author hneemann * @author hneemann
@ -28,10 +26,12 @@ public class LibrarySelector implements ElementNotFoundNotification {
private JMenu customMenu; private JMenu customMenu;
private InsertHistory insertHistory; private InsertHistory insertHistory;
private CircuitComponent circuitComponent; private CircuitComponent circuitComponent;
private ArrayList<String> importedElements;
public LibrarySelector(ElementLibrary library) { public LibrarySelector(ElementLibrary library) {
this.library = library; this.library = library;
library.setElementNotFoundNotification(this); library.setElementNotFoundNotification(this);
importedElements = new ArrayList<>();
} }
public JMenu buildMenu(InsertHistory insertHistory, CircuitComponent circuitComponent) { public JMenu buildMenu(InsertHistory insertHistory, CircuitComponent circuitComponent) {
@ -42,7 +42,7 @@ public class LibrarySelector implements ElementNotFoundNotification {
customMenu = new JMenu("Custom"); customMenu = new JMenu("Custom");
parts.add(customMenu); parts.add(customMenu);
ToolTipAction importAction = new ToolTipAction("Import") { customMenu.add(new ToolTipAction("Import") {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
JFileChooser fc = Main.getjFileChooser(lastFile); JFileChooser fc = Main.getjFileChooser(lastFile);
@ -50,8 +50,15 @@ public class LibrarySelector implements ElementNotFoundNotification {
importElement(fc.getSelectedFile()); importElement(fc.getSelectedFile());
} }
} }
}.setToolTip("Imports a model as a useable Element!"); }.setToolTip("Imports a model as a useable Element!"));
customMenu.add(importAction.createJMenuItem());
customMenu.add(new ToolTipAction("Refresh") {
@Override
public void actionPerformed(ActionEvent e) {
for (String name : importedElements)
library.removeElement(name);
}
}.setToolTip("Imports a model as a useable Element!"));
JMenu subMenu = null; JMenu subMenu = null;
@ -105,15 +112,16 @@ public class LibrarySelector implements ElementNotFoundNotification {
private ElementTypeDescription importElement(File file) { private ElementTypeDescription importElement(File file) {
try { try {
System.out.println("load element " + file);
Circuit circuit = Circuit.loadCircuit(file); Circuit circuit = Circuit.loadCircuit(file);
ElementTypeDescription description = new ElementTypeDescription(file.getName(), new ElementFactory() { ElementTypeDescription description =
@Override new ElementTypeDescription(file.getName(),
public Element create(ElementAttributes attributes) { attributes -> new CustomElement(circuit, library),
return new CustomElement(circuit, library); circuit.getInputNames(library))
} .setShortName(createShortName(file));
}, circuit.getInputNames(library)).setShortName(createShortName(file));
library.addDescription(description); library.addDescription(description);
customMenu.add(new InsertAction(description.getName(), insertHistory, circuitComponent)); customMenu.add(new InsertAction(description.getName(), insertHistory, circuitComponent));
importedElements.add(description.getName());
return description; return description;
} catch (Exception e) { } catch (Exception e) {
new ErrorMessage("error importing model").addCause(e).show(); new ErrorMessage("error importing model").addCause(e).show();

View File

@ -82,10 +82,6 @@ public class ElementLibrary implements Iterable<ElementLibrary.ElementContainer>
return pd; return pd;
} }
private ElementTypeDescription loadPart(String elementName) {
return null;
}
@Override @Override
public Iterator<ElementContainer> iterator() { public Iterator<ElementContainer> iterator() {
return list.iterator(); return list.iterator();
@ -95,6 +91,10 @@ public class ElementLibrary implements Iterable<ElementLibrary.ElementContainer>
this.elementNotFoundNotification = elementNotFoundNotification; this.elementNotFoundNotification = elementNotFoundNotification;
} }
public void removeElement(String name) {
map.remove(name);
}
public static class ElementContainer { public static class ElementContainer {
private final String name; private final String name;
private final String treePath; private final String treePath;

View File

@ -93,7 +93,7 @@ public class ModelDescription implements Iterable<ModelEntry> {
thisNet.removePin(p); thisNet.removePin(p);
// and connect it to the nested inner net! // and connect it to the nested inner net!
thisNet.addAll(childNet.getPins()); thisNet.addAll(childNet.getPins());
// remove connecte net form child // remove connected net form child
child.remove(childNet); child.remove(childNet);
} }
it.remove(); it.remove();