View Javadoc

1   /*
2    * All Rights Reserved.
3    * Copyright (C) 1999-2005 Tsukuba Bunko.
4    *
5    * Licensed under the BSD License ("the License"); you may not use
6    * this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *       http://www.tsukuba-bunko.org/licenses/LICENSE.txt
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   *
17   * $Id: Marker.java,v 1.2 2005/07/23 18:55:18 ppoi Exp $
18   */
19  package tsukuba_bunko.peko.canvas.text;
20  
21  import	java.awt.Dimension;
22  import	java.awt.Graphics;
23  import	java.awt.Graphics2D;
24  
25  import	java.awt.font.TextLayout;
26  
27  import	javax.swing.JComponent;
28  
29  import	tsukuba_bunko.peko.Logger;
30  
31  
32  /***
33   * テキストの末尾に表示する待ちマーカーです。
34   * @author	$Author: ppoi $
35   * @version	$Revision: 1.2 $
36   */
37  public class Marker	extends JComponent	implements Runnable	{
38  
39  	/***
40  	 * serial version UID
41  	 */
42  	private static final long	serialVersionUID	= -3817987151858534019L;
43  
44  	/***
45  	 * マーカー文字
46  	 */
47  	private Line	_text = null;
48  
49  	/***
50  	 * 点滅フラグ
51  	 */
52  	private boolean	_light = false;
53  
54  	/***
55  	 * 点滅スレッド
56  	 */
57  	private Thread	_blinker = null;
58  
59  	/***
60  	 * 点滅状態
61  	 */
62  	private boolean	_blinking = false;
63  
64  
65  	/***
66  	 * <code>Marker</code> のインスタンスを作成します。
67  	 */
68  	public Marker()
69  	{
70  		super();
71  	}
72  
73  
74  	/***
75  	 * マーカー文字を設定します。
76  	 * @param	text	マーカー文字
77  	 */
78  	public void setText( Line text )
79  	{
80  		_text = text;
81  		int	height = (int)(text.getAscent() + text.getDescent());
82  		int	width = (int)text.getTextLayout().getAdvance();
83  		Dimension	size = new Dimension( width, height );
84  		setPreferredSize( size );
85  		setSize( size );
86  	}
87  
88  	/***
89  	 * マーカー文字を設定します。
90  	 * @param	text	マーカー文字
91  	 * @param	page	ページ
92  	 */
93  	public void setText( String text, Page page )
94  	{
95  		setForeground( page.getForeground() );
96  		TextLayout	layout = new TextLayout( text, page.getDefaultFont(), page.getFontRenderContext() );
97  		Line	line = new Line();
98  		line.setLineSpan( 0f );
99  		line.setTextLayout( layout );
100 		line.setForeground( page.getForeground() );
101 		line.setShadowColor( page.getShadow() );
102 		setText( line );
103 	}
104 
105 	/***
106 	 * マーカー文字を取得します。
107 	 * @return	マーカー文字
108 	 */
109 	public Line getText()
110 	{
111 		return _text;
112 	}
113 
114 	/***
115 	 * 点滅を開始します。
116 	 */
117 	public void start()
118 	{
119 		if( _blinker == null )	{
120 			synchronized( this )	{
121 				if( _blinker == null )	{
122 					_blinker = new Thread( this );
123 					_blinking = true;
124 					_blinker.start();
125 				}
126 			}
127 		}
128 	}
129 
130 	/***
131 	 * 点滅を終了します。
132 	 */
133 	public void stop()
134 	{
135 		synchronized( this )	{
136 			_blinking = false;
137 			notify();
138 		}
139 	}
140 
141 	/***
142 	 * マーカーの左下隅の位置を設定します。
143 	 * @param	x	X 座標値
144 	 * @param	y	Y 座標値
145 	 */
146 	public void setPosition( int x, int y )
147 	{
148 		if( _text != null )	{
149 			setLocation( x, (y - (int)(_text.getAscent() + _text.getDescent())) );
150 		}
151 		else	{
152 			setLocation( x, y );
153 		}
154 	}
155 
156 
157 //
158 //	Runnable の実装
159 //
160 	public void run()
161 	{
162 		Logger.debug( "[canvas.text] blinking start." );
163 		while( _blinking )	{
164 			synchronized( this )	{
165 				try	{
166 					wait( 520 );
167 				}
168 				catch( InterruptedException ie )	{
169 				}
170 			}
171 			_light = !_light;
172 			if( isVisible() )	{
173 				repaint();
174 			}
175 		}
176 		Logger.debug( "[canvas.text] blinking stop." );
177 	}
178 
179 
180 //
181 //	JComponent の実装
182 //
183 	protected void paintComponent( Graphics g )
184 	{
185 		if( !_light || (_text == null) || !isVisible() )	{
186 			return;
187 		}
188 
189 		_text.draw( (Graphics2D)g, 0, _text.getAscent() );
190 	}
191 }