/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2010 C.Marchand - christophe.marchand@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package fr.gouv.finances.cp.xemelios.ui;

import java.awt.Color;
import java.io.InputStream;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Element;

public class CodeViewer extends JFrame {

    private static JTextArea jta;
    private static JTextArea lines;
    private JScrollPane jsp;
    private URL input;

    public CodeViewer(URL input) {
        super(input.getFile());
        this.input = input;
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jsp = new JScrollPane();
        jta = new JTextArea();
//        jta.setEditable(false);
        lines = new JTextArea("1");

        lines.setBackground(Color.LIGHT_GRAY);
        lines.setEditable(false);

        jta.getDocument().addDocumentListener(new DocumentListener() {

            public String getText() {
                int caretPosition = jta.getDocument().getLength();
                Element root = jta.getDocument().getDefaultRootElement();
                String text = "1" + System.getProperty("line.separator");
                for (int i = 2; i < root.getElementIndex(caretPosition) + 2; i++) {
                    text += i + System.getProperty("line.separator");
                }
                return text;
            }

            public void changedUpdate(DocumentEvent de) {
                lines.setText(getText());
            }

            public void insertUpdate(DocumentEvent de) {
                lines.setText(getText());
            }

            public void removeUpdate(DocumentEvent de) {
                lines.setText(getText());
            }
        });

        jsp.getViewport().add(jta);
        jsp.setRowHeaderView(lines);
        jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        add(jsp);
        pack();
        setSize(800, 600);
        setVisible(true);

        doLoadFile();

    }

    protected void doLoadFile() {
        try {
            InputStream is = input.openStream();
            StringBuilder sb = new StringBuilder();
            byte[] buffer = new byte[1024];
            int read=is.read(buffer);
            while(read>0) {
                sb.append(new String(buffer,0,read));
                read=is.read(buffer);
            }
            jta.setText(sb.toString());
            // on se repositionne en haut
            jta.setCaretPosition(0);
        } catch(Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage(), "Erreur",JOptionPane.ERROR_MESSAGE);
        }
    }
}
