fixed consistency bug in InsertHistory

This commit is contained in:
hneemann 2017-03-27 20:25:04 +02:00
parent 25036d0169
commit 2fdc975efd
4 changed files with 53 additions and 17 deletions

View File

@ -201,6 +201,16 @@ public class ElementLibrary implements Iterable<ElementLibrary.ElementContainer>
}
}
/**
* Returns the node or null if node not present.
*
* @param elementName the name
* @return the node or null
*/
public LibraryNode getElementNodeOrNull(String elementName) {
return map.get(elementName);
}
/**
* Returns a {@link ElementTypeDescription} by a given name.
* If not found its tried to load it.
@ -211,9 +221,9 @@ public class ElementLibrary implements Iterable<ElementLibrary.ElementContainer>
*/
public ElementTypeDescription getElementType(String elementName) throws ElementNotFoundException {
try {
LibraryNode description = map.get(elementName);
if (description != null)
return description.getDescription();
LibraryNode node = map.get(elementName);
if (node != null)
return node.getDescription();
// effects only some old files!
elementName = elementName.replace("\\", "/");
@ -221,18 +231,18 @@ public class ElementLibrary implements Iterable<ElementLibrary.ElementContainer>
elementName = new File(elementName).getName();
}
description = map.get(elementName);
if (description != null)
return description.getDescription();
node = map.get(elementName);
if (node != null)
return node.getDescription();
if (rootLibraryPath == null)
throw new ElementNotFoundException(Lang.get("err_fileNeedsToBeSaved"));
rescanFolder();
description = map.get(elementName);
if (description != null)
return description.getDescription();
node = map.get(elementName);
if (node != null)
return node.getDescription();
} catch (IOException e) {
throw new ElementNotFoundException(Lang.get("msg_errorImportingModel"), e);
}
@ -304,7 +314,7 @@ public class ElementLibrary implements Iterable<ElementLibrary.ElementContainer>
*/
public void addListener(LibraryListener listener) {
listeners.add(listener);
LOGGER.debug("added library listener "+listener.getClass().getSimpleName()+", listeners: "+listeners.size());
LOGGER.debug("added library listener " + listener.getClass().getSimpleName() + ", listeners: " + listeners.size());
}
/**
@ -314,7 +324,7 @@ public class ElementLibrary implements Iterable<ElementLibrary.ElementContainer>
*/
public void removeListener(LibraryListener listener) {
listeners.remove(listener);
LOGGER.debug("removed library listener "+listener.getClass().getSimpleName()+", listeners: "+listeners.size());
LOGGER.debug("removed library listener " + listener.getClass().getSimpleName() + ", listeners: " + listeners.size());
}

View File

@ -19,10 +19,10 @@ import java.io.IOException;
* Created by hneemann on 25.03.17.
*/
public final class InsertAction extends ToolTipAction {
private final LibraryNode node;
private final InsertHistory insertHistory;
private final CircuitComponent circuitComponent;
private final ShapeFactory shapeFactory;
private LibraryNode node;
/**
* Creates a new instance
@ -69,4 +69,18 @@ public final class InsertAction extends ToolTipAction {
return node.getName();
}
/**
* Updates this action to a new node
*
* @param node the node
*/
public void update(LibraryNode node) {
this.node = node;
try {
final ImageIcon icon = node.getIcon(shapeFactory);
setIcon(icon);
} catch (IOException ex) {
SwingUtilities.invokeLater(new ErrorMessage(Lang.get("msg_errorImportingModel")).addCause(ex));
}
}
}

View File

@ -1,5 +1,6 @@
package de.neemann.digital.gui;
import de.neemann.digital.draw.library.ElementLibrary;
import de.neemann.digital.draw.library.LibraryListener;
import de.neemann.digital.draw.library.LibraryNode;
@ -18,16 +19,18 @@ public class InsertHistory implements LibraryListener {
private static final int MAX_ICONS = 6;
private final JToolBar bar;
private final ElementLibrary library;
private final ArrayList<WrapperAction> wrappers;
private int mainTime;
/**
* Creates a new instance
*
* @param bar the toolbar to put the elements to
* @param bar the toolbar to put the elements to
*/
public InsertHistory(JToolBar bar) {
public InsertHistory(JToolBar bar, ElementLibrary library) {
this.bar = bar;
this.library = library;
wrappers = new ArrayList<>();
}
@ -93,8 +96,12 @@ public class InsertHistory implements LibraryListener {
while (it.hasNext()) {
WrapperAction w = it.next();
if (w.action.isCustom()) {
removeWrapperFromBar(w);
it.remove();
LibraryNode n = library.getElementNodeOrNull(w.action.getName());
if (n == null) { // is'nt there, so delete
removeWrapperFromBar(w);
it.remove();
} else
w.update(n);
}
}
bar.revalidate();
@ -117,5 +124,10 @@ public class InsertHistory implements LibraryListener {
action.actionPerformed(e);
time = mainTime++;
}
public void update(LibraryNode n) {
action.update(n);
putValue(Action.SMALL_ICON, action.getValue(Action.SMALL_ICON));
}
}
}

View File

@ -222,7 +222,7 @@ public class Main extends JFrame implements ClosingWindowListener.ConfirmSave, E
toolBar.addSeparator();
insertHistory = new InsertHistory(toolBar);
insertHistory = new InsertHistory(toolBar, library);
library.addListener(insertHistory);
final LibrarySelector librarySelector = new LibrarySelector(library, shapeFactory);
library.addListener(librarySelector);