/* LanguageTool, a natural language style checker 
 * Copyright (C) 2005 Daniel Naber (http://www.danielnaber.de)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */
package de.danielnaber.languagetool.gui;

public class Tools {

  private static final int DEFAULT_CONTEXT_SIZE = 25;

  public static String getContext(int fromPos, int toPos, String text) {
    return getContext(fromPos, toPos, text, DEFAULT_CONTEXT_SIZE);
  }
  
  public static String getContext(int fromPos, int toPos, String fileContents, int contextSize) {
    fileContents = fileContents.replaceAll("\n", " ");
    // calculate context region:
    int startContent = fromPos - contextSize;
    String prefix = "...";
    String postfix = "...";
    String markerPrefix = "   ";
    if (startContent < 0) {
      prefix = "";
      markerPrefix = "";
      startContent = 0;
    }
    int endContent = toPos + contextSize;
    if (endContent > fileContents.length()) {
      postfix = "";
      endContent = fileContents.length();
    }
    // make "^" marker. inefficient but robust implementation:
    StringBuilder marker = new StringBuilder();
    for (int i = 0; i < fileContents.length() + prefix.length(); i++) {
      if (i >= fromPos && i < toPos)
        marker.append("^");
      else
        marker.append(" ");
    }
    // now build context string plus marker:
    StringBuilder sb = new StringBuilder();
    sb.append(prefix);
    sb.append(fileContents.substring(startContent, endContent));
    sb.append(postfix);
    String markerStr = markerPrefix + marker.substring(startContent, endContent);
    int startMark = markerStr.indexOf("^");
    int endMark = markerStr.lastIndexOf("^");
    String result = sb.toString();
    result = result.substring(0, startMark) + "<b><font color=\"red\">" + 
      result.substring(startMark, endMark+1) + "</font></b>" + result.substring(endMark+1);
    //String result = sb.s
    return result;
  }

}

@@ -971,17 +971,284 @@
 ZE = 25;
-%0A
+   // characters%0A  private static final String MARKER_START = %22%3Cb%3E%3Cfont color=%5C%22red%5C%22%3E%22;%0A  private static final String MARKER_END = %22%3C/font%3E%3C/b%3E%22;%0A%0A  /**%0A   * Get the default context (25 characters) of the given text range,%0A   * highlighting the range with HTML.%0A   */
 %0A  publi
@@ -1387,16 +1387,151 @@
 %0A  %7D%0A  %0A
+  /**%0A   * Get the context (%3Ccode%3EcontextSize%3C/code%3E characters) of the given text range,%0A   * highlighting the range with HTML.%0A   */%0A
   public
@@ -2851,33 +2851,20 @@
 ) + 
-%22%3Cb%3E%3Cfont color=%5C%22red%5C%22%3E%22
+MARKER_START
  + %0A
@@ -2910,29 +2910,26 @@
 rk+1) + 
-%22%3C/font%3E%3C/b%3E%22
+MARKER_END
  + resul
@@ -2956,35 +2956,8 @@
 1);%0A
-    //String result = sb.s%0A
     

add javadoc; tiny refactoring