001: /*
002: * Copyright (c) 2005-2008 Substance Kirill Grouchnikov. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of Substance Kirill Grouchnikov nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030: package org.jvnet.substance.painter.text;
031:
032: import java.awt.Component;
033: import java.awt.Rectangle;
034: import java.util.LinkedList;
035: import java.util.List;
036:
037: import javax.swing.JComponent;
038:
039: import org.jvnet.substance.utils.SubstanceCoreUtilities;
040:
041: /**
042: * Base implementation of {@link SubstanceTextPainter} that provides common
043: * functionality for core text painters.
044: *
045: * @author Kirill Grouchnikov
046: */
047: public abstract class AbstractTextPainter implements
048: SubstanceTextPainter {
049: /**
050: * Information on a single text line.
051: *
052: * @author Kirill Grouchnikov
053: */
054: public static class TextLineInfo {
055: /**
056: * Text rectangle.
057: */
058: public Rectangle textRect;
059:
060: /**
061: * The text itself.
062: */
063: public String text;
064:
065: /**
066: * Mnemonic index. Can be -1 if no mnemonic.
067: */
068: public int mnemonicIndex;
069:
070: /**
071: * Font for the text.
072: */
073: public java.awt.Font font;
074:
075: /**
076: * Color for the text.
077: */
078: public java.awt.Color color;
079:
080: /**
081: * Clipping rectangle for the text.
082: */
083: public java.awt.Rectangle clip;
084:
085: /**
086: * Transformation for the text.
087: */
088: public java.awt.geom.AffineTransform transform;
089:
090: /**
091: * Creates a new text line info object.
092: *
093: * @param textRect
094: * Text rectangle.
095: * @param text
096: * The text itself.
097: * @param mnemonicIndex
098: * Mnemonic index. Can be -1 if no mnemonic.
099: * @param font
100: * Font for the text.
101: * @param color
102: * Color for the text.
103: * @param clip
104: * Clipping rectangle for the text.
105: */
106: public TextLineInfo(Rectangle textRect, String text,
107: int mnemonicIndex, java.awt.Font font,
108: java.awt.Color color, java.awt.Rectangle clip,
109: java.awt.geom.AffineTransform transform) {
110: this .textRect = textRect;
111: this .text = text;
112: this .mnemonicIndex = mnemonicIndex;
113: this .font = font;
114: this .color = color;
115: this .clip = clip;
116: this .transform = transform;
117: }
118: }
119:
120: /**
121: * Attached background painting callbacks.
122: */
123: protected List<SubstanceTextPainter.BackgroundPaintingCallback> callbackList;
124:
125: /**
126: * Attached texts.
127: */
128: protected List<TextLineInfo> textLines;
129:
130: /**
131: * If <code>true</code>, the painter implementation will fill the
132: * background and invoke all callbacks even when there are no attached texts
133: * or all texts are empty.
134: */
135: protected boolean toEnforceRenderOnNoTexts;
136:
137: protected Component comp;
138:
139: /**
140: * Creates a new abstract text painter. Is protected to enforce that it
141: * can't be directly created.
142: */
143: protected AbstractTextPainter() {
144: this .callbackList = new LinkedList<SubstanceTextPainter.BackgroundPaintingCallback>();
145: this .textLines = new LinkedList<TextLineInfo>();
146: }
147:
148: /*
149: * (non-Javadoc)
150: *
151: * @see org.jvnet.substance.painter.text.SubstanceTextPainter#init(javax.swing.JComponent,
152: * java.awt.Rectangle, boolean)
153: */
154: public void init(JComponent comp, Rectangle clip,
155: boolean toEnforceRenderOnNoTexts) {
156: if (!this .toIgnoreBackgroundFill(comp))
157: this .callbackList.clear();
158: this .comp = comp;
159: this .textLines.clear();
160: this .toEnforceRenderOnNoTexts = toEnforceRenderOnNoTexts;
161: }
162:
163: /*
164: * (non-Javadoc)
165: *
166: * @see org.jvnet.substance.painter.text.SubstanceTextPainter#attachCallback(org.jvnet.substance.painter.text.SubstanceTextPainter.BackgroundPaintingCallback)
167: */
168: public void attachCallback(
169: SubstanceTextPainter.BackgroundPaintingCallback backgroundPaintingCallback) {
170: this .callbackList.add(backgroundPaintingCallback);
171: }
172:
173: /*
174: * (non-Javadoc)
175: *
176: * @see org.jvnet.substance.painter.text.SubstanceTextPainter#attachText(javax.swing.JComponent,
177: * java.awt.Rectangle, java.lang.String, int, java.awt.Font,
178: * java.awt.Color, java.awt.Rectangle, java.awt.geom.AffineTransform)
179: */
180: public void attachText(JComponent comp, Rectangle textRect,
181: String text, int mnemonicIndex, java.awt.Font font,
182: java.awt.Color color, java.awt.Rectangle clip) {
183: this .textLines.add(new TextLineInfo(textRect, text,
184: mnemonicIndex, font, color, clip, null));
185: }
186:
187: /**
188: * Checks whether the background fill should be skipped for the specified
189: * component.
190: *
191: * @param comp
192: * Component.
193: * @return <code>true</code> if the background fill should be skipped for
194: * the specified component, <code>false</code> otherwise.
195: */
196: protected boolean toIgnoreBackgroundFill(Component comp) {
197: Component c = comp;
198: while (c != null) {
199: if (c instanceof JComponent) {
200: JComponent jc = (JComponent) c;
201: if (Boolean.TRUE
202: .equals(jc
203: .getClientProperty(SubstanceCoreUtilities.DO_NOT_FILL_BACKGROUND))) {
204: return true;
205: }
206: }
207: c = c.getParent();
208: }
209: return false;
210: }
211: }
|