/*
 * JRTextExpressionArea.java
 *
 * iReport  --  Visual designer for generating JasperReports Documents
 *  Copyright (C) 2002-2003  Giulio Toffoli gt@businesslogic.it
 *
 *  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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Giulio Toffoli
 *  Via T.Aspetti, 233
 *  35100 Padova ITALY
 *  gt@businesslogic.it
 *
 * Created on 28 aprile 2003, 22.55
 */

package it.businesslogic.ireport.gui;
import org.syntax.jedit.*;
import org.syntax.jedit.tokenmarker.*;

import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;

import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import it.businesslogic.ireport.util.I18n;
/**
 *
 * @author  Administrator
 */
public class JRTextExpressionArea extends JEditTextArea {
    
    private int spessore = 0;
    protected boolean viewScrollbars = true;
    
    private javax.swing.JPopupMenu jPopupMenuTextField;
    private javax.swing.JMenuItem jMenuItemTextEditor;
    private javax.swing.JSeparator jSeparator8;
    private javax.swing.JMenuItem jMenuItemCut;
    private javax.swing.JMenuItem jMenuItemCopy;
    private javax.swing.JMenuItem jMenuItemPaste;
    private javax.swing.JMenuItem jMenuItemSelectAll;
    private javax.swing.JSeparator jSeparator9;
    private javax.swing.JMenu jMenuFields;
    private javax.swing.JMenu jMenuVariables;
    private javax.swing.JMenu jMenuParameters;
    
    
    /** Creates a new instance of JRTextExpressionArea */
    public JRTextExpressionArea() {
        super();
        
        

        painter.setEOLMarkersPainted(false);
        painter.setInvalidLinesPainted(false);
        painter.setLineHighlightEnabled(false);
        
        spessore = vertical.getWidth();
        super.painter.setLineHighlightColor(java.awt.Color.WHITE);
        this.setDocument(new SyntaxDocument());

        setTokenMarker(new JavaTokenMarker());
        
        getDocument().addUndoableEditListener(new SimpleUndoableEditListener());
        addMouseWheelListener(new SimpleMouseWheelListener());
        initPopupMenu();
/*        
        this.getInputHandler().addKeyBinding("C+Z", new java.awt.event.ActionListener() {

             public void actionPerformed(java.awt.event.ActionEvent evt)
             {                 
                  undo();
                  ;
                  System.out.println(getName() + " " + getDocument().getUM());
             }
        } );
  */                 
        
        
    }
    
    
    public void initPopupMenu()
    {
        
        jPopupMenuTextField = new javax.swing.JPopupMenu();
        jMenuItemTextEditor = new javax.swing.JMenuItem();
        jSeparator8 = new javax.swing.JSeparator();
        jMenuItemCut = new javax.swing.JMenuItem();
        jMenuItemCopy = new javax.swing.JMenuItem();
        jMenuItemPaste = new javax.swing.JMenuItem();
        jMenuItemSelectAll = new javax.swing.JMenuItem();
        jSeparator9 = new javax.swing.JSeparator();
        jMenuFields = new javax.swing.JMenu();
        jMenuVariables = new javax.swing.JMenu();
        jMenuParameters = new javax.swing.JMenu();
        
        jMenuItemTextEditor.setText("Use texteditor");
        jMenuItemTextEditor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemTextEditorActionPerformed(evt);
            }
        });

        jPopupMenuTextField.add(jMenuItemTextEditor);

        jPopupMenuTextField.add(jSeparator8);

        jMenuItemCut.setFont(new java.awt.Font("Dialog", 0, 11));
        jMenuItemCut.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/businesslogic/ireport/icons/menu/cut.gif")));
        jMenuItemCut.setText( I18n.getString("cut","Cut") );
        //jMenuItemCut.setEnabled(false);
        jPopupMenuTextField.add(jMenuItemCut);

        jMenuItemCut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemCutActionPerformed(evt);
            }
        });
        
        jMenuItemCopy.setFont(new java.awt.Font("Dialog", 0, 11));
        jMenuItemCopy.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/businesslogic/ireport/icons/menu/copy.gif")));
        jMenuItemCopy.setText(I18n.getString("copy","Copy"));
        //jMenuItemCopy.setEnabled(false);
        jPopupMenuTextField.add(jMenuItemCopy);

        jMenuItemCopy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemCopyActionPerformed(evt);
            }
        });
        
        jMenuItemPaste.setFont(new java.awt.Font("Dialog", 0, 11));
        jMenuItemPaste.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/businesslogic/ireport/icons/menu/paste.gif")));
        jMenuItemPaste.setText(I18n.getString("paste","Paste"));
        //jMenuItemPaste.setEnabled(false);
        jPopupMenuTextField.add(jMenuItemPaste);

        jMenuItemPaste.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemPasteActionPerformed(evt);
            }
        });
        
        jMenuItemSelectAll.setFont(new java.awt.Font("Dialog", 0, 11));
        jMenuItemSelectAll.setText("Select all");
        //jMenuItemPaste.setEnabled(false);
        jPopupMenuTextField.add(jMenuItemSelectAll);

        jMenuItemSelectAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemSelectAllActionPerformed(evt);
            }
        });
        
        
        
        /*
        jPopupMenuTextField.add(jSeparator9);

        jMenuFields.setText("Fields");
        jPopupMenuTextField.add(jMenuFields);

        jMenuVariables.setText("Variables");
        jPopupMenuTextField.add(jMenuVariables);

        jMenuParameters.setText("Parameters");
        jPopupMenuTextField.add(jMenuParameters);
        */
        
        this.setRightClickPopup(jPopupMenuTextField);
    }
    
    private void jMenuItemTextEditorActionPerformed(java.awt.event.ActionEvent evt)
    {
        ExpressionEditor ed = new ExpressionEditor();
        if (MainFrame.getMainInstance().getActiveReportFrame() != null)
        {
            ed.setJrf( MainFrame.getMainInstance().getActiveReportFrame() );
            ed.setExpression( getText() );
            System.out.println(getText());
            ed.show();
            if (ed.getDialogResult() == javax.swing.JOptionPane.OK_OPTION)
            {
                this.setText( ed.getExpression());
            }
            
        }
    }
    
    private void jMenuItemCutActionPerformed(java.awt.event.ActionEvent evt)
    {
        this.cut();
    }
    
    private void jMenuItemCopyActionPerformed(java.awt.event.ActionEvent evt)
    {
        this.copy();
    }
    
    private void jMenuItemPasteActionPerformed(java.awt.event.ActionEvent evt)
    {
        this.paste();
    }
    
    private void jMenuItemSelectAllActionPerformed(java.awt.event.ActionEvent evt)
    {
        this.setSelectionStart(0);
        this.setSelectionEnd( getText().length());
    }
    
    /** Getter for property viewScrollbars.
     * @return Value of property viewScrollbars.
     *
     */
    public boolean isViewScrollbars() {
        return viewScrollbars;
    }
    
    /** Setter for property viewScrollbars.
     * @param viewScrollbars New value of property viewScrollbars.
     *
     */
    public void setViewScrollbars(boolean viewScrollbars) {
        
         //System.out.println("Ops"+viewScrollbars);
        //if (this.viewScrollbars == viewScrollbars) return;
        if (!viewScrollbars)
        {
            //super.vertical.setVisible(false);
            //super.horizontal.setVisible(false);
            
            //super.vertical.setSize(0,0);
        }
        else
        {
            //super.vertical.setVisible(true);
            //super.horizontal.setVisible(true);
            //super.vertical.setSize(spessore,  super.vertical.getHeight());
            //super.horizontal.setSize(spessore,  super.horizontal.getWidth());
        }
        this.viewScrollbars = viewScrollbars;
    }
    

    public void setEnabled(boolean enabled)
    {
        if (super.isEnabled() == enabled) return;
        super.setEnabled(enabled);
        if (!super.isEnabled())
        {
            for (int i=0; i<this.getComponentCount(); ++i)
                this.getComponent(i).setBackground(java.awt.Color.GRAY);
            this.setCaretVisible(false);
           this.getPainter().setLineHighlightEnabled(false);
           
            
        }
        else
        {
            for (int i=0; i<this.getComponentCount(); ++i)
                this.getComponent(i).setBackground(java.awt.Color.WHITE);
            this.setCaretVisible(true);
            this.getPainter().setLineHighlightEnabled(true);
        }  
    }
    
    
    class SimpleUndoableEditListener implements UndoableEditListener
    {
        public void undoableEditHappened(UndoableEditEvent e)
        {
            addEdit(e);
            //System.out.println("Add to undo.." + getDocument().getUM().canUndo());
            
        }

        protected SimpleUndoableEditListener()
        {
        }
    }
    
    class SimpleMouseWheelListener implements MouseWheelListener
    {

        public void mouseWheelMoved(MouseWheelEvent e)
        {
            if(e.getScrollType() == 0 && ((getFirstLine() + getVisibleLines()) - 1) + e.getWheelRotation() < getLineCount() && getFirstLine() + e.getWheelRotation() >= 0)
                setFirstLine(getFirstLine() + e.getWheelRotation());
        }

        protected SimpleMouseWheelListener()
        {
        }
    }

     public void addEdit(UndoableEditEvent e)
     {
          getDocument().getUM().addEdit( e.getEdit() );
          //System.out.println( "Add edit to doc: " + getDocument().getUM() );
     }

}
