1 /* 2 * Copyright (c) 2017 Helmut Neemann 3 * Use of this source code is governed by the GPL v3 license 4 * that can be found in the LICENSE file. 5 */ 6 package de.neemann.digital.gui; 7 8 import de.neemann.digital.draw.elements.VisualElement; 9 import de.neemann.digital.draw.graphics.Vector; 10 import de.neemann.digital.draw.library.LibraryNode; 11 import de.neemann.digital.draw.shapes.MissingShape; 12 import de.neemann.digital.draw.shapes.ShapeFactory; 13 import de.neemann.digital.gui.components.CircuitComponent; 14 import de.neemann.digital.lang.Lang; 15 import de.neemann.gui.ErrorMessage; 16 import de.neemann.gui.ToolTipAction; 17 18 import javax.swing.*; 19 import java.awt.event.ActionEvent; 20 import java.io.IOException; 21 22 /** 23 * Action to insert the given node to the given circuit 24 */ 25 public final class InsertAction extends ToolTipAction { 26 private final InsertHistory insertHistory; 27 private final CircuitComponent circuitComponent; 28 private final ShapeFactory shapeFactory; 29 private LibraryNode node; 30 31 /** 32 * Creates a new instance 33 * 34 * @param node the node which holds the element to add 35 * @param insertHistory the history to add the element to 36 * @param circuitComponent the component to add the element to 37 * @param shapeFactory the shapeFactory to create the icon 38 */ InsertAction(LibraryNode node, InsertHistory insertHistory, CircuitComponent circuitComponent, ShapeFactory shapeFactory)39 public InsertAction(LibraryNode node, InsertHistory insertHistory, CircuitComponent circuitComponent, ShapeFactory shapeFactory) { 40 super(node.getTranslatedName(), node.getIconOrNull(shapeFactory)); 41 this.shapeFactory = shapeFactory; 42 this.node = node; 43 this.insertHistory = insertHistory; 44 this.circuitComponent = circuitComponent; 45 setEnabled(node.isUnique()); 46 } 47 48 @Override actionPerformed(ActionEvent e)49 public void actionPerformed(ActionEvent e) { 50 if (node.isUnique()) { 51 VisualElement visualElement = node.setWideShapeFlagTo(new VisualElement(node.getName()).setPos(new Vector(10, 10)).setShapeFactory(shapeFactory)); 52 if (getIcon() == null) { 53 try { 54 node.getDescription(); 55 setIcon(node.getIcon(shapeFactory)); 56 } catch (IOException ex) { 57 SwingUtilities.invokeLater(new ErrorMessage(Lang.get("msg_errorImportingModel_N0", node.getName())).addCause(ex)); 58 } 59 } 60 61 if (visualElement.getShape() instanceof MissingShape) 62 return; 63 64 circuitComponent.setPartToInsert(visualElement); 65 insertHistory.add(this); 66 } 67 } 68 69 /** 70 * @return true if element to insert is a custom element 71 */ isCustom()72 public boolean isCustom() { 73 return node.isCustom(); 74 } 75 76 /** 77 * @return the name of the node to insert 78 */ getName()79 public String getName() { 80 return node.getName(); 81 } 82 83 /** 84 * Updates this action to a new node 85 * 86 * @param node the node 87 */ update(LibraryNode node)88 public void update(LibraryNode node) { 89 this.node = node; 90 try { 91 final Icon icon = node.getIcon(shapeFactory); 92 setIcon(icon); 93 } catch (IOException ex) { 94 SwingUtilities.invokeLater(new ErrorMessage(Lang.get("msg_errorImportingModel_N0", node.getName())).addCause(ex)); 95 } 96 } 97 98 /** 99 * @return the library node 100 */ getNode()101 public LibraryNode getNode() { 102 return node; 103 } 104 105 106 /** 107 * Implements a lazy loading of the tooltips. 108 * Avoids the reading of all tooltips from the lib files if menu is created. 109 * This code ensures that the tooltips are only loaded from the file if the text is shown to the user. 110 * 111 * @return the JMenuItem created 112 */ 113 @Override createJMenuItem()114 public JMenuItem createJMenuItem() { 115 JMenuItem i = new JMenuItem(node.getTranslatedName(), getIcon()) { 116 @Override 117 public String getToolTipText() { 118 return node.getToolTipText(); 119 } 120 }; 121 i.addActionListener(InsertAction.this); 122 i.setEnabled(node.isUnique()); 123 ToolTipManager.sharedInstance().registerComponent(i); 124 return i; 125 } 126 } 127