package com.dbxml.db.admin.components;

/*
 * 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: TextEditor.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.admin.TabbedNodeEditor;
import com.dbxml.db.admin.swing.SyntaxTextArea;
import com.dbxml.xml.dom.DOMHelper;
import com.dbxml.xml.dom.TextWriter;
import java.awt.BorderLayout;
import java.awt.Cursor;
import javax.swing.JPanel;
import org.w3c.dom.Document;

/**
 * TextEditor
 */

public class TextEditor extends JPanel implements TabbedNodeEditor {
   private DocWrapper doc;
   private boolean reset;
   private String value = "";

   private SyntaxTextArea textEdit = new SyntaxTextArea();
   private BorderLayout thisLayout = new BorderLayout();

   public TextEditor() {
      try {
         jbInit();
         textEdit.setText(value);
      }
      catch ( Exception e ) {
         Admin.getInstance().addMessage(e.toString());
      }
   }

   private void jbInit() throws Exception {
      this.setLayout(thisLayout);
      this.add(textEdit, BorderLayout.CENTER);

   }

   public void requestFocus() {
      textEdit.requestFocus();
   }

   public boolean isModified() {
      return !textEdit.getText().equals(value);
   }

   public void reset() {
      reset = true;
   }

   public void setLanguage(String language) {
      textEdit.setLanguage(language);
   }

   public void setDocWrapper(DocWrapper doc) {
      reset = this.doc != doc;
      this.doc = doc;
   }

   public DocWrapper getDocWrapper() {
      return doc;
   }

   public void setEnabled(boolean enabled) {
      super.setEnabled(enabled);
      textEdit.setEnabled(enabled);
   }

   public boolean isEnabled() {
      return textEdit.isEnabled();
   }

   public void activate(boolean readOnly) {
      Admin admin = Admin.getInstance();
      admin.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
      try {
         if ( readOnly == textEdit.isEditable() || reset ) {
            value = doc.getText();
            textEdit.setText(value);
            textEdit.setCaretPosition(0);
            textEdit.setEditable(!readOnly);
         }
      }
      finally {
         admin.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
      }
   }

   public boolean isCutEnabled() {
      String sel = textEdit.getSelectedText();
      return sel != null && sel.length() > 0 && textEdit.isEditable();
   }

   public void cut() {
      textEdit.cut();
   }

   public boolean isCopyEnabled() {
      String sel = textEdit.getSelectedText();
      return sel != null && sel.length() > 0;
   }

   public void copy() {
      textEdit.copy();
   }

   public boolean isPasteEnabled() {
      return textEdit.isEditable();
   }

   public void paste() {
      textEdit.paste();
   }

   public boolean isUndoEnabled() {
      return false;
   }

   public void undo() {
      /** @todo This */
   }

   public void find() {
      /** @todo This */
   }

   public void findNext() {
      /** @todo This */
   }

   public boolean isBeautifyEnabled() {
      return value.length() > 0 && textEdit.getLanguage() == SyntaxTextArea.LANG_XML;
   }

   public void beautify() {
      Admin admin = Admin.getInstance();
      admin.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
      try {
         String text = textEdit.getText();
         Document d = DOMHelper.parseText(text);
         String newText = TextWriter.toPrettyString(d).trim();
         textEdit.setText(newText);
         if ( !textEdit.isEditable() )
            value = newText;
         textEdit.setCaretPosition(0);
      }
      catch ( Exception e ) {
      }
      finally {
         admin.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
      }
   }

   public boolean deactivate() {
      try {
         if ( isModified() ) {
            String text = textEdit.getText();
            doc.setText(text);
         }
         return true;
      }
      catch ( Exception ex ) {
         Admin.getInstance().setStatus("Error parsing Document text - Couldn't save Document");
         return false;
      }
   }
}

