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. * Returns a {@link ElementTypeDescription} by a given name.
* If not found its tried to load it. * 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 { public ElementTypeDescription getElementType(String elementName) throws ElementNotFoundException {
try { try {
LibraryNode description = map.get(elementName); LibraryNode node = map.get(elementName);
if (description != null) if (node != null)
return description.getDescription(); return node.getDescription();
// effects only some old files! // effects only some old files!
elementName = elementName.replace("\\", "/"); elementName = elementName.replace("\\", "/");
@ -221,18 +231,18 @@ public class ElementLibrary implements Iterable<ElementLibrary.ElementContainer>
elementName = new File(elementName).getName(); elementName = new File(elementName).getName();
} }
description = map.get(elementName); node = map.get(elementName);
if (description != null) if (node != null)
return description.getDescription(); return node.getDescription();
if (rootLibraryPath == null) if (rootLibraryPath == null)
throw new ElementNotFoundException(Lang.get("err_fileNeedsToBeSaved")); throw new ElementNotFoundException(Lang.get("err_fileNeedsToBeSaved"));
rescanFolder(); rescanFolder();
description = map.get(elementName); node = map.get(elementName);
if (description != null) if (node != null)
return description.getDescription(); return node.getDescription();
} catch (IOException e) { } catch (IOException e) {
throw new ElementNotFoundException(Lang.get("msg_errorImportingModel"), 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) { public void addListener(LibraryListener listener) {
listeners.add(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) { public void removeListener(LibraryListener listener) {
listeners.remove(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. * Created by hneemann on 25.03.17.
*/ */
public final class InsertAction extends ToolTipAction { public final class InsertAction extends ToolTipAction {
private final LibraryNode node;
private final InsertHistory insertHistory; private final InsertHistory insertHistory;
private final CircuitComponent circuitComponent; private final CircuitComponent circuitComponent;
private final ShapeFactory shapeFactory; private final ShapeFactory shapeFactory;
private LibraryNode node;
/** /**
* Creates a new instance * Creates a new instance
@ -69,4 +69,18 @@ public final class InsertAction extends ToolTipAction {
return node.getName(); 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; 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.LibraryListener;
import de.neemann.digital.draw.library.LibraryNode; import de.neemann.digital.draw.library.LibraryNode;
@ -18,6 +19,7 @@ public class InsertHistory implements LibraryListener {
private static final int MAX_ICONS = 6; private static final int MAX_ICONS = 6;
private final JToolBar bar; private final JToolBar bar;
private final ElementLibrary library;
private final ArrayList<WrapperAction> wrappers; private final ArrayList<WrapperAction> wrappers;
private int mainTime; private int mainTime;
@ -26,8 +28,9 @@ public class InsertHistory implements LibraryListener {
* *
* @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.bar = bar;
this.library = library;
wrappers = new ArrayList<>(); wrappers = new ArrayList<>();
} }
@ -93,8 +96,12 @@ public class InsertHistory implements LibraryListener {
while (it.hasNext()) { while (it.hasNext()) {
WrapperAction w = it.next(); WrapperAction w = it.next();
if (w.action.isCustom()) { if (w.action.isCustom()) {
LibraryNode n = library.getElementNodeOrNull(w.action.getName());
if (n == null) { // is'nt there, so delete
removeWrapperFromBar(w); removeWrapperFromBar(w);
it.remove(); it.remove();
} else
w.update(n);
} }
} }
bar.revalidate(); bar.revalidate();
@ -117,5 +124,10 @@ public class InsertHistory implements LibraryListener {
action.actionPerformed(e); action.actionPerformed(e);
time = mainTime++; 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(); toolBar.addSeparator();
insertHistory = new InsertHistory(toolBar); insertHistory = new InsertHistory(toolBar, library);
library.addListener(insertHistory); library.addListener(insertHistory);
final LibrarySelector librarySelector = new LibrarySelector(library, shapeFactory); final LibrarySelector librarySelector = new LibrarySelector(library, shapeFactory);
library.addListener(librarySelector); library.addListener(librarySelector);