separated InsertAction.java to use it in component tree view

This commit is contained in:
hneemann 2017-03-25 20:53:46 +01:00
parent 6dc25b04ea
commit fa8278924d
9 changed files with 138 additions and 75 deletions

View File

@ -1,7 +1,6 @@
package de.neemann.digital.draw.library;
import de.neemann.digital.core.arithmetic.*;
import de.neemann.digital.core.arithmetic.Comparator;
import de.neemann.digital.core.basic.*;
import de.neemann.digital.core.element.*;
import de.neemann.digital.core.flipflops.FlipflopD;
@ -34,7 +33,10 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
/**
* @author hneemann
@ -142,7 +144,11 @@ public class ElementLibrary implements Iterable<ElementLibrary.ElementContainer>
node.add(TestCaseElement.TESTCASEDESCRIPTION);
root.add(node);
populateNodeMap();
try {
populateNodeMap();
} catch (IOException e) {
// can not happen because there are no custom elements yet
}
}
/**
@ -154,13 +160,11 @@ public class ElementLibrary implements Iterable<ElementLibrary.ElementContainer>
this.shapeFactory = shapeFactory;
}
private void populateNodeMap() {
private void populateNodeMap() throws IOException {
map.clear();
root.traverse(libraryNode -> {
if (libraryNode.isLeaf()) {
map.put(libraryNode.getName(), libraryNode);
}
});
String dn = root.traverse(new PopulateModelVisitor(map)).getDoubleNode();
if (dn != null)
throw new IOException(Lang.get("err_file_N0_ExistsTwiceBelow_N1", dn, rootLibraryPath));
}
/**
@ -220,7 +224,7 @@ public class ElementLibrary implements Iterable<ElementLibrary.ElementContainer>
throw new ElementNotFoundException(Lang.get("err_element_N_notFound", elementName));
}
private void rescanFolder() {
private void rescanFolder() throws IOException {
LOGGER.debug("rescan folder");
if (customNode == null) {
customNode = new LibraryNode(Lang.get("menu_custom"));
@ -338,7 +342,12 @@ public class ElementLibrary implements Iterable<ElementLibrary.ElementContainer>
return new LibraryNode(file.getName(), () -> {
try {
LOGGER.debug("load element " + file);
Circuit circuit = Circuit.loadCircuit(file, shapeFactory);
Circuit circuit;
try {
circuit = Circuit.loadCircuit(file, shapeFactory);
} catch (IOException e) {
throw new IOException(Lang.get("err_couldNotFindIncludedFile_N0", file));
}
ElementTypeDescriptionCustom description =
new ElementTypeDescriptionCustom(file,
attributes -> new CustomElement(circuit, ElementLibrary.this, file),
@ -464,4 +473,29 @@ public class ElementLibrary implements Iterable<ElementLibrary.ElementContainer>
return treePath;
}
}
private static final class PopulateModelVisitor implements Visitor {
private final HashMap<String, LibraryNode> map;
private String doubleNode;
private PopulateModelVisitor(HashMap<String, LibraryNode> map) {
this.map = map;
}
@Override
public void visit(LibraryNode libraryNode) {
if (libraryNode.isLeaf()) {
final String name = libraryNode.getName();
if (map.containsKey(name))
doubleNode = name;
map.put(name, libraryNode);
}
}
private String getDoubleNode() {
return doubleNode;
}
}
}

View File

@ -0,0 +1,78 @@
package de.neemann.digital.gui;
import de.neemann.digital.draw.elements.VisualElement;
import de.neemann.digital.draw.graphics.Vector;
import de.neemann.digital.draw.library.ElementLibrary;
import de.neemann.digital.draw.library.LibraryNode;
import de.neemann.digital.draw.shapes.ShapeFactory;
import de.neemann.digital.gui.components.CircuitComponent;
import de.neemann.digital.lang.Lang;
import de.neemann.gui.ErrorMessage;
import de.neemann.gui.ToolTipAction;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.io.IOException;
/**
* Action to insert the given node to the given circuit
* Created by hneemann on 25.03.17.
*/
final class InsertAction extends ToolTipAction {
private final LibraryNode node;
private final InsertHistory insertHistory;
private final CircuitComponent circuitComponent;
private final ShapeFactory shapeFactory;
/**
* Creates a new instance
*
* @param node the node which holds the element to add
* @param insertHistory the history to add the element to
* @param circuitComponent the component to add the element to
* @param shapeFactory the shapeFactory to create the icon
*/
public InsertAction(LibraryNode node, InsertHistory insertHistory, CircuitComponent circuitComponent, ShapeFactory shapeFactory) {
super(node.getTranslatedName(), createIcon(node, shapeFactory));
this.shapeFactory = shapeFactory;
this.node = node;
this.insertHistory = insertHistory;
this.circuitComponent = circuitComponent;
}
@Override
public void actionPerformed(ActionEvent e) {
VisualElement visualElement = new VisualElement(node.getName()).setPos(new Vector(10, 10)).setShapeFactory(shapeFactory);
circuitComponent.setPartToInsert(visualElement);
if (getIcon() == null) {
try {
node.getDescription();
setIcon(createIcon(node, shapeFactory));
} catch (IOException ex) {
SwingUtilities.invokeLater(new ErrorMessage(Lang.get("msg_errorImportingModel")).addCause(ex));
}
}
insertHistory.add(this);
}
/**
* @return true if element to insert is a custom element
*/
public boolean isCustom() {
return node.getDescriptionOrNull() instanceof ElementLibrary.ElementTypeDescriptionCustom;
}
private static ImageIcon createIcon(LibraryNode node, ShapeFactory shapeFactory) {
// doesn't load the description if only the icon is needed
// create action without an icon instead
if (node.isDescriptionLoaded()) {
try {
return new VisualElement(node.getDescription().getName()).setShapeFactory(shapeFactory).createIcon(75);
} catch (IOException ex) {
SwingUtilities.invokeLater(new ErrorMessage(Lang.get("msg_errorImportingModel")).addCause(ex));
}
}
return null;
}
}

