imported models are put to the history like normal elements

This commit is contained in:
hneemann 2016-04-10 16:22:50 +02:00
parent 68ff8c506a
commit 43f755a394
3 changed files with 35 additions and 11 deletions

View File

@ -105,7 +105,7 @@ public class ElementLibrary implements Iterable<ElementLibrary.ElementContainer>
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));
}

View File

@ -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);
}

View File

@ -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;
}
}
}