1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
/*
* java-gnome, a UI library for writing GTK and GNOME programs from Java!
*
* Copyright © 2006-2010 Operational Dynamics Consulting, Pty Ltd
*
* The code in this file, and the program it is a part of, is made available
* to you by its authors as open source software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License version
* 2 ("GPL") as published by the Free Software Foundation.
*
* 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 GPL for more details.
*
* You should have received a copy of the GPL along with this program. If not,
* see http://www.gnu.org/licenses/. The authors of this program may be
* contacted through http://java-gnome.sourceforge.net/.
*/
package com.operationaldynamics.ui;
/*
* This class imported from ObjectiveAccounts accounting package where it was
* originally deployed as GPL code in generic.ui.Text with the following
* headers:
*
* Word wrap algorithm GPL code imported from xseq.ui.OverviewWindow
* Copyright (c) 2004-2005 Operational Dynamics Consulting Pty Ltd
*
* Comma padding algorithm imported from pulseSession.session.cmdHandler of
* SINS (SINS Is Not ShadNet) codebase, redistributable under the GNU GPL v2.
* Copyright (c) 1997-1998 Andrew Cowie
*/
import java.text.DecimalFormat;
/**
* This class also has numerous static methods with useful routines for doing
* basic formatting on Strings.
*
* @author Andrew Cowie
*/
public abstract class Text
{
/**
* @param str
* the String to pad.
* @param width
* maximum length of the padded result.
* @param justify
* if RIGHT, right justify. If LEFT, normal left justification.
* @return the padded String.
*/
public static String pad(String str, int width, Align justify) {
String trimmed = null;
/*
* crop
*/
int len;
if (str == null) {
len = 0;
trimmed = "";
} else {
len = str.length();
if (len > width) {
trimmed = str.substring(0, width);
len = width;
} else {
trimmed = str;
}
}
int spaces = width - len;
/*
* pad
*/
StringBuffer buf = new StringBuffer("");
if (justify == Align.LEFT) {
buf.append(trimmed);
}
for (int i = 0; i < spaces; i++) {
buf.append(" ");
}
if (justify == Align.RIGHT) {
buf.append(trimmed);
}
return buf.toString();
}
/**
* If argument is longer than width, then trim it back and add three dots.
*/
public static String chomp(String str, int width) {
if (width < 2) { // 4
throw new IllegalArgumentException(
"Can't chomp to less than a width of 2 because of adding elipses");
}
if (str == null) {
return "";
}
/*
* crop
*/
int len = str.length();
if (len > width) {
StringBuffer buf = new StringBuffer(str.substring(0, width - 1)); // 3
final int end = buf.length() - 1;
if (buf.charAt(end) == ' ') {
buf.deleteCharAt(end);
}
// We now use an ellipsis, … instead of "..."
buf.append("\u2026");
return buf.toString();
} else {
return str;
}
}
/**
* Carry out manual word wrap on a String. Normalizes all \n and \t
* characters to spaces, trims leading and training whitespace, and then
* inserts \n characters to "wrap" at the specified width boundary.
* <p>
* This code came about because unfortunately, Pango markup has no syntax
* for expressing auto word wrap, and worse, GtkCellRendererText has no
* ability to wrap text. So we (ick) do it by hand.
*
* @return a single String with newline characters inserted at appropriate
* points to cause the effect of wrapping at the specified width.
*/
public static String wrap(String str, int width) {
StringBuffer buf = new StringBuffer(str);
int index;
/*
* normalize any existing IFS characters to spaces
*/
while ((index = buf.indexOf("\n")) != -1) {
buf.setCharAt(index, ' ');
}
while ((index = buf.indexOf("\t")) != -1) {
buf.setCharAt(index, ' ');
}
/*
* trim. Yes, I know about String.trim(), but we've already done half
* the work it does; no need to be inefficient.
*/
while (buf.charAt(0) == ' ') {
buf.deleteCharAt(0);
}
while (buf.charAt(buf.length() - 1) == ' ') {
buf.deleteCharAt(buf.length() - 1);
}
while ((index = buf.indexOf(" ")) != -1) {
buf.deleteCharAt(index);
}
/*
* word wrap.
*/
int next_space = 0;
int line_start = 0;
while (next_space != -1) {
if ((next_space - line_start) > width) {
buf.setCharAt(next_space, '\n');
line_start = next_space;
}
next_space = buf.indexOf(" ", next_space + 1); // bounds?
}
return buf.toString();
}
/**
* Pad and justify a long. This is useful for displaying the memory usage
* numbers from Runtime.freeMemory() and Runtime.totalMemory()
*
* @param num
* a number to render as a comma padded string.
* @return a 25 character wide string with the number right justified wnd
* with comma characters at thousand marks.
*/
/*
* from pulseSession.session.cmdHandler
*/
public static String padComma(long num) {
DecimalFormat df = new DecimalFormat("#,###,###,###,###,###,###");
StringBuffer comma = new StringBuffer(df.format(num));
int pad = 25 - comma.length();
for (int i = 0; i < pad; i++) {
comma.insert(0, ' ');
}
return comma.toString();
}
}
|