
org.nakedobjects.plugins.html.context.ObjectHistory Maven / Gradle / Ivy
package org.nakedobjects.plugins.html.context;
import java.util.Enumeration;
import java.util.Vector;
import org.apache.log4j.Logger;
import org.nakedobjects.metamodel.adapter.NakedObject;
import org.nakedobjects.metamodel.commons.debug.DebugString;
import org.nakedobjects.metamodel.commons.exceptions.UnknownTypeException;
import org.nakedobjects.plugins.html.component.Block;
import org.nakedobjects.plugins.html.component.Component;
import org.nakedobjects.runtime.context.NakedObjectsContext;
public class ObjectHistory {
private static final Logger LOG = Logger.getLogger(ObjectHistory.class);
private static final int MAX = 8;
private final Vector history = new Vector();
private void add(final HistoryEntry entry) {
history.removeElement(entry);
history.addElement(entry);
LOG.debug("added to history: " + entry);
if (history.size() > MAX) {
LOG.debug("purging from history: " + history.elementAt(0));
history.removeElementAt(0);
}
}
public void debug(final DebugString debug) {
for (int i = history.size() - 1; i >= 0; i--) {
final HistoryEntry object = ((HistoryEntry) history.elementAt(i));
debug.appendln(object.toString());
}
}
public void listObjects(final Context context, final Block navigation) {
final Block taskBar = context.getComponentFactory().createBlock("history", null);
taskBar.add(context.getComponentFactory().createHeading("History"));
for (int i = history.size() - 1; i >= 0; i--) {
final HistoryEntry item = (HistoryEntry) history.elementAt(i);
Component icon;
if (item.type == HistoryEntry.OBJECT) {
final NakedObject object = context.getMappedObject(item.id);
NakedObjectsContext.getPersistenceSession().resolveImmediately(object);
icon = context.getComponentFactory().createObjectIcon(object, item.id, "item");
} else if (item.type == HistoryEntry.COLLECTION) {
final NakedObject object = context.getMappedCollection(item.id);
icon = context.getComponentFactory().createCollectionIcon(object, item.id);
} else {
throw new UnknownTypeException(item);
}
taskBar.add(icon);
}
navigation.add(taskBar);
}
public void addObject(final String idString) {
add(new HistoryEntry(idString, HistoryEntry.OBJECT));
}
public void addCollection(final String idString) {
add(new HistoryEntry(idString, HistoryEntry.COLLECTION));
}
public Enumeration elements() {
return history.elements();
}
}
// Copyright (c) Naked Objects Group Ltd.
© 2015 - 2025 Weber Informatics LLC | Privacy Policy