001 package org.pscode.xui.image.bean;
002
003 import java.awt.Color;
004 import java.awt.Point;
005 import java.awt.Dimension;
006 import java.awt.Rectangle;
007 import javax.swing.JLabel;
008
009 /** JavaBean used to store caption description.
010 Designed for use with the J2SE XMLEncoder/Decoder.
011 @author Andrew Thompson
012 @version 2010-01-20
013 @license LGPL */
014 public class ImageCaption implements Comparable {
015 /** The text or caption to display. */
016 public String captionText = "Image title..";
017
018 /** Font family to use for rendering this caption. This is used as
019 the default font - styles used in the HTML might override it. */
020 public String fontFamily = "Sansserif";
021
022 /** The color of the text of this caption. */
023 public Color captionColor = Color.BLACK;
024
025 /** The background color to display behind this caption.
026 Can be 'null' for no backgrountitleSize. If not null, will support
027 partial transparency in the color. */
028 public Color backgroundColor = new Color(255,255,255,192);
029
030 /** The width of the text. Height is determined by the number of
031 lines needed to render the text, given the text and width. */
032 public int width = 150;
033
034 /** Upper left corner of title. */
035 public Point location = new Point(20,20);
036 /** Insert arrow pointing to this location on the imageSize. */
037 public Point connectTo = new Point(-1,-1);
038
039 public int fontSize = 14;
040
041 public ImageCaption() {
042 }
043
044 public int getFontSize() {
045 return fontSize;
046 }
047
048 public void setFontSize(int fontSize) {
049 this.fontSize = fontSize;
050 }
051
052 public void setCaptionText(String captionText) {
053 this.captionText = captionText;
054 }
055
056 public String getCaptionText() {
057 return captionText;
058 }
059
060 public void setWidth(int width) {
061 this.width = width;
062 }
063
064 public int getWidth() {
065 return width;
066 }
067
068 public void setFontFamily(String fontFamily) {
069 this.fontFamily = fontFamily;
070 }
071
072 public String getFontFamily() {
073 return fontFamily;
074 }
075
076 public void setCaptionColor(Color captionColor) {
077 this.captionColor = captionColor;
078 }
079
080 public Color getCaptionColor() {
081 return captionColor;
082 }
083
084 public void setBackgroundColor(Color backgroundColor) {
085 this.backgroundColor = backgroundColor;
086 }
087
088 public Color getBackgroundColor() {
089 return backgroundColor;
090 }
091
092 public void setLocation(Point location) {
093 this.location = location;
094 }
095
096 public Point getLocation() {
097 return location;
098 }
099
100 public void setConnectTo(Point connectTo) {
101 this.connectTo = connectTo;
102 }
103
104 public Point getConnectTo() {
105 return connectTo;
106 }
107
108 /** Provides a rectangle encapsulating the title for the given image size. It is necessary to
109 have the image size to calculate location constraints < 1 (center & indent from right/bottom). */
110 public Rectangle getBoundsOfCaption(Dimension imageSize) {
111
112 int x = location.x;
113 JLabel label = new JLabel(getHtmlString());
114 Dimension titleSize = label.getPreferredSize();
115 if ( x<0 ) {
116 // from right
117 x = (int)(imageSize.getWidth()-titleSize.getWidth())+x;
118 } else if (x==0) {
119 // center
120 x = (int)(( imageSize.getWidth()-titleSize.getWidth() )/2d);
121 }
122 int y = location.y;
123 if ( y<0 ) {
124 // from bottom
125 y = (int)(imageSize.getHeight()-titleSize.getHeight())+y;
126 } else if (y==0) {
127 // center
128 y = (int)(( imageSize.getHeight()-titleSize.getHeight() )/2d);
129 }
130 Rectangle rect = new Rectangle( x,y,(int)titleSize.getWidth(),(int)titleSize.getHeight() );
131 return rect;
132 }
133
134 public String getHtmlString() {
135 String color =
136 "rgb(" +
137 captionColor.getRed() +
138 "," +
139 captionColor.getGreen() +
140 "," +
141 captionColor.getBlue() +
142 ")"
143 ;
144 String style = "style='" +
145 "width: " + width + "px; " +
146 "font-size: " + fontSize + "px; " +
147 "font-family: " + fontFamily + "; " +
148 "color: " + color + "; " +
149 "padding: 8px;" +
150 "'";
151 return
152 "<html><body " + style + ">" +
153 captionText;
154 }
155
156 public String toString() {
157 return
158 "" +
159 location.x +
160 "x" +
161 location.y +
162 ": " +
163 captionText;
164 }
165
166 /** Compares in order the
167 - location x/y
168 - connectTo x/y
169 - text
170 and returns the comparison of the first that is not equal. If all else fails, returns '0' to mean equal. */
171 public int compareTo(Object object) {
172 try {
173 ImageCaption caption = (ImageCaption)object;
174 if ( location.x != caption.getLocation().x ) {
175 return location.x - caption.getLocation().x;
176 } else if ( location.y != caption.getLocation().y ) {
177 return location.y - caption.getLocation().y;
178 } else if ( connectTo.x != caption.getConnectTo().x ) {
179 return connectTo.x - caption.getConnectTo().x;
180 } else if ( connectTo.y != caption.getConnectTo().y ) {
181 return connectTo.y - caption.getConnectTo().y;
182 } else if ( !captionText.equals(caption.getCaptionText()) ) {
183 return captionText.compareTo(caption.getCaptionText());
184 }
185 return 0;
186 } catch(ClassCastException cce) {
187 return -1;
188 }
189 }
190 }