ActionMapTreeTableModel.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:4k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: ActionMapTreeTableModel.java,v 1.1 2005/06/15 17:14:32 kleopatra Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  */
  7. package org.jdesktop.demo.swingx.common;
  8. import java.util.ArrayList;
  9. import java.util.Collections;
  10. import java.util.List;
  11. import javax.swing.Action;
  12. import javax.swing.ActionMap;
  13. import javax.swing.JComponent;
  14. import javax.swing.tree.DefaultMutableTreeNode;
  15. import org.jdesktop.swingx.treetable.DefaultTreeTableModel;
  16. /**
  17.  * Convenience TreeTableModel for wrapping an ActionMap hierarchy.
  18.  * 
  19.  * @author Jeanette Winzenburg, Berlin
  20.  */
  21. public class ActionMapTreeTableModel extends DefaultTreeTableModel {
  22.     public ActionMapTreeTableModel(JComponent comp) {
  23.         super(createRootNodeExt(comp), false);
  24.     }
  25.     public Object getChild(Object parent, int index) {
  26.         return ((ActionEntryNode) parent).getChildren().get(index);
  27.     }
  28.     public int getChildCount(Object parent) {
  29.         return ((ActionEntryNode) parent).getChildren().size();
  30.     }
  31.     public int getColumnCount() {
  32.         return 2;
  33.     }
  34.     public String getColumnName(int column) {
  35.         switch (column) {
  36.         case 0:
  37.             return "Key Name";
  38.         case 1:
  39.             return "Action Name";
  40.         case 2:
  41.             return "Action Command";
  42.         default:
  43.             return "Column " + column;
  44.         }
  45.     }
  46.     public Object getValueAt(Object node, int column) {
  47.         ActionEntryNode actionNode = (ActionEntryNode) node;
  48.         switch (column) {
  49.         case 0:
  50.             return actionNode.key;
  51.         case 1:
  52.             if (actionNode.isLeaf())
  53.                 return actionNode.getAction().getValue(Action.NAME);
  54.             return null;
  55.         case 2:
  56.             if (actionNode.isLeaf())
  57.                 return actionNode.getAction().getValue(
  58.                         Action.ACTION_COMMAND_KEY);
  59.         // case 3:
  60.         // return "Modification Date";
  61.         default:
  62.             return null;
  63.         }
  64.     }
  65.     private static ActionEntryNode createRootNodeExt(JComponent comp) {
  66.         ActionMap map = comp.getActionMap();
  67.         if (map == null)
  68.             throw new IllegalArgumentException("Component must have ActionMap");
  69.         List actionMaps = new ArrayList();
  70.         actionMaps.add(map);
  71.         while ((map = map.getParent()) != null) {
  72.             actionMaps.add(0, map);
  73.         }
  74.         return createActionEntryNodes(actionMaps);
  75.     }
  76.     private static ActionEntryNode createActionEntryNodes(List actionMaps) {
  77.         ActionMap topLevel = (ActionMap) actionMaps.get(0);
  78.         ActionEntryNode mapRoot = new ActionEntryNode("topLevel", topLevel);
  79.         ActionEntryNode current = mapRoot;
  80.         for (int i = 1; i < actionMaps.size(); i++) {
  81.             current = current.addActionMapAsChild("childMap " + i,
  82.                     (ActionMap) actionMaps.get(i));
  83.         }
  84.         return mapRoot;
  85.     }
  86.     private static class ActionEntryNode extends DefaultMutableTreeNode {
  87.         Object key;
  88.         Action action;
  89.         ActionMap actionMap;
  90.         List children;
  91.         public ActionEntryNode(Object key, Action action) {
  92.             this.key = key;
  93.             this.action = action;
  94.             children = Collections.EMPTY_LIST;
  95.         }
  96.         public ActionEntryNode(Object key, ActionMap map) {
  97.             this.key = key;
  98.             this.actionMap = map;
  99.             children = new ArrayList();
  100.             Object[] keys = map.keys();
  101.             for (int i = 0; i < keys.length; i++) {
  102.                 children.add(new ActionEntryNode(keys[i], (Action) map
  103.                         .get(keys[i])));
  104.             }
  105.         }
  106.         /**
  107.          * pre: !isLeaf
  108.          * 
  109.          * @param key
  110.          * @param map
  111.          */
  112.         public ActionEntryNode addActionMapAsChild(Object key, ActionMap map) {
  113.             ActionEntryNode actionEntryNode = new ActionEntryNode(key, map);
  114.             getChildren().add(0, actionEntryNode);
  115.             return actionEntryNode;
  116.         }
  117.         public List getChildren() {
  118.             return children;
  119.         }
  120.         public boolean isLeaf() {
  121.             return action != null;
  122.         }
  123.         public ActionMap getActionMap() {
  124.             return actionMap;
  125.         }
  126.         public Action getAction() {
  127.             return action;
  128.         }
  129.         public boolean getAllowsChildren() {
  130.             return !isLeaf();
  131.         }
  132.         public String toString() {
  133.             return key.toString();
  134.         }
  135.     }
  136. }