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: FileNode.java,v 1.7 2004/08/22 04:18:40 bradford Exp $
 */

import com.dbxml.db.admin.Admin;
import com.dbxml.db.admin.DocWrapper;
import com.dbxml.util.Stopwatch;
import java.awt.Cursor;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 * FileNode
 */

public final class FileNode extends AdminNode implements HasMenu, HasDocWrapper {
   private static final Icon FileIcon = new ImageIcon(FileNode.class.getResource("file_mini.gif"));

   public static final int ACTION_EDIT = 0;
   public static final int ACTION_DELETE = 1;
   public static final int ACTION_XSLT = 2;

   private static final MenuItem[] Menu = new MenuItem[]{
      new MenuItem(ACTION_EDIT, "Open Document"),
      new MenuItem(ACTION_DELETE, "Delete Document"),
      MenuItem.SEPARATOR,
      new MenuItem(ACTION_XSLT, "Select as Stylesheet")
   };

   private String filename;
   private File file;

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

      this.file = file;
      filename = file.getName();
   }

   public String getLabel() {
      return filename;
   }

   public File getFile() {
      return file;
   }

   public Icon getIcon() {
      return FileIcon;
   }

   public DocWrapper getDocWrapper() {
      try {
         FileInputStream fis = new FileInputStream(file);
         BufferedInputStream bis = new BufferedInputStream(fis, 4096);
         InputStreamReader isr = new InputStreamReader(bis, "UTF8");
         char[] c = new char[(int)file.length()];
         int size = isr.read(c);
         isr.close();
         return new DocWrapper(new String(c, 0, size), null, file.getParentFile(), file.getName(), true, true);
      }
      catch ( Exception e ) {
         return null;
      }
   }

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

   public MenuItem[] getMenu() {
      return Menu;
   }

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

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

         case ACTION_EDIT:
            admin.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            Stopwatch sw = new Stopwatch("Document Retrieval", true);
            DocWrapper dw = getDocWrapper();
            sw.stop();
            admin.setStatus(sw.toString());
            admin.editDocument(dw);
            admin.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            break;

         case ACTION_XSLT:
            String name = file.getAbsolutePath();
            admin.xsltViewer.setStylesheet(name, getDocWrapper());
            break;

      }
      return REFRESH_NONE;
   }
}



