1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }