/* ElementStructure7.java --
   Copyright (C) 2006 Red Hat
This file is part of Mauve.

Mauve 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, or (at your option)
any later version.

Mauve 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 Mauve; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

*/

package gnu.testlet.javax.swing.text.DefaultStyledDocument.ElementBuffer;

import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;

import java.util.Vector;

import javax.swing.event.DocumentEvent;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Element;

public class ElementStructure7 extends DefaultStyledDocument implements Testlet
{
  /**
   * Starts the test run.
   * 
   * @param harness
   *          the test harness to use
   */
  public void test(TestHarness harness)
  {
    ElementStructure7 doc = new ElementStructure7();
    h2 = harness;
    try
      {
        doc.insertString(0, "aaaaaaaaa\nbbbbbbbbb", null);
        doc.insertString(5, "\nN", null);
      }
    catch (Exception ex)
      {
        // ex.printStackTrace();
        h2.debug(ex);
      }
    catch (AssertionError e)
      {
        // e.printStackTrace();
        harness.debug(e);
      }
    // printElements(doc.getDefaultRootElement(), 0);
  }
  
  static TestHarness h2;
  
  static DefaultDocumentEvent docEvent = null;

  static int numInserts = 0;

  // We override the constructor so we can explicitly set the type of the
  // buffer to be our ElementBuffer2, allowing us to test some internals.
  public ElementStructure7()
  {
    super();
    buffer = new ElementBuffer2(createDefaultRoot());
  }

  // Overriding this method allows us to check that the right number
  // of newLines was encountered and that the event has the proper
  // offset and length.
  protected void insertUpdate(DefaultDocumentEvent ev, AttributeSet attr)
  {
    int l = ev.getLength();
    int o = ev.getOffset();
    if (numInserts == 0)
      {
        h2.checkPoint("first doc event");
        h2.check(o == 0);
        h2.check(l == 19);
      }
    else if (numInserts == 1)
      {
        h2.checkPoint("second doc event");
        h2.check(o == 5);
        h2.check(l == 2);
      }
    else
      h2.fail ("too many calls to DefaultStyledDocument.insertUpdate");

    super.insertUpdate (ev, attr);
  }

  // A class to be the buffer of the styled document.
  // This allows us to check that some values are correct internally within
  // the ElementBuffer.
  public class ElementBuffer2 extends ElementBuffer
  {
    public ElementBuffer2(Element root)
    {
      super(root);
    }

    // This method allows us to check that the ElementSpecs generated by 
    // DefaultStyledDocument.insertUpdate are correct.
    protected void insertUpdate(ElementSpec[] data)
    {
      numInserts++;
      if (numInserts == 1)
        {
          h2.checkPoint("ElementBuffer insertUpdate: first insertion");
          h2.check (data.length == 4);
          h2.check(data[0].getType() == ElementSpec.ContentType);
          h2.check
            (data[0].getDirection() == ElementSpec.JoinPreviousDirection);
          h2.check(data[0].getOffset() == 0);
          h2.check(data[0].getLength() == 10);

          h2.check(data[1].getType() == ElementSpec.EndTagType);
          h2.check(data[1].getDirection() == ElementSpec.OriginateDirection);
          h2.check(data[1].getOffset() == 0);
          h2.check(data[1].getLength() == 0);

          h2.check(data[2].getType() == ElementSpec.StartTagType);
          h2.check
            (data[2].getDirection() == ElementSpec.JoinFractureDirection);
          h2.check(data[2].getOffset() == 0);
          h2.check(data[2].getLength() == 0);
          
          h2.check(data[3].getType() == ElementSpec.ContentType);
          h2.check(data[3].getDirection() == ElementSpec.JoinNextDirection);
          h2.check(data[3].getOffset() == 0);
          h2.check(data[3].getLength() == 9);
        }
      else if (numInserts == 2)
        {
          h2.checkPoint("ElementBuffer insertUpdate: second insertion");
          h2.check (data.length == 4);
          h2.check(data[0].getType() == ElementSpec.ContentType);
          h2.check
            (data[0].getDirection() == ElementSpec.JoinPreviousDirection);
          h2.check(data[0].getOffset() == 0);
          h2.check(data[0].getLength() == 1);
          
          h2.check(data[1].getType() == ElementSpec.EndTagType);
          h2.check(data[1].getDirection() == ElementSpec.OriginateDirection);
          h2.check(data[1].getOffset() == 0);
          h2.check(data[1].getLength() == 0);
          
          h2.check(data[2].getType() == ElementSpec.StartTagType);
          h2.check
            (data[2].getDirection() == ElementSpec.JoinFractureDirection);
          h2.check(data[2].getOffset() == 0);
          h2.check(data[2].getLength() == 0);
          
          h2.check(data[3].getType() == ElementSpec.ContentType);
          h2.check(data[3].getDirection() == ElementSpec.JoinNextDirection);
          h2.check(data[3].getOffset() == 0);
          h2.check(data[3].getLength() == 1);

        }
      else
        h2.fail("too many ElementSpecs created");
      h2.check(docEvent.getChange(getDefaultRootElement()) == null);
      super.insertUpdate(data);
      h2.check(docEvent.getChange(getDefaultRootElement()) == null);
    }
    
    public void insert(int offset, int length, ElementSpec[] data,
                       DefaultDocumentEvent ev)
    {
      docEvent = new DefaultDocumentEvent(ev.getOffset(), ev.getLength(),
                                           ev.getType());
      super.insert(offset, length, data, docEvent);
      h2.check(data.length == 4);
      h2.check(data[0].getType() == ElementSpec.ContentType);
      h2.check(data[0].getDirection() == ElementSpec.JoinPreviousDirection);
      h2.check(data[1].getType() == ElementSpec.EndTagType);
      h2.check(data[1].getDirection() == ElementSpec.OriginateDirection);
      h2.check(data[2].getType() == ElementSpec.StartTagType);
      h2.check(data[2].getDirection() == ElementSpec.JoinFractureDirection);
      h2.check(data[3].getType() == ElementSpec.ContentType);
      h2.check(data[3].getDirection() == ElementSpec.JoinNextDirection);
    }
  }
}
