001: /*
002: * Created on 06.08.2003
003: *
004: * To change the template for this generated file go to
005: * Window>Preferences>Java>Code Generation>Code and Comments
006: */
007: package org.columba.core.gui.plugin;
008:
009: import java.util.Enumeration;
010: import java.util.HashMap;
011: import java.util.Map;
012:
013: import javax.swing.table.TableColumn;
014: import javax.swing.tree.DefaultTreeModel;
015: import javax.swing.tree.TreeNode;
016: import javax.swing.tree.TreePath;
017:
018: import org.columba.api.plugin.PluginMetadata;
019: import org.columba.core.plugin.PluginManager;
020: import org.frapuccino.treetable.Tree;
021: import org.frapuccino.treetable.TreeTable;
022:
023: /**
024: * TreeTable component responsible for displaying plugins in a categorized way.
025: *
026: * Additionally shows plugin version information, the plugin description as
027: * tooltip.
028: *
029: * The third column is a checkbox to enable/disable the plugin.
030: *
031: * @author fdietz
032: */
033: public class PluginTree extends TreeTable {
034:
035: final static String[] columns = { "Description", "Version",
036: "Enabled" };
037:
038: final static String[] CATEGORIES = { "Look and Feel", "Filter",
039: "Filter Action", "Spam", "Mail Import",
040: "Addressbook Import", "Interpreter Language", "Examples",
041: "Uncategorized" };
042:
043: protected Map map;
044:
045: protected PluginTreeTableModel model;
046:
047: /**
048: *
049: */
050: public PluginTree() {
051: super ();
052:
053: map = new HashMap();
054:
055: model = new PluginTreeTableModel(columns);
056: model.setTree((Tree) getTree());
057:
058: // ((DefaultTreeModel)
059: // model.getTree().getModel()).setAsksAllowsChildren(true);
060:
061: initTree();
062:
063: setModel(model);
064:
065: getTree().setCellRenderer(new DescriptionTreeRenderer());
066:
067: // make "version" column fixed size
068: TableColumn tc = getColumn(columns[1]);
069: tc.setCellRenderer(new VersionRenderer());
070: tc.setMaxWidth(80);
071: tc.setMinWidth(80);
072:
073: // make "enabled" column fixed size
074: tc = getColumn(columns[2]);
075: tc.setCellRenderer(new EnabledRenderer());
076: tc.setCellEditor(new EnabledEditor());
077:
078: tc.setMaxWidth(80);
079: tc.setMinWidth(80);
080:
081: }
082:
083: public void addPlugin(PluginMetadata metadata) {
084: // plugin wasn't correctly loaded
085: if (metadata == null) {
086: return;
087: }
088:
089: String category = metadata.getCategory();
090:
091: if (category == null) {
092: // this plugin doesn't define a category to which it belongs
093: category = "Uncategorized";
094: }
095:
096: PluginNode childNode = new PluginNode();
097: childNode.setCategory(false);
098: childNode.setId(metadata.getId());
099: childNode.setTooltip(metadata.getDescription());
100: childNode.setVersion(metadata.getVersion());
101:
102: boolean enabled = metadata.isEnabled();
103:
104: childNode.setEnabled(enabled);
105: childNode.setAllowsChildren(false);
106:
107: PluginNode node = (PluginNode) map.get(category);
108:
109: if (node == null) {
110: // unknown category found
111: // -> just add this plugin to "Uncategorized"
112: category = "Uncategorized";
113: node = (PluginNode) map.get(category);
114: }
115:
116: // add node
117: node.add(childNode);
118:
119: // notify tree model
120: ((DefaultTreeModel) getTree().getModel())
121: .nodeStructureChanged(node);
122:
123: // make new node visible
124: getTree().expandPath(new TreePath(childNode));
125:
126: }
127:
128: public void initTree() {
129: PluginNode root = new PluginNode();
130: root.setId("root");
131:
132: initCategories(root);
133:
134: Enumeration e = PluginManager.getInstance()
135: .getPluginMetadataEnumeration();
136: while (e.hasMoreElements()) {
137: PluginMetadata metadata = (PluginMetadata) e.nextElement();
138: addPlugin(metadata);
139: }
140:
141: model.set(root);
142: }
143:
144: protected void initCategories(PluginNode root) {
145: for (int i = 0; i < CATEGORIES.length; i++) {
146: String c = CATEGORIES[i];
147: PluginNode node = new PluginNode();
148: node.setAllowsChildren(true);
149: node.setId(c);
150: node.setEnabled(true);
151: node.setCategory(true);
152: root.add(node);
153: map.put(c, node);
154: }
155: }
156:
157: public void removePluginNode(PluginNode node) {
158: TreeNode parent = node.getParent();
159:
160: // notify tree
161: node.removeFromParent();
162:
163: // update tree model
164: ((DefaultTreeModel) getTree().getModel())
165: .nodeStructureChanged(parent);
166:
167: }
168: }
|