KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > text > Banner


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.text;
5
6 public class Banner {
7
8   public static void errorBanner(String JavaDoc message) {
9     System.err.println(makeBanner(message, "ERROR"));
10     System.err.flush();
11   }
12
13   public static void warnBanner(String JavaDoc message) {
14     System.err.println(makeBanner(message, "WARNING"));
15     System.err.flush();
16   }
17
18   private static final int MAX_LINE = 72;
19   private static final int BOX_WIDTH = MAX_LINE + 4;
20
21   private static String JavaDoc makeBanner(String JavaDoc message, String JavaDoc type) {
22     if (message == null) {
23       message = "<no message>";
24     }
25
26     final int topStars = BOX_WIDTH - (type.length() + 2); // +2 for spaces on either side of "type"
27
final int begin = topStars / 2;
28     final int end = (topStars % 2 == 0) ? begin : begin + 1;
29
30     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
31     buf.append("\n");
32
33     for (int i = 0; i < begin; i++) {
34       buf.append('*');
35     }
36
37     buf.append(' ').append(type).append(' ');
38
39     for (int i = 0; i < end; i++) {
40       buf.append('*');
41     }
42
43     String JavaDoc[] words = message.split(" ");
44
45     int word = 0;
46     while (word < words.length) {
47       int length = words[word].length();
48       buf.append("\n* ").append(words[word]);
49       word++;
50
51       while (length <= MAX_LINE && word < words.length) {
52         int next = words[word].length() + 1; // +1 for space
53
if (length + next <= MAX_LINE) {
54           buf.append(' ').append(words[word++]);
55           length += next;
56         } else {
57           break;
58         }
59       }
60     }
61
62     buf.append("\n");
63     for (int i = 0; i < BOX_WIDTH; i++) {
64       buf.append('*');
65     }
66     buf.append("\n");
67
68     return buf.toString();
69   }
70 }
71
Popular Tags