// StripExtraWhiteSpaces.java
// Cyno's Editor Plug-In

// Cynosure.X International
// 10-30-1997

// Copyright (C) 1997, 1998 by Chieh Cheng.
// All Rights Reserved.

// This plug-in strip extra white spaces.

package plug_ins;

import cynosurex.awt.*;
import java.util.*;

public class StripExtraWhiteSpaces
{
  // This variable specifies the string to be displayed
  // by Cyno's Editor.
  // This variable is required for all plug-ins.

  public final static String description = "Strip Extra White Spaces";

  // This constructor is the constructor
  // to be executed by Cyno's Editor.
  // This constructor is required for all plug-ins.

  public StripExtraWhiteSpaces (ShareData shared)
  {
    EnhancedTextArea eta = shared.getTextArea ();
    int start = eta.getSelectionStart ();
    int end = eta.getSelectionEnd ();
    String str = eta.getSelectedText ();

    if (start != end)
    {
      String separator = System.getProperty ("line.separator");
      StringBuffer buffer = new StringBuffer ();
      StringTokenizer lineTokens = new StringTokenizer (str, "\r\n");

      while (lineTokens.hasMoreTokens ())
      {
        StringTokenizer wordTokens = new StringTokenizer (lineTokens.nextToken ());
        while (wordTokens.hasMoreTokens ())
        {
          buffer.append (wordTokens.nextToken () + " ");
        }
        buffer.setLength (buffer.length () - 1);
        buffer.append (separator);
      }

      eta.replaceRange (buffer.toString (), start, end);
      eta.select (start, start + buffer.toString ().length ());
    }
  }
}
