2 * (C) Copyright IBM Corp. 1998-2005. All Rights Reserved.
\r
4 * The program is provided "as is" without any warranty express or
\r
5 * implied, including the warranty of non-infringement and the implied
\r
6 * warranties of merchantibility and fitness for a particular purpose.
\r
7 * IBM will not be liable for any damages suffered by you as a result
\r
8 * of using the Program. In no event will IBM be liable for any
\r
9 * special, indirect or consequential damages or lost profits even if
\r
10 * IBM has been advised of the possibility of their occurrence. IBM
\r
11 * will not be liable for any third party claims against you.
\r
13 package com.ibm.richtext.textpanel;
\r
15 import java.awt.Color;
\r
16 import java.io.Serializable;
\r
17 import java.util.Hashtable;
\r
19 import com.ibm.richtext.textlayout.attributes.AttributeMap;
\r
20 import com.ibm.richtext.textlayout.attributes.TextAttribute;
\r
21 import com.ibm.richtext.styledtext.StandardTabRuler;
\r
24 * This class contains settings used when constructing an MTextPanel.
\r
25 * The settings controled by this class include:
\r
27 * <li>whether the text in the MTextPanel can be scrolled</li>
\r
28 * <li>whether scroll bars in the MTextPanel are visible</li>
\r
29 * <li>whether the text in the MTextPanel can be selected</li>
\r
30 * <li>whether the text in the MTextPanel can be edited</li>
\r
31 * <li>whether lines of text wrap to the MTextPanel's width, or
\r
32 * only end at paragraph separators</li>
\r
33 * <li>the default values for unspecified styles</li>
\r
35 * Some settings are dependent on others. Scroll bars are visible
\r
36 * only if the text is scrollable. Also, text which is not editable
\r
37 * if it is not selectable.
\r
42 public final class TextPanelSettings implements Cloneable, Serializable {
\r
44 static final String COPYRIGHT =
\r
45 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
\r
47 static final long serialVersionUID = -4089186663018707478L;
\r
49 private static final AttributeMap DEFAULTS;
\r
52 final Float floatZero = new Float(0.0f);
\r
54 Hashtable defaults = new Hashtable();
\r
55 defaults.put(TextAttribute.FAMILY, "Serif");
\r
56 defaults.put(TextAttribute.WEIGHT, new Float(1.0f));
\r
57 defaults.put(TextAttribute.POSTURE, floatZero);
\r
58 defaults.put(TextAttribute.SIZE, new Float(18.0f));
\r
59 defaults.put(TextAttribute.SUPERSCRIPT, new Integer(0));
\r
60 defaults.put(TextAttribute.FOREGROUND, Color.black);
\r
61 defaults.put(TextAttribute.UNDERLINE, new Integer(-1));
\r
62 defaults.put(TextAttribute.STRIKETHROUGH, Boolean.FALSE);
\r
64 defaults.put(TextAttribute.EXTRA_LINE_SPACING, floatZero);
\r
65 defaults.put(TextAttribute.FIRST_LINE_INDENT, floatZero);
\r
66 defaults.put(TextAttribute.MIN_LINE_SPACING, floatZero);
\r
67 defaults.put(TextAttribute.LINE_FLUSH, TextAttribute.FLUSH_LEADING);
\r
68 defaults.put(TextAttribute.LEADING_MARGIN, floatZero);
\r
69 defaults.put(TextAttribute.TRAILING_MARGIN, floatZero);
\r
70 defaults.put(TextAttribute.TAB_RULER, new StandardTabRuler());
\r
72 DEFAULTS = new AttributeMap(defaults);
\r
75 private boolean fScrollable = true;
\r
76 private boolean fScrollBarsVisible = true;
\r
77 private boolean fSelectable = true;
\r
78 private boolean fEditable = true;
\r
79 private boolean fWraps = true;
\r
80 private AttributeMap fDefaultValues = DEFAULTS;
\r
83 * Create a TextPanelSettings instance with all settings
\r
86 public TextPanelSettings() {
\r
90 * Return a new TextPanelSettings instance with the
\r
91 * same settings as this.
\r
92 * @return a new TextPanelSettings instance
\r
94 public Object clone() {
\r
96 TextPanelSettings rhs = new TextPanelSettings();
\r
98 rhs.fScrollable = fScrollable;
\r
99 rhs.fScrollBarsVisible = fScrollBarsVisible;
\r
100 rhs.fSelectable = fSelectable;
\r
101 rhs.fEditable = fEditable;
\r
102 rhs.fWraps = fWraps;
\r
103 rhs.fDefaultValues = fDefaultValues;
\r
109 * Return the scrollable setting, which determines whether text
\r
110 * in an MTextPanel can be scrolled.
\r
111 * @return the scrollable setting
\r
113 public boolean getScrollable() {
\r
115 return fScrollable;
\r
119 * Set the scrollable setting.
\r
120 * @param scrollable the scrollable setting. If false,
\r
121 * the scrollBarsVisible setting is also set to false.
\r
123 public void setScrollable(boolean scrollable) {
\r
125 fScrollable = scrollable;
\r
126 fScrollBarsVisible &= scrollable;
\r
130 * Return the scrollBarsVisible setting, which determines whether
\r
131 * scroll bars in an MTextPanel are visible.
\r
132 * @return the scrollBarsVisible setting
\r
134 public boolean getScrollBarsVisible() {
\r
136 return fScrollBarsVisible;
\r
140 * Set the scrollBarsVisible setting.
\r
141 * @param vis the scrollBarsVisible setting. If true,
\r
142 * the scrollable setting is also set to true.
\r
144 public void setScrollBarsVisible(boolean vis) {
\r
146 fScrollBarsVisible = vis;
\r
147 fScrollable |= vis;
\r
151 * Return the selectable setting, which determines whether
\r
152 * text in an MTextPanel can be selected.
\r
153 * @return the selectable setting
\r
155 public boolean getSelectable() {
\r
157 return fSelectable;
\r
161 * Set the selectable setting.
\r
162 * @param selectable the selectable setting. If false,
\r
163 * the editable setting is also set to false.
\r
165 public void setSelectable(boolean selectable) {
\r
167 fSelectable = selectable;
\r
168 fEditable &= selectable;
\r
172 * Return the editable setting, which determines whether
\r
173 * text in an MTextPanel can be edited.
\r
174 * @return the editable setting
\r
176 public boolean getEditable() {
\r
182 * Set the editable setting.
\r
183 * @param editable the editable setting. If true,
\r
184 * the selectable setting is also set to true.
\r
186 public void setEditable(boolean editable) {
\r
188 fEditable = editable;
\r
189 fSelectable |= editable;
\r
193 * Return the wraps setting, which determines whether
\r
194 * lines of text wrap to the length of the MTextPanel,
\r
195 * or only at paragraph separators.
\r
196 * @return the wraps setting
\r
198 public boolean getWraps() {
\r
204 * Set the wraps setting.
\r
205 * @param wraps the wraps setting
\r
207 public void setWraps(boolean wraps) {
\r
213 * Return the AttributeMap of default values for certain keys.
\r
214 * When a key in this AttributeMap is not specified, its value
\r
215 * is taken from this AttributeMap.
\r
216 * @return the AttributeMap of default values
\r
217 * @see MTextPanel#getDefaultValues
\r
219 public AttributeMap getDefaultValues() {
\r
221 return fDefaultValues;
\r
225 * Add the key-value pairs in the given AttributeMap to the
\r
226 * default values. If a key does not appear in the given
\r
227 * AttributeMap, its value in the default value map is
\r
229 * @param map an AttributeMap containing new default values
\r
231 public void addDefaultValues(AttributeMap map) {
\r
233 fDefaultValues = fDefaultValues.addAttributes(map);
\r
237 * Compare this to another Object. This is equal
\r
238 * to another Object if the other Object is a
\r
239 * TextPanelSettings instance with the same
\r
240 * settings as this one.
\r
241 * @param rhs the Object to compare to
\r
243 public boolean equals(Object rhs) {
\r
253 TextPanelSettings other;
\r
255 other = (TextPanelSettings) rhs;
\r
257 catch(ClassCastException e) {
\r
261 return other.fScrollable == this.fScrollable &&
\r
262 other.fScrollBarsVisible == this.fScrollBarsVisible &&
\r
263 other.fSelectable == this.fSelectable &&
\r
264 other.fEditable == this.fEditable &&
\r
265 other.fWraps == this.fWraps &&
\r
266 other.fDefaultValues.equals(this.fDefaultValues);
\r
270 * Return the hash code for this Object.
\r
271 * @return the hash code for this Object
\r
273 public int hashCode() {
\r
275 int code = fDefaultValues.hashCode();
\r
276 code = code*2 + (fScrollable? 1:0);
\r
277 code = code*2 + (fScrollBarsVisible? 1:0);
\r
278 code = code*2 + (fSelectable? 1:0);
\r
279 code = code*2 + (fEditable? 1:0);
\r
280 code = code*2 + (fWraps? 1:0);
\r