Package share.folder

Source Code of share.folder.FolderManager

package share.folder;


import java.util.Collection;
import java.util.Timer;
import java.util.TimerTask;

import main.settings.Settings;

import share.folder.tree.ThreadedTreeManager;
import share.folder.tree.node.AbstractFolderTreeNode;
import ui.tabs.share.SharedFolderTableModel;


public class FolderManager extends TimerTask
{
  private ThreadedTreeManager treeManager;
  private SharedFolderTableModel sharedFolderTableModel;

  public FolderManager()
  {
    treeManager = new ThreadedTreeManager();
    sharedFolderTableModel = new SharedFolderTableModel(this);

    if (treeManager.readTreeFromFile())
    {
      updateTableModel();
    }
    else
    {
      shareDownloadFolder();
    }

    Timer timer = new Timer("Scheduler: update folders");
    timer.scheduleAtFixedRate(this, 0, Settings.share.update_rate);
  }

  private void updateTableModel()
  {
    for (AbstractFolderTreeNode node : treeManager.getTreeRoots())
    {
      sharedFolderTableModel.addFolder(node.getFolder(), node.includesSubfolders());
    }
  }

  private void shareDownloadFolder()
  {
    try
    {
      addFolder(Settings.share.folder.download, true);
    }
    catch (FolderNotShareableException e)
    {
      System.out.println("Unable to share download folder");
    }
  }

  public void run()
  {
    treeManager.updateFolders();
  }

  public void addFolder(String path, boolean subfolders)
      throws FolderNotShareableException
  {
    Folder newFolder = new Folder(path);
    if (!newFolder.isShareable())
    {
      throw new FolderNotShareableException();
    }

    if (isDirectoryAlreadyShared(newFolder))
    {
      return;
    }

    sharedFolderTableModel.addFolder(newFolder, subfolders);
    treeManager.addFolder(newFolder, subfolders);
  }

  private boolean isDirectoryAlreadyShared(Folder directory)
  {
    Collection<AbstractFolderTreeNode> treeRoots = treeManager.getTreeRoots();
    for (AbstractFolderTreeNode node : treeRoots)
    {
      if (node.isFolderParentOf(directory))
      {
        return true;
      }

      if (node.getFolder().equals(directory))
      {
        return true;
      }
    }

    return false;
  }

  public void changeFolderMode(Folder folder, boolean modeFlag)
  {
    for (AbstractFolderTreeNode node : treeManager.getTreeRoots())
    {
      if (node.getFolder().equals(folder))
      {
        sharedFolderTableModel.changeFolderMode(folder, modeFlag);
        treeManager.changeFolderMode(folder, modeFlag);

        return;
      }
    }
  }

  public void changeFolder(String oldPath, String newPath)
  {
    Folder oldFolder = new Folder(oldPath);
    for (int i = 0; i < treeManager.getTreeRoots().size(); i++)
    {
      AbstractFolderTreeNode node = treeManager.getTreeRoots().get(i);
      if (node.getFolder().equals(oldFolder))
      {
        Folder newFolder = new Folder(newPath);
        sharedFolderTableModel.changeFolder(oldFolder, newFolder);

        treeManager.removeFolder(oldFolder);
        treeManager.addFolder(newFolder, !node.includesSubfolders());

        return;
      }
    }
  }

  public void removeFolder(String path)
  {
    Folder folder = new Folder(path);
    sharedFolderTableModel.removeFolder(folder);
    treeManager.removeFolder(folder);
  }

  public SharedFolderTableModel getTableModel()
  {
    return sharedFolderTableModel;
  }

}
TOP

Related Classes of share.folder.FolderManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.