More 3.1 compatibility
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / ui / text / rules / MultiViewPartitioner.java
index cc10e0d..b33b381 100644 (file)
@@ -8,7 +8,7 @@
  * Contributors:
  *     Igor Malinin - initial contribution
  * 
- * $Id: MultiViewPartitioner.java,v 1.2 2004-09-22 18:51:51 jsurfer Exp $
+ * $Id: MultiViewPartitioner.java,v 1.9 2005-05-13 20:17:31 axelcl Exp $
  */
 
 package net.sourceforge.phpeclipse.ui.text.rules;
@@ -116,11 +116,21 @@ public abstract class MultiViewPartitioner extends AbstractPartitioner {
       if (DEBUG) {
         Assert.isTrue(position.offset >= 0, Integer.toString(position.offset));
       }
-      DocumentEvent event = new DocumentEvent(outerDocument, outerDocument.getLocalOffset(position.offset), position.length, null);
-
-      outerDocument.fireDocumentAboutToBeChanged(event);
+      int outerOffset = outerDocument.getLocalOffset(position.offset);
+      // axelcl start
+      DocumentEvent event = null;
+      if (outerOffset >= 0) {
+        // axelcl end
+        event = new DocumentEvent(outerDocument, outerOffset, position.length, null);
+
+        outerDocument.fireDocumentAboutToBeChanged(event);
+      }
       super.addInnerRegion(position);
-      outerDocument.fireDocumentChanged(event);
+      //    axelcl start
+      if (event != null) {
+        // axelcl end
+        outerDocument.fireDocumentChanged(event);
+      }
     } else {
       super.addInnerRegion(position);
     }
