1   package org.wcb.gui.component;
2   
3   import javax.swing.*;
4   import javax.swing.text.*;
5   import java.awt.*;
6   
7   /**
8    * <small>
9    * <p/>
10   * Copyright (C)  2006  wbogaardt.
11   * This library is free software; you can redistribute it and/or
12   * modify it under the terms of the GNU Lesser General Public
13   * License as published by the Free Software Foundation; either
14   * version 2.1 of the License, or (at your option) any later version.
15   * <p/>
16   * This library is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19   * Lesser General Public License for more details.
20   * <p/>
21   * You should have received a copy of the GNU Lesser General Public
22   * License along with this library; if not, write to the Free Software
23   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
24   * <p/>
25   * $File:  $ <br>
26   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Nov 7, 2006 1:29:39 PM $ <br>
27   * </small>
28   *
29   * @author wbogaardt
30   *         This is to display simple messages as a text pane.
31   */
32  
33  public class JMessageTextPane {
34  
35      private static final String NEWLINE = "\n";
36  
37      private String[] stringInit;
38      private Color backgroundColor = new Color(0xefd67a);
39      private JTextPane textPane;
40  
41      public JMessageTextPane() {
42          textPane = new JTextPane();
43      }
44  
45      public JMessageTextPane(String[] textString) {
46          this.stringInit = textString;
47          textPane = new JTextPane();
48      }
49  
50      public void setBackground(Color color) {
51          this.backgroundColor = color;
52      }
53  
54      public JTextPane getTextPane() {
55          textPane.setBackground(this.backgroundColor);
56          StyledDocument doc = textPane.getStyledDocument();
57          Style def = StyleContext.getDefaultStyleContext().
58                  getStyle(StyleContext.DEFAULT_STYLE);
59          doc.addStyle("regular", def);
60          StyleConstants.setFontFamily(def, "SansSerif");
61  
62  
63          try {
64              for (String aStringInit : stringInit) {
65                  doc.insertString(doc.getLength(), aStringInit + NEWLINE,
66                          doc.getStyle("regular"));
67              }
68          } catch (BadLocationException ble) {
69              System.err.println("Bad document error in JMessageTextPane " + ble);
70          }
71          textPane.setEditable(false);
72          return textPane;
73      }
74  
75      public JTextPane createTextPane(String[] initString) {
76          textPane.setBackground(this.backgroundColor);
77          StyledDocument doc = textPane.getStyledDocument();
78          Style def = StyleContext.getDefaultStyleContext().
79                  getStyle(StyleContext.DEFAULT_STYLE);
80          doc.addStyle("regular", def);
81          StyleConstants.setFontFamily(def, "SansSerif");
82  
83  
84          try {
85              for (int i=0; i < initString.length; i++) {
86                  doc.insertString(doc.getLength(), initString[i] + NEWLINE,
87                          doc.getStyle("regular"));
88              }
89          } catch (BadLocationException ble) {
90              System.err.println("Bad document error in JMessageTextPane " + ble);
91          }
92          textPane.setEditable(false);
93          return textPane;
94      }
95  }