package com.dbxml.db.admin.nodes;

/*
 * dbXML - Native XML Database
 * Copyright (C) 1999-2004  The dbXML Group, L.L.C.
 *
 * 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; either version 2
 * of the License, or (at your option) any later version.
 *
 * 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.
 *
 * $Id: DirectoryNode.java,v 1.5 2004/07/01 02:05:44 bradford Exp $
 */

import com.dbxml.db.admin.Admin;
import com.dbxml.db.admin.AdminConfig;
import com.dbxml.db.admin.DocWrapper;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 * DirectoryNode
 */

public final class DirectoryNode extends AdminNode implements HasMenu, HasChildren, HasStorage {
   private static final Icon FSIcon = new ImageIcon(DirectoryNode.class.getResource("filesystem_mini.gif"));
   private static final Icon DirIcon = new ImageIcon(DirectoryNode.class.getResource("directory_mini.gif"));

   public static final int ACTION_DELETE = 0;
   public static final int ACTION_DETACH = 1;

   private String filename;
   private String label;
   private File file;
   private Icon ico;
   private boolean topLevel;

   public DirectoryNode(AdminNode parentNode, String filename, String label) {
      super(parentNode);

      this.filename = filename;
      this.label = label;
      this.file = new File(filename);
      ico = FSIcon;
      topLevel = true;
   }

   public DirectoryNode(AdminNode parentNode, File file) {
      super(parentNode);

      this.file = file;
      filename = file.getName();
      label = filename;
      ico = DirIcon;
      topLevel = false;
   }

   public String getLabel() {
      return label;
   }

   public Icon getIcon() {
      return ico;
   }

   public File getFile() {
      return file;
   }

   public boolean storeDocWrapper(DocWrapper doc) {
      doc.setFile(file);
      return doc.store();
   }

   public AdminNode getRefreshTarget() {
      return this;
   }

   public AdminNode[] getChildren() {
      try {
         File[] list = file.listFiles(new FileFilter() {
            public boolean accept(File f) {
               return !f.isHidden() && (f.isDirectory() || f.isFile());
            }
         });
         List l = new ArrayList();
         List files = new ArrayList();
         for ( int i = 0; i < list.length; i++ ) {
            if ( list[i].isDirectory() )
               l.add(new DirectoryNode(this, list[i]));
            else
               files.add(new FileNode(this, list[i]));
         }
         l.addAll(files);
         return (AdminNode[])l.toArray(new AdminNode[0]);
      }
      catch ( Exception e ) {
         return new AdminNode[0];
      }
   }

   public String getCanonicalName() {
      return file.getAbsolutePath();
   }

   public MenuItem[] getMenu() {
      if ( topLevel )
         return new MenuItem[] {new MenuItem(ACTION_DETACH, "Detach Filesystem")};
      else
         return new MenuItem[] {new MenuItem(ACTION_DELETE, "Delete Directory")};
   }

   public boolean isParentMenuInherited() {
      return topLevel;
   }

   public String getMenuTitle() {
      return "Directory";
   }

   public int menuAction(int action) {
      Admin admin = Admin.getInstance();

      switch ( action ) {
         case ACTION_DETACH:
            try {
               AdminConfig cfg = admin.getAdminConfig();
               cfg.removeFileSystem(label);
               cfg.save();
               return REMOVE_SELF;
            }
            catch ( Exception e ) {
               admin.addMessage(e.getMessage());
            }
            break;

         case ACTION_DELETE:
            try {
               String title = "Delete Directory";
               String message = "Are you sure you want to delete the\n"
                              + "Directory '"+file.getName()+"'?";
               int res = JOptionPane.showConfirmDialog(admin, message, title, JOptionPane.YES_NO_OPTION);
               if ( res == JOptionPane.YES_OPTION ) {
                  file.delete();
                  return REFRESH_PARENT;
               }
            }
            catch ( Exception e ) {
               admin.addMessage(e.getMessage());
            }
            break;

      }
      return REFRESH_NONE;
   }
}
