001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.swing;
024:
025: import java.util.Iterator;
026: import java.util.Map;
027:
028: import javax.swing.table.DefaultTableModel;
029: import javax.swing.table.TableModel;
030: import javax.swing.tree.DefaultMutableTreeNode;
031: import javax.swing.tree.MutableTreeNode;
032:
033: import biz.hammurapi.swing.CompositeVisualizer.Member;
034:
035: /**
036: * @author Pavel Vlasov
037: *
038: * @version $Revision: 1.1 $
039: */
040: public class MapVisualizer implements Member {
041:
042: private CompositeVisualizer owner;
043:
044: public Visualizable toVisualizable(final Map map) {
045: return new Visualizable() {
046:
047: public MutableTreeNode toTree(final String title) {
048: DefaultMutableTreeNode ret = new DefaultMutableTreeNode(
049: map) {
050: public String toString() {
051: return title + " [" + map.getClass().getName()
052: + "] size=" + map.size();
053: }
054: };
055:
056: Iterator it = map.entrySet().iterator();
057: while (it.hasNext()) {
058: Visualizable ev = owner.getStackHead()
059: .toVisualizable(it.next());
060: if (ev != null) {
061: ret.add(ev.toTree("entry"));
062: }
063: }
064:
065: return ret;
066: }
067:
068: public TableModel toTable() {
069: DefaultTableModel tm = new DefaultTableModel(2, 2);
070: tm.setColumnIdentifiers(new String[] { "Property",
071: "Value" });
072: tm.setValueAt("type", 0, 0);
073: tm.setValueAt(map.getClass().getName(), 0, 1);
074: tm.setValueAt("size", 1, 0);
075: tm.setValueAt(String.valueOf(map.size()), 1, 1);
076:
077: return tm;
078: }
079: };
080: }
081:
082: public Visualizable toVisualizable(final Map.Entry entry) {
083: return new Visualizable() {
084:
085: public MutableTreeNode toTree(final String title) {
086: DefaultMutableTreeNode ret = new DefaultMutableTreeNode(
087: entry) {
088: public String toString() {
089: return title;
090: }
091: };
092:
093: Visualizable kv = owner.getStackHead().toVisualizable(
094: entry.getKey());
095: ret.add(kv.toTree("key"));
096:
097: Visualizable vv = owner.getStackHead().toVisualizable(
098: entry.getValue());
099: ret.add(vv.toTree("value"));
100:
101: return ret;
102: }
103:
104: public TableModel toTable() {
105: DefaultTableModel tm = new DefaultTableModel(2, 3);
106: tm.setColumnIdentifiers(new String[] { "Member",
107: "Type", "Value" });
108: tm.setValueAt("key", 0, 0);
109: tm.setValueAt(entry.getKey() == null ? "(null)" : entry
110: .getKey().getClass().getName(), 0, 1);
111: tm.setValueAt(entry.getKey() == null ? "(null)" : entry
112: .getKey(), 0, 2);
113:
114: tm.setValueAt("value", 1, 0);
115: tm.setValueAt(entry.getValue() == null ? "(null)"
116: : entry.getValue().getClass().getName(), 1, 1);
117: tm.setValueAt(entry.getValue() == null ? "(null)"
118: : entry.getValue(), 1, 2);
119:
120: return tm;
121: }
122: };
123: }
124:
125: /**
126: * Callback method
127: */
128: public void setOwner(CompositeVisualizer owner) {
129: this.owner = owner;
130: }
131: }
|