Java tutorial
//package com.java2s; /* * AnyPlace: A free and open Indoor Navigation Service with superb accuracy! * * Anyplace is a first-of-a-kind indoor information service offering GPS-less * localization, navigation and search inside buildings using ordinary smartphones. * * Author(s): Lambros Petrou, Timotheos Constambeys * * Supervisor: Demetrios Zeinalipour-Yazti * * URL: http://anyplace.cs.ucy.ac.cy * Contact: anyplace@cs.ucy.ac.cy * * Copyright (c) 2015, Data Management Systems Lab (DMSL), University of Cyprus. * All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in the * Software without restriction, including without limitation the rights to use, copy, * modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, * and to permit persons to whom the Software is furnished to do so, subject to the * following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. * */ import android.text.TextPaint; public class Main { public static String fillTextBox(TextPaint paint, int fragmentWidth, String source) { StringBuilder sb = new StringBuilder(); final int length = source.length(); // Display whole words only int lastWhiteSpace = 0; for (int index = 0; paint.measureText(sb.toString()) < fragmentWidth && index < length; index++) { char c = source.charAt(index); if (Character.isWhitespace(c)) lastWhiteSpace = index; sb.append(c); } if (sb.length() != length) { // Delete last word part sb.delete(lastWhiteSpace, sb.length()); sb.append("..."); } return sb.toString(); } public static String fillTextBox(TextPaint paint, int fragmentWidth, String source, int start) { StringBuilder sb = new StringBuilder(); final int length = source.length(); int indexLeft = start; int indexRight = start + 1; int lastWhiteSpaceL = 0; int lastWhiteSpaceR = 0; while (paint.measureText(sb.toString()) < fragmentWidth && (indexLeft >= 0 || indexRight < length)) { if (indexLeft >= 0) { char c = source.charAt(indexLeft); if (Character.isWhitespace(c)) lastWhiteSpaceL = indexLeft; sb.insert(0, c); indexLeft--; } if (indexRight < length) { char c = source.charAt(indexRight); if (Character.isWhitespace(c)) lastWhiteSpaceR = indexRight; sb.append(c); indexRight++; } } if (indexLeft >= 0) { // Delete first word part sb.delete(0, lastWhiteSpaceL - indexLeft); sb.insert(0, "..."); indexLeft = lastWhiteSpaceL - 3; // Set new index left } if (indexRight < length) { // Delete last word part sb.delete(lastWhiteSpaceR - (indexLeft + 1), sb.length()); sb.append("..."); } return sb.toString(); } }