View Javadoc

1   /*
2    *   Magic-Project is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  package net.sf.magicproject.ui.wizard;
20  
21  import java.awt.Dimension;
22  import java.awt.event.ActionEvent;
23  import java.util.List;
24  
25  import javax.swing.DefaultListModel;
26  import javax.swing.JList;
27  import javax.swing.JOptionPane;
28  import javax.swing.JScrollPane;
29  import javax.swing.ListSelectionModel;
30  import javax.swing.WindowConstants;
31  import javax.swing.event.ListSelectionEvent;
32  import javax.swing.event.ListSelectionListener;
33  
34  import net.sf.magicproject.action.BackgroundMessaging;
35  import net.sf.magicproject.clickable.ability.Ability;
36  import net.sf.magicproject.event.context.ContextEventListener;
37  import net.sf.magicproject.ui.i18n.LanguageManager;
38  
39  /***
40   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
41   * @since 0.85
42   */
43  public class Choice extends YesNo implements ListSelectionListener {
44  
45  	/***
46  	 * Creates a new instance of Choice <br>
47  	 * 
48  	 * @param context
49  	 *          context of associated ability. This context will be used to
50  	 *          restart this wizard in case of Backgroud button is used.
51  	 * @param ability
52  	 *          ability to associate to this ability. If this ability has an
53  	 *          assosciated picture, it will be used instead of given picture.
54  	 *          Ability's name is also used to fill the title. This ability will
55  	 *          be used to restart this wizard in case of Background button is
56  	 *          used.
57  	 * @param action
58  	 *          the action's name and content will be used in the wizard totle and
59  	 *          also message text.
60  	 * @param allowCancel
61  	 *          Is the cancel button is allowed.
62  	 * @param actions
63  	 *          set of avalaible choices?
64  	 */
65  	public Choice(ContextEventListener context, Ability ability,
66  			BackgroundMessaging action, boolean allowCancel, List<String> actions) {
67  		super(context, ability, action, LanguageManager
68  				.getString("wiz_choice.title"), LanguageManager
69  				.getString("wiz_choice.description")
70  				+ ability.getAbilityTitle(), "wiz_choice.gif", LanguageManager
71  				.getString("ok"), LanguageManager.getString("cancel"), 500, 300);
72  		this.actions = actions;
73  		int count = actions.size();
74  		for (int i = actions.size(); i-- > 0;) {
75  			if (actions.get(i) == null) {
76  				count--;
77  			}
78  		}
79  		if (count == 0) {
80  			// cancel button is disabled but no possible choice !?
81  			indexAnswer = 0;
82  			optionAnswer = JOptionPane.NO_OPTION;
83  			return;
84  		}
85  		/*
86  		 * TODO Enable the auto choice? if (count == 1 &&
87  		 * Magic.makeChoiceAsPossible) { // cancel button is disabled and there is
88  		 * only one possibility for (int i = actions.size(); i-- > 0;) { if
89  		 * (actions.get(i) != null) { indexAnswer = i; } } optionAnswer =
90  		 * JOptionPane.YES_OPTION; return; }
91  		 */
92  		if (allowCancel) {
93  			// "cancel" button is enabled and is the default of "ESC" action
94  			setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
95  		} else {
96  			// "cancel" button is disabled, window cannot be closed without choice
97  			setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
98  			this.cancelBtn.setEnabled(false);
99  			this.okBtn.setEnabled(false);
100 		}
101 		final DefaultListModel model = new DefaultListModel();
102 		actionList = new JList(model);
103 		actionList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
104 		actionList.setLayoutOrientation(JList.VERTICAL);
105 		actionList.addListSelectionListener(this);
106 		for (int i = 0; i < actions.size(); i++) {
107 			if (actions.get(i) != null) {
108 				model.addElement("<html>" + actions.get(i));
109 			}
110 		}
111 		final JScrollPane listScrollerLeft = new JScrollPane(actionList);
112 		listScrollerLeft.setPreferredSize(new Dimension(100, 280));
113 		gameParamPanel.add(listScrollerLeft);
114 
115 		// TODO makeChoiceAsPossible = new JCheckBox(LanguageManager
116 		// .getString("makeChoiceAsPossible"));
117 		// makeChoiceAsPossible.setSelected(Magic.makeChoiceAsPossible);
118 	}
119 
120 	@Override
121 	public void setVisible(boolean visible) {
122 		if (visible && actionList == null) {
123 			return;
124 		}
125 		super.setVisible(visible);
126 	}
127 
128 	public void valueChanged(ListSelectionEvent e) {
129 		if (!e.getValueIsAdjusting()) {
130 			if (actionList.getSelectedIndex() == -1) {
131 				// No selection, disable fire button.
132 				okBtn.setEnabled(false);
133 			} else {
134 				// Selection, enable the fire button.
135 				okBtn.setEnabled(true);
136 			}
137 		}
138 	}
139 
140 	@Override
141 	public void actionPerformed(ActionEvent event) {
142 		if (event.getSource() == cancelBtn) {
143 			// set the static integer token to the
144 			indexAnswer = 0;
145 		} else if (event.getSource() == okBtn) {
146 			if (actionList.getSelectedValue() == null) {
147 				// no selected value TODO disable OK button while no selection
148 				return;
149 			}
150 			// set the static integer token
151 			indexAnswer = -1;
152 			for (int i = actions.size(); i-- > 0;) {
153 				if (actions.get(i).equals(
154 						actionList.getSelectedValue().toString().substring(
155 								"<html>".length()))) {
156 					indexAnswer = i;
157 					break;
158 				}
159 			}
160 			if (indexAnswer == -1) {
161 				throw new IllegalStateException(
162 						"Unable to find selected index in choice");
163 			}
164 			// TODO Magic.makeChoiceAsPossible = makeChoiceAsPossible.isSelected();
165 		}
166 		super.actionPerformed(event);
167 	}
168 
169 	/***
170 	 * The list of available actions
171 	 */
172 	private JList actionList;
173 
174 	/***
175 	 * The actions list displayed in List GUI component. This array may contain
176 	 * <code>null</code> objects. These ones are ignored.
177 	 */
178 	private final List<String> actions;
179 }