Coverage Summary for Class: CheckBoxListener (it.polimi.ingsw.Client.GUI.Listeners)
| Class | Class, % | Method, % | Branch, % | Line, % |
|---|---|---|---|---|
| CheckBoxListener | 0% (0/1) | 0% (0/2) | 0% (0/12) | 0% (0/15) |
1 package it.polimi.ingsw.Client.GUI.Listeners; 2 3 import javax.swing.*; 4 import java.awt.event.ItemEvent; 5 import java.awt.event.ItemListener; 6 7 /** 8 * Listener responsible for restricting the maximum number of checkboxes that can be selected 9 */ 10 public class CheckBoxListener implements ItemListener { 11 /** 12 * limit of selectable checkboxes 13 */ 14 private final int maxCheckBoxesSelectable; 15 /** 16 * "listened" checkboxes 17 */ 18 private final JCheckBox[] checkBoxes; 19 /** 20 * Selected checkboxes 21 */ 22 private int selectionCounter = 0; 23 24 /** 25 * Create a new checkBoxListener 26 * 27 * @param limit maximum number of selectable checkBoxes 28 * @param checkBoxes checkboxes that will be listened by this listener 29 */ 30 public CheckBoxListener(int limit, JCheckBox[] checkBoxes) { 31 this.maxCheckBoxesSelectable = limit; 32 this.checkBoxes = checkBoxes; 33 } 34 35 /** 36 * As soon as one checkbox has been selected, verify that selected checkBoxes are less than maximum 37 * 38 * @param e the event to be processed 39 */ 40 @Override 41 public void itemStateChanged(ItemEvent e) { 42 JCheckBox source = (JCheckBox) e.getSource(); 43 if (source.isSelected()) { 44 selectionCounter++; 45 // check for max selections: 46 if (selectionCounter == maxCheckBoxesSelectable) 47 for (JCheckBox box : checkBoxes) 48 if (!box.isSelected()) 49 box.setEnabled(false); 50 } else { 51 selectionCounter--; 52 // check for less than max selections: 53 if (selectionCounter < maxCheckBoxesSelectable) 54 for (JCheckBox box : checkBoxes) 55 box.setEnabled(true); 56 } 57 } 58 }