LibraryTree.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.gui.tree |
![]() |
![]() |
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.tree;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.*;
import javax.swing.JPopupMenu;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import org.apache.log4j.Logger;
import org.xnap.XNap;
import org.xnap.gui.*;
import org.xnap.gui.StatusBar;
import org.xnap.gui.action.EnqueueDirectoryAction;
import org.xnap.gui.action.PlayDirectoryAction;
import org.xnap.gui.component.XNapMenuItem;
import org.xnap.gui.event.PopupListener;
import org.xnap.gui.util.*;
import org.xnap.search.MediaType;
import org.xnap.search.SearchManager;
import org.xnap.util.Preferences;
/**
* LibraryTree handles all things having to do with the tree on the left side
* of the {@link org.xnap.gui.LibraryPanel}.
*/
public class LibraryTree extends DroppableTree
implements PropertyChangeListener, DirectoryProvider
{
//--- Constant(s) ---
//--- Data field(s) ---
private FileTreeModel ftm;
private JPopupMenu popup = new JPopupMenu();
private Preferences prefs = Preferences.getInstance();
private static Logger logger = Logger.getLogger(LibraryTree.class);
//--- Constructor(s) ---
public LibraryTree(Component parent)
{
super(new FileTreeModel(XNap.tr("Files")), parent);
ftm = (FileTreeModel)getModel();
initialize();
prefs.addPropertyChangeListener(this);
}
//--- Method(s) ---
private void initialize()
{
setCellRenderer(new FileCellRenderer());
putClientProperty("JTree.lineStyle", "Angled");
// popup menu
popup.add(new XNapMenuItem(new PlayDirectoryAction(this)));
popup.add(new XNapMenuItem(new EnqueueDirectoryAction(this)));
popup.addSeparator();
popup.add(new XNapMenuItem(new ShareDirectoryAction()));
popup.add(new XNapMenuItem(new UnshareDirectoryAction()));
popup.add(new XNapMenuItem(new UnshareAllDirectoriesAction()));
popup.addSeparator();
popup.add(new XNapMenuItem(new AddToLibraryAction()));
popup.add(new XNapMenuItem(new RemoveFromLibraryAction()));
popup.addSeparator();
popup.add(new XNapMenuItem(new ReloadTreeAction()));
addMouseListener(new PopupListener(popup));
// allow multiple selections in tree
getSelectionModel().setSelectionMode
(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
updateTree();
}
/**
* Adds the main download directory and all configured media type download
* directories to the tree.
*/
private void addDownloadDirs()
{
// add download dir
ftm.addChildOfSubRoot(new File(prefs.getDownloadDir()),
XNap.tr("Downloaded Files"));
MediaType[] types = SearchManager.DEFAULT_MEDIA_TYPES;
for (int i = 1; i < types.length; i++) {
String dir = prefs.getMediaTypeDownloadDir(types[i].getRealm());
if (dir.length() > 0) {
ftm.addChildOfSubRoot(new File(dir),
XNap.tr("Downloaded Files"),
" (" + types[i].getName() + ")");
}
}
}
/**
* Returns the menu which pops up over the tree.
*/
public JPopupMenu getPopupMenu()
{
return popup;
}
/**
* Returns the currently selected directory.
*
* Implements the {@link DirectoryProvider} interface.
*/
public File getDirectory()
{
TreePath selected = getSelectionPath();
if (selected != null) {
Object o = selected.getLastPathComponent();
if (o instanceof File) {
return (File)o;
}
}
return null;
}
/**
* Implements the {@link DirectoryProvider} interface.
*
* Since we don't care for the content of the directories we don't do
* anything here.
*/
public void hasChanged(File directory)
{
}
/**
* Implements the {@link PropertyChangeListener} interface.
*/
public void propertyChange(PropertyChangeEvent e)
{
String p = e.getPropertyName();
if (e.getSource() == prefs) {
if (p.equals("incompleteDir")) {
ftm.removeChildrenOfSubRoot(XNap.tr("Incomplete Files"));
ftm.addChildOfSubRoot(new File(prefs.getIncompleteDir()),
XNap.tr("Incomplete Files"));
}
else if (p.equals("libraryDirs")) {
ftm.removeChildrenOfSubRoot(XNap.tr("Library Directories"));
String[] dirs = prefs.getLibraryDirs();
for (int i = 0; i < dirs.length; i++) {
ftm.addChildOfSubRoot(new File(dirs[i]),
XNap.tr("Library Directories"));
}
}
else if (p.equals("uploadDirs")) {
ftm.removeChildrenOfSubRoot(XNap.tr("Shared Directories"));
String[] dirs = prefs.getUploadDirs();
for (int i = 0; i < dirs.length; i++) {
ftm.addChildOfSubRoot(new File(dirs[i]),
XNap.tr("Shared Directories"));
}
}
else if (p.equals("downloadDir") || p.indexOf("DownloadDir")
!= -1) {
ftm.removeChildrenOfSubRoot(XNap.tr("Downloaded Files"));
addDownloadDirs();
}
}
}
/**
* Clears the tree and adds a couple of nodes.
*/
public void updateTree()
{
ftm.removeSubRoots();
int failed = 0;
String[] nodes = prefs.getLibraryTreeNodesArray();
for (int j = 0; j < nodes.length; j++) {
if (nodes[j].equals("library")) {
// add library
ftm.addSubRoot(XNap.tr("Library Directories"));
String[] dirs = prefs.getLibraryDirs();
for (int i = 0; i < dirs.length; i++) {
ftm.addChildOfSubRoot(new File(dirs[i]),
XNap.tr("Library Directories"));
}
expandRow(getRowCount() - 1);
}
else if (nodes[j].equals("shares")) {
// add shares
ftm.addSubRoot(XNap.tr("Shared Directories"));
String[] dirs = prefs.getUploadDirs();
for (int i = 0; i < dirs.length; i++) {
ftm.addChildOfSubRoot(new File(dirs[i]),
XNap.tr("Shared Directories"));
}
expandRow(getRowCount() - 1);
}
else if (nodes[j].equals("incompletes")) {
// add incomplete dir
ftm.addChildOfSubRoot(new File(prefs.getIncompleteDir()),
XNap.tr("Incomplete Files"));
// ensure that the actual incomplete directory is viewable
expandRow(getRowCount() - 1);
}
else if (nodes[j].equals("downloads")) {
addDownloadDirs();
expandRow(getRowCount() - 1);
}
else if (nodes[j].equals("home")) {
ftm.addChildOfSubRoot(new File
(System.getProperty("user.home")),
XNap.tr("Home Directory"));
expandRow(getRowCount() - 1);
}
else if (nodes[j].equals("root")) {
// add system roots
File[] roots = File.listRoots();
for (int i = 0; i < roots.length; i++) {
ftm.addChildOfSubRoot(roots[i], XNap.tr("Root Directory"));
}
expandRow(getRowCount() - 1);
}
else {
failed++;
}
}
if (failed == nodes.length) {
// always show at least system roots
ftm.addSubRoot(XNap.tr("Root Directory"));
File[] roots = File.listRoots();
for (int i = 0; i < roots.length; i++) {
ftm.addChildOfSubRoot(roots[i], XNap.tr("Root Directory"));
}
expandRow(getRowCount() - 1);
}
}
/**
* Adds selected directories to the {@link org.xnap.gui.LibraryPanel}.
*/
private class AddToLibraryAction extends AbstractAction
{
public AddToLibraryAction()
{
putValue(Action.NAME, XNap.tr("Add Directory To Library"));
putValue(Action.SHORT_DESCRIPTION,
XNap.tr("Add selected directories to the Library."));
putValue(IconHelper.XNAP_ICON, "edit_add.png");
}
public void actionPerformed(ActionEvent event)
{
TreePath[] selected = getSelectionPaths();
if (selected == null)
return;
List dirs = new LinkedList(Arrays.asList(prefs.getLibraryDirs()));
for (int i = 0; i < selected.length; i++) {
Object o = selected[i].getLastPathComponent();
if (o instanceof FileNode) {
String path = ((FileNode)o).getAbsolutePath();
if (!dirs.contains(path)) {
dirs.add(path);
}
else {
StatusBar.setText(XNap.tr("\"{0}\" is already in Library",
o));
}
}
else {
StatusBar.setText(XNap.tr("\"{0}\" is not a directory",
o));
}
}
prefs.setLibraryDirs((String[])dirs.toArray(new String[0]));
}
}
/**
* Remove selected directories from the {@link org.xnap.gui.LibraryPanel}.
*/
private class RemoveFromLibraryAction extends AbstractAction
{
public RemoveFromLibraryAction()
{
putValue(Action.NAME, XNap.tr("Remove Directory From Library"));
putValue(Action.SHORT_DESCRIPTION,
XNap.tr("Remove selected directories from the Library."));
putValue(IconHelper.XNAP_ICON, "edit_remove.png");
}
public void actionPerformed(ActionEvent event)
{
TreePath[] selected = getSelectionPaths();
if (selected == null)
return;
List dirs = new LinkedList(Arrays.asList(prefs.getLibraryDirs()));
for (int i = 0; i < selected.length; i++) {
Object o = selected[i].getLastPathComponent();
if (o instanceof FileNode) {
if (!dirs.remove(((FileNode)o).getAbsolutePath())) {
StatusBar.setText(XNap.tr("Could not remove \"{0}\"",
o));
}
}
}
prefs.setLibraryDirs((String[])dirs.toArray(new String[0]));
}
}
/**
* Share selected directories.
*/
private class ShareDirectoryAction extends AbstractAction
{
public ShareDirectoryAction()
{
putValue(Action.NAME, XNap.tr("Share Directory"));
putValue(Action.SHORT_DESCRIPTION,
XNap.tr("Shares selected directory."));
putValue(IconHelper.XNAP_ICON, "connect_established.png");
}
public void actionPerformed(ActionEvent event)
{
int response = Dialogs.showConfirmDialog
(XNapFrame.getInstance(), "ShareFolder",
XNap.tr("Share Directory"),
XNap.tr("Please note: Directories are shared <b>recursively</b>, all subdirectories and their content may be publicly accessible to anyone. <br><p><b>Please make sure you act in accordance to your local copyright law when sharing data.</b>"),
JOptionPane.OK_CANCEL_OPTION, prefs);
if (response == JOptionPane.CANCEL_OPTION) {
return;
}
TreePath[] selected = getSelectionPaths();
if (selected == null)
return;
List dirs = new LinkedList(Arrays.asList(prefs.getUploadDirs()));
for (int i = 0; i < selected.length; i++) {
Object o = selected[i].getLastPathComponent();
if (o instanceof FileNode) {
String path = ((FileNode)o).getAbsolutePath();
if (!dirs.contains(path)) {
dirs.add(path);
}
else {
StatusBar.setText(XNap.tr("\"{0}\" is already shared",
o));
}
}
else {
StatusBar.setText(XNap.tr("\"{0}\" is not a directory",
o));
}
}
prefs.setUploadDirs((String[])dirs.toArray(new String[0]));
}
}
/**
* Unshare selected directories.
*/
private class UnshareDirectoryAction extends AbstractAction
{
public UnshareDirectoryAction()
{
putValue(Action.NAME, XNap.tr("Unshare Directory"));
putValue(Action.SHORT_DESCRIPTION,
XNap.tr("Unshares selected directory."));
putValue(IconHelper.XNAP_ICON, "connect_no.png");
}
public void actionPerformed(ActionEvent event)
{
TreePath[] selected = getSelectionPaths();
if (selected == null)
return;
List dirs = new LinkedList(Arrays.asList(prefs.getUploadDirs()));
for (int i = 0; i < selected.length; i++) {
Object o = selected[i].getLastPathComponent();
if (o instanceof FileNode) {
if (!dirs.remove(((FileNode)o).getAbsolutePath())) {
StatusBar.setText(XNap.tr("Could not unshare \"{0}\".",
o));
}
}
}
prefs.setUploadDirs((String[])dirs.toArray(new String[0]));
}
}
/**
* Unshare selected directories.
*/
private class UnshareAllDirectoriesAction extends AbstractAction
{
public UnshareAllDirectoriesAction()
{
putValue(Action.NAME, XNap.tr("Unshare All Directories"));
putValue(Action.SHORT_DESCRIPTION,
XNap.tr("Unshares all directories."));
putValue(IconHelper.XNAP_ICON, "connect_no.png");
}
public void actionPerformed(ActionEvent event)
{
prefs.setUploadDirs(new String[0]);
}
}
/**
* Refresh file and directory listings
*/
private class ReloadTreeAction extends AbstractAction
{
public ReloadTreeAction()
{
putValue(Action.NAME, XNap.tr("Reload Tree"));
putValue(Action.SHORT_DESCRIPTION,
XNap.tr("Reload Tree"));
putValue(IconHelper.XNAP_ICON, "reload.png");
}
public void actionPerformed(ActionEvent event)
{
ftm.reload();
}
}
}
The table below shows all metrics for LibraryTree.java.




