File Source: TextLabel.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.components.text.TextLabel__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.dmdirc.addons.ui_swing.components.text;
24
25 import java.awt.Font;
26 import java.awt.Insets;
27
28 import javax.swing.JTextPane;
29 import javax.swing.UIManager;
30 import javax.swing.plaf.basic.BasicTextPaneUI;
31 import javax.swing.text.DefaultStyledDocument;
32 import javax.swing.text.SimpleAttributeSet;
33 import javax.swing.text.StyleConstants;
34 import javax.swing.text.StyledDocument;
35 import javax.swing.text.html.HTMLDocument;
36 import javax.swing.text.html.HTMLEditorKit;
37
38 /**
39 * Dyamnic text label.
40 */
/*
P/P * Method: Document getDocument()
*
* Postconditions:
* init'ed(return_value)
*/
41 public class TextLabel extends JTextPane {
42
43 /**
44 * A version number for this class. It should be changed whenever the
45 * class structure is changed (or anything else that would prevent
46 * serialized objects being unserialized with the new class).
47 */
48 private static final long serialVersionUID = 1;
49 /** Simple attribute set. */
50 private SimpleAttributeSet sas;
51
52 /**
53 * Creates a new instance of TextLabel.
54 */
55 public TextLabel() {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.text.TextLabel()
*
* Postconditions:
* this.sas == &new SimpleAttributeSet(TextLabel#6)
* new SimpleAttributeSet(TextLabel#6) num objects == 1
*/
56 this(null, true);
57 }
58
59 /**
60 * Creates a new instance of TextLabel.
61 *
62 * @param text Text to display
63 */
64 public TextLabel(final String text) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.text.TextLabel(String)
*
* Postconditions:
* this.sas == &new SimpleAttributeSet(TextLabel#6)
* new SimpleAttributeSet(TextLabel#6) num objects == 1
*/
65 this(text, true);
66 }
67
68 /**
69 * Creates a new instance of TextLabel.
70 *
71 * @param text Text to display
72 * @param justified Justify the text?
73 */
74 public TextLabel(final String text, final boolean justified) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.text.TextLabel(String, bool)
*
* Presumptions:
* javax.swing.JTextPane:getDocument(...)@101 != null
* javax.swing.UIManager:getFont(...)@79 != null
* javax.swing.text.html.HTMLDocument:getStyleSheet(...)@80 != null
* javax.swing.text.html.HTMLDocument:getStyleSheet(...)@83 != null
*
* Postconditions:
* this.sas == &new SimpleAttributeSet(TextLabel#6)
* new SimpleAttributeSet(TextLabel#6) num objects == 1
*
* Test Vectors:
* justified: {0}, {1}
*/
75 super(new DefaultStyledDocument());
76 setEditorKit(new HTMLEditorKit());
77 setUI(new BasicTextPaneUI());
78
79 final Font font = UIManager.getFont("Label.font");
80 ((HTMLDocument) getDocument()).getStyleSheet().addRule("body " +
81 "{ font-family: " + font.getFamily() + "; " + "font-size: " +
82 font.getSize() + "pt; }");
83 ((HTMLDocument) getDocument()).getStyleSheet().addRule("p { margin: 0; }");
84
85 setOpaque(false);
86 setEditable(false);
87 setHighlighter(null);
88 setMargin(new Insets(0, 0, 0, 0));
89
90 sas = new SimpleAttributeSet();
91 if (justified) {
92 StyleConstants.setAlignment(sas, StyleConstants.ALIGN_JUSTIFIED);
93 }
94
95 setText(text);
96 }
97
98 /** {@inheritDoc} */
99 @Override
100 public StyledDocument getDocument() {
/*
P/P * Method: StyledDocument getDocument()
*
* Postconditions:
* init'ed(return_value)
*/
101 return (StyledDocument) super.getDocument();
102 }
103
104 /** {@inheritDoc} */
105 @Override
106 public void setText(final String t) {
/*
P/P * Method: void setText(String)
*
* Preconditions:
* (soft) init'ed(this.sas)
*
* Presumptions:
* javax.swing.JTextPane:getDocument(...)@101 != null
*
* Test Vectors:
* t: Addr_Set{null}, Inverse{null}
* java.lang.String:isEmpty(...)@108: {1}, {0}
*/
107 super.setText(t);
108 if (t != null && !t.isEmpty()) {
109 getDocument().setParagraphAttributes(0, t.length(), sas, false);
110 }
111 }
112 }
SofCheck Inspector Build Version : 2.17854
| TextLabel.java |
2009-Jun-25 01:54:24 |
| TextLabel.class |
2009-Sep-02 17:04:14 |