/**
   * return true if can create region
   *
   * @param beginOffset
   * @param endOffset
   * @throws CollabException
   * @return
   */
  public boolean testCreateRegion(int beginOffset, int endOffset) throws CollabException {
    Debug.log(this, "CollabJavaFileHandler, testCreateRegion"); // NoI18n

    // check for initial guarded sections
    if (!testOverlap(beginOffset, endOffset) || !testOverlap(beginOffset + 1, endOffset)) {
      Debug.log(this, "CollabJavaFileHandler, found initial guarded section"); // NoI18n

      return false;
    }

    int length = endOffset - beginOffset;
    CollabRegion region = getContainingRegion(beginOffset, length, true);

    if (region != null) {
      if (!region.isGuarded()) {
        Debug.log(this, "CollabJavaFileHandler, region instanceof CollabJavaRegion"); // NoI18n

        return false;
      } else {
        Debug.log(
            this, "CollabJavaFileHandler, region instanceof JavaEditor.SimpleSection"); // NoI18n

        return testOverlap(beginOffset, endOffset);
      }
    }

    return true;
  }
  /**
   * creates a CollabRegion, a super-class for all regions
   *
   * @param testOverlap
   * @param regionName the regionName
   * @param beginOffset the beginOffset
   * @param endOffset the endOffset
   * @param testOverlap
   * @param guarded
   * @throws CollabException
   * @return
   */
  public CollabRegion createRegion(
      String regionName, int beginOffset, int endOffset, boolean testOverlap, boolean guarded)
      throws CollabException {
    Debug.log(this, "CollabJavaFileHandler, createRegion"); // NoI18n

    if (testOverlap) {
      // Testing overlap, if false, cannot create region
      if (!testCreateRegion(beginOffset, endOffset)) {
        Debug.log(this, "CollabJavaFileHandler, testCreateRegion: false"); // NoI18n

        return null;
      }

      Debug.log(this, "CollabJavaFileHandler, testCreateRegion: true"); // NoI18n
    }

    // correction for adjacent regions
    /*if (beginOffset > 0) {
        beginOffset += 1;
    }*/

    CollabRegion region = null;

    try {
      region =
          new CollabJavaFileHandler.CollabJavaRegion(regionName, beginOffset, endOffset, guarded);
    } catch (Throwable th) {
      // ignore
    }

    return region;
  }
  /** updateDocument */
  protected void updateDocument(String content) throws CollabException {
    Debug.log(this, "CollabJavaFileHandler, updateDocument"); // NoI18n

    if ((getInitialGuardedSections() == null) || (getInitialGuardedSections().size() == 0)) {
      Debug.log(this, "CollabJavaFileHandler, no initial guarded sections"); // NoI18n	
      super.updateDocument(content);
    } else {
      Debug.log(this, "CollabJavaFileHandler, initial guarded sections exist"); // NoI18n	
      updateFileObject(content);
      getFileObject().refresh(false);
    }
  }
  /** lockEditor */
  protected EditorLock doLockEditor() throws CollabException {
    Debug.log(this, "CollabJavaHandler, locking Editor"); // NoI18n

    if ((getInitialGuardedSections() != null)
        && (getInitialGuardedSections().size() > 0)) { // other guarded section, return
      Debug.log(this, "CollabJavaHandler, found initial GuardedSections");
      disableUnlockTimer(true);

      return null;
    } else {
      return super.doLockEditor();
    }
  }
  /** isDocumentModified */
  public boolean isDocumentModified() throws CollabException {
    boolean isModified = false;
    EditorCookie cookie = getEditorCookie();

    if (cookie != null) {
      isModified = cookie.isModified();
    }

    Debug.log(
        this, "CollabJavaHandler, isModified: " + isModified + " for file " + getName()); // NoI18n

    return isModified;
  }
  /** saveDocument */
  public boolean saveDocument() throws CollabException {
    Debug.log(
        this, // NoI18n
        "CollabJavaHandler, saving document: " + getName()); // NoI18n

    try {
      EditorCookie cookie = getEditorCookie();

      if (cookie != null) {
        cookie.saveDocument();
      }
    } catch (IOException iox) {
      Debug.log(
          this, // NoI18n
          "CollabJavaHandler, Exception occured while saving the document: " // NoI18n
              + getName()); // NoI18n

      return false;
    }

    return true;
  }
  /** findInitialGuardedSections */
  protected void findInitialGuardedSections() throws CollabException {
    Debug.log(this, "CollabJavaHandler, findInitialGuardedSections"); // NoI18n

    StyledDocument docu = getDocument();
    synchronized (docu) {
      GuardedSectionManager man = GuardedSectionManager.getInstance(docu);
      if (man == null) return; // no guarded sections

      Iterator it = man.getGuardedSections().iterator();

      while (it.hasNext()) {
        GuardedSection sect = (GuardedSection) it.next();
        String name = sect.getName();
        this.otherGuardedSections.put(name, sect);

        if (sect instanceof SimpleSection) {
          Debug.log(this, "CollabJavaHandler, simple_sect: " + name); // NoI18n
        } else {
          Debug.log(this, "CollabJavaHandler, inter_sect: " + name); // NoI18n
        }
      }
    }
  }
  /**
   * skip initialGuarded
   *
   * @param offset
   * @param length
   * @return true if skip
   */
  protected boolean skipInitialGuarded(int offset) {
    Debug.log(
        this,
        "CollabJavaHandler, skipInitialGuarded, "
            + "	otherGuardedSections size: "
            + otherGuardedSections.size()); // NoI18n

    Iterator it = this.otherGuardedSections.values().iterator();
    while (it.hasNext()) {
      GuardedSection sect = (GuardedSection) it.next();
      int beginOffset = sect.getStartPosition().getOffset();
      int endOffset = sect.getEndPosition().getOffset();
      if ((offset >= beginOffset) && (offset <= endOffset)) return true;
    }
    return false;
  }
  /**
   * return editor cookie for this filehandler
   *
   * @throws CollabException
   * @return cookie
   */
  public EditorCookie getEditorCookie() throws CollabException {
    Debug.log(
        this,
        "CollabJavaHandler, "
            + // NoI18n
            "geteditorCookie for file: "
            + getName()); // NoI18n

    try {
      if (editorCookie == null) {
        FileObject file = getFileObject();

        // Get the FileObject
        if (file == null) {
          return null;
        }

        // Get the DataObject
        DataObject dataObject = DataObject.find(file);

        if (dataObject == null) {
          throw new IllegalArgumentException("No DataObject found for file \"" + getName() + "\"");
        }

        // Get the editor cookie for the file
        editorCookie = (EditorCookie) dataObject.getCookie(EditorCookie.class);

        // add reset Document Reference Listener
        addResetDocumentRefListener(editorCookie, getEditorObservableCookie());
      }

      return editorCookie;
    } catch (org.openide.loaders.DataObjectNotFoundException notFound) {
      throw new CollabException(notFound);
    } catch (java.io.IOException io) {
      throw new CollabException(io);
    }
  }
  /**
   * skip removeUpdate
   *
   * @param offset
   * @param length
   * @return true if skip
   */
  public boolean skipRemoveUpdate(int offset, int length) {
    Debug.log(this, "CollabJavaHandler, skipRemoveUpdate"); // NoI18n

    return skipInitialGuarded(offset) || super.skipRemoveUpdate(offset, length);
  }
  /**
   * skip insertUpdate
   *
   * @param offset
   * @return true if skip
   */
  public boolean skipInsertUpdate(int offset) {
    Debug.log(this, "CollabJavaHandler, skipInsertUpdate"); // NoI18n

    return skipInitialGuarded(offset) || super.skipInsertUpdate(offset);
  }