summaryrefslogtreecommitdiff
path: root/src/FamilyButtonListener.java
blob: 95a4bc48a9b8a37004518b70c1f55c4dd3c5b6a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.awt.event.*;

/*
 * @author
 * Kian Agheli
 *
 * References:
 *
 * Date:
 * 2024-05-25
 *
 * Purpose of class:
 * Listen for state changes in an instantiation of FamilyButton.
 */
public class FamilyButtonListener implements ItemListener
{
	private MetadataView metadataView; // A FamilyButtonListener has a metadataView
	private FamilyButton[] buttons; // A FamilyButtonListener has an array of buttons
	private int buttonIndex; // A FamilyButtonListener has a button at a specified index in the array

	public FamilyButtonListener(FamilyButton[] buttons, int buttonIndex, MetadataView metadataView) {
		/* Set each of the instance's fields to the given objects. */
		this.buttons = buttons;
		this.buttonIndex = buttonIndex;
		this.metadataView = metadataView;
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		/* Get the state of the button. */
		int state = e.getStateChange();
		/* If the button is selected: */
		if (state == ItemEvent.SELECTED) {
			/* Deselect all other buttons. */
			for (int i = 0; i < buttons.length; i++) {
				if (i != buttonIndex) {
					buttons[i].setSelected(false);
				}
			}

			/* Set the metadata on display to the metadata of the FontFamily
			associated with the selected button. */
			metadataView.setText(buttons[buttonIndex].getFamily().toString());
		/* If the button is deselected: */
		} else {
			/* Check if every button is deselected. */
			boolean selected = true;
			for (int i = 0; i < buttons.length; i++) {
				if (!buttons[i].isSelected()) {
					selected = false;
				}
			}

			/* If every button is deselected, reset the metadata to display
			the default text. */
			if (!selected) {
				metadataView.reset();
			}
		}
	}
}