Here you can find the source of splitStringWithLength(String s, int length, FontMetrics fm)
Parameter | Description |
---|---|
s | the string |
length | the length |
fm | the font metrics |
public final static String[] splitStringWithLength(String s, int length, FontMetrics fm)
//package com.java2s; /******************************************************************************* * GenPlay, Einstein Genome Analyzer/* ww w . j a v a 2s . co m*/ * Copyright (C) 2009, 2014 Albert Einstein College of Medicine * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * Authors: Julien Lajugie <julien.lajugie@einstein.yu.edu> * Nicolas Fourel <nicolas.fourel@einstein.yu.edu> * Eric Bouhassira <eric.bouhassira@einstein.yu.edu> * * Website: <http://genplay.einstein.yu.edu> ******************************************************************************/ import java.awt.FontMetrics; import java.util.ArrayList; import java.util.List; public class Main { /** * This methods will split a String in different lines according to a maximum length. * It does split in middle of a word but at a whitespace if necessary * @param s the string * @param length the length * @param fm the font metrics * @return the formatted line */ public final static String[] splitStringWithLength(String s, int length, FontMetrics fm) { List<String> result = new ArrayList<String>(); String[] array = split(s, ' '); String current = array[0]; int currentLength = fm.stringWidth(current); for (int i = 1; i < array.length; i++) { String tmp = array[i]; currentLength += fm.stringWidth(" " + tmp); if (currentLength < length) { current += " " + tmp; } else { result.add(current); current = tmp; currentLength = fm.stringWidth(tmp); } } result.add(current); String[] arrayResult = new String[result.size()]; for (int i = 0; i < arrayResult.length; i++) { arrayResult[i] = result.get(i); } return arrayResult; } /** * Split a string using the char code of a character. * @param s the string to split * @param c the integer code of the character * @return an array containing the split string (or empty if the string is null) */ public final static String[] split(String s, int c) { List<String> list = new ArrayList<String>(); if (s != null) { int pos = 0, end; while ((end = s.indexOf(c, pos)) >= 0) { String sub = s.substring(pos, end); list.add(sub); pos = end + 1; } list.add(s.substring(pos)); } if (list.isEmpty()) { return null; } int size = list.size(); if (list.get(size - 1).isEmpty()) { size--; } String[] result = new String[size]; for (int i = 0; i < size; i++) { result[i] = list.get(i); } return result; } }