OOP2

DetailedCourseView.java

1
import javax.swing.*;
2
3
/**
4
 * This class creates a JPanel using the given course. This JPanel is then a detailed description of said course.
5
 * @author Maarten Vangeneugden - 1438256
6
 */
7
8
class DetailedCourseView {
9
		private Course m_course; // The course of this class.
10
		public Course course() {
11
				return m_course;
12
		}
13
		public void setCourse(Course course) { // Sets the course, but also triggers to update the panel itself. This is necessary, since the panel will otherwise not be a representation of the course itself.
14
				m_course = course;
15
				setContent();
16
		}
17
		
18
		private JPanel m_panel; // The panel that will represent the course.
19
		public JPanel panel() {
20
				return m_panel;
21
		}
22
		private void setPanel(JPanel panel) {
23
				m_panel = panel;
24
		}
25
		//There is no public panel setter. The panel can only be changed by modifying the course, since this panel represents the course.
26
		
27
		public DetailedCourseView(Course course) {
28
				setCourse(course);
29
		}
30
31
		/**
32
		 * Will update the panel of the class according to the given course.
33
		 * @pre The course of the class is not null.
34
		 * @post The panel of the class is updated and is now an update representation of the given course.
35
		 */
36
		private void setContent() { // Creates the content for this JPanel. Not that it's private; Its functionality may only be triggered by its own instance.
37
				// First, we handle the common members, since we know the base class can't be smaller than inherited classes.
38
				JLabel lblName = new JLabel("Naam: " + course().name());
39
				JLabel lblID = new JLabel("ID: " + Integer.toString(course().ID()));
40
				JLabel lblStudyPoints = new JLabel("Studiepunten: " + Integer.toString(course().studyPoints()));
41
				JLabel lblSemester = new JLabel("Semester: " + Integer.toString(course().semester()));
42
				JLabel lblType = new JLabel(); // We wil fill this in later.
43
44
				JLabel lblPass = new JLabel();
45
				if(course().creditAcquired())
46
						lblPass.setText("Verworven credit");
47
				else
48
						lblPass.setText("Credit niet verworven");
49
				
50
				// Second, we do a RTTI on the given course by checking the instance type. (RTTI = RunTime Type Identification)
51
				if(course() instanceof MandatoryCourse) {
52
						lblType.setText("Verplicht vak - Jaar" + Integer.toString(((MandatoryCourse)course()).year()));
53
				}
54
				else if(course() instanceof ChoiceCourse) {
55
						lblType.setText("Keuzevak");
56
				}
57
58
				// Third, we add all the labels to the panel.
59
				JPanel panel = new JPanel();
60
				panel.add(lblName);
61
				panel.add(lblID);
62
				panel.add(lblStudyPoints);
63
				panel.add(lblSemester);
64
				panel.add(lblType);
65
				panel.add(lblPass);
66
67
				setPanel(panel);
68
		}
69
}
70