4 * License : The MIT License
5 * Copyright(c) 2008 olyutorskii
8 package jp.sfjp.jindolf.glyph;
10 import java.awt.BasicStroke;
11 import java.awt.Color;
12 import java.awt.Graphics2D;
13 import java.awt.Point;
14 import java.awt.Rectangle;
15 import java.awt.Stroke;
16 import java.awt.image.BufferedImage;
17 import java.io.IOException;
18 import java.util.regex.Pattern;
19 import jp.sfjp.jindolf.data.Avatar;
20 import jp.sfjp.jindolf.data.DialogPref;
21 import jp.sfjp.jindolf.data.Period;
22 import jp.sfjp.jindolf.data.Talk;
23 import jp.sfjp.jindolf.data.Village;
28 public class AnchorDraw extends AbstractTextRow{
30 private static final Color COLOR_ANCHOR = new Color(0xffff99);
31 private static final Color COLOR_SIMPLEFG = Color.BLACK;
32 private static final int UPPER_MARGIN = 3;
33 private static final int UNDER_MARGIN = 3;
35 private static final Stroke STROKE_DASH;
38 float[] dash = {3.0f};
39 STROKE_DASH = new BasicStroke(1.0f,
40 BasicStroke.CAP_SQUARE,
41 BasicStroke.JOIN_MITER,
47 private final Talk talk;
48 private final GlyphDraw caption;
49 private final GlyphDraw dialog;
50 private final BufferedImage faceImage;
51 private final Point imageOrigin = new Point();
52 private final Point captionOrigin = new Point();
53 private final Point dialogOrigin = new Point();
55 private DialogPref dialogPref;
61 public AnchorDraw(Talk talk){
62 this(talk, new DialogPref(), FontInfo.DEFAULT_FONTINFO);
70 * @param fontInfo フォント設定
72 public AnchorDraw(Talk talk, DialogPref pref, FontInfo fontInfo){
76 this.caption = new GlyphDraw(getCaptionString(), this.fontInfo);
77 this.dialog = new GlyphDraw(this.talk.getDialog(), this.fontInfo);
78 this.dialogPref = pref;
79 this.faceImage = getFaceImage();
90 private BufferedImage getFaceImage(){
91 Period period = this.talk.getPeriod();
92 Village village = period.getVillage();
95 if(this.talk.isGrave()){
96 image = village.getGraveImage();
98 Avatar avatar = this.talk.getAvatar();
99 image = village.getAvatarFaceImage(avatar);
108 private void setColorDesign(){
110 if(this.dialogPref.isSimpleMode()){
111 fgColor = COLOR_SIMPLEFG;
113 fgColor = COLOR_ANCHOR;
115 this.caption.setColor(fgColor);
116 this.dialog .setColor(fgColor);
124 private CharSequence getCaptionString(){
125 StringBuilder result = new StringBuilder();
126 Avatar avatar = this.talk.getAvatar();
128 if(this.talk.hasTalkNo()){
129 result.append(this.talk.getAnchorNotation_G()).append(' ');
131 result.append(avatar.getFullName())
133 .append(this.talk.getAnchorNotation());
140 * @return {@inheritDoc}
143 public Rectangle recalcBounds(){
144 int newWidth = getWidth();
148 if( ! this.dialogPref.isSimpleMode() ){
149 imageWidth = this.faceImage.getWidth(null);
150 imageHeight = this.faceImage.getHeight(null);
153 this.caption.setWidth(newWidth - imageWidth);
154 int captionWidth = this.caption.getWidth();
155 int captionHeight = this.caption.getHeight();
157 this.dialog.setWidth(newWidth);
158 int dialogWidth = this.dialog.getWidth();
159 int dialogHeight = this.dialog.getHeight();
161 int headerHeight = Math.max(imageHeight, captionHeight);
163 int totalWidth = Math.max(imageWidth + captionWidth, dialogWidth);
165 int totalHeight = headerHeight;
166 totalHeight += dialogHeight;
170 if(imageHeight > captionHeight){
172 captionYpos = (imageHeight - captionHeight) / 2;
174 imageYpos = (captionHeight - imageHeight) / 2;
178 this.imageOrigin .setLocation(0,
179 UPPER_MARGIN + imageYpos);
180 this.captionOrigin.setLocation(imageWidth,
181 UPPER_MARGIN + captionYpos);
182 this.dialogOrigin .setLocation(0,
183 UPPER_MARGIN + headerHeight);
185 if(this.dialogPref.isSimpleMode()){
186 this.bounds.width = newWidth;
188 this.bounds.width = totalWidth;
190 this.bounds.height = UPPER_MARGIN + totalHeight + UNDER_MARGIN;
197 * @param fontInfo {@inheritDoc}
200 public void setFontInfo(FontInfo fontInfo){
201 super.setFontInfo(fontInfo);
203 this.caption.setFontInfo(this.fontInfo);
204 this.dialog .setFontInfo(this.fontInfo);
215 public void setDialogPref(DialogPref pref){
216 this.dialogPref = pref;
226 * @param from {@inheritDoc}
227 * @param to {@inheritDoc}
230 public void drag(Point from, Point to){
231 this.caption.drag(from, to);
232 this.dialog.drag(from, to);
238 * @param appendable {@inheritDoc}
239 * @return {@inheritDoc}
240 * @throws java.io.IOException {@inheritDoc}
243 public Appendable appendSelected(Appendable appendable)
245 this.caption.appendSelected(appendable);
246 this.dialog.appendSelected(appendable);
254 public void clearSelect(){
255 this.caption.clearSelect();
256 this.dialog.clearSelect();
262 * @param searchRegex パターン
265 public int setRegex(Pattern searchRegex){
268 total += this.dialog.setRegex(searchRegex);
275 * @param g {@inheritDoc}
278 public void paint(Graphics2D g){
279 final int xPos = this.bounds.x;
280 final int yPos = this.bounds.y;
282 if(this.dialogPref.isSimpleMode()){
283 Stroke oldStroke = g.getStroke();
284 g.setStroke(STROKE_DASH);
285 g.drawLine(xPos, this.bounds.y,
286 xPos + this.bounds.width, this.bounds.y );
287 g.setStroke(oldStroke);
289 g.drawImage(this.faceImage,
290 xPos + this.imageOrigin.x,
291 yPos + this.imageOrigin.y,
295 this.caption.setPos(xPos + this.captionOrigin.x,
296 yPos + this.captionOrigin.y );
297 this.caption.paint(g);
299 this.dialog.setPos(xPos + this.dialogOrigin.x,
300 yPos + this.dialogOrigin.y );
301 this.dialog.paint(g);