4 * Copyright(c) 2008 olyutorskii
\r
5 * $Id: AnchorDraw.java 995 2010-03-15 03:54:09Z olyutorskii $
\r
8 package jp.sourceforge.jindolf;
\r
10 import java.awt.BasicStroke;
\r
11 import java.awt.Color;
\r
12 import java.awt.Graphics2D;
\r
13 import java.awt.Point;
\r
14 import java.awt.Rectangle;
\r
15 import java.awt.Stroke;
\r
16 import java.awt.image.BufferedImage;
\r
17 import java.io.IOException;
\r
18 import java.util.regex.Pattern;
\r
23 public class AnchorDraw extends AbstractTextRow{
\r
25 private static final Color COLOR_ANCHOR = new Color(0xffff99);
\r
26 private static final Color COLOR_SIMPLEFG = Color.BLACK;
\r
27 private static final int UPPER_MARGIN = 3;
\r
28 private static final int UNDER_MARGIN = 3;
\r
30 private static final Stroke STROKE_DASH;
\r
33 float[] dash = {3.0f};
\r
34 STROKE_DASH = new BasicStroke(1.0f,
\r
35 BasicStroke.CAP_SQUARE,
\r
36 BasicStroke.JOIN_MITER,
\r
42 private final Talk talk;
\r
43 private final GlyphDraw caption;
\r
44 private final GlyphDraw dialog;
\r
45 private final BufferedImage faceImage;
\r
46 private final Point imageOrigin = new Point();
\r
47 private final Point captionOrigin = new Point();
\r
48 private final Point dialogOrigin = new Point();
\r
50 private DialogPref dialogPref;
\r
56 public AnchorDraw(Talk talk){
\r
57 this(talk, new DialogPref(), FontInfo.DEFAULT_FONTINFO);
\r
64 * @param pref 発言表示設定
\r
65 * @param fontInfo フォント設定
\r
67 public AnchorDraw(Talk talk, DialogPref pref, FontInfo fontInfo){
\r
71 this.caption = new GlyphDraw(getCaptionString(), this.fontInfo);
\r
72 this.dialog = new GlyphDraw(this.talk.getDialog(), this.fontInfo);
\r
73 this.dialogPref = pref;
\r
74 this.faceImage = getFaceImage();
\r
85 private BufferedImage getFaceImage(){
\r
86 Period period = this.talk.getPeriod();
\r
87 Village village = period.getVillage();
\r
89 BufferedImage image;
\r
90 if(this.talk.isGrave()){
\r
91 image = village.getGraveImage();
\r
93 Avatar avatar = this.talk.getAvatar();
\r
94 image = village.getAvatarFaceImage(avatar);
\r
103 private void setColorDesign(){
\r
105 if(this.dialogPref.isSimpleMode()){
\r
106 fgColor = COLOR_SIMPLEFG;
\r
108 fgColor = COLOR_ANCHOR;
\r
110 this.caption.setColor(fgColor);
\r
111 this.dialog .setColor(fgColor);
\r
117 * @return キャプション文字列
\r
119 private CharSequence getCaptionString(){
\r
120 StringBuilder result = new StringBuilder();
\r
121 Avatar avatar = this.talk.getAvatar();
\r
123 if(this.talk.hasTalkNo()){
\r
124 result.append(this.talk.getAnchorNotation_G()).append(' ');
\r
126 result.append(avatar.getFullName())
\r
128 .append(this.talk.getAnchorNotation());
\r
135 * @return {@inheritDoc}
\r
137 public Rectangle recalcBounds(){
\r
138 int newWidth = getWidth();
\r
140 int imageWidth = 0;
\r
141 int imageHeight = 0;
\r
142 if( ! this.dialogPref.isSimpleMode() ){
\r
143 imageWidth = this.faceImage.getWidth(null);
\r
144 imageHeight = this.faceImage.getHeight(null);
\r
147 this.caption.setWidth(newWidth - imageWidth);
\r
148 int captionWidth = this.caption.getWidth();
\r
149 int captionHeight = this.caption.getHeight();
\r
151 this.dialog.setWidth(newWidth);
\r
152 int dialogWidth = this.dialog.getWidth();
\r
153 int dialogHeight = this.dialog.getHeight();
\r
155 int headerHeight = Math.max(imageHeight, captionHeight);
\r
157 int totalWidth = Math.max(imageWidth + captionWidth, dialogWidth);
\r
159 int totalHeight = headerHeight;
\r
160 totalHeight += dialogHeight;
\r
164 if(imageHeight > captionHeight){
\r
166 captionYpos = (imageHeight - captionHeight) / 2;
\r
168 imageYpos = (captionHeight - imageHeight) / 2;
\r
172 this.imageOrigin .setLocation(0,
\r
173 UPPER_MARGIN + imageYpos);
\r
174 this.captionOrigin.setLocation(imageWidth,
\r
175 UPPER_MARGIN + captionYpos);
\r
176 this.dialogOrigin .setLocation(0,
\r
177 UPPER_MARGIN + headerHeight);
\r
179 if(this.dialogPref.isSimpleMode()){
\r
180 this.bounds.width = newWidth;
\r
182 this.bounds.width = totalWidth;
\r
184 this.bounds.height = UPPER_MARGIN + totalHeight + UNDER_MARGIN;
\r
186 return this.bounds;
\r
191 * @param fontInfo {@inheritDoc}
\r
194 public void setFontInfo(FontInfo fontInfo){
\r
195 super.setFontInfo(fontInfo);
\r
197 this.caption.setFontInfo(this.fontInfo);
\r
198 this.dialog .setFontInfo(this.fontInfo);
\r
209 public void setDialogPref(DialogPref pref){
\r
210 this.dialogPref = pref;
\r
220 * @param from {@inheritDoc}
\r
221 * @param to {@inheritDoc}
\r
223 public void drag(Point from, Point to){
\r
224 this.caption.drag(from, to);
\r
225 this.dialog.drag(from, to);
\r
231 * @param appendable {@inheritDoc}
\r
232 * @return {@inheritDoc}
\r
233 * @throws java.io.IOException {@inheritDoc}
\r
235 public Appendable appendSelected(Appendable appendable)
\r
236 throws IOException{
\r
237 this.caption.appendSelected(appendable);
\r
238 this.dialog.appendSelected(appendable);
\r
245 public void clearSelect(){
\r
246 this.caption.clearSelect();
\r
247 this.dialog.clearSelect();
\r
253 * @param searchRegex パターン
\r
256 public int setRegex(Pattern searchRegex){
\r
259 total += this.dialog.setRegex(searchRegex);
\r
266 * @param g {@inheritDoc}
\r
268 public void paint(Graphics2D g){
\r
269 final int xPos = this.bounds.x;
\r
270 final int yPos = this.bounds.y;
\r
272 if(this.dialogPref.isSimpleMode()){
\r
273 Stroke oldStroke = g.getStroke();
\r
274 g.setStroke(STROKE_DASH);
\r
275 g.drawLine(xPos, this.bounds.y,
\r
276 xPos + this.bounds.width, this.bounds.y );
\r
277 g.setStroke(oldStroke);
\r
279 g.drawImage(this.faceImage,
\r
280 xPos + this.imageOrigin.x,
\r
281 yPos + this.imageOrigin.y,
\r
285 this.caption.setPos(xPos + this.captionOrigin.x,
\r
286 yPos + this.captionOrigin.y );
\r
287 this.caption.paint(g);
\r
289 this.dialog.setPos(xPos + this.dialogOrigin.x,
\r
290 yPos + this.dialogOrigin.y );
\r
291 this.dialog.paint(g);
\r