package com.dbxml.db.admin.xmleditor;

/*
 * 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: DefaultPopupMenu.java,v 1.2 2004/02/12 00:17:55 bradford Exp $
 */

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import org.w3c.dom.Node;

/**
 * DefaultPopupMenu
 */

final class DefaultPopupMenu extends JPopupMenu implements ActionListener {
   private static final int ACTION_OPEN = 0;
   private static final int ACTION_CLOSE = 1;

   private static final int ACTION_EXPAND = 2;
   private static final int ACTION_COLLAPSE = 3;

   private static final int ACTION_INSERT = 4;
   private static final int ACTION_APPEND = 5;
   private static final int ACTION_ATTRIBUTE = 6;
   private static final int ACTION_DELETE = 7;

   DefaultNodeEditor parent;
   boolean canExpand;
   SourceCallback source;

   JMenuItem mnuOpen = new JMenuItem("Open", ACTION_OPEN);
   JMenuItem mnuClose = new JMenuItem("Close", ACTION_CLOSE);
   JMenuItem mnuExpandAll = new JMenuItem("Expand All", ACTION_EXPAND);
   JMenuItem mnuCollapseAll = new JMenuItem("Collapse All", ACTION_COLLAPSE);
   JMenuItem mnuInsert = new JMenuItem("Insert", ACTION_INSERT);
   JMenuItem mnuAppend = new JMenuItem("Append", ACTION_APPEND);
   JMenuItem mnuAttribute = new JMenuItem("Attribute", ACTION_ATTRIBUTE);
   JMenuItem mnuDelete = new JMenuItem("Delete", ACTION_DELETE);

   public DefaultPopupMenu(DefaultNodeEditor parent, boolean canExpand, SourceCallback source) {
      super();
      this.parent = parent;
      this.source = source;

      if ( canExpand ) {
         JMenuItem m = parent.expanded ? mnuClose : mnuOpen;
         add(m);
         m.addActionListener(this);

         addSeparator();
         add(mnuExpandAll);
         mnuExpandAll.addActionListener(this);
         add(mnuCollapseAll);
         mnuCollapseAll.addActionListener(this);
      }

      if ( canExpand && !parent.owner.readOnly )
         addSeparator();

      if ( !parent.owner.readOnly ) {
         add(mnuInsert);
         mnuInsert.addActionListener(this);
         add(mnuAppend);
         mnuAppend.addActionListener(this);
         add(mnuAttribute);
         mnuAttribute.addActionListener(this);
         addSeparator();
         add(mnuDelete);
         mnuDelete.addActionListener(this);
         mnuDelete.setEnabled(parent.owner.rootEdit != parent);
      }
   }

   public void actionPerformed(ActionEvent e) {
      JMenuItem item = (JMenuItem)e.getSource();
      switch ( item.getMnemonic() ) {
         case ACTION_CLOSE:
            mnuCloseClicked();
            break;

         case ACTION_OPEN:
            mnuOpenClicked();
            break;

         case ACTION_EXPAND:
            mnuExpandAllClicked();
            break;

         case ACTION_COLLAPSE:
            mnuCollapseAllClicked();
            break;

         case ACTION_INSERT:
            mnuInsertClicked();
            break;

         case ACTION_APPEND:
            mnuAppendClicked();
            break;

         case ACTION_ATTRIBUTE:
            mnuAttributeClicked();
            break;

         case ACTION_DELETE:
            mnuDeleteClicked();
            break;
      }
   }

   void mnuCloseClicked() {
      parent.setExpanded(false);
   }

   void mnuOpenClicked() {
      parent.setExpanded(true);
   }

   void mnuExpandAllClicked() {
      parent.expandAll();
   }

   void mnuCollapseAllClicked() {
      parent.collapseAll();
   }

   void mnuInsertClicked() {
      ElementDialog d = new ElementDialog();
      d.show();
   }

   void mnuAppendClicked() {
      ElementDialog d = new ElementDialog();
      d.show();
   }

   void mnuAttributeClicked() {
      AttributeDialog d = new AttributeDialog();
      d.show();
   }

   void mnuDeleteClicked() {
      Node n = parent.node;
      Node pn = n.getParentNode();
      pn.removeChild(n);
      parent.parent.setNode(parent.parent.getNode());
      parent.owner.modified = true;
   }
}
