2 * JCarder -- cards Java programs to keep threads disentangled
4 * Copyright (C) 2006-2007 Enea AB
5 * Copyright (C) 2007 Ulrik Svensson
6 * Copyright (C) 2007 Joel Rosdahl
8 * This program is made available under the GNU GPL version 2, with a special
9 * exception for linking with JUnit. See the accompanying file LICENSE.txt for
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE.
17 package com.enea.jcarder.util;
19 import java.util.ArrayList;
20 import java.util.List;
23 * This is a helper class for an OptionParser.
25 class OptionFormatter {
26 private final int mOptionIndent;
27 private final int mDescrIndent;
28 private final int mMaxWidth;
30 public OptionFormatter(int optionIndent, int descrIndent, int maxWidth) {
31 mOptionIndent = optionIndent;
32 mDescrIndent = descrIndent;
36 public void format(StringBuilder sb, String option, String descr) {
37 addSpace(sb, mOptionIndent);
39 final int firstIndent;
40 if (mOptionIndent + option.length() >= mDescrIndent) {
42 firstIndent = mDescrIndent;
44 firstIndent = mDescrIndent - (mOptionIndent + option.length());
49 List<String> descrLines = wrapText(descr, mMaxWidth - mDescrIndent);
50 indentText(sb, descrLines, mDescrIndent, firstIndent);
54 static void addSpace(StringBuilder sb, int n) {
55 for (int i = 0; i < n; ++i) {
60 static List<String> wrapText(String text, int maxWidth) {
61 ArrayList<String> result = new ArrayList<String>();
63 StringBuilder sb = new StringBuilder();
65 for (String word : text.split("\\s+")) {
66 if (sb.length() > 0 && word.length() + 1 > maxWidth - sb.length()) {
67 result.add(sb.toString());
72 if (sb.length() > 0) {
79 if (sb.length() > 0) {
80 result.add(sb.toString());
86 static void indentText(StringBuilder sb,
91 addSpace(sb, firstIndent);
92 for (String line : lines) {