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: DocumentNode.java,v 1.5 2004/02/12 00:17:54 bradford Exp $
 */

import com.dbxml.db.admin.Admin;
import com.dbxml.db.admin.DocWrapper;
import com.dbxml.db.client.CollectionClient;
import com.dbxml.util.Stopwatch;
import com.dbxml.util.dbXMLException;
import java.awt.Cursor;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 * DocumentNode
 */

public final class DocumentNode extends AdminNode implements HasCollection, HasMenu, HasDocWrapper {
   private static final Icon DocumentIcon = new ImageIcon(DocumentNode.class.getResource("doc_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 CollectionClient col;
   private String id;

   public DocumentNode(AdminNode parentNode, CollectionClient col, String id) {
      super(parentNode);

      this.col = col;
      this.id = id;
   }

   public String getLabel() {
      return id;
   }

   public Icon getIcon() {
      return DocumentIcon;
   }

   public CollectionClient getCollection() {
      return col;
   }

   public DocWrapper getDocWrapper() {
      try {
         if ( col.getCollectionType() == CollectionClient.TYPE_DOCUMENTS )
            return new DocWrapper(col.getDocumentAsText(id), col, null, id, true);
         else {
            return new DocWrapper(new String(col.getValue(id)), col, null, id, true, true);
         }
      }
      catch ( Exception e ) {
         Admin.getInstance().addMessage(e.toString());
         return null;
      }
   }

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

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

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

      switch ( action ) {
         case ACTION_DELETE:
            try {
               String title = "Delete Document";
               String message = "Are you sure you want to delete the\n"
                              + "Document '"+id+"'?";
               int res = JOptionPane.showConfirmDialog(Admin.getInstance(), message, title, JOptionPane.YES_NO_OPTION);
               if ( res == JOptionPane.YES_OPTION ) {
                  col.remove(id);
                  return REMOVE_SELF;
               }
            }
            catch ( Exception e ) {
               Admin.getInstance().addMessage(e.toString());
            }
            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:
            try {
               String name = col.getCanonicalName() + '/' + id;
               admin.xsltViewer.setStylesheet(name, getDocWrapper());
            }
            catch ( dbXMLException e ) {
               Admin.getInstance().addMessage(e.getMessage());
            }
            break;

      }
      return REFRESH_NONE;
   }
}


