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.deckbuilder;
20  
21  import java.awt.Component;
22  import java.awt.Dimension;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.List;
26  
27  import javax.swing.BoxLayout;
28  import javax.swing.ImageIcon;
29  import javax.swing.JButton;
30  import javax.swing.JPanel;
31  import javax.swing.SwingConstants;
32  
33  import net.sf.magicproject.tools.MToolKit;
34  import net.sf.magicproject.ui.i18n.LanguageManager;
35  
36  /***
37   * A container of a set of constraints. Constraints are validated each time the
38   * deck is updated.
39   * 
40   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
41   * @since 0.94
42   */
43  public class ConstraintsChecker extends JPanel {
44  
45  	private static final String IMAGE_CHECK = "constraint_check.gif";
46  
47  	private static final String IMAGE_FAIL = "constraint_fail.gif";
48  
49  	private final List<DeckConstraint> constraints;
50  
51  	private final ImageIcon iconFail;
52  
53  	private final ImageIcon iconCheck;
54  
55  	/***
56  	 * The deck attached to this checker.
57  	 */
58  	private Deck deck;
59  
60  	/***
61  	 * Create a new instance of this class.
62  	 */
63  	public ConstraintsChecker() {
64  		super();
65  		constraints = new ArrayList<DeckConstraint>();
66  		constraints.addAll(DeckConstraints.getDeckConstraints());
67  		iconFail = new ImageIcon(MToolKit.getIconPath(IMAGE_FAIL));
68  		iconCheck = new ImageIcon(MToolKit.getIconPath(IMAGE_CHECK));
69  		Collections.sort(constraints);
70  		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
71  		for (DeckConstraint constraint : constraints) {
72  			final JButton checker = new JButton(constraint.getConstraintLocalName());
73  			checker.setAlignmentY(Component.LEFT_ALIGNMENT);
74  			checker.setHorizontalAlignment(SwingConstants.LEFT);
75  			checker.setMinimumSize(new Dimension(190, 20));
76  			checker.setMaximumSize(new Dimension(2000, 20));
77  			checker.setBorder(null);
78  			checker.setFocusPainted(false);
79  			add(checker);
80  		}
81  	}
82  
83  	/***
84  	 * Return the checker at the specified index.
85  	 * 
86  	 * @return the checker at the specified index.
87  	 */
88  	private JButton getChecker(int index) {
89  		return (JButton) getComponent(index);
90  	}
91  
92  	/***
93  	 * Set the deck attached to this checker.
94  	 * 
95  	 * @param deck
96  	 *          the deck attached to this checker.
97  	 */
98  	public void setDeck(Deck deck) {
99  		this.deck = deck;
100 	}
101 
102 	/***
103 	 * Update the constraint checkers.
104 	 */
105 	public void updateCheckers() {
106 		for (int index = 0; index < getComponentCount(); index++) {
107 			final JButton checker = getChecker(index);
108 			final DeckConstraint constraint = constraints.get(index);
109 			final List<String> errors = constraint.validate(deck);
110 			if (errors.isEmpty()) {
111 				checker.setIcon(iconCheck);
112 				checker.setToolTipText("<html>"
113 						+ LanguageManager.getString("db_constraint_check"));
114 			} else {
115 				checker.setIcon(iconFail);
116 				StringBuilder errorsString = new StringBuilder();
117 				for (String error : errors) {
118 					if (errorsString.length() != 0) {
119 						errorsString.append("<br>");
120 					}
121 					errorsString.append(error);
122 				}
123 				checker.setToolTipText("<html>"
124 						+ LanguageManager.getString("db_constraint_fail", errorsString
125 								.toString()));
126 			}
127 		}
128 	}
129 }