@@ -144,15 +154,19 @@ public abstract class MultiViewPartitioner extends AbstractPartitioner {
     try {
       if (outerDocument != null) {
         DocumentEvent event = null;
-        if (position.offset >= 0) {
-          event = new DocumentEvent(outerDocument, outerDocument.getLocalOffset(position.offset), 0, document.get(position.offset,
-              position.length));
+        if (position.offset >= 0 && position.length >= 0) {
+          int outerOffset = outerDocument.getLocalOffset(position.offset);
+          if (outerOffset > 0) {
+            event = new DocumentEvent(outerDocument, outerOffset, 0, document.get(position.offset, position.length));
 
-          outerDocument.fireDocumentAboutToBeChanged(event);
+            outerDocument.fireDocumentAboutToBeChanged(event);
+          }
         }
         super.removeInnerRegion(position);
         if (position.offset >= 0) {
-          outerDocument.fireDocumentChanged(event);
+          if (event != null) {
+            outerDocument.fireDocumentChanged(event);
+          }
         }
       } else {
         super.removeInnerRegion(position);
@@ -287,10 +301,12 @@ public abstract class MultiViewPartitioner extends AbstractPartitioner {
 
       String text = event.getText();
 
-      if (left != right || text != null && text.length() > 0) {
-        outerDocumentEvent = new DocumentEvent(outerDocument, left, right - left, text);
+      if (left >= 0 && (right - left >= 0)) {
+        if (left != right || text != null && text.length() > 0) {
+          outerDocumentEvent = new DocumentEvent(outerDocument, left, right - left, text);
 
-        outerDocument.fireDocumentAboutToBeChanged(outerDocumentEvent);
+          outerDocument.fireDocumentAboutToBeChanged(outerDocumentEvent);
+        }
       }
     }
   }
@@ -320,7 +336,7 @@ public abstract class MultiViewPartitioner extends AbstractPartitioner {
           if (p instanceof ViewNode) {
             // TODO: revisit condition
             InnerDocumentView innerDocument = ((ViewNode) p).view;
-            if (innerDocument != null) { 
+            if (innerDocument != null) {
               int start = innerDocument.getLocalOffset(offset);
               innerDocument.fireDocumentChanged(new DocumentEvent(innerDocument, start, length, text));
             }
@@ -422,7 +438,16 @@ public abstract class MultiViewPartitioner extends AbstractPartitioner {
    */
   public ITypedRegion[] computePartitioning(int offset, int length) {
     List list = new ArrayList();
-
+//        if (DEBUG) {
+//          if (length>=9400) {
+//            length--;
+//          }
+//          System.out.print("MultiViewPartitioner::computePartitioning - Offset: ");
+//          System.out.print(offset);
+//          System.out.print(", Length: ");
+//          System.out.print(length);
+//          System.out.println("");
+//        }
     int end = offset + length;
 
     int index = computeFlatNodeIndex(offset);
@@ -451,10 +476,34 @@ public abstract class MultiViewPartitioner extends AbstractPartitioner {
 
       ++index;
     }
+    //    if (DEBUG) {
+    //      showList(list);
+    //    }
 
     return (TypedRegion[]) list.toArray(new TypedRegion[list.size()]);
   }
 
+  private void showList(List list) {
+    try {
+      throw new NullPointerException();
+    } catch (Exception e) {
+      e.printStackTrace();
+    }
+    System.out.println(">>>>>List start");
+    TypedRegion temp;
+    for (int i = 0; i < list.size(); i++) {
+      temp = (TypedRegion) list.get(i);
+      System.out.print("Offset: ");
+      System.out.print(temp.getOffset());
+      System.out.print(", Length: ");
+      System.out.print(temp.getLength());
+      System.out.print(", Type: ");
+      System.out.print(temp.getType());
+      System.out.println("");
+    }
+    System.out.println("<<<<<List end");
+  }
+
   private void addOuterPartitions(List list, int offset, int length, FlatNode prev, FlatNode next) {
     // limit region
     int start = offset;
@@ -473,17 +522,30 @@ public abstract class MultiViewPartitioner extends AbstractPartitioner {
     }
 
     if (outerDocument == null) {
+      //      if (DEBUG) {
+      //        if (end - start<0) {
+      //          throw new IndexOutOfBoundsException();
+      //        }
+      //      }
       list.add(new TypedRegion(start, end - start, getContentType(null, IDocument.DEFAULT_CONTENT_TYPE)));
       return;
     }
 
-    try {
+   try {
       // convert to outer offsets
       start = outerDocument.getLocalOffset(start);
       end = outerDocument.getLocalOffset(end);
-      if (end - start >= 0) {//jsurfer insert line
-        ITypedRegion[] regions = outerDocument.computePartitioning(start, end - start);
-
+      int len = end - start;
+      if (len >= 0) {        
+        ITypedRegion[] regions = null;
+        try {
+          regions = outerDocument.computePartitioning(start, len);
+        } catch (Exception e) {
+          // nasty workaround, which prevents cursor from moveing backwards in the editor
+          // but doesn't solve the partitioning problem
+          regions = new ITypedRegion[0];
+          System.out.println("MultiViewerPartitioner#addOuterPartitions failure\n"+"start:"+start +" length:" +len+"\n"+outerDocument.get(start,len));
+        }
         for (int i = 0; i < regions.length; i++) {
           ITypedRegion region = regions[i];
 
@@ -504,7 +566,21 @@ public abstract class MultiViewPartitioner extends AbstractPartitioner {
               end = offset;
             }
           }
-
+          //          if (DEBUG) {
+          //            if (end - start<0) {
+          //              showList(list);
+          //              System.out.print("MultiViewPartitioner::addOuterPartitions - Offset: ");
+          //              System.out.print(offset);
+          //              System.out.print(", Start: ");
+          //              System.out.print(start);
+          //              System.out.print(", End: ");
+          //              System.out.print(end);
+          //              System.out.print(", Type: ");
+          //              System.out.print(region.getType());
+          //              System.out.println("");
+          //              throw new IndexOutOfBoundsException();
+          //            }
+          //          }
           list.add(new TypedRegion(start, end - start, getContentType(null, region.getType())));
         }
       }
@@ -521,6 +597,11 @@ public abstract class MultiViewPartitioner extends AbstractPartitioner {
 
     if (innerDocument == null) {
       // simple partition
+      //      if (DEBUG) {
+      //        if (position.length<0) {
+      //          throw new IndexOutOfBoundsException();
+      //        }
+      //      }
       list.add(new TypedRegion(position.offset, position.length, getContentType(position.type, null)));
       return;
     }
@@ -543,7 +624,11 @@ public abstract class MultiViewPartitioner extends AbstractPartitioner {
         // convert back to parent offsets
         offset = innerDocument.getParentOffset(region.getOffset());
         length = region.getLength();
-
+        //        if (DEBUG) {
+        //          if (length<0) {
+        //            throw new IndexOutOfBoundsException();
+        //          }
+        //        }
         list.add(new TypedRegion(offset, length, getContentType(position.type, region.getType())));
       }
     } catch (BadLocationException x) {
@@ -598,16 +683,25 @@ public abstract class MultiViewPartitioner extends AbstractPartitioner {
         end = document.getLength();
         type = getContentType(null, IDocument.DEFAULT_CONTENT_TYPE);
       } else {
-        ITypedRegion region = outerDocument.getPartition(outerDocument.getLocalOffset(offset));
+        int outerOffset = outerDocument.getLocalOffset(offset);
+        //axelcl start
+        if (outerOffset < 0) {
+          start = 0;
+          end = document.getLength();
+          type = getContentType(null, IDocument.DEFAULT_CONTENT_TYPE);
+        } else {
+          //        axelcl end
+          ITypedRegion region = outerDocument.getPartition(outerOffset);
 
-        start = region.getOffset();
-        end = start + region.getLength();
+          start = region.getOffset();
+          end = start + region.getLength();
 
-        // convert to parent offset
-        start = outerDocument.getParentOffset(start);
-        end = outerDocument.getParentOffset(end);
+          // convert to parent offset
+          start = outerDocument.getParentOffset(start);
+          end = outerDocument.getParentOffset(end);
 
-        type = getContentType(null, region.getType());
+          type = getContentType(null, region.getType());
+        }
       }
 
       if (prev != null) {
@@ -626,6 +720,7 @@ public abstract class MultiViewPartitioner extends AbstractPartitioner {
 
       return new TypedRegion(start, end - start, type);
     } catch (BadLocationException x) {
+      x.printStackTrace();
       throw new IllegalArgumentException();
     }
   }