mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-15 15:58:41 -04:00
imported models are put to the history like normal elements
This commit is contained in:
parent
68ff8c506a
commit
43f755a394
@ -105,7 +105,7 @@ public class ElementLibrary implements Iterable<ElementLibrary.ElementContainer>
|
|||||||
ElementTypeDescription pd = map.get(elementName);
|
ElementTypeDescription pd = map.get(elementName);
|
||||||
if (pd == null) {
|
if (pd == null) {
|
||||||
if (elementNotFoundNotification != null)
|
if (elementNotFoundNotification != null)
|
||||||
pd = elementNotFoundNotification.notFound(elementName);
|
pd = elementNotFoundNotification.elementNotFound(elementName);
|
||||||
if (pd == null)
|
if (pd == null)
|
||||||
throw new RuntimeException(Lang.get("err_element_N_notFound", elementName));
|
throw new RuntimeException(Lang.get("err_element_N_notFound", elementName));
|
||||||
}
|
}
|
||||||
|
@ -3,8 +3,16 @@ package de.neemann.digital.draw.library;
|
|||||||
import de.neemann.digital.core.element.ElementTypeDescription;
|
import de.neemann.digital.core.element.ElementTypeDescription;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Interface to request unknown {@link ElementTypeDescription}s
|
||||||
|
*
|
||||||
* @author hneemann
|
* @author hneemann
|
||||||
*/
|
*/
|
||||||
public interface ElementNotFoundNotification {
|
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);
|
||||||
}
|
}
|
||||||
|
@ -75,12 +75,13 @@ public class LibrarySelector implements ElementNotFoundNotification {
|
|||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
JFileChooser fc = new JFileChooser(filePath);
|
JFileChooser fc = new JFileChooser(filePath);
|
||||||
fc.addChoosableFileFilter(new FileNameExtensionFilter("Circuit", "dig"));
|
fc.setFileFilter(new FileNameExtensionFilter("Circuit", "dig"));
|
||||||
if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
|
if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
|
||||||
ElementTypeDescription des = importElement(fc.getSelectedFile());
|
Imported imp = importElement(fc.getSelectedFile());
|
||||||
if (des != null) {
|
if (imp != null) {
|
||||||
VisualElement visualElement = new VisualElement(des.getName()).setPos(new Vector(10, 10)).setShapeFactory(shapeFactory);
|
VisualElement visualElement = new VisualElement(imp.description.getName()).setPos(new Vector(10, 10)).setShapeFactory(shapeFactory);
|
||||||
circuitComponent.setPartToInsert(visualElement);
|
circuitComponent.setPartToInsert(visualElement);
|
||||||
|
insertHistory.add(imp.insertAction);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -122,8 +123,12 @@ public class LibrarySelector implements ElementNotFoundNotification {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ElementTypeDescription notFound(String elementName) {
|
public ElementTypeDescription elementNotFound(String elementName) {
|
||||||
return importElement(new File(filePath, elementName));
|
Imported imported = importElement(new File(filePath, elementName));
|
||||||
|
if (imported == null)
|
||||||
|
return null;
|
||||||
|
else
|
||||||
|
return imported.description;
|
||||||
}
|
}
|
||||||
|
|
||||||
private final class InsertAction extends ToolTipAction {
|
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 {
|
try {
|
||||||
System.out.println("load element " + file);
|
System.out.println("load element " + file);
|
||||||
Circuit circuit = Circuit.loadCircuit(file, shapeFactory);
|
Circuit circuit = Circuit.loadCircuit(file, shapeFactory);
|
||||||
@ -159,7 +164,8 @@ public class LibrarySelector implements ElementNotFoundNotification {
|
|||||||
.setShortName(createShortName(file));
|
.setShortName(createShortName(file));
|
||||||
library.addDescription(description);
|
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());
|
ImportedItem item = findImportedItem(description.getName());
|
||||||
if (item != null) {
|
if (item != null) {
|
||||||
if (customMenu != null) {
|
if (customMenu != null) {
|
||||||
@ -170,7 +176,7 @@ public class LibrarySelector implements ElementNotFoundNotification {
|
|||||||
importedElements.add(new ImportedItem(description.getName(), menuEntry));
|
importedElements.add(new ImportedItem(description.getName(), menuEntry));
|
||||||
if (customMenu != null)
|
if (customMenu != null)
|
||||||
customMenu.add(menuEntry);
|
customMenu.add(menuEntry);
|
||||||
return description;
|
return new Imported(description, insertAction);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
SwingUtilities.invokeLater(new ErrorMessage(Lang.get("msg_errorImportingModel")).addCause(e));
|
SwingUtilities.invokeLater(new ErrorMessage(Lang.get("msg_errorImportingModel")).addCause(e));
|
||||||
}
|
}
|
||||||
@ -243,4 +249,14 @@ public class LibrarySelector implements ElementNotFoundNotification {
|
|||||||
return attributes;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user