OSDN Git Service

start 4.101.7-SNAPSHOT
[jindolf/Jindolf.git] / src / main / java / jp / sfjp / jindolf / glyph / SysEventDraw.java
1 /*
2  * system event drawing
3  *
4  * License : The MIT License
5  * Copyright(c) 2008 olyutorskii
6  */
7
8 package jp.sfjp.jindolf.glyph;
9
10 import java.awt.Color;
11 import java.awt.Graphics2D;
12 import java.awt.Point;
13 import java.awt.Rectangle;
14 import java.awt.RenderingHints;
15 import java.io.IOException;
16 import jp.osdn.jindolf.parser.content.DecodedContent;
17 import jp.sfjp.jindolf.data.DialogPref;
18 import jp.sfjp.jindolf.data.SysEvent;
19
20 /**
21  * システムイベントメッセージの描画。
22  */
23 public class SysEventDraw extends AbstractTextRow{
24
25     /** announceメッセージの色。 */
26     public static final Color COLOR_ANNOUNCE = new Color(0xffffff);
27     /** orderメッセージの色。 */
28     public static final Color COLOR_ORDER    = new Color(0xf04040);
29     /** extraメッセージの色。 */
30     public static final Color COLOR_EXTRA    = new Color(0x808080);
31
32     private static final Color COLOR_SIMPLEFG = Color.BLACK;
33
34     private static final int INSET = 10;
35     private static final int UNDER_MARGIN = 15;
36
37     private final SysEvent sysEvent;
38     private final GlyphDraw sysMessage;
39
40     private DialogPref dialogPref;
41     private Color fgColor;
42
43     /**
44      * コンストラクタ。
45      * @param sysEvent システムイベント
46      */
47     public SysEventDraw(SysEvent sysEvent){
48         this(sysEvent, new DialogPref(), FontInfo.DEFAULT_FONTINFO);
49         return;
50     }
51
52     /**
53      * コンストラクタ。
54      * @param sysEvent システムイベント
55      * @param pref 発言表示設定
56      * @param fontInfo フォント設定
57      */
58     public SysEventDraw(SysEvent sysEvent,
59                           DialogPref pref,
60                           FontInfo fontInfo){
61         super(fontInfo);
62         this.sysEvent = sysEvent;
63
64         DecodedContent content = this.sysEvent.getContent();
65         CharSequence rawContent = content.getRawContent();
66
67         this.sysMessage = new GlyphDraw(rawContent, this.fontInfo);
68
69         this.dialogPref = pref;
70
71         setColorDesign();
72
73         return;
74     }
75
76     /**
77      * 配色を設定する。
78      */
79     private void setColorDesign(){
80         if(this.dialogPref.isSimpleMode()){
81             this.fgColor = COLOR_SIMPLEFG;
82         }else{
83             this.fgColor = getEventColor();
84         }
85
86         this.sysMessage.setColor(this.fgColor);
87
88         return;
89     }
90
91     /**
92      * システムイベントの取得。
93      * @return システムイベント
94      */
95     public SysEvent getSysEvent(){
96         return this.sysEvent;
97     }
98
99     /**
100      * イベント種別に応じた前景色を返す。
101      * @return イベント種別前景色
102      */
103     private Color getEventColor(){
104         Color result;
105         switch(this.sysEvent.getEventFamily()){
106         case ANNOUNCE:
107             result = COLOR_ANNOUNCE;
108             break;
109         case ORDER:
110             result = COLOR_ORDER;
111             break;
112         case EXTRA:
113             result = COLOR_EXTRA;
114             break;
115         default:
116             assert false;
117             result = null;
118         }
119         return result;
120     }
121
122     /**
123      * {@inheritDoc}
124      * @return {@inheritDoc}
125      */
126     @Override
127     public Rectangle recalcBounds(){
128         int newWidth = getWidth();
129
130         Rectangle child;
131         child = this.sysMessage.setWidth(newWidth - INSET - INSET);
132         this.bounds.width = newWidth;
133         this.bounds.height = child.height + INSET + INSET + UNDER_MARGIN;
134         return this.bounds;
135     }
136
137     /**
138      * {@inheritDoc}
139      * @param fontInfo {@inheritDoc}
140      */
141     @Override
142     public void setFontInfo(FontInfo fontInfo){
143         super.setFontInfo(fontInfo);
144         this.sysMessage.setFontInfo(this.fontInfo);
145         recalcBounds();
146         return;
147     }
148
149     /**
150      * 発言設定を更新する。
151      * @param pref 発言設定
152      */
153     public void setDialogPref(DialogPref pref){
154         this.dialogPref = pref;
155
156         setColorDesign();
157         recalcBounds();
158
159         return;
160     }
161
162     /**
163      * {@inheritDoc}
164      * @param from {@inheritDoc}
165      * @param to {@inheritDoc}
166      */
167     @Override
168     public void drag(Point from, Point to){
169         this.sysMessage.drag(from, to);
170         return;
171     }
172
173     /**
174      * {@inheritDoc}
175      * @param appendable {@inheritDoc}
176      * @return {@inheritDoc}
177      * @throws java.io.IOException {@inheritDoc}
178      */
179     @Override
180     public Appendable appendSelected(Appendable appendable)
181             throws IOException{
182         this.sysMessage.appendSelected(appendable);
183         return appendable;
184     }
185
186     /**
187      * {@inheritDoc}
188      */
189     @Override
190     public void clearSelect(){
191         this.sysMessage.clearSelect();
192         return;
193     }
194
195     /**
196      * {@inheritDoc}
197      * @param xPos {@inheritDoc}
198      * @param yPos {@inheritDoc}
199      */
200     @Override
201     public void setPos(int xPos, int yPos){
202         super.setPos(xPos, yPos);
203         this.sysMessage.setPos(this.bounds.x + INSET, this.bounds.y + INSET);
204         return;
205     }
206
207     /**
208      * {@inheritDoc}
209      * @param g {@inheritDoc}
210      */
211     @Override
212     public void paint(Graphics2D g){
213         g.setColor(this.fgColor);
214
215         RenderingHints.Key aaHintKey = RenderingHints.KEY_ANTIALIASING;
216         Object aaHintTemp = RenderingHints.VALUE_ANTIALIAS_OFF;
217         Object aaHintOrig = g.getRenderingHint(aaHintKey);
218
219         RenderingHints.Key strokeHintKey = RenderingHints.KEY_STROKE_CONTROL;
220         Object strokeHintTemp = RenderingHints.VALUE_STROKE_NORMALIZE;
221         Object strokeHintOrig = g.getRenderingHint(strokeHintKey);
222
223         g.setRenderingHint(aaHintKey, aaHintTemp);
224         g.setRenderingHint(strokeHintKey, strokeHintTemp);
225
226         if(this.dialogPref.isSimpleMode()){
227             g.drawLine(this.bounds.x,                     this.bounds.y,
228                        this.bounds.x + this.bounds.width, this.bounds.y );
229         }else{
230             g.drawRect(this.bounds.x,
231                        this.bounds.y,
232                        this.bounds.width  - 1,
233                        this.bounds.height - UNDER_MARGIN);
234         }
235
236         g.setRenderingHint(aaHintKey, aaHintOrig);
237         g.setRenderingHint(strokeHintKey, strokeHintOrig);
238
239         this.sysMessage.paint(g);
240
241         return;
242     }
243 }