PluginTree.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.gui.plugin |
![]() |
![]() |
XNap 3 |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* XNap - A P2P framework and client.
*
* See the file AUTHORS for copyright information.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.xnap.gui.plugin;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.StringTokenizer;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import org.xnap.XNap;
import org.xnap.gui.util.IconHelper;
import org.xnap.pkg.DefaultPackageSection;
import org.xnap.pkg.PackageInfo;
import org.xnap.pkg.PackageSection;
import org.xnap.pkg.XNapPackageManager;
import org.xnap.util.StringHelper;
public class PluginTree extends JTree {
//--- Data field(s) ---
private DefaultMutableTreeNode root;
private DefaultTreeModel model;
private Hashtable nodeBySectionName = new Hashtable();
private Hashtable sectionByName = new Hashtable();
//--- Constructor(s) ---
public PluginTree()
{
root = new DefaultMutableTreeNode(XNap.tr("Plugins"));
model = new DefaultTreeModel(root);
setModel(model);
setRootVisible(false);
mapSection("net", XNap.tr("Network"), "network.png");
mapSection("chat", XNap.tr("Chat"), "mail_generic2.png");
mapSection("laf", XNap.tr("Look and Feel"), "list.png");
mapSection("lib", XNap.tr("Library"));
}
//--- Method(s) ---
public void createPackageNode(DefaultMutableTreeNode parent,
PackageInfo info)
{
String section = "/" + info.getSection() + "/" + info.getName();
DefaultMutableTreeNode node
= (DefaultMutableTreeNode)nodeBySectionName.get(section);
if (node == null) {
node = new PluginNode(info);
nodeBySectionName.put(section, node);
parent.add(node);
}
else if (node instanceof PluginNode) {
PluginNode n = (PluginNode)node;
n.setMarked(false);
// always set the highest available version
// info must be a higher version, since the packages
// are sorted by lower version first
n.setPackageInfo(info);
// default to install for updated packages
if (n.isUpdateAvailable() || n.getPluginInfo() != null) {
n.setAction(PackageInfo.ACTION_INSTALL);
}
}
}
/**
* Creates a section node and returns it.
*/
public DefaultMutableTreeNode createSectionNodes(String sectionName)
{
DefaultMutableTreeNode parent = root;
StringBuffer totalSection = new StringBuffer(sectionName.length());
StringTokenizer t = new StringTokenizer(sectionName, "/");
while (t.hasMoreTokens()) {
totalSection.append("/");
totalSection.append(t.nextToken());
DefaultMutableTreeNode node
= getNodeBySection(parent, totalSection.toString());
parent = node;
}
return parent;
}
private DefaultMutableTreeNode getNodeBySection
(DefaultMutableTreeNode parent, String section)
{
DefaultMutableTreeNode node
= (DefaultMutableTreeNode)nodeBySectionName.get(section);
if (node == null) {
String s = StringHelper.lastToken(section, "/");
PackageSection ps = (PackageSection)sectionByName.get(s);
if (ps == null) {
ps = new DefaultPackageSection(StringHelper.toFirstUpper(s),
null);
sectionByName.put(s, ps);
}
node = new DefaultMutableTreeNode(ps);
nodeBySectionName.put(section, node);
parent.add(node);
}
return node;
}
public DefaultMutableTreeNode getRoot()
{
return root;
}
public PluginNode getSelectedNode()
{
if (getSelectionPath() != null) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)
getSelectionPath().getLastPathComponent();
if (node instanceof PluginNode) {
return (PluginNode)node;
}
}
return null;
}
public PackageSection mapSection(String sectionName, String name,
String iconFilename)
{
PackageSection section = new DefaultPackageSection
(name, (iconFilename != null)
? IconHelper.getIcon(iconFilename, 16, false)
: null);
sectionByName.put(sectionName, section);
return section;
}
public PackageSection mapSection(String sectionName, String name)
{
return mapSection(sectionName, name, null);
}
public void pathChanged(TreePath path)
{
model.nodeChanged((TreeNode)path.getLastPathComponent());
}
public void reload()
{
model.reload();
// expand important nodes
expandRow(0);
expandNodes(root.getChildAt(0));
expandNodes(root.getChildAt(1));
}
public void remove(PluginNode node)
{
PackageInfo info = node.getPackageInfo();
String section = "/" + info.getSection() + "/" + info.getName();
((DefaultMutableTreeNode)node.getParent()).remove(node);
nodeBySectionName.remove(section);
}
/**
* Rebuilds the tree from the packages contained in XNapPackageManager.
*/
public void update()
{
// create default section
createSectionNodes("plugin");
createSectionNodes("base");
// remove packages that are not available anymore
Enumeration it = root.depthFirstEnumeration();
while (it.hasMoreElements()) {
Object o = it.nextElement();
if (o instanceof PluginNode) {
PluginNode node = (PluginNode)o;
node.setMarked(true);
}
}
// add all packages
for (Iterator it2 = XNapPackageManager.getInstance().packages();
it2.hasNext();) {
PackageInfo info = (PackageInfo)it2.next();
if (info.isPlugin() || info.isBase()) {
DefaultMutableTreeNode parent
= createSectionNodes(info.getSection());
createPackageNode(parent, info);
}
}
// remove packages that are not available anymore
LinkedList toRemove = new LinkedList();
it = root.depthFirstEnumeration();
while (it.hasMoreElements()) {
Object o = it.nextElement();
if (o instanceof PluginNode) {
PluginNode node = (PluginNode)o;
if (node.isMarked()) {
toRemove.add(node);
}
}
}
for (Iterator it2 = toRemove.iterator(); it2.hasNext();) {
remove((PluginNode)it2.next());
}
// update tree
reload();
}
public void selectedNodeChanged()
{
model.nodeChanged
((TreeNode)getSelectionPath().getLastPathComponent());
}
private void expandNodes(TreeNode child)
{
expandPath(new TreePath(new Object[] { root, child }));
for (int i = child.getChildCount() - 1; i >= 0; i--) {
Object[] path
= new Object[] { root, child, child.getChildAt(i) };
expandPath(new TreePath(path));
}
}
}
The table below shows all metrics for PluginTree.java.




