/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.yawni.wn.browser;

import javax.swing.text.*;

/**
 * java.swing.text.Document to constrain a search field
 * to non-junk.
 */
class SearchFieldDocument extends PlainDocument {
  private static final long serialVersionUID = 1L;

  private final int MAX_LEN = 256;

  @Override
  public void insertString(
      final int insOffset,
      String text,
      final AttributeSet attr) throws BadLocationException {
    //// consider whether this insert will match
    //final String proposedInsert =
    //  getText(0, insOffset) +
    //  text +
    //  getText(insOffset, getLength() - insOffset);
    ////System.out.println("proposing to change to: " + proposedInsert);
    //if (matcher != null) {
    //  matcher.reset(proposedInsert);
    //  //System.out.println("matcher reset");
    //  if (false == matcher.matches()) {
    //    //System.out.println("insert doesn't match");
    //    return;
    //  }
    //}

    //System.err.println("input text: "+text);
    // TODO scrub text and then insert the remains
    // shortcut common case: is short and contains nothing but letters or digits
    boolean dirty = false == isClean(text, insOffset);
    if (dirty) {
      //System.err.println("dirty insert?: \""+text+"\"");
      text = scrub(text, insOffset);
    }
    super.insertString(insOffset, text, attr);
    if (dirty) {
      //System.err.println("dirty doc: "+getText(0, getLength()));
    }
  }

  @Override
  public void replace(
      final int offset,
      final int length,
      String text,
      final AttributeSet attr) throws BadLocationException {
    if (length != 0 && false == isClean(text, offset)) {
      //final String toReplace = getText(offset, length);
      //System.err.println("toReplace: \""+toReplace+"\""+
      //    " (length: "+length+") with \""+text+"\" (length: "+text.length()+")");
      text = scrub(text, offset);
    }
    //System.err.printf("length: %s text: \"%s\"\n", length, text);
    if (text != null && text.length() != 0) {
      // prevent slash when text is selected from deleting word
      super.replace(offset, length, text, attr);
    }
  }

  // allow all inserts to run, just some wth empty strings
  private String scrub(final String proposedNewText, final int insOffset) {
//    final int currDocLen = getLength();
//    final String prevText;
//    final String nextText;
//    try {
//      prevText = getText(0, insOffset);
//      nextText = currDocLen == insOffset ? "" :
//        getText(insOffset, currDocLen - insOffset);
//    } catch (BadLocationException ble) {
//      throw new RuntimeException(ble);
//    }
//    final int lastChar = prevText.length() != 0 ? prevText.charAt(0) : 0;
    String toInsert = proposedNewText;
    // - bound total document length to MAX_LEN
    toInsert = toInsert.substring(0, Math.min(toInsert.length(), MAX_LEN));
    // ignore navigation-only slash
    toInsert = toInsert.replace("/", "");
    // - normalize any internal whitespace
    //   - corner case: allow it to end with up to 1 space or hyphen
    return toInsert;
  }

  private boolean isClean(final String proposedNewText, final int insOffset) {
    for (int i = 0, len = proposedNewText.length();
        i < len; i++) {
      if (false == Character.isLetterOrDigit(proposedNewText.charAt(i))) {
        return false;
      }
    }
    return true;
  }
}
@@ -871,21 +871,29 @@
 %0A%0A/**%0A *
+ %7B@link
  java
+x
 .swing.t
@@ -904,16 +904,17 @@
 Document
+%7D
  to cons
@@ -1008,60 +1008,8 @@
 t %7B%0A
-  private static final long serialVersionUID = 1L;%0A%0A
   pr
@@ -2621,222 +2621,228 @@
     
-text = scrub(text, offset);%0A    %7D%0A    //System.err.printf(%22length: %25s text: %5C%22%25s%5C%22%5Cn%22, length, text);%0A    if (text != null && text.length() != 0) %7B%0A      // prevent slash when text is selected from deleting word%0A  
+// prevent slash when text is selected from deleting word%0A      if (%22/%22.equals(text)) %7B%0A        return;%0A      %7D%0A      text = scrub(text, offset);%0A    %7D%0A    //System.err.printf(%22length: %25s text: %5C%22%25s%5C%22%5Cn%22, length, text);%0A
     
@@ -2884,22 +2884,16 @@
  attr);%0A
-    %7D%0A
   %7D%0A%0A  /
@@ -3609,24 +3609,26 @@
  MAX_LEN));%0A
+//
     // ignor
@@ -3651,16 +3651,18 @@
 y slash%0A
+//
     toIn

remove silly serialVersionUID, allow blank inserts and slashes when there is not selection