package org.cristalise.gui.tree; import java.util.ArrayList; import javax.swing.tree.DefaultMutableTreeNode; import org.cristalise.gui.ItemTabManager; import org.cristalise.gui.MainFrame; import org.cristalise.kernel.collection.Collection; import org.cristalise.kernel.collection.CollectionMember; import org.cristalise.kernel.collection.Dependency; import org.cristalise.kernel.common.ObjectNotFoundException; import org.cristalise.kernel.entity.proxy.ItemProxy; import org.cristalise.kernel.entity.proxy.MemberSubscription; import org.cristalise.kernel.entity.proxy.ProxyObserver; import org.cristalise.kernel.lookup.ItemPath; import org.cristalise.kernel.lookup.Path; import org.cristalise.kernel.persistency.ClusterStorage; import org.cristalise.kernel.utils.CastorHashMap; import org.cristalise.kernel.utils.KeyValuePair; import org.cristalise.kernel.utils.Logger; public class NodeCollection extends Node implements ProxyObserver> { ItemProxy parent; Collection thisCollection; String path; public NodeCollection(ItemProxy parent, String name, ItemTabManager desktop) { super(desktop); this.parent = parent; this.name = name; this.path = parent.getPath()+"/"+ClusterStorage.COLLECTION+"/"+name+"/last"; createTreeNode(); this.makeExpandable(); } public NodeCollection(ItemProxy parent, Collection coll, ItemTabManager desktop) { super(desktop); this.parent = parent; this.name = coll.getName(); this.path = parent.getPath()+"/"+ClusterStorage.COLLECTION+"/"+name+"/last"; createTreeNode(); this.makeExpandable(); add(coll); } @Override public void loadChildren() { Logger.msg(8, "NodeCollection::loadChildren()"); try { if (thisCollection == null) { Collection initColl = (Collection)parent.getObject(ClusterStorage.COLLECTION+"/"+name+"/last"); add(initColl); } parent.subscribe(new MemberSubscription>(this, ClusterStorage.COLLECTION, false)); } catch (ObjectNotFoundException ex) { end(false); return; } } @Override public void add(Collection contents) { if (!contents.getName().equals(name)) return; this.type = contents.getClass().getSimpleName(); ArrayList newMembers = contents.getMembers().list; ArrayList oldMembers; if (thisCollection == null) oldMembers = new ArrayList(); else oldMembers = thisCollection.getMembers().list; ArrayList currentPaths = new ArrayList(); // add any missing paths for (CollectionMember newMember : newMembers) { ItemPath itemPath = newMember.getItemPath(); if (!oldMembers.contains(newMember) && itemPath != null) { currentPaths.add(itemPath); NodeItem newMemberNode = new NodeItem(itemPath, desktop); newMemberNode.setCollection(contents, newMember.getID(), parent); newMemberNode.setToolTip(getPropertyToolTip(newMember.getProperties())); add(newMemberNode); } } // remove those no longer present for (Path childPath : childNodes.keySet()) { if (!currentPaths.contains(childPath)) { remove(childPath); } } thisCollection = contents; if (isDependency()) setToolTip(getPropertyToolTip(((Dependency)contents).getProperties())); end(false); } public boolean addMember(ItemPath itemPath) { if (!isDependency()) return false; String[] params = { thisCollection.getName(), itemPath.getUUID().toString() }; try { MainFrame.userAgent.execute(parent, "AddMemberToCollection", params); return true; } catch (Exception e1) { MainFrame.exceptionDialog(e1); return false; } } public static String getPropertyToolTip(CastorHashMap props) { if (props.size() == 0) return null; StringBuffer verStr = new StringBuffer(""); for (KeyValuePair prop : props.getKeyValuePairs()) { verStr.append("").append(prop.getKey()).append(": ").append(prop.getValue()).append("
"); } return verStr.append("").toString(); } @Override public DefaultMutableTreeNode getTreeNode() { return treeNode; } @Override public void remove(String id) { // TODO Auto-generated method stub } @Override public void control(String control, String msg) { // TODO Auto-generated method stub } public boolean isDependency() { return thisCollection instanceof Dependency; } }