OOP2

WalkSearchView.java

1
import java.util.ArrayList;
2
import java.util.Date;
3
4
import javax.swing.*;
5
6
/**
7
 * @author Maarten Vangeneugden - 1438256
8
 */
9
public class WalkSearchView {
10
11
	private ReservationController rc;
12
	private WalkController wc;
13
14
	private Window window;
15
	private JTextField groupNameField;
16
	private JTextField walkNameField;
17
	private JTextField walkDateField;
18
19
	public WalkSearchView(ReservationController rc, WalkController wc) {
20
		this.rc = rc;
21
		this.wc = wc;
22
		this.window = new Window("Search screen");
23
		this.addFields();
24
	}
25
26
	private void addFields() {
27
		// Reservations querying
28
		this.window.createLabel("Search walk reservations:");
29
		this.groupNameField = this.window.createTextField("Search by group name");
30
		this.walkNameField = this.window.createTextField("Search by walk name");
31
		this.walkDateField = this.window.createTextField(new Date().toGMTString());
32
		this.window.createButton("Search walk reservation", "", "queryWalkReservations", this);
33
		this.window.createButton("Query walk day", "", "queryWalkDay", this);
34
	}
35
36
	public void queryWalkReservations() {
37
		ArrayList<WalkReservation> foundWalkReservations = new ArrayList<>();
38
		String groupNameQuery = this.groupNameField.getText();
39
		String walkNameQuery = this.walkNameField.getText();
40
		for(WalkReservation activeWalkReservation : this.wc.getWalkReservations()) {
41
			if(activeWalkReservation.getGroupName().contains(groupNameQuery) &&
42
					activeWalkReservation.getWalk().getName().contains(walkNameQuery)) {
43
				foundWalkReservations.add(activeWalkReservation);
44
				}
45
		}
46
		// After collecting all results, offer the user the choice about which
47
		// one to take:
48
		String[] walkReservationNames = new String[foundWalkReservations.size()];
49
		for(int i=0; i<foundWalkReservations.size(); i++) {
50
			String text = foundWalkReservations.get(i).getGroupName() +" ("+
51
				foundWalkReservations.get(i).getWalk().getName() +")";
52
			walkReservationNames[i] = text;
53
		}
54
		int choice = this.window.choiceDialog("Multiple results were found. Specify which one you want to view:", walkReservationNames);
55
		if(choice == -1) { // No result found
56
			this.window.messageDialog("No walk reservations matched the given details.");
57
		}
58
		else {
59
			new WalkView(foundWalkReservations.get(choice), this.rc, this.wc);
60
		}
61
	}
62
63
64
	public void setRc(ReservationController rc) {
65
		this.rc = rc;
66
	}
67
68
	public ReservationController getRc() {
69
		return rc;
70
	}
71
72
	public void setWc(WalkController wc) {
73
		this.wc = wc;
74
	}
75
76
	public WalkController getWc() {
77
		return wc;
78
	}
79
80
}
81