package com.c2kernel.gui.tabs.collection; import java.awt.HeadlessException; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.ArrayList; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.AbstractTableModel; import com.c2kernel.collection.Collection; import com.c2kernel.common.ObjectNotFoundException; import com.c2kernel.entity.C2KLocalObject; import com.c2kernel.entity.proxy.EntityProxy; import com.c2kernel.entity.proxy.EntityProxyObserver; import com.c2kernel.entity.proxy.ItemProxy; import com.c2kernel.events.Event; import com.c2kernel.gui.MainFrame; import com.c2kernel.lifecycle.instance.predefined.PredefinedStep; import com.c2kernel.lookup.EntityPath; import com.c2kernel.lookup.InvalidEntityPathException; import com.c2kernel.persistency.ClusterStorage; import com.c2kernel.persistency.outcome.Outcome; import com.c2kernel.process.Gateway; import com.c2kernel.utils.CastorXMLUtility; import com.c2kernel.utils.Language; import com.c2kernel.utils.Logger; public class CollectionHistoryWindow extends JFrame { JTable historyTable; HistoryTableModel historyModel; public CollectionHistoryWindow(ItemProxy item, Collection coll) throws HeadlessException { super("Collection History"); historyModel = new HistoryTableModel(item, coll); historyTable = new JTable(historyModel); this.getContentPane().add(new JScrollPane(historyTable)); historyTable.addMouseListener(new HistoryTableListener(item)); this.pack(); super.toFront(); this.validate(); this.show(); } private class HistoryTableModel extends AbstractTableModel implements EntityProxyObserver { ItemProxy item; ArrayList collEvents, collEventData; Collection coll; public HistoryTableModel(ItemProxy item, Collection coll) { this.item = item; this.coll = coll; collEvents = new ArrayList(); collEventData = new ArrayList(); item.subscribe(this, ClusterStorage.HISTORY, true); } public int getColumnCount() { // TODO Auto-generated method stub return 4; } public String getColumnName(int columnIndex) { switch(columnIndex) { case 0: return Language.translate("Date"); case 1: return Language.translate("Operation"); case 2: return Language.translate("Slot"); case 3: return Language.translate("Child"); default: return ""; } } public int getRowCount() { // TODO Auto-generated method stub return collEvents.size(); } public Object getValueAt(int rowIndex, int columnIndex) { Event ev = (Event)collEvents.get(rowIndex); switch (columnIndex) { case 0: return ev.getTimeString(); case 1: if (ev.getStepName().equals("AssignItemToSlot")) return "Item Assigned"; else return "Collection replaced"; case 2: if (ev.getStepName().equals("AssignItemToSlot")) return ((String[])collEventData.get(rowIndex))[1]; return ""; case 3: if (ev.getStepName().equals("AddC2KObject")) return "Click to view"; String name; try { EntityProxy childItem = Gateway.getProxyManager().getProxy(new EntityPath(Integer.parseInt(((String[])collEventData.get(rowIndex))[2]))); name = childItem.getName(); } catch (NumberFormatException e) { name = "Invalid entity key: "+((String[])collEventData.get(rowIndex))[2]; } catch (ObjectNotFoundException e) { name = "Item deleted: "+((String[])collEventData.get(rowIndex))[2]; } catch (InvalidEntityPathException e) { name = "Invalid entity key: "+((String[])collEventData.get(rowIndex))[2]; } return name; default: return ""; } } public Object getEventData(int row) { return collEventData.get(row); } public void add(C2KLocalObject contents) { if (!(contents instanceof Event)) return; Event thisEv = (Event)contents; if (thisEv.getStepName().equals("AssignItemToSlot") || thisEv.getStepName().equals("AddC2KObject")) { String[] params; try { Outcome oc = (Outcome)item.getObject(ClusterStorage.OUTCOME+"/PredefinedStepOutcome/0/"+thisEv.getID()); params = PredefinedStep.getDataList(oc.getData()); } catch (ObjectNotFoundException ex) { return; } if (thisEv.getStepName().equals("AssignItemToSlot")) { if (params[0].equals(coll.getName())) collEventData.add(params); else return; } else { Object obj; try { obj = CastorXMLUtility.unmarshall(params[0]); } catch (Exception e) { Logger.error(e); return; } if (obj instanceof Collection) collEventData.add(obj); else return; } } else return; collEvents.add(thisEv); fireTableRowsInserted(collEvents.size()-1, collEvents.size()-1); } public void remove(String id) { } } private class HistoryTableListener extends MouseAdapter { ItemProxy item; public HistoryTableListener(ItemProxy item) { this.item = item; } public void mouseClicked(MouseEvent e) { if (e.getClickCount()==2) { int row = historyTable.getSelectedRow(); Object data = historyModel.getEventData(row); if (data instanceof Collection) { showColl((Collection)data); } else { String[] params = (String[])data; try { EntityProxy childItem = Gateway.getProxyManager().getProxy(new EntityPath(Integer.parseInt(params[2]))); MainFrame.itemFinder.pushNewKey(childItem.getName()); } catch (Exception ex) { } } } } public void showColl(Collection coll) { JFrame newFrame = new JFrame(); AggregationView newView = new AggregationView(); newView.setCollection(coll); newView.setItem(item); newFrame.getContentPane().add(newView); newFrame.pack(); newFrame.toFront(); newFrame.validate(); newFrame.show(); } } }