View File

@ -83,8 +83,8 @@ public class InsertHistory {
Iterator<WrapperAction> it = wrappers.iterator();
while (it.hasNext()) {
WrapperAction w = it.next();
if (w.action instanceof LibrarySelector.InsertAction) {
if (((LibrarySelector.InsertAction) w.action).isCustom()) {
if (w.action instanceof InsertAction) {
if (((InsertAction) w.action).isCustom()) {
removeWrapperFromBar(w);
it.remove();
}

View File

@ -1,13 +1,10 @@
package de.neemann.digital.gui;
import de.neemann.digital.draw.elements.VisualElement;
import de.neemann.digital.draw.graphics.Vector;
import de.neemann.digital.draw.library.ElementLibrary;
import de.neemann.digital.draw.library.LibraryListener;
import de.neemann.digital.draw.library.LibraryNode;
import de.neemann.digital.draw.shapes.ShapeFactory;
import de.neemann.digital.gui.components.CircuitComponent;
import de.neemann.digital.gui.state.State;
import de.neemann.digital.lang.Lang;
import de.neemann.gui.ErrorMessage;
import de.neemann.gui.StringUtils;
@ -26,7 +23,6 @@ import java.io.IOException;
public class LibrarySelector implements LibraryListener {
private final ElementLibrary library;
private final ShapeFactory shapeFactory;
private final State elementState;
private JMenu componentsMenu;
private InsertHistory insertHistory;
private CircuitComponent circuitComponent;
@ -37,13 +33,11 @@ public class LibrarySelector implements LibraryListener {
*
* @param library the library to select elements from
* @param shapeFactory The shape factory
* @param elementState the elements state
*/
public LibrarySelector(ElementLibrary library, ShapeFactory shapeFactory, State elementState) {
public LibrarySelector(ElementLibrary library, ShapeFactory shapeFactory) {
this.library = library;
library.addListener(this);
this.shapeFactory = shapeFactory;
this.elementState = elementState;
}
/**
@ -91,7 +85,7 @@ public class LibrarySelector implements LibraryListener {
private void addComponents(JMenu parts, LibraryNode node) {
if (node.isLeaf()) {
parts.add(new InsertAction(node, insertHistory, circuitComponent)
parts.add(new InsertAction(node, insertHistory, circuitComponent, shapeFactory)
.setToolTip(createToolTipText(node.getName()))
.createJMenuItem());
} else {
@ -105,51 +99,4 @@ public class LibrarySelector implements LibraryListener {
private static String createToolTipText(String elementName) {
return StringUtils.textToHTML(Lang.getNull("elem_" + elementName + "_tt"));
}
final class InsertAction extends ToolTipAction {
private final LibraryNode node;
private final InsertHistory insertHistory;
private final CircuitComponent circuitComponent;
private InsertAction(LibraryNode node, InsertHistory insertHistory, CircuitComponent circuitComponent) {
super(node.getTranslatedName(), createIcon(node, shapeFactory));
this.node = node;
this.insertHistory = insertHistory;
this.circuitComponent = circuitComponent;
}
@Override
public void actionPerformed(ActionEvent e) {
VisualElement visualElement = new VisualElement(node.getName()).setPos(new Vector(10, 10)).setShapeFactory(shapeFactory);
elementState.enter();
circuitComponent.setPartToInsert(visualElement);
if (getIcon() == null) {
try {
node.getDescription();
setIcon(createIcon(node, shapeFactory));
} catch (IOException ex) {
SwingUtilities.invokeLater(new ErrorMessage(Lang.get("msg_errorImportingModel")).addCause(ex));
}
}
insertHistory.add(this);
}
public boolean isCustom() {
return node.getDescriptionOrNull() instanceof ElementLibrary.ElementTypeDescriptionCustom;
}
}
private static ImageIcon createIcon(LibraryNode node, ShapeFactory shapeFactory) {
// don't load the description if only the icon is needed
// create action without an icon instead
if (node.isDescriptionLoaded()) {
try {
return new VisualElement(node.getDescription().getName()).setShapeFactory(shapeFactory).createIcon(75);
} catch (IOException ex) {
SwingUtilities.invokeLater(new ErrorMessage(Lang.get("msg_errorImportingModel")).addCause(ex));
}
}
return null;
}
}

View File

@ -225,7 +225,7 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave, E
toolBar.addSeparator();
librarySelector = new LibrarySelector(library, shapeFactory, stoppedState);
librarySelector = new LibrarySelector(library, shapeFactory);
menuBar.add(librarySelector.buildMenu(new InsertHistory(toolBar), circuitComponent));
getContentPane().add(toolBar, BorderLayout.NORTH);
@ -797,6 +797,14 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave, E
});
}
/**
* stops the model
*/
public void stopModel() {
stoppedState.enter();
}
private static JFileChooser getJFileChooser(File filename) {
File folder = null;
if (filename != null)

View File

@ -398,6 +398,7 @@ public class CircuitComponent extends JComponent implements Circuit.ChangedListe
* @param element the element to insert
*/
public void setPartToInsert(VisualElement element) {
parent.stopModel();
mouseInsertElement.activate(element);
}

View File

@ -601,8 +601,6 @@ Es sind nur {1} Variablen erlaubt, es wurden jedoch {2} gefunden.</string>
<string name="menu_orderOutputs">Sortieren der Ausgänge</string>
<string name="menu_orderOutputs_tt">Sortiert die Ausgänge für die Verwendung als eingebettete Schaltung</string>
<string name="menu_paste">Einfügen</string>
<string name="menu_refresh">Alle neu laden</string>
<string name="menu_refresh_tt">Alle importierten Schaltungen werden neu geladen</string>
<string name="menu_rotate">Rotieren</string>
<string name="menu_run">Start der Simulation</string>
<string name="menu_run_tt">Startet die Simulation der Schaltung</string>

View File

@ -588,8 +588,6 @@ allowed are {1} variables but {2} are found.</string>
<string name="menu_orderOutputs">Order Outputs</string>
<string name="menu_orderOutputs_tt">Order the outputs for the usage as nested circuit</string>
<string name="menu_paste">Paste</string>
<string name="menu_refresh">Reload</string>
<string name="menu_refresh_tt">Reload all imported circuits</string>
<string name="menu_rotate">Rotate</string>
<string name="menu_run">Start Simulation</string>
<string name="menu_run_tt">Starts the simulation of the circuit.</string>

View File

@ -23,7 +23,6 @@ public class TestElemConsistence extends TestCase {
* @throws NodeException
*/
public void testConsistence() throws NodeException, PinException {
/*
ElementLibrary library = new ElementLibrary();
for (ElementLibrary.ElementContainer e : library) {
ElementTypeDescription etd = e.getDescription();
@ -38,7 +37,7 @@ public class TestElemConsistence extends TestCase {
checkPins(key, etd.getInputDescription(new ElementAttributes()));
checkPins(key, etd.getOutputDescriptions(new ElementAttributes()));
}
}*/ fail();
}
}
private void checkPins(String key, PinDescriptions pins) {