Coverage report

| %line | %branch
--- | --- | ---
tsukuba_bunko.peko.session.SaveDataDialog |
0% |
0% |

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

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.