Wrap multi-line strings (and get the individual lines) : BreakIterator « I18N « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » I18N » BreakIteratorScreenshots 
Wrap multi-line strings (and get the individual lines)
   
/*
 * $Id: Utilities.java,v 1.11 2008/10/14 22:31:46 rah003 Exp $
 *
 * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle,
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 *
 * 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
 */

import java.text.BreakIterator;
import java.util.StringTokenizer;

/**
 * Contribution from NetBeans: Issue #319-swingx.
 * <p>
 
 * PENDING: need to reconcile with OS, JVM... added as-is because needed the
 * shortcut handling to fix #
 
 @author apple
 */
public class Utils {

  /**
   * Wrap multi-line strings (and get the individual lines).
   
   @param original
   *          the original string to wrap
   @param width
   *          the maximum width of lines
   @param breakIterator
   *          breaks original to chars, words, sentences, depending on what
   *          instance you provide.
   @param removeNewLines
   *          if <code>true</code>, any newlines in the original string are
   *          ignored
   @return the lines after wrapping
   */
  public static String[] wrapStringToArray(String original, int width, BreakIterator breakIterator,
      boolean removeNewLines) {
    if (original.length() == 0) {
      return new String[] { original };
    }

    String[] workingSet;

    // substitute original newlines with spaces,
    // remove newlines from head and tail
    if (removeNewLines) {
      original = trimString(original);
      original = original.replace('\n'' ');
      workingSet = new String[] { original };
    else {
      StringTokenizer tokens = new StringTokenizer(original, "\n")// NOI18N
      int len = tokens.countTokens();
      workingSet = new String[len];

      for (int i = 0; i < len; i++) {
        workingSet[i= tokens.nextToken();
      }
    }

    if (width < 1) {
      width = 1;
    }

    if (original.length() <= width) {
      return workingSet;
    }

    widthcheck: {
      boolean ok = true;

      for (int i = 0; i < workingSet.length; i++) {
        ok = ok && (workingSet[i].length() < width);

        if (!ok) {
          break widthcheck;
        }
      }

      return workingSet;
    }

    java.util.ArrayList<String> lines = new java.util.ArrayList<String>();

    int lineStart = 0// the position of start of currently processed line in
                        // the original string

    for (int i = 0; i < workingSet.length; i++) {
      if (workingSet[i].length() < width) {
        lines.add(workingSet[i]);
      else {
        breakIterator.setText(workingSet[i]);

        int nextStart = breakIterator.next();
        int prevStart = 0;

        do {
          while (((nextStart - lineStart< width&& (nextStart != BreakIterator.DONE)) {
            prevStart = nextStart;
            nextStart = breakIterator.next();
          }

          if (nextStart == BreakIterator.DONE) {
            nextStart = prevStart = workingSet[i].length();
          }

          if (prevStart == 0) {
            prevStart = nextStart;
          }

          lines.add(workingSet[i].substring(lineStart, prevStart));

          lineStart = prevStart;
          prevStart = 0;
        while (lineStart < workingSet[i].length());

        lineStart = 0;
      }
    }

    String[] s = new String[lines.size()];

    return (String[]) lines.toArray(s);
  }

  private static String trimString(String s) {
    int idx = 0;
    char c;
    final int slen = s.length();

    if (slen == 0) {
      return s;
    }

    do {
      c = s.charAt(idx++);
    while (((c == '\n'|| (c == '\r')) && (idx < slen));

    s = s.substring(--idx);
    idx = s.length() 1;

    if (idx < 0) {
      return s;
    }

    do {
      c = s.charAt(idx--);
    while (((c == '\n'|| (c == '\r')) && (idx >= 0));

    return s.substring(0, idx + 2);
  }
}

   
    
    
  
Related examples in the same category
1. BreakIterator for difference localesBreakIterator for difference locales
2. BreakIterator DemoBreakIterator Demo
3. Determining the Character Boundaries in a Unicode String
4. Determining the Word Boundaries in a Unicode String
5. Determining the Sentence Boundaries in a Unicode String
6. Determining Potential Line Breaks in a Unicode String
7. Searching, Sorting, and Text Boundary Detection: Detecting Text BoundariesSearching, Sorting, and Text Boundary Detection: Detecting Text Boundaries
8. Behaves similiar to BreakIterator.getWordInstance() but handles line break delimeters as simple whitespaces.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.