From 43f755a3948c5544d957ca843ab3a5ad9af39499 Mon Sep 17 00:00:00 2001 From: hneemann Date: Sun, 10 Apr 2016 16:22:50 +0200 Subject: [PATCH] imported models are put to the history like normal elements --- .../digital/draw/library/ElementLibrary.java | 2 +- .../library/ElementNotFoundNotification.java | 10 +++++- .../neemann/digital/gui/LibrarySelector.java | 34 ++++++++++++++----- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/main/java/de/neemann/digital/draw/library/ElementLibrary.java b/src/main/java/de/neemann/digital/draw/library/ElementLibrary.java index 657f2bd87..5b488072e 100644 --- a/src/main/java/de/neemann/digital/draw/library/ElementLibrary.java +++ b/src/main/java/de/neemann/digital/draw/library/ElementLibrary.java @@ -105,7 +105,7 @@ public class ElementLibrary implements Iterable ElementTypeDescription pd = map.get(elementName); if (pd == null) { if (elementNotFoundNotification != null) - pd = elementNotFoundNotification.notFound(elementName); + pd = elementNotFoundNotification.elementNotFound(elementName); if (pd == null) throw new RuntimeException(Lang.get("err_element_N_notFound", elementName)); } diff --git a/src/main/java/de/neemann/digital/draw/library/ElementNotFoundNotification.java b/src/main/java/de/neemann/digital/draw/library/ElementNotFoundNotification.java index c11b9dca0..e45cb5e3c 100644 --- a/src/main/java/de/neemann/digital/draw/library/ElementNotFoundNotification.java +++ b/src/main/java/de/neemann/digital/draw/library/ElementNotFoundNotification.java @@ -3,8 +3,16 @@ package de.neemann.digital.draw.library; import de.neemann.digital.core.element.ElementTypeDescription; /** + * Interface to request unknown {@link ElementTypeDescription}s + * * @author hneemann */ public interface ElementNotFoundNotification { - ElementTypeDescription notFound(String elementName); + /** + * called if the library could not create an element + * + * @param elementName the elements name + * @return the element or null if not loadable + */ + ElementTypeDescription elementNotFound(String elementName); } diff --git a/src/main/java/de/neemann/digital/gui/LibrarySelector.java b/src/main/java/de/neemann/digital/gui/LibrarySelector.java index 256b5c3e2..2628d15db 100644 --- a/src/main/java/de/neemann/digital/gui/LibrarySelector.java +++ b/src/main/java/de/neemann/digital/gui/LibrarySelector.java @@ -75,12 +75,13 @@ public class LibrarySelector implements ElementNotFoundNotification { @Override public void actionPerformed(ActionEvent e) { JFileChooser fc = new JFileChooser(filePath); - fc.addChoosableFileFilter(new FileNameExtensionFilter("Circuit", "dig")); + fc.setFileFilter(new FileNameExtensionFilter("Circuit", "dig")); if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { - ElementTypeDescription des = importElement(fc.getSelectedFile()); - if (des != null) { - VisualElement visualElement = new VisualElement(des.getName()).setPos(new Vector(10, 10)).setShapeFactory(shapeFactory); + Imported imp = importElement(fc.getSelectedFile()); + if (imp != null) { + VisualElement visualElement = new VisualElement(imp.description.getName()).setPos(new Vector(10, 10)).setShapeFactory(shapeFactory); circuitComponent.setPartToInsert(visualElement); + insertHistory.add(imp.insertAction); } } } @@ -122,8 +123,12 @@ public class LibrarySelector implements ElementNotFoundNotification { } @Override - public ElementTypeDescription notFound(String elementName) { - return importElement(new File(filePath, elementName)); + public ElementTypeDescription elementNotFound(String elementName) { + Imported imported = importElement(new File(filePath, elementName)); + if (imported == null) + return null; + else + return imported.description; } private final class InsertAction extends ToolTipAction { @@ -148,7 +153,7 @@ public class LibrarySelector implements ElementNotFoundNotification { } } - private ElementTypeDescription importElement(File file) { + private Imported importElement(File file) { try { System.out.println("load element " + file); Circuit circuit = Circuit.loadCircuit(file, shapeFactory); @@ -159,7 +164,8 @@ public class LibrarySelector implements ElementNotFoundNotification { .setShortName(createShortName(file)); library.addDescription(description); - JMenuItem menuEntry = new InsertAction(description.getName(), insertHistory, circuitComponent).createJMenuItem(); + InsertAction insertAction = new InsertAction(description.getName(), insertHistory, circuitComponent); + JMenuItem menuEntry = insertAction.createJMenuItem(); ImportedItem item = findImportedItem(description.getName()); if (item != null) { if (customMenu != null) { @@ -170,7 +176,7 @@ public class LibrarySelector implements ElementNotFoundNotification { importedElements.add(new ImportedItem(description.getName(), menuEntry)); if (customMenu != null) customMenu.add(menuEntry); - return description; + return new Imported(description, insertAction); } catch (Exception e) { SwingUtilities.invokeLater(new ErrorMessage(Lang.get("msg_errorImportingModel")).addCause(e)); } @@ -243,4 +249,14 @@ public class LibrarySelector implements ElementNotFoundNotification { return attributes; } } + + private static class Imported { + private final ElementTypeDescription description; + private final InsertAction insertAction; + + private Imported(ElementTypeDescription description, InsertAction insertAction) { + this.description = description; + this.insertAction = insertAction; + } + } }