001: /*
002: * Copyright (c) 2004 JETA Software, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without modification,
005: * 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 JETA Software nor the names of its contributors may
015: * be used to endorse or promote products derived from this software without
016: * 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, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
021: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
022: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
023: * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
024: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
025: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026: * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029:
030: package com.jeta.forms.store.properties;
031:
032: import java.awt.Color;
033: import java.awt.Component;
034: import java.awt.Font;
035: import java.io.IOException;
036:
037: import javax.swing.BorderFactory;
038: import javax.swing.JComponent;
039: import javax.swing.border.Border;
040: import javax.swing.border.TitledBorder;
041:
042: import com.jeta.forms.gui.beans.JETABean;
043: import com.jeta.forms.store.JETAObjectInput;
044: import com.jeta.forms.store.JETAObjectOutput;
045:
046: /**
047: * Base class for all border properties. Stores common border attributes.
048: *
049: * @author Jeff Tassin
050: */
051: public class BorderProperty extends JETAProperty {
052: static final long serialVersionUID = -9007148666135189228L;
053:
054: /**
055: * The version of this class.
056: */
057: public static final int VERSION = 4;
058:
059: /**
060: * Flags that indicate which part of the border should be painted.
061: * Currently, only LineBorders support this capability.
062: */
063: private boolean m_top = true;
064:
065: private boolean m_left = true;
066:
067: private boolean m_right = true;
068:
069: private boolean m_bottom = true;
070:
071: /**
072: * Set to true if this border has a title
073: */
074: private boolean m_include_title;
075:
076: /**
077: * The title text.
078: */
079: private String m_title;
080:
081: /**
082: * The title justification: TitledBorder.LEFT, CENTER, RIGHT, LEADING,
083: * TRAILING, DEFAULT_JUSTICATION
084: */
085: private int m_justification = TitledBorder.DEFAULT_JUSTIFICATION;
086:
087: /**
088: * The title position
089: * TitledBorder.ABOVE_TOP,TOP,BELOW_TOP,ABOVE_BOTTOM,BOTTOM,BELOW_BOTTOM,DEFAULT_POSITION
090: * (top)
091: */
092: private int m_position = TitledBorder.DEFAULT_POSITION;
093:
094: /**
095: * Default title text color
096: */
097: private static ColorProperty DEFAULT_TEXT_COLOR = new ColorProperty(
098: "TitledBorder.titleColor");
099:
100: /**
101: * The title text color.
102: */
103: private ColorProperty m_text_color = new ColorProperty(
104: "TitledBorder.titleColor");
105:
106: /**
107: * The name for this property.
108: */
109: public static final String PROPERTY_ID = "border";
110:
111: /**
112: * Creates an unitialized <code>BorderProperty</code> instance.
113: */
114: public BorderProperty() {
115: super (PROPERTY_ID);
116: }
117:
118: /**
119: * Creates a Swing border instance based on the values in this
120: * BorderProperty instance.
121: *
122: * @param comp
123: * the component that is the basis for the border. This is
124: * normally ignored except when assigning a default border on a
125: * component. The comp instance is used to obtains the default
126: * border.
127: * @return a Swing border instance based on the values in this
128: * BorderProperty instance.
129: */
130: public Border createBorder(Component comp) {
131: return null;
132: }
133:
134: /**
135: * Adds a title decoration to the given border. This method will add the
136: * title only if <code>isIncludeTitle()</code> is true.
137: *
138: * @param b
139: * the border to decorate with a title.
140: * @return a border instance with the given title decoration.
141: */
142: public Border createTitle(Border b) {
143: if (isIncludeTitle()) {
144: return BorderFactory.createTitledBorder(b, getTitle(),
145: getJustification(), getPosition(), getTextFont(),
146: getTextColor());
147: } else {
148: return b;
149: }
150: }
151:
152: /**
153: * Object equals implementation.
154: */
155: public boolean equals(Object object) {
156: if (object instanceof BorderProperty) {
157: BorderProperty bp = (BorderProperty) object;
158: return (super .equals(object) && m_top == bp.m_top
159: && m_left == bp.m_left && m_right == bp.m_right
160: && m_bottom == bp.m_bottom
161: && m_include_title == bp.m_include_title
162: && isEqual(m_title, bp.m_title)
163: && m_justification == bp.m_justification
164: && m_position == bp.m_position && isEqual(
165: m_text_color, bp.m_text_color));
166:
167: } else {
168: return false;
169: }
170: }
171:
172: /**
173: * Returns the TitledBorder position constant for the given string
174: * representation
175: *
176: * @param pos
177: * the String representation of the TitledBorder position value.
178: * @return the TitleBorder position value. The constants are defined in
179: * <code>TitledBorder</code>: <code>ABOVE_TOP</code>
180: * <code>BELOW_TOP</code>
181: * <code>TOP</code>
182: * <code>ABOVE_BOTTOM</code>
183: * <code>BOTTOM</code>
184: * <code>BELOW_BOTTOMN</code>
185: * <code>DEFAULT_POSITION</code>
186: */
187: public static int fromPositionString(String pos) {
188: if ("ABOVE_TOP".equals(pos))
189: return TitledBorder.ABOVE_TOP;
190: else if ("TOP".equals(pos))
191: return TitledBorder.TOP;
192: else if ("BELOW_TOP".equals(pos))
193: return TitledBorder.BELOW_TOP;
194: else if ("ABOVE_BOTTOM".equals(pos))
195: return TitledBorder.ABOVE_BOTTOM;
196: else if ("BOTTOM".equals(pos))
197: return TitledBorder.BOTTOM;
198: else if ("BELOW_BOTTOM".equals(pos))
199: return TitledBorder.BELOW_BOTTOM;
200: else
201: return TitledBorder.DEFAULT_POSITION;
202: }
203:
204: /**
205: * Returns the TitledBorder justification constant for the given string
206: * representation
207: *
208: * @param just
209: * the String representation of the TitledBorder justification
210: * value.
211: * @return the TitleBorder justification value. The constants are defined in
212: * <code>TitledBorder</code>: <code>LEFT</code>
213: * <code>CENTER</code>
214: * <code>RIGHT</code>
215: * <code>LEADING</code>
216: * <code>TRAILING</code>
217: * <code>DEFAULT_JUSTIFICATION</code>
218: */
219: public static int fromJustificationString(String just) {
220: if ("LEFT".equals(just))
221: return TitledBorder.LEFT;
222: else if ("CENTER".equals(just))
223: return TitledBorder.CENTER;
224: else if ("RIGHT".equals(just))
225: return TitledBorder.RIGHT;
226: else if ("LEADING".equals(just))
227: return TitledBorder.LEADING;
228: else if ("TRAILING".equals(just))
229: return TitledBorder.TRAILING;
230: else
231: return TitledBorder.DEFAULT_JUSTIFICATION;
232: }
233:
234: /**
235: * Returns true if this border has a title.
236: *
237: * @return true if this border has a title
238: */
239: public boolean isIncludeTitle() {
240: return m_include_title;
241: }
242:
243: /**
244: * Return the text for this border's title. <code>isIncludeTitle()</code>
245: * must return true for this to have an effect.
246: *
247: * @return the text for the title
248: */
249: public String getTitle() {
250: return m_title;
251: }
252:
253: /**
254: * Returns the title justification. <code>isIncludeTitle()</code> must
255: * return true for this to have an effect.
256: *
257: * @return the title justification. TitledBorder.LEFT, CENTER, RIGHT,
258: * LEADING, TRAILING, DEFAULT_JUSTICATION
259: */
260: public int getJustification() {
261: return m_justification;
262: }
263:
264: /**
265: * Returns the title position. <code>isIncludeTitle()</code> must return
266: * true for this to have an effect.
267: *
268: * @return the title position
269: * TitledBorder.ABOVE_TOP,TOP,BELOW_TOP,ABOVE_BOTTOM,BOTTOM,BELOW_BOTTOM,DEFAULT_POSITION
270: * (top)
271: */
272: public int getPosition() {
273: return m_position;
274: }
275:
276: /**
277: * Returns the title text color. <code>isIncludeTitle()</code> must return
278: * true for this to have an effect.
279: *
280: * @return the color of the text in the title
281: */
282: public Color getTextColor() {
283: return m_text_color.getColor();
284: }
285:
286: /**
287: * Returns the color property for the title text.
288: *
289: * @return the color property for the title text.
290: */
291: public ColorProperty getTextColorProperty() {
292: return m_text_color;
293: }
294:
295: /**
296: * The title text font
297: *
298: * @return the font of the title text.
299: */
300: public Font getTextFont() {
301: return javax.swing.UIManager.getFont("Table.font");
302: }
303:
304: /**
305: * Returns true if the top part of the border is painted. Currently, only
306: * LineBorders support this capability.
307: *
308: * @return true if the top part of the border is painted
309: */
310: public boolean isTopPainted() {
311: return m_top;
312: }
313:
314: /**
315: * Returns true if the left part of the border is painted. Currently, only
316: * LineBorders support this capability.
317: *
318: * @return true if the left part of the border is painted
319: */
320: public boolean isLeftPainted() {
321: return m_left;
322: }
323:
324: /**
325: * Returns true if the bottom part of the border is painted. Currently, only
326: * LineBorders support this capability.
327: *
328: * @return true if the bottom part of the border is painted
329: */
330: public boolean isBottomPainted() {
331: return m_bottom;
332: }
333:
334: /**
335: * Returns true if the right part of the border is painted. Currently, only
336: * LineBorders support this capability.
337: *
338: * @return true if the right part of the border is painted
339: */
340: public boolean isRightPainted() {
341: return m_right;
342: }
343:
344: /**
345: * Set the flag that indicates if this border has a title
346: */
347: public void setIncludeTitle(boolean includeTitle) {
348: m_include_title = includeTitle;
349: }
350:
351: /**
352: * Sets the text for the title.
353: *
354: * @param title
355: * the title to set
356: */
357: public void setTitle(String title) {
358: m_title = title;
359: }
360:
361: /**
362: * Sets the title justification.
363: *
364: * @param justification
365: * TitledBorder.LEFT, CENTER, RIGHT, LEADING, TRAILING,
366: * DEFAULT_JUSTICATION
367: */
368: public void setJustification(int justification) {
369: m_justification = justification;
370: }
371:
372: /**
373: * Sets the title position
374: *
375: * @param position
376: * TitledBorder.ABOVE_TOP,TOP,BELOW_TOP,ABOVE_BOTTOM,BOTTOM,BELOW_BOTTOM,DEFAULT_POSITION
377: * (top)
378: */
379: public void setPosition(int position) {
380: m_position = position;
381: }
382:
383: /**
384: * Sets the title text color.
385: */
386: public void setTextColorProperty(ColorProperty c) {
387: m_text_color.setValue(c);
388: }
389:
390: /**
391: * Set to true if the top part of the border is painted. False if the top
392: * part of the border is not painted.
393: */
394: public void setTopPainted(boolean bpaint) {
395: m_top = bpaint;
396: }
397:
398: /**
399: * Set to true if the left part of the border is painted. False if the left
400: * part of the border is not painted.
401: */
402: public void setLeftPainted(boolean bpaint) {
403: m_left = bpaint;
404: }
405:
406: /**
407: * Set to true if the bottom part of the border is painted. False if the
408: * bottom part of the border is not painted.
409: */
410: public void setBottomPainted(boolean bpaint) {
411: m_bottom = bpaint;
412: }
413:
414: /**
415: * Set to true if the right part of the border is painted. False if the
416: * right part of the border is not painted.
417: */
418: public void setRightPainted(boolean bpaint) {
419: m_right = bpaint;
420: }
421:
422: public void setValue(Object obj) {
423: if (obj instanceof BorderProperty) {
424: BorderProperty bp = (BorderProperty) obj;
425: m_include_title = bp.m_include_title;
426: m_title = bp.m_title;
427: m_justification = bp.m_justification;
428: m_position = bp.m_position;
429: m_text_color = bp.m_text_color;
430: m_top = bp.m_top;
431: m_left = bp.m_left;
432: m_bottom = bp.m_bottom;
433: m_right = bp.m_right;
434: } else {
435: assert (false);
436: }
437: }
438:
439: public static String toPositionString(int pos) {
440: switch (pos) {
441: case TitledBorder.ABOVE_TOP:
442: return "ABOVE_TOP";
443: case TitledBorder.TOP:
444: return "TOP";
445: case TitledBorder.BELOW_TOP:
446: return "BELOW_TOP";
447: case TitledBorder.ABOVE_BOTTOM:
448: return "ABOVE_BOTTOM";
449: case TitledBorder.BOTTOM:
450: return "BOTTOM";
451: case TitledBorder.BELOW_BOTTOM:
452: return "BELOW_BOTTOM";
453: default:
454: return "DEFAULT_POSITION";
455: }
456: }
457:
458: public static String toJustificationString(int just) {
459: switch (just) {
460: case TitledBorder.LEFT:
461: return "LEFT";
462:
463: case TitledBorder.CENTER:
464: return "CENTER";
465:
466: case TitledBorder.RIGHT:
467: return "RIGHT";
468:
469: case TitledBorder.LEADING:
470: return "LEADING";
471:
472: case TitledBorder.TRAILING:
473: return "TRAILING";
474:
475: default:
476: return "DEFAULT_JUSTIFICATION";
477: }
478: }
479:
480: /**
481: * JETAPersistable Implementation
482: */
483: public void read(JETAObjectInput in) throws ClassNotFoundException,
484: IOException {
485: super .read(in.getSuperClassInput());
486:
487: int version = in.readVersion();
488: if (version == 1) {
489: m_title = (String) in.readObject("title");
490: } else {
491: m_include_title = in.readBoolean("includetitle", false);
492: m_title = (String) in.readObject("title");
493: m_justification = in.readInt("justification",
494: TitledBorder.DEFAULT_JUSTIFICATION);
495: m_position = in.readInt("position",
496: TitledBorder.DEFAULT_POSITION);
497:
498: Object col = in.readObject("textcolor");
499: if (col instanceof Color) {
500: m_text_color.setConstantColor((Color) col);
501: } else if (col instanceof ColorProperty) {
502: m_text_color = (ColorProperty) col;
503: } else if (col != null) {
504: System.out.println("BorderProperty: Unknown object: "
505: + col);
506: assert (false);
507: }
508:
509: if (m_text_color == null)
510: m_text_color = new ColorProperty(
511: "TitledBorder.titleColor");
512:
513: if (version > 2) {
514: m_top = in.readBoolean("top", true);
515: m_left = in.readBoolean("left", true);
516: m_bottom = in.readBoolean("bottom", true);
517: m_right = in.readBoolean("right", true);
518: }
519: }
520: }
521:
522: /**
523: * JETAPersistable Implementation
524: */
525: public void write(JETAObjectOutput out) throws IOException {
526: super .write(out.getSuperClassOutput(JETAProperty.class));
527: out.writeVersion(VERSION);
528: out.writeBoolean("includetitle", m_include_title, false);
529: out.writeObject("title", m_title);
530: out.writeInt("justification", m_justification,
531: TitledBorder.DEFAULT_JUSTIFICATION);
532: out.writeInt("position", m_position,
533: TitledBorder.DEFAULT_POSITION);
534:
535: /**
536: * optimization. give persistent store the option of not storing text
537: * color if it is the default
538: */
539: if (DEFAULT_TEXT_COLOR.equals(m_text_color)) {
540: out.writeObject("textcolor", m_text_color);
541: } else {
542: out.writeObject("textcolor", null);
543: }
544:
545: out.writeBoolean("top", m_top, true);
546: out.writeBoolean("left", m_left, true);
547: out.writeBoolean("bottom", m_bottom, true);
548: out.writeBoolean("right", m_right, true);
549: }
550:
551: public void updateBean(JETABean jbean) {
552: Component comp = null;
553: if (jbean != null)
554: comp = jbean.getDelegate();
555:
556: if (comp instanceof JComponent) {
557: ((JComponent) comp).setBorder(createBorder(comp));
558: } else {
559: assert (false);
560: }
561: }
562:
563: }
|