| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| tsukuba_bunko.peko.canvas.text.Marker |
|
|
| 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 | 0 | private Line _text = null; |
| 48 | ||
| 49 | /** |
|
| 50 | * 点滅フラグ |
|
| 51 | */ |
|
| 52 | 0 | private boolean _light = false; |
| 53 | ||
| 54 | /** |
|
| 55 | * 点滅スレッド |
|
| 56 | */ |
|
| 57 | 0 | private Thread _blinker = null; |
| 58 | ||
| 59 | /** |
|
| 60 | * 点滅状態 |
|
| 61 | */ |
|
| 62 | 0 | private boolean _blinking = false; |
| 63 | ||
| 64 | ||
| 65 | /** |
|
| 66 | * <code>Marker</code> のインスタンスを作成します。 |
|
| 67 | */ |
|
| 68 | public Marker() |
|
| 69 | { |
|
| 70 | 0 | super(); |
| 71 | 0 | } |
| 72 | ||
| 73 | ||
| 74 | /** |
|
| 75 | * マーカー文字を設定します。 |
|
| 76 | * @param text マーカー文字 |
|
| 77 | */ |
|
| 78 | public void setText( Line text ) |
|
| 79 | { |
|
| 80 | 0 | _text = text; |
| 81 | 0 | int height = (class="keyword">int)(text.getAscent() + text.getDescent()); |
| 82 | 0 | int width = (class="keyword">int)text.getTextLayout().getAdvance(); |
| 83 | 0 | Dimension size = new Dimension( width, height ); |
| 84 | 0 | setPreferredSize( size ); |
| 85 | 0 | setSize( size ); |
| 86 | 0 | } |
| 87 | ||
| 88 | /** |
|
| 89 | * マーカー文字を設定します。 |
|
| 90 | * @param text マーカー文字 |
|
| 91 | * @param page ページ |
|
| 92 | */ |
|
| 93 | public void setText( String text, Page page ) |
|
| 94 | { |
|
| 95 | 0 | setForeground( page.getForeground() ); |
| 96 | 0 | TextLayout layout = new TextLayout( text, page.getDefaultFont(), page.getFontRenderContext() ); |
| 97 | 0 | Line line = new Line(); |
| 98 | 0 | line.setLineSpan( 0f ); |
| 99 | 0 | line.setTextLayout( layout ); |
| 100 | 0 | line.setForeground( page.getForeground() ); |
| 101 | 0 | line.setShadowColor( page.getShadow() ); |
| 102 | 0 | setText( line ); |
| 103 | 0 | } |
| 104 | ||
| 105 | /** |
|
| 106 | * マーカー文字を取得します。 |
|
| 107 | * @return マーカー文字 |
|
| 108 | */ |
|
| 109 | public Line getText() |
|
| 110 | { |
|
| 111 | 0 | return _text; |
| 112 | } |
|
| 113 | ||
| 114 | /** |
|
| 115 | * 点滅を開始します。 |
|
| 116 | */ |
|
| 117 | public void start() |
|
| 118 | { |
|
| 119 | 0 | if( _blinker == null ) { |
| 120 | 0 | synchronized( this ) { |
| 121 | 0 | if( _blinker == null ) { |
| 122 | 0 | _blinker = new Thread( this ); |
| 123 | 0 | _blinking = true; |
| 124 | 0 | _blinker.start(); |
| 125 | } |
|
| 126 | 0 | } |
| 127 | } |
|
| 128 | 0 | } |
| 129 | ||
| 130 | /** |
|
| 131 | * 点滅を終了します。 |
|
| 132 | */ |
|
| 133 | public void stop() |
|
| 134 | { |
|
| 135 | 0 | synchronized( this ) { |
| 136 | 0 | _blinking = false; |
| 137 | 0 | notify(); |
| 138 | 0 | } |
| 139 | 0 | } |
| 140 | ||
| 141 | /** |
|
| 142 | * マーカーの左下隅の位置を設定します。 |
|
| 143 | * @param x X 座標値 |
|
| 144 | * @param y Y 座標値 |
|
| 145 | */ |
|
| 146 | public void setPosition( int x, class="keyword">int y ) |
|
| 147 | { |
|
| 148 | 0 | if( _text != null ) { |
| 149 | 0 | setLocation( x, (y - (int)(_text.getAscent() + _text.getDescent())) ); |
| 150 | 0 | } |
| 151 | else { |
|
| 152 | 0 | setLocation( x, y ); |
| 153 | } |
|
| 154 | 0 | } |
| 155 | ||
| 156 | ||
| 157 | // |
|
| 158 | // Runnable の実装 |
|
| 159 | // |
|
| 160 | public void run() |
|
| 161 | { |
|
| 162 | 0 | Logger.debug( "[canvas.text] blinking start." ); |
| 163 | 0 | while( _blinking ) { |
| 164 | 0 | synchronized( this ) { |
| 165 | try { |
|
| 166 | 0 | wait( 520 ); |
| 167 | } |
|
| 168 | 0 | catch( InterruptedException ie ) { |
| 169 | 0 | } |
| 170 | 0 | } |
| 171 | 0 | _light = !_light; |
| 172 | 0 | if( isVisible() ) { |
| 173 | 0 | repaint(); |
| 174 | 0 | } |
| 175 | } |
|
| 176 | 0 | Logger.debug( "[canvas.text] blinking stop." ); |
| 177 | 0 | } |
| 178 | ||
| 179 | ||
| 180 | // |
|
| 181 | // JComponent の実装 |
|
| 182 | // |
|
| 183 | protected void paintComponent( Graphics g ) |
|
| 184 | { |
|
| 185 | 0 | if( !_light || (_text == null) || !isVisible() ) { |
| 186 | 0 | return; |
| 187 | } |
|
| 188 | ||
| 189 | 0 | _text.draw( (Graphics2D)g, 0, _text.getAscent() ); |
| 190 | 0 | } |
| 191 | } |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |