package eg.document;

import java.awt.Color;

import javax.swing.JTextPane;

import javax.swing.text.BadLocationException;
import javax.swing.text.Element;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyledDocument;
import javax.swing.text.StyleConstants;

//--Eadgyth--//
import eg.utils.FileUtils;
import eg.utils.LinesFinder;
import eg.ui.LineNrWidthAdaptable;

/**
 * The line numbering
 */
public class LineNumbers {

   private final static Color GRAY = new Color(170, 170, 170);
   private final static SimpleAttributeSet SET = new SimpleAttributeSet();

   private final StyledDocument doc;
   private final LineNrWidthAdaptable lineNrWidth;
   private final StringBuilder lineNrBuilder = new StringBuilder();

   private int nOld = 1;

   static {
      StyleConstants.setForeground(SET, GRAY);
      StyleConstants.setAlignment(SET, StyleConstants.ALIGN_RIGHT);
      StyleConstants.setLineSpacing(SET, 0.25f);
   }

   /**
    * @param lineNrArea  the <code>JTextPane</code> that displays line
    * numbers
    * @param  lineNrWidth  the {@link LineNrWidthAdaptable}
    */
   public LineNumbers(JTextPane lineNrArea, LineNrWidthAdaptable lineNrWidth) {
      this.doc = lineNrArea.getStyledDocument();
      this.lineNrWidth = lineNrWidth;
      Element el = doc.getParagraphElement(0);
      doc.setParagraphAttributes(0, el.getEndOffset(), SET, false);
      appendLineNumbers(0, nOld);
   }

   /**
    * Updates the line numbers for the specified text
    *
    * @param text  the text
    */
   public void updateLineNumber(String text) {
      int nNew = LinesFinder.lineCount(text);
      if (nNew > nOld) {
         appendLineNumbers(nOld, nNew);
      }
      else if (nNew < nOld) {
         removeLineNumbers(nOld, nNew);
      }
      nOld = nNew;
   }

   //
   //--private--/
   //

   private void appendLineNumbers(int prevLineNr, int lineNr) {
      lineNrBuilder.setLength(0);
      for (int i = prevLineNr + 1; i <= lineNr; i++) {
         lineNrBuilder.append(Integer.toString(i)).append("\n");
      }
      try {
         doc.insertString(doc.getLength(), lineNrBuilder.toString(), SET);
         lineNrWidth.adaptLineNrWidth(prevLineNr, lineNr);
      }
      catch(BadLocationException e) {
         FileUtils.log(e);
      }
   }

   private void removeLineNumbers(int prevLineNr, int lineNr) {
      int length = 0;
      for (int i = prevLineNr; i > lineNr; i--) {
          length += ((int) Math.log10(i) + 1) + 1;
      }
      try {
         doc.remove(doc.getLength() - length, length);
         lineNrWidth.adaptLineNrWidth(prevLineNr, lineNr);
      }
      catch (BadLocationException e) {
         FileUtils.log(e);
      }
   }
}

@@ -349,43 +349,8 @@
 der;
-%0Aimport eg.ui.LineNrWidthAdaptable;
 %0A%0A/*
@@ -585,59 +585,8 @@
 oc;%0A
-   private final LineNrWidthAdaptable lineNrWidth;%0A
    p
@@ -954,68 +954,8 @@
 ers%0A
-    * @param  lineNrWidth  the %7B@link LineNrWidthAdaptable%7D%0A
     
@@ -1003,42 +1003,8 @@
 Area
-, LineNrWidthAdaptable lineNrWidth
 ) %7B%0A
@@ -1056,46 +1056,8 @@
 ();%0A
-      this.lineNrWidth = lineNrWidth;%0A
     
@@ -1926,67 +1926,8 @@
 T);%0A
-         lineNrWidth.adaptLineNrWidth(prevLineNr, lineNr);%0A
     
@@ -2275,67 +2275,8 @@
 h);%0A
-         lineNrWidth.adaptLineNrWidth(prevLineNr, lineNr);%0A
     

remove method to update line number width