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: SaveDataDialog.java,v 1.2 2005/07/23 18:57:38 ppoi Exp $
    18   */
    19  package tsukuba_bunko.peko.session;
    20
    21  import	java.awt.Dimension;
    22  import	java.awt.FlowLayout;
    23  import	java.awt.Rectangle;
    24
    25  import	java.awt.event.ActionEvent;
    26  import	java.awt.event.ActionListener;
    27
    28  import	javax.swing.JButton;
    29  import javax.swing.JDialog;
    30  import	javax.swing.JList;
    31  import	javax.swing.JOptionPane;
    32  import	javax.swing.JPanel;
    33  import	javax.swing.JScrollPane;
    34  import	javax.swing.ListSelectionModel;
    35
    36  import	javax.swing.event.ListSelectionEvent;
    37  import	javax.swing.event.ListSelectionListener;
    38
    39  import	tsukuba_bunko.peko.Logger;
    40  import	tsukuba_bunko.peko.PekoSystem;
    41
    42  import	tsukuba_bunko.peko.resource.ResourceManager;
    43
    44
    45  /***
    46   * セーブデータ選択ダイアログです。
    47   * @author	$Author: ppoi $
    48   * @version	$Revision: 1.2 $
    49   */
    50  public class SaveDataDialog	extends JDialog	implements ActionListener, ListSelectionListener	{
    51
    52  	/***
    53  	 * serial version UID
    54  	 */
    55  	private static final long	serialVersionUID	= 1778414332192365566L;
    56
    57  	/***
    58  	 * ダイアログの用途：セーブ用
    59  	 */
    60  	public static final boolean	FOR_SAVE = true;
    61
    62  	/***
    63  	 * ダイアログの用途：ロード用
    64  	 */
    65  	public static final boolean	FOR_LOAD = false;
    66
    67
    68
    69  	/***
    70  	 * 選択された番号
    71  	 */
    72  	protected int	_selected = -1;
    73
    74  	/***
    75  	 * 表示されるセーブデータ一覧
    76  	 */
    77  	protected SaveDataInfo[]	_infoList = null;
    78
    79
    80  	/***
    81  	 * セーブデータ一覧
    82  	 */
    83  	protected JList	_list = null;
    84
    85  	/***
    86  	 * OK ボタン
    87  	 */
    88  	protected JButton	_ok = null;
    89
    90  	/***
    91  	 * Cancel ボタン
    92  	 */
    93  	protected JButton	_cancel = null;
    94
    95  	/***
    96  	 * ダイアログの用途
    97  	 */
    98  	protected boolean _forSave = false;
    99
    100 	/***
    101 	 * セーブデータ情報を描画する ListCellRenderer
    102 	 */
    103 	protected SaveDataInfoRenderer	_renderer = null;
    104
    105
    106 	/***
    107 	 * <code>SaveDataDialig</code> のインスタンスを生成します。
    108 	 * @param	forSave	ダイアログの用途
    109 	 */
    110 	public SaveDataDialog( boolean forSave )
    111 	{
    112 		super( PekoSystem.getInstance().getMainWindow(), true );
    113 		_forSave = forSave;
    114
    115 		ResourceManager	resources = ResourceManager.getInstance();
    116 		Boolean	decorated = (Boolean)resources.getResource( ResourceIDs.DIALOG_DECORATED, true );
    117 		if( decorated != null )	{
    118 			setUndecorated( !decorated.booleanValue() );
    119 		}
    120
    121 		if( forSave )	{
    122 			String	title = (String)resources.getResource( ResourceIDs.DIALOG_TITLE_SAVE );
    123 			if( title == null )	{
    124 				Logger.warn( MessageIDs.SAV0012W );
    125 			}
    126 			else	{
    127 				setTitle( title );
    128 			}
    129 		}
    130 		else	{
    131 			String	title = (String)resources.getResource( ResourceIDs.DIALOG_TITLE_LOAD );
    132 			if( title == null )	{
    133 				Logger.warn( MessageIDs.SAV0012W );
    134 			}
    135 			else	{
    136 				setTitle( title );
    137 			}
    138 		}
    139
    140 		_renderer = new SaveDataInfoRenderer();
    141 	}
    142
    143
    144 	/***
    145 	 * <code>SaveDataDialog</code> の初期化を行います。
    146 	 * @param	infoList	表示するセーブデータ一覧
    147 	 */
    148 	protected void initialize( SaveDataInfo[] infoList, int initialIndex )
    149 	{
    150 		ResourceManager	resources = ResourceManager.getInstance();
    151
    152 		JList	list = new JList( infoList );
    153 		list.setAutoscrolls( true );
    154 		list.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
    155 		list.setCellRenderer( _renderer );
    156 		list.addListSelectionListener( this );
    157
    158 		JScrollPane	container = new JScrollPane( list );
    159 		container.setAutoscrolls( true );
    160
    161 		Integer	atonce = (Integer)resources.getResource( ResourceIDs.ITEMS_AT_ONCE );
    162 		if( atonce == null )	{
    163 			atonce = new Integer( 5 );
    164 			Logger.warn( MessageIDs.SAV0013W, new Object[]{"5"} );
    165 		}
    166 		else if( atonce.intValue() <= 0 )	{
    167 			atonce = new Integer( 5 );
    168 			Logger.warn( MessageIDs.SAV0014W, new Object[]{"5"} );
    169 		}
    170 		Dimension	size = container.getPreferredSize();
    171 		Dimension	cellSize = _renderer.getPreferredSize();
    172 		size.height = cellSize.height * atonce.intValue() + 5;
    173 		container.setPreferredSize( size );
    174 		container.setSize( size );
    175
    176 		Rectangle	visibleRect = new Rectangle( 5, (cellSize.height * (initialIndex - 1)), cellSize.width, cellSize.height );
    177 		container.getViewport().scrollRectToVisible( visibleRect );
    178
    179 		JPanel	buttons = new JPanel( new FlowLayout() );
    180 		JButton	ok = new JButton();
    181 		String	caption = (String)resources.getResource( ResourceIDs.BUTTON_OK );
    182 		if( caption == null )	{
    183 			caption = "OK";
    184 			Logger.warn( MessageIDs.SAV0019W, new Object[]{"OK", caption} );
    185 		}
    186 		ok.setText( caption );
    187 		ok.addActionListener( this );
    188 		JButton	cancel = new JButton();
    189 		caption = (String)resources.getResource( ResourceIDs.BUTTON_CANCEL );
    190 		if( caption == null )	{
    191 			caption = "CANCEL";
    192 			Logger.warn( MessageIDs.SAV0019W, new Object[]{"Cancel", caption} );
    193 		}
    194 		cancel.setText( caption );
    195 		cancel.addActionListener( this );
    196 		buttons.add( ok );
    197
    198 		buttons.add( cancel );
    199
    200 		JPanel	cp = new JPanel( new java.awt.BorderLayout() );
    201 		cp.add( container );
    202 		cp.add( buttons, java.awt.BorderLayout.SOUTH );
    203 		setContentPane( cp );
    204
    205 		pack();
    206 		setResizable( false );
    207
    208 		_list = list;
    209 		_ok = ok;
    210 		_cancel = cancel;
    211 		getRootPane().setDefaultButton( _ok );
    212
    213 		_infoList = infoList;
    214 		list.setSelectedIndex( initialIndex );
    215 	}
    216
    217
    218 	/***
    219 	 * 現在選択されているセーブデータのインデックスを取得します。
    220 	 * @return	選択されているセーブデータのインデックス
    221 	 */
    222 	public int getSelectedIndex()
    223 	{
    224 		return _selected;
    225 	}
    226
    227
    228 //
    229 //	ActionListener の実装
    230 //
    231 	public void actionPerformed( ActionEvent ev )
    232 	{
    233 		if( ev.getSource() == _ok )	{
    234 			if( _list.getSelectedValue() == null )	{
    235 				if( _forSave )	{
    236 					_selected = _list.getSelectedIndex();
    237 				}
    238 				else	{
    239 					return;
    240 				}
    241 			}
    242 			else	{
    243 				if( _forSave )	{
    244 					ResourceManager	resources = ResourceManager.getInstance();
    245 					String	title = (String)resources.getResource( ResourceIDs.DIALOG_CONFIRM_TITLE );
    246 					if( title == null )	{
    247 						Logger.warn( MessageIDs.SAV0015W );
    248 					}
    249 					String	message = (String)resources.getResource( ResourceIDs.DIALOG_CONFIRM_MESSAGE );
    250 					if( message == null )	{
    251 						message = "Do you overwrite save data ?";
    252 						Logger.warn( MessageIDs.SAV0016W, new Object[]{"\"" + message + "\""} );
    253 					}
    254
    255 					if( JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(this, message, title, JOptionPane.YES_NO_OPTION) )	{
    256 						_selected = _list.getSelectedIndex();
    257 					}
    258 					else	{
    259 						return;
    260 					}
    261 				}
    262 				else	{
    263 					_selected = _list.getSelectedIndex();
    264 				}
    265 			}
    266 		}
    267 		else	{
    268 			_selected = -1;
    269 		}
    270
    271 		dispose();
    272 	}
    273
    274
    275 //
    276 //	ListSelectionListener の実装
    277 //
    278 	public void valueChanged( ListSelectionEvent ev )
    279 	{
    280 		if( _forSave )	{
    281 			return;
    282 		}
    283
    284 		int	selected = _list.getSelectedIndex();
    285 		if( (selected != -1) && (_infoList[selected] != null) )	{
    286 			_ok.setEnabled( true );
    287 		}
    288 		else	{
    289 			_ok.setEnabled( false );
    290 		}
    291 	}
    292
    293
    294
    295
    296 //
    297 //	ユーティリティ
    298 //
    299 	/***
    300 	 * セーブデータ選択ダイアログを表示し、選択されたセーブデータのインデックスを取得します。
    301 	 * @param	list	表示するセーブデータ一覧
    302 	 * @return	選択されたセーブデータのインデックス
    303 	 */
    304 	public static int showDialog( SaveDataInfo[] list, int initialIndex, boolean forSave )
    305 	{
    306 		SaveDataDialog	dialog = new SaveDataDialog( forSave );
    307 		dialog.initialize( list, initialIndex );
    308 		dialog.setLocationRelativeTo( dialog.getOwner() );
    309 		dialog.setVisible( true );
    310 		return dialog.getSelectedIndex();
    311 	}
    312 }