OSDN Git Service

Merge branch 'Branch_release-'
[jindolf/Jindolf.git] / src / main / java / jp / sfjp / jindolf / glyph / AnchorDraw.java
-/*\r
- * アンカー描画\r
- *\r
- * Copyright(c) 2008 olyutorskii\r
- * $Id: AnchorDraw.java 995 2010-03-15 03:54:09Z olyutorskii $\r
- */\r
-\r
-package jp.sourceforge.jindolf;\r
-\r
-import java.awt.BasicStroke;\r
-import java.awt.Color;\r
-import java.awt.Graphics2D;\r
-import java.awt.Point;\r
-import java.awt.Rectangle;\r
-import java.awt.Stroke;\r
-import java.awt.image.BufferedImage;\r
-import java.io.IOException;\r
-import java.util.regex.Pattern;\r
-\r
-/**\r
- * アンカー描画。\r
- */\r
-public class AnchorDraw extends AbstractTextRow{\r
-\r
-    private static final Color COLOR_ANCHOR = new Color(0xffff99);\r
-    private static final Color COLOR_SIMPLEFG = Color.BLACK;\r
-    private static final int UPPER_MARGIN = 3;\r
-    private static final int UNDER_MARGIN = 3;\r
-\r
-    private static final Stroke STROKE_DASH;\r
-\r
-    static{\r
-        float[] dash = {3.0f};\r
-        STROKE_DASH = new BasicStroke(1.0f,\r
-                                      BasicStroke.CAP_SQUARE,\r
-                                      BasicStroke.JOIN_MITER,\r
-                                      10.0f,\r
-                                      dash,\r
-                                      0.0f);\r
-    }\r
-\r
-    private final Talk talk;\r
-    private final GlyphDraw caption;\r
-    private final GlyphDraw dialog;\r
-    private final BufferedImage faceImage;\r
-    private final Point imageOrigin   = new Point();\r
-    private final Point captionOrigin = new Point();\r
-    private final Point dialogOrigin  = new Point();\r
-\r
-    private DialogPref dialogPref;\r
-\r
-    /**\r
-     * コンストラクタ。\r
-     * @param talk 発言\r
-     */\r
-    public AnchorDraw(Talk talk){\r
-        this(talk, new DialogPref(), FontInfo.DEFAULT_FONTINFO);\r
-        return;\r
-    }\r
-\r
-    /**\r
-     * コンストラクタ。\r
-     * @param talk 発言\r
-     * @param pref 発言表示設定\r
-     * @param fontInfo フォント設定\r
-     */\r
-    public AnchorDraw(Talk talk, DialogPref pref, FontInfo fontInfo){\r
-        super(fontInfo);\r
-\r
-        this.talk = talk;\r
-        this.caption = new GlyphDraw(getCaptionString(), this.fontInfo);\r
-        this.dialog  = new GlyphDraw(this.talk.getDialog(), this.fontInfo);\r
-        this.dialogPref = pref;\r
-        this.faceImage = getFaceImage();\r
-\r
-        setColorDesign();\r
-\r
-        return;\r
-    }\r
-\r
-    /**\r
-     * 顔アイコンイメージを返す。\r
-     * @return アイコンイメージ\r
-     */\r
-    private BufferedImage getFaceImage(){\r
-        Period period = this.talk.getPeriod();\r
-        Village village = period.getVillage();\r
-\r
-        BufferedImage image;\r
-        if(this.talk.isGrave()){\r
-            image = village.getGraveImage();\r
-        }else{\r
-            Avatar avatar = this.talk.getAvatar();\r
-            image = village.getAvatarFaceImage(avatar);\r
-        }\r
-\r
-        return image;\r
-    }\r
-\r
-    /**\r
-     * 配色を設定する。\r
-     */\r
-    private void setColorDesign(){\r
-        Color fgColor;\r
-        if(this.dialogPref.isSimpleMode()){\r
-            fgColor = COLOR_SIMPLEFG;\r
-        }else{\r
-            fgColor = COLOR_ANCHOR;\r
-        }\r
-        this.caption.setColor(fgColor);\r
-        this.dialog .setColor(fgColor);\r
-        return;\r
-    }\r
-\r
-    /**\r
-     * キャプション文字列の取得。\r
-     * @return キャプション文字列\r
-     */\r
-    private CharSequence getCaptionString(){\r
-        StringBuilder result = new StringBuilder();\r
-        Avatar avatar = this.talk.getAvatar();\r
-\r
-        if(this.talk.hasTalkNo()){\r
-            result.append(this.talk.getAnchorNotation_G()).append(' ');\r
-        }\r
-        result.append(avatar.getFullName())\r
-              .append(' ')\r
-              .append(this.talk.getAnchorNotation());\r
-\r
-        return result;\r
-    }\r
-\r
-    /**\r
-     * {@inheritDoc}\r
-     * @return {@inheritDoc}\r
-     */\r
-    public Rectangle recalcBounds(){\r
-        int newWidth = getWidth();\r
-\r
-        int imageWidth  = 0;\r
-        int imageHeight = 0;\r
-        if( ! this.dialogPref.isSimpleMode() ){\r
-            imageWidth  = this.faceImage.getWidth(null);\r
-            imageHeight = this.faceImage.getHeight(null);\r
-        }\r
-\r
-        this.caption.setWidth(newWidth - imageWidth);\r
-        int captionWidth  = this.caption.getWidth();\r
-        int captionHeight = this.caption.getHeight();\r
-\r
-        this.dialog.setWidth(newWidth);\r
-        int dialogWidth  = this.dialog.getWidth();\r
-        int dialogHeight = this.dialog.getHeight();\r
-\r
-        int headerHeight = Math.max(imageHeight, captionHeight);\r
-\r
-        int totalWidth = Math.max(imageWidth + captionWidth, dialogWidth);\r
-\r
-        int totalHeight = headerHeight;\r
-        totalHeight += dialogHeight;\r
-\r
-        int imageYpos;\r
-        int captionYpos;\r
-        if(imageHeight > captionHeight){\r
-            imageYpos = 0;\r
-            captionYpos = (imageHeight - captionHeight) / 2;\r
-        }else{\r
-            imageYpos = (captionHeight - imageHeight) / 2;\r
-            captionYpos = 0;\r
-        }\r
-\r
-        this.imageOrigin  .setLocation(0,\r
-                                       UPPER_MARGIN + imageYpos);\r
-        this.captionOrigin.setLocation(imageWidth,\r
-                                       UPPER_MARGIN + captionYpos);\r
-        this.dialogOrigin .setLocation(0,\r
-                                       UPPER_MARGIN + headerHeight);\r
-\r
-        if(this.dialogPref.isSimpleMode()){\r
-            this.bounds.width  = newWidth;\r
-        }else{\r
-            this.bounds.width  = totalWidth;\r
-        }\r
-        this.bounds.height = UPPER_MARGIN + totalHeight + UNDER_MARGIN;\r
-\r
-        return this.bounds;\r
-    }\r
-\r
-    /**\r
-     * {@inheritDoc}\r
-     * @param fontInfo {@inheritDoc}\r
-     */\r
-    @Override\r
-    public void setFontInfo(FontInfo fontInfo){\r
-        super.setFontInfo(fontInfo);\r
-\r
-        this.caption.setFontInfo(this.fontInfo);\r
-        this.dialog .setFontInfo(this.fontInfo);\r
-\r
-        recalcBounds();\r
-\r
-        return;\r
-    }\r
-\r
-    /**\r
-     * 発言設定を更新する。\r
-     * @param pref 発言設定\r
-     */\r
-    public void setDialogPref(DialogPref pref){\r
-        this.dialogPref = pref;\r
-\r
-        setColorDesign();\r
-        recalcBounds();\r
-\r
-        return;\r
-    }\r
-\r
-    /**\r
-     * {@inheritDoc}\r
-     * @param from {@inheritDoc}\r
-     * @param to {@inheritDoc}\r
-     */\r
-    public void drag(Point from, Point to){\r
-        this.caption.drag(from, to);\r
-        this.dialog.drag(from, to);\r
-        return;\r
-    }\r
-\r
-    /**\r
-     * {@inheritDoc}\r
-     * @param appendable {@inheritDoc}\r
-     * @return {@inheritDoc}\r
-     * @throws java.io.IOException {@inheritDoc}\r
-     */\r
-    public Appendable appendSelected(Appendable appendable)\r
-            throws IOException{\r
-        this.caption.appendSelected(appendable);\r
-        this.dialog.appendSelected(appendable);\r
-        return appendable;\r
-    }\r
-\r
-    /**\r
-     * {@inheritDoc}\r
-     */\r
-    public void clearSelect(){\r
-        this.caption.clearSelect();\r
-        this.dialog.clearSelect();\r
-        return;\r
-    }\r
-\r
-    /**\r
-     * 検索文字列パターンを設定する。\r
-     * @param searchRegex パターン\r
-     * @return ヒット数\r
-     */\r
-    public int setRegex(Pattern searchRegex){\r
-        int total = 0;\r
-\r
-        total += this.dialog.setRegex(searchRegex);\r
-\r
-        return total;\r
-    }\r
-\r
-    /**\r
-     * {@inheritDoc}\r
-     * @param g {@inheritDoc}\r
-     */\r
-    public void paint(Graphics2D g){\r
-        final int xPos = this.bounds.x;\r
-        final int yPos = this.bounds.y;\r
-\r
-        if(this.dialogPref.isSimpleMode()){\r
-            Stroke oldStroke = g.getStroke();\r
-            g.setStroke(STROKE_DASH);\r
-            g.drawLine(xPos,                     this.bounds.y,\r
-                       xPos + this.bounds.width, this.bounds.y );\r
-            g.setStroke(oldStroke);\r
-        }else{\r
-            g.drawImage(this.faceImage,\r
-                        xPos + this.imageOrigin.x,\r
-                        yPos + this.imageOrigin.y,\r
-                        null );\r
-        }\r
-\r
-        this.caption.setPos(xPos + this.captionOrigin.x,\r
-                            yPos + this.captionOrigin.y );\r
-        this.caption.paint(g);\r
-\r
-        this.dialog.setPos(xPos + this.dialogOrigin.x,\r
-                           yPos + this.dialogOrigin.y );\r
-        this.dialog.paint(g);\r
-\r
-        return;\r
-    }\r
-\r
-}\r
+/*
+ * アンカー描画
+ *
+ * License : The MIT License
+ * Copyright(c) 2008 olyutorskii
+ */
+
+package jp.sfjp.jindolf.glyph;
+
+import java.awt.BasicStroke;
+import java.awt.Color;
+import java.awt.Graphics2D;
+import java.awt.Point;
+import java.awt.Rectangle;
+import java.awt.Stroke;
+import java.awt.image.BufferedImage;
+import java.io.IOException;
+import java.util.regex.Pattern;
+import jp.sfjp.jindolf.data.Avatar;
+import jp.sfjp.jindolf.data.DialogPref;
+import jp.sfjp.jindolf.data.Period;
+import jp.sfjp.jindolf.data.Talk;
+import jp.sfjp.jindolf.data.Village;
+
+/**
+ * アンカー描画。
+ */
+public class AnchorDraw extends AbstractTextRow{
+
+    private static final Color COLOR_ANCHOR = new Color(0xffff99);
+    private static final Color COLOR_SIMPLEFG = Color.BLACK;
+    private static final int UPPER_MARGIN = 3;
+    private static final int UNDER_MARGIN = 3;
+
+    private static final Stroke STROKE_DASH;
+
+    static{
+        float[] dash = {3.0f};
+        STROKE_DASH = new BasicStroke(1.0f,
+                                      BasicStroke.CAP_SQUARE,
+                                      BasicStroke.JOIN_MITER,
+                                      10.0f,
+                                      dash,
+                                      0.0f);
+    }
+
+    private final Talk talk;
+    private final GlyphDraw caption;
+    private final GlyphDraw dialog;
+    private final BufferedImage faceImage;
+    private final Point imageOrigin   = new Point();
+    private final Point captionOrigin = new Point();
+    private final Point dialogOrigin  = new Point();
+
+    private DialogPref dialogPref;
+
+    /**
+     * コンストラクタ。
+     * @param talk 発言
+     */
+    public AnchorDraw(Talk talk){
+        this(talk, new DialogPref(), FontInfo.DEFAULT_FONTINFO);
+        return;
+    }
+
+    /**
+     * コンストラクタ。
+     * @param talk 発言
+     * @param pref 発言表示設定
+     * @param fontInfo フォント設定
+     */
+    public AnchorDraw(Talk talk, DialogPref pref, FontInfo fontInfo){
+        super(fontInfo);
+
+        this.talk = talk;
+        this.caption = new GlyphDraw(getCaptionString(), this.fontInfo);
+        this.dialog  = new GlyphDraw(this.talk.getDialog(), this.fontInfo);
+        this.dialogPref = pref;
+        this.faceImage = getFaceImage();
+
+        setColorDesign();
+
+        return;
+    }
+
+    /**
+     * 顔アイコンイメージを返す。
+     * @return アイコンイメージ
+     */
+    private BufferedImage getFaceImage(){
+        Period period = this.talk.getPeriod();
+        Village village = period.getVillage();
+
+        BufferedImage image;
+        if(this.talk.isGrave()){
+            image = village.getGraveImage();
+        }else{
+            Avatar avatar = this.talk.getAvatar();
+            image = village.getAvatarFaceImage(avatar);
+        }
+
+        return image;
+    }
+
+    /**
+     * 配色を設定する。
+     */
+    private void setColorDesign(){
+        Color fgColor;
+        if(this.dialogPref.isSimpleMode()){
+            fgColor = COLOR_SIMPLEFG;
+        }else{
+            fgColor = COLOR_ANCHOR;
+        }
+        this.caption.setColor(fgColor);
+        this.dialog .setColor(fgColor);
+        return;
+    }
+
+    /**
+     * キャプション文字列の取得。
+     * @return キャプション文字列
+     */
+    private CharSequence getCaptionString(){
+        StringBuilder result = new StringBuilder();
+        Avatar avatar = this.talk.getAvatar();
+
+        if(this.talk.hasTalkNo()){
+            result.append(this.talk.getAnchorNotation_G()).append(' ');
+        }
+        result.append(avatar.getFullName())
+              .append(' ')
+              .append(this.talk.getAnchorNotation());
+
+        return result;
+    }
+
+    /**
+     * {@inheritDoc}
+     * @return {@inheritDoc}
+     */
+    @Override
+    public Rectangle recalcBounds(){
+        int newWidth = getWidth();
+
+        int imageWidth  = 0;
+        int imageHeight = 0;
+        if( ! this.dialogPref.isSimpleMode() ){
+            imageWidth  = this.faceImage.getWidth(null);
+            imageHeight = this.faceImage.getHeight(null);
+        }
+
+        this.caption.setWidth(newWidth - imageWidth);
+        int captionWidth  = this.caption.getWidth();
+        int captionHeight = this.caption.getHeight();
+
+        this.dialog.setWidth(newWidth);
+        int dialogWidth  = this.dialog.getWidth();
+        int dialogHeight = this.dialog.getHeight();
+
+        int headerHeight = Math.max(imageHeight, captionHeight);
+
+        int totalWidth = Math.max(imageWidth + captionWidth, dialogWidth);
+
+        int totalHeight = headerHeight;
+        totalHeight += dialogHeight;
+
+        int imageYpos;
+        int captionYpos;
+        if(imageHeight > captionHeight){
+            imageYpos = 0;
+            captionYpos = (imageHeight - captionHeight) / 2;
+        }else{
+            imageYpos = (captionHeight - imageHeight) / 2;
+            captionYpos = 0;
+        }
+
+        this.imageOrigin  .setLocation(0,
+                                       UPPER_MARGIN + imageYpos);
+        this.captionOrigin.setLocation(imageWidth,
+                                       UPPER_MARGIN + captionYpos);
+        this.dialogOrigin .setLocation(0,
+                                       UPPER_MARGIN + headerHeight);
+
+        if(this.dialogPref.isSimpleMode()){
+            this.bounds.width  = newWidth;
+        }else{
+            this.bounds.width  = totalWidth;
+        }
+        this.bounds.height = UPPER_MARGIN + totalHeight + UNDER_MARGIN;
+
+        return this.bounds;
+    }
+
+    /**
+     * {@inheritDoc}
+     * @param fontInfo {@inheritDoc}
+     */
+    @Override
+    public void setFontInfo(FontInfo fontInfo){
+        super.setFontInfo(fontInfo);
+
+        this.caption.setFontInfo(this.fontInfo);
+        this.dialog .setFontInfo(this.fontInfo);
+
+        recalcBounds();
+
+        return;
+    }
+
+    /**
+     * 発言設定を更新する。
+     * @param pref 発言設定
+     */
+    public void setDialogPref(DialogPref pref){
+        this.dialogPref = pref;
+
+        setColorDesign();
+        recalcBounds();
+
+        return;
+    }
+
+    /**
+     * {@inheritDoc}
+     * @param from {@inheritDoc}
+     * @param to {@inheritDoc}
+     */
+    @Override
+    public void drag(Point from, Point to){
+        this.caption.drag(from, to);
+        this.dialog.drag(from, to);
+        return;
+    }
+
+    /**
+     * {@inheritDoc}
+     * @param appendable {@inheritDoc}
+     * @return {@inheritDoc}
+     * @throws java.io.IOException {@inheritDoc}
+     */
+    @Override
+    public Appendable appendSelected(Appendable appendable)
+            throws IOException{
+        this.caption.appendSelected(appendable);
+        this.dialog.appendSelected(appendable);
+        return appendable;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void clearSelect(){
+        this.caption.clearSelect();
+        this.dialog.clearSelect();
+        return;
+    }
+
+    /**
+     * 検索文字列パターンを設定する。
+     * @param searchRegex パターン
+     * @return ヒット数
+     */
+    public int setRegex(Pattern searchRegex){
+        int total = 0;
+
+        total += this.dialog.setRegex(searchRegex);
+
+        return total;
+    }
+
+    /**
+     * {@inheritDoc}
+     * @param g {@inheritDoc}
+     */
+    @Override
+    public void paint(Graphics2D g){
+        final int xPos = this.bounds.x;
+        final int yPos = this.bounds.y;
+
+        if(this.dialogPref.isSimpleMode()){
+            Stroke oldStroke = g.getStroke();
+            g.setStroke(STROKE_DASH);
+            g.drawLine(xPos,                     this.bounds.y,
+                       xPos + this.bounds.width, this.bounds.y );
+            g.setStroke(oldStroke);
+        }else{
+            g.drawImage(this.faceImage,
+                        xPos + this.imageOrigin.x,
+                        yPos + this.imageOrigin.y,
+                        null );
+        }
+
+        this.caption.setPos(xPos + this.captionOrigin.x,
+                            yPos + this.captionOrigin.y );
+        this.caption.paint(g);
+
+        this.dialog.setPos(xPos + this.dialogOrigin.x,
+                           yPos + this.dialogOrigin.y );
+        this.dialog.paint(g);
+
+        return;
+    }
+
+}