/*******************************************************************************
 * See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package hr.fer.zemris.vhdllab.applets.texteditor;

import hr.fer.zemris.vhdllab.entity.File;
import hr.fer.zemris.vhdllab.platform.manager.editor.impl.AbstractEditor;

import java.awt.Color;
import java.io.PrintWriter;
import java.io.StringWriter;

import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;

import org.springframework.binding.value.CommitTrigger;
import org.springframework.richclient.text.TextComponentPopup;

public class TextEditor extends AbstractEditor implements DocumentListener,
        CaretListener {

    private JTextPane textPane;
    private CommitTrigger commitTrigger;

    private Object highlighted;

    @Override
    protected JComponent doInitWithoutData() {
        textPane = new JTextPane();
        textPane.getDocument().addDocumentListener(this);
        textPane.addCaretListener(this);
        commitTrigger = new CommitTrigger();
        TextComponentPopup.attachPopup(textPane, commitTrigger);

        return textPane;
    }

    @Override
    protected void doInitWithData(File f) {
        textPane.setText(f.getData());
        commitTrigger.commit();
    }

    @Override
    protected String getData() {
        return textPane.getText();
    }

    @Override
    protected void doDispose() {
    }

    @Override
    public void setEditable(boolean flag) {
        textPane.setEditable(flag);
    }

    @Override
    public void highlightLine(int line) {
        int caret = textPane.getCaretPosition();
        Highlighter h = textPane.getHighlighter();
        h.removeAllHighlights();
        String content = textPane.getText();
        textPane.setCaretPosition(caret);

        int pos = 0;
        line--;
        while (line > 0) {
            pos = content.indexOf('\n', pos) + 1;
            line--;
        }
        int last = content.indexOf('\n', pos) + 1;
        if (last == 0) {
            last = content.length();
        }
        try {
            highlighted = h.addHighlight(pos, last,
                    new DefaultHighlighter.DefaultHighlightPainter(new Color(
                            180, 210, 238)));
        } catch (BadLocationException e) {
            e.printStackTrace();
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            JOptionPane.showMessageDialog(null, sw.toString());
        }
    }

    //
    // DocumentListener implementation
    //

    @Override
    public void changedUpdate(DocumentEvent e) {
        setModified(true);
    }

    @Override
    public void insertUpdate(DocumentEvent e) {
        setModified(true);
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        setModified(true);
    }

    //
    // CaretListener implementation
    //

    @Override
    public void caretUpdate(CaretEvent e) {
        if (highlighted != null) {
            textPane.getHighlighter().removeHighlight(highlighted);
            highlighted = null;
        }
    }

}

@@ -1101,24 +1101,51 @@
 JComponent;%0A
+import javax.swing.JFrame;%0A
 import javax
@@ -1160,24 +1160,24 @@
 OptionPane;%0A
-
 import javax
@@ -1346,32 +1346,112 @@
 cumentListener;%0A
+import javax.swing.text.AbstractDocument;%0Aimport javax.swing.text.AttributeSet;%0A
 import javax.swi
@@ -1520,24 +1520,98 @@
 ighlighter;%0A
+import javax.swing.text.Document;%0Aimport javax.swing.text.DocumentFilter;%0A
 import javax
@@ -1688,24 +1688,87 @@
 mitTrigger;%0A
+import org.springframework.richclient.application.Application;%0A
 import org.s
@@ -1894,24 +1894,16 @@
 istener,
-%0A       
  CaretLi
@@ -2115,32 +2115,52 @@
 tPane();%0A       
+ Document document =
  textPane.getDoc
@@ -2166,16 +2166,34 @@
 cument()
+;%0A        document
 .addDocu
@@ -2204,32 +2204,869 @@
 Listener(this);%0A
+%0A        if (document instanceof AbstractDocument) %7B%0A            ((AbstractDocument) document).setDocumentFilter(new DocumentFilter() %7B%0A%0A                @Override%0A                public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs)%0A                        throws BadLocationException %7B%0A                    if (length == 0 && text != null && text.length() %3E 10) %7B%0A                        JFrame frame = Application.instance().getActiveWindow().getControl();%0A                        JOptionPane.showMessageDialog(frame, %22Pasting text is disabled!%22, %22Paste text%22,%0A                                JOptionPane.INFORMATION_MESSAGE);%0A                    %7D else %7B%0A                        super.replace(fb, offset, length, text, attrs);%0A                    %7D%0A                %7D%0A%0A            %7D);%0A        %7D%0A%0A
         textPane
@@ -4222,36 +4222,16 @@
 s, last,
-%0A                   
  new Def
@@ -4280,16 +4280,25 @@
 w Color(
+180, 210,
 %0A       
@@ -4313,26 +4313,8 @@
     
-         180, 210,
  238

Complete - # 42: zabrana koristenja sistemskog clipboarda http://morgoth.zemris.fer.hr/trac/vhdllab/ticket/42