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: SaveDataInfoRenderer.java,v 1.2 2005/07/23 19:01:33 ppoi Exp $
    18   */
    19  package tsukuba_bunko.peko.session;
    20
    21  import	java.awt.Color;
    22  import	java.awt.Component;
    23  import	java.awt.Dimension;
    24  import	java.awt.Font;
    25  import	java.awt.Graphics;
    26  import	java.awt.Graphics2D;
    27  import	java.awt.RenderingHints;
    28
    29  import	java.awt.font.TextAttribute;
    30  import	java.awt.font.TextLayout;
    31
    32  import	java.text.AttributedString;
    33  import	java.text.MessageFormat;
    34  import	java.text.SimpleDateFormat;
    35
    36  import	java.util.Map;
    37
    38  import	javax.swing.BorderFactory;
    39  import javax.swing.JComponent;
    40  import	javax.swing.JList;
    41  import	javax.swing.ListCellRenderer;
    42
    43  import	tsukuba_bunko.peko.Logger;
    44
    45  import	tsukuba_bunko.peko.resource.ResourceManager;
    46  import	tsukuba_bunko.peko.resource.ColorManager;
    47  import	tsukuba_bunko.peko.resource.FontManager;
    48
    49
    50  /***
    51   * セーブデータ情報を描画する <code>ListCellRenderer</code> です。
    52   * @author	$Author: ppoi $
    53   * @version	$Revision: 1.2 $
    54   */
    55  public class SaveDataInfoRenderer	extends JComponent	implements ListCellRenderer	{
    56
    57  	/***
    58  	 * serial version UID
    59  	 */
    60  	private static final long	serialVersionUID	= 2614501005677152982L;
    61
    62  	/***
    63  	 * 描画するセーブデータ情報
    64  	 */
    65  	private SaveDataInfo	_info = null;
    66
    67  	/***
    68  	 * 選択されているかどうか
    69  	 */
    70  	private boolean	_selected = false;
    71
    72  	/***
    73  	 */
    74  	private int	_index = -1;
    75
    76  	/***
    77  	 * 選択時の背景色
    78  	 */
    79  	private Color	_selectedBackground = null;
    80
    81  	/***
    82  	 * 非選択時の背景色
    83  	 */
    84  	private Color	_unselectedBackground = null;
    85
    86  	/***
    87  	 * タイトルフォント
    88  	 */
    89  	private Font	_titleFont = null;
    90
    91  	/***
    92  	 * タイトル前景色
    93  	 */
    94  	private Color	_titleColor = null;
    95
    96  	/***
    97  	 * タイムスタンプフォント
    98  	 */
    99  	private Font	_timestampFont = null;
    100
    101 	/***
    102 	 * タイムスタンプ前景色
    103 	 */
    104 	private Color	_timestampColor = null;
    105
    106 	/***
    107 	 * コメントフォント
    108 	 */
    109 	private Font	_commentFont = null;
    110
    111 	/***
    112 	 * コメント前景色
    113 	 */
    114 	private Color	_commentColor;
    115
    116
    117 	/***
    118 	 * タイトルのフォーマット
    119 	 */
    120 	private MessageFormat	_titleFormat = null;
    121
    122 	/***
    123 	 * タイムスタンプのフォーマット
    124 	 */
    125 	private SimpleDateFormat	_timestampFormat = null;
    126
    127 	/***
    128 	 * 未登録セルのタイトル
    129 	 */
    130 	private String	_nodataTitle = null;
    131
    132
    133 	/***
    134 	 * <code>SaveDataInfoRenderer</code> のインスタンスを生成します。
    135 	 */
    136 	public SaveDataInfoRenderer()
    137 	{
    138 		ResourceManager	resources = ResourceManager.getInstance();
    139 		ColorManager	colors = ColorManager.getInstance();
    140 		FontManager	fonts = FontManager.getInstance();
    141
    142 		Dimension	size = (Dimension)resources.getResource( ResourceIDs.CELL_SIZE );
    143 		if( size == null )	{
    144 			Logger.warn( MessageIDs.SAV0009W, new Object[]{"380,65"} );
    145 			size = new Dimension( 380, 65 );
    146 		}
    147 		setPreferredSize( size );
    148 		setSize( size );
    149
    150 		_selectedBackground = (Color)resources.getResource( ResourceIDs.CELL_BACKGROUND_SELECTED );
    151 		if( _selectedBackground == null )	{
    152 			_selectedBackground = colors.getColor( "#FF88FF" );
    153 			Logger.warn( MessageIDs.SAV0024W, new Object[]{"cell.background.selected", "#FF88FF"} );
    154 		}
    155
    156 		_unselectedBackground = (Color)resources.getResource( ResourceIDs.CELL_BACKGROUND_UNSELECTED );
    157 		if( _unselectedBackground == null )	{
    158 			_unselectedBackground = colors.getColor( "white" );
    159 			Logger.warn( MessageIDs.SAV0024W, new Object[]{"cell.background.unselected", "white"} );
    160 		}
    161
    162 		_titleColor = (Color)resources.getResource( ResourceIDs.CELL_TITLE_COLOR );
    163 		if( _titleColor == null )	{
    164 			_titleColor = colors.getColor( "black" );
    165 			Logger.warn( MessageIDs.SAV0023W, new Object[]{"cell.title", "black", } );
    166 		}
    167
    168 		_timestampColor = (Color)resources.getResource( ResourceIDs.CELL_TIMESTAMP_COLOR );
    169 		if( _timestampColor == null )	{
    170 			_timestampColor = colors.getColor( "black" );
    171 			Logger.warn( MessageIDs.SAV0023W, new Object[]{"cell.timestamp", "black"} );
    172 		}
    173
    174 		_commentColor = (Color)resources.getResource( ResourceIDs.CELL_COMMENT_COLOR );
    175 		if( _commentColor == null )	{
    176 			_commentColor = colors.getColor( "black" );
    177 			Logger.warn( MessageIDs.SAV0023W, new Object[]{"cell.comment", "black"} );
    178 		}
    179
    180
    181 		_titleFont = (Font)resources.getResource( ResourceIDs.CELL_TITLE_FONT );
    182 		if( _titleFont == null )	{
    183 			Logger.warn( MessageIDs.SAV0010W, new Object[]{"cell.title", "family:SansSerif; size:16.0; style:normal; weight:bold"} );
    184 			Map	attributes = new java.util.HashMap();
    185 			attributes.put( TextAttribute.FAMILY, "SansSerif" );
    186 			attributes.put( TextAttribute.SIZE, new Float(16f) );
    187 			attributes.put( TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR );
    188 			attributes.put( TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD );
    189 			_titleFont = fonts.getFont( attributes );
    190 		}
    191
    192 		_timestampFont = (Font)resources.getResource( ResourceIDs.CELL_TIMESTAMP_FONT );
    193 		if( _timestampFont == null )	{
    194 			Logger.warn( MessageIDs.SAV0010W, new Object[]{"cell.timestamp", "family:SansSerif; size:11.0; style:normal; weight:normarl"} );
    195 			Map	attributes = new java.util.HashMap();
    196 			attributes.put( TextAttribute.FAMILY, "SansSerif" );
    197 			attributes.put( TextAttribute.SIZE, new Float(11f) );
    198 			attributes.put( TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR );
    199 			attributes.put( TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD );
    200 			_timestampFont = fonts.getFont( attributes );
    201 		}
    202
    203 		_commentFont = (Font)resources.getResource( ResourceIDs.CELL_COMMENT_FONT );
    204 		if( _timestampFont == null )	{
    205 			Logger.warn( MessageIDs.SAV0010W, new Object[]{"cell.comment", "family:SansSerif; size:16.0; style:normal; weight:normarl"} );
    206 			Map	attributes = new java.util.HashMap();
    207 			attributes.put( TextAttribute.FAMILY, "SansSerif" );
    208 			attributes.put( TextAttribute.SIZE, new Float(11f) );
    209 			attributes.put( TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR );
    210 			attributes.put( TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD );
    211 			_commentFont = fonts.getFont( attributes );
    212 		}
    213
    214 		_titleFormat = (MessageFormat)resources.getResource( ResourceIDs.CELL_TITLE_FORMAT );
    215 		if( _titleFormat == null )	{
    216 			_titleFormat = new MessageFormat( "No.{0}  {1}" );
    217 			Logger.warn( MessageIDs.SAV0011W, new Object[]{"\"No.{0}  {1}\""} );
    218 		}
    219
    220 		String	timestampFormat = (String)resources.getResource( ResourceIDs.CELL_TIMESTAMP_FORMAT );
    221 		if( timestampFormat == null )	{
    222 			timestampFormat = "yyyy/MM/dd hh:mm";
    223 			Logger.warn( MessageIDs.SAV0017W, new Object[]{"\"" + timestampFormat + "\""} );
    224 		}
    225 		_timestampFormat = new SimpleDateFormat( timestampFormat );
    226
    227 		_nodataTitle = (String)resources.getResource( ResourceIDs.CELL_NO_DATA_TITLE );
    228 		if( _nodataTitle == null )	{
    229 			_nodataTitle = "-NO DATA-";
    230 			Logger.warn( MessageIDs.SAV0018W, new Object[]{"\"" + _nodataTitle + "\""} );
    231 		}
    232 	}
    233
    234
    235 //
    236 //	JComponent の実装
    237 //
    238 	public void paintComponent( Graphics g )
    239 	{
    240 		Dimension	size = getSize();
    241 		if( _selected )	{
    242 			g.setColor( _selectedBackground );
    243 		}
    244 		else	{
    245 			g.setColor( _unselectedBackground );
    246 		}
    247 		g.fillRect( 0, 0, size.width, size.height );
    248
    249 		g.setColor( Color.black );
    250
    251 		Graphics2D	g2 = (Graphics2D)g;
    252 		g2.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON );
    253 		float	y = 3f;
    254 		AttributedString	as = null;
    255 		TextLayout	layout = null;
    256
    257 		if( _info == null )	{
    258 			as = new AttributedString( _titleFormat.format(new Object[]{String.valueOf(_index + 1), _nodataTitle}) );
    259 			as.addAttribute( TextAttribute.FONT, _titleFont );
    260 			as.addAttribute( TextAttribute.FOREGROUND, _titleColor );
    261
    262 			layout = new TextLayout( as.getIterator(), g2.getFontRenderContext() );
    263 			layout.draw( g2, 5, layout.getAscent() + y );
    264 			y += layout.getAscent() + layout.getDescent() + 5f;
    265 		}
    266 		else	{
    267 			as = new AttributedString( _titleFormat.format(new Object[]{String.valueOf(_index + 1), _info.getTitle()}) );
    268 			as.addAttribute( TextAttribute.FONT, _titleFont );
    269 			as.addAttribute( TextAttribute.FOREGROUND, _titleColor );
    270
    271 			layout = new TextLayout( as.getIterator(), g2.getFontRenderContext() );
    272 			layout.draw( g2, 5, layout.getAscent() + y );
    273 			y += layout.getAscent() + layout.getDescent() + 5f;
    274
    275 			as = new AttributedString( _timestampFormat.format(_info.getTimestamp()) );
    276 			as.addAttribute( TextAttribute.FONT, _timestampFont );
    277 			as.addAttribute( TextAttribute.FOREGROUND, _timestampColor );
    278
    279 			layout = new TextLayout( as.getIterator(), g2.getFontRenderContext() );
    280 			layout.draw( g2, 5, layout.getAscent() + y );
    281 			y += layout.getAscent() + layout.getDescent() + 5f;
    282
    283 			if( _info.getComment() != null )	{
    284 				as = new AttributedString( _info.getComment() );
    285 				as.addAttribute( TextAttribute.FONT, _commentFont );
    286 				as.addAttribute( TextAttribute.FOREGROUND, _commentColor );
    287
    288 				layout = new TextLayout( as.getIterator(), g2.getFontRenderContext() );
    289 				layout.draw( g2, 5, layout.getAscent() + y );
    290 			}
    291 		}
    292 	}
    293
    294
    295 //
    296 //	ListCellRenderer の実装
    297 //
    298 	public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasForcus )
    299 	{
    300 		_info = (SaveDataInfo)value;
    301 		_selected = isSelected;
    302 		_index = index;
    303 		setBorder( BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.LOWERED) );
    304 		return this;
    305 	}
    306 }