/* 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;

/**
 * GUI-related tools.
 * 
 * @author Daniel Naber
 */
public class Tools {

  private static final int DEFAULT_CONTEXT_SIZE = 40;   // characters
  private static final String MARKER_START = "<b><font color=\"red\">";
  private static final String MARKER_END = "</font></b>";

  /**
   * Get the default context (25 characters) of the given text range,
   * highlighting the range with HTML.
   */
  public static String getContext(int fromPos, int toPos, String text) {
    return getContext(fromPos, toPos, text, DEFAULT_CONTEXT_SIZE);
  }
  
  /**
   * Get the context (<code>contextSize</code> characters) of the given text range,
   * highlighting the range with HTML.
   */
  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));
    String markerStr = markerPrefix + marker.substring(startContent, endContent);
    sb.append(postfix);
    int startMark = markerStr.indexOf("^");
    int endMark = markerStr.lastIndexOf("^");
    String result = sb.toString();
    result = result.substring(0, startMark) + MARKER_START + 
      result.substring(startMark, endMark+1) + MARKER_END + result.substring(endMark+1);
    return result;
  }

}

@@ -1217,10 +1217,10 @@
 xt (
-25
+40
  cha
@@ -1442,18 +1442,16 @@
 E);%0A  %7D%0A
-  
 %0A  /**%0A 
@@ -1562,32 +1562,37 @@
  range with HTML
+ code
 .%0A   */%0A  public
@@ -1677,24 +1677,420 @@
 textSize) %7B%0A
+    return getContext(fromPos, toPos, fileContents, contextSize, MARKER_START, MARKER_END);%0A  %7D%0A  %0A  /**%0A   * Get the context (%3Ccode%3EcontextSize%3C/code%3E characters) of the given text range,%0A   * highlighting the range with the given marker strings.%0A   */%0A  public static String getContext(int fromPos, int toPos, String fileContents, int contextSize,%0A      String markerStart, String markerEnd) %7B%0A
     fileCont
@@ -3304,28 +3304,27 @@
 Mark) + 
-MARKER_START
+markerStart
  + %0A    
@@ -3366,26 +3366,25 @@
 rk+1) + 
-MARKER_END
+markerEnd
  + resul

make it possible to specify start and end markers in helper method