001 /* 002 @license.text@ 003 */ 004 package biz.hammurapi.swing; 005 006 import java.util.ArrayList; 007 import java.util.Collection; 008 import java.util.Iterator; 009 import java.util.List; 010 011 import javax.swing.table.DefaultTableModel; 012 import javax.swing.table.TableModel; 013 import javax.swing.tree.TreeNode; 014 015 import biz.hammurapi.convert.Converter; 016 017 /** 018 * @author Pavel Vlasov 019 * 020 * @version $Revision: 1.1 $ 021 */ 022 public class CollectionVisualizer { 023 024 public Visualizable convert(final Collection col, final Converter master) { 025 return new Visualizable() { 026 027 public TreeNode toTreeNode(TreeNode parent, String title) { 028 return new LazyTreeNodeTableVisualizable(parent, title) { 029 030 protected List loadChildren() { 031 ArrayList ret = new ArrayList(); 032 Iterator it = col.iterator(); 033 for (int i=0; it.hasNext(); ++i) { 034 Object next = it.next(); 035 String cName; 036 if (next==null) { 037 cName = "(null)"; 038 } else { 039 String nName = next.getClass().getName(); 040 int idx = nName.lastIndexOf("."); 041 cName = idx==-1 ? nName : nName.substring(idx+1); 042 } 043 Visualizable vs = (Visualizable) master.convert(next, Visualizable.class, null); 044 ret.add(vs.toTreeNode(this, cName)); 045 } 046 return ret; 047 } 048 049 public TableModel toTable() { 050 DefaultTableModel tm=new DefaultTableModel(2,2); 051 tm.setColumnIdentifiers(new String[] {"Property", "Value"}); 052 tm.setValueAt("type", 0, 0); 053 tm.setValueAt(col.getClass().getName(), 0, 1); 054 tm.setValueAt("size", 1, 0); 055 tm.setValueAt(String.valueOf(col.size()), 1, 1); 056 057 return tm; 058 } 059 060 }; 061 } 062 063 }; 064 } 065 }