Skip to content

Commit 4caf7a3

Browse files
Example fixes
1 parent 24b97e8 commit 4caf7a3

14 files changed

+60
-41
lines changed
113 Bytes
Binary file not shown.
113 Bytes
Binary file not shown.

bin/models/Model1.class

-536 Bytes
Binary file not shown.

bin/models/SchedulerIO.class

846 Bytes
Binary file not shown.

bin/views/EventListView.class

172 Bytes
Binary file not shown.

bin/views/NewEventView$1.class

0 Bytes
Binary file not shown.

bin/views/NewEventView$2.class

0 Bytes
Binary file not shown.

bin/views/NewEventView.class

200 Bytes
Binary file not shown.

example/controllers/EventListController.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public void run()
2929
{
3030
table = new JTable(getDataColumns(), getNameColumns());
3131
eventListView = new EventListView(this, table);
32-
// addView("EventListView", eventListView);
3332
}
3433

3534
/**
@@ -84,7 +83,9 @@ public Vector<Vector<Object>> getDataColumns()
8483
Vector<Vector<Object>> dataColumns = null;
8584

8685
try {
87-
dataColumns = SchedulerIO.getEvents();
86+
SchedulerIO schedulerIO = new SchedulerIO();
87+
schedulerIO.attach(eventListView);
88+
dataColumns = schedulerIO.getEvents();
8889
} catch (Exception ex) { }
8990

9091
return dataColumns;

example/controllers/NewEventController.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public NewEventController(EventListController eventListController)
4444
public void run()
4545
{
4646
newEventView = new NewEventView(this);
47-
// addView("NewEventView", newEventView);
4847
}
4948

5049
/**
@@ -62,11 +61,14 @@ public void addEvent(SchedulerEvent event)
6261
metadata[4] = event.getAlarm() ? "ON" : "OFF";
6362

6463
try {
65-
SchedulerIO.saveEvent(event);
64+
SchedulerIO schedulerIO = new SchedulerIO();
65+
schedulerIO.attach(newEventView);
66+
schedulerIO.saveEvent(event);
6667
} catch (Exception e) {
6768
JOptionPane.showMessageDialog(null, "ERROR", e.getMessage(), JOptionPane.ERROR_MESSAGE);
6869
}
6970

71+
7072
eventListController.addNewRow(metadata);
7173
}
7274

example/models/Model1.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

example/models/SchedulerIO.java

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,70 @@
66
import java.io.FileNotFoundException;
77
import java.io.FileReader;
88
import java.io.FileWriter;
9+
import java.util.ArrayList;
10+
import java.util.List;
911
import java.util.Vector;
1012

13+
import core.Model;
14+
import core.View;
15+
1116

1217
/**
1318
* Responsible for reading / writing events saved.
1419
*/
15-
public class SchedulerIO
20+
public class SchedulerIO implements Model
1621
{
1722
//-----------------------------------------------------------------------
1823
// Attributes
1924
//-----------------------------------------------------------------------
2025
private static final String DIRECTORY = ".";
2126
private static final String FILE = "events.txt";
27+
private List<View> views = new ArrayList<>();
28+
private String notice;
2229

2330

2431
//-----------------------------------------------------------------------
2532
// Methods
2633
//-----------------------------------------------------------------------
34+
@Override
35+
public void attach(View view)
36+
{
37+
views.add(view);
38+
}
39+
40+
@Override
41+
public void detach(View view)
42+
{
43+
views.remove(view);
44+
}
45+
46+
@Override
47+
public void notifyViews()
48+
{
49+
for (View v : views) {
50+
v.update(this, notice);
51+
}
52+
}
53+
2754
/**
2855
* Saves a {@link SchedulerEvent} in disk in {@link #DIRECTORY}.{@link #FILE}.
2956
*
3057
* @param event {@link SchedulerEvent Event} to be saved
3158
* @throws Exception If it can't save the event
3259
*/
33-
public static void saveEvent(SchedulerEvent event) throws Exception
60+
public void saveEvent(SchedulerEvent event) throws Exception
3461
{
3562
try {
3663
BufferedWriter writer = new BufferedWriter(new FileWriter(new File(DIRECTORY, FILE), true));
3764
writer.write(event.toString(), 0, event.toString().length());
3865
writer.newLine();
3966
writer.close();
4067
} catch (FileNotFoundException fnfe) {
41-
throw new Exception("File not found");
68+
notice = "File not found";
69+
notifyViews();
4270
} catch (Exception ex) {
43-
throw new Exception("Error while writing the file");
71+
notice = "Error while writing the file";
72+
notifyViews();
4473
}
4574
}
4675

@@ -49,7 +78,7 @@ public static void saveEvent(SchedulerEvent event) throws Exception
4978
* @return List of lists (matrix) of the events
5079
* @throws Exception If it can't read event file
5180
*/
52-
public static Vector<Vector<Object>> getEvents() throws Exception
81+
public Vector<Vector<Object>> getEvents() throws Exception
5382
{
5483
Vector<Vector<Object>> response = new Vector<Vector<Object>>();
5584

@@ -72,11 +101,12 @@ public static Vector<Vector<Object>> getEvents() throws Exception
72101
}
73102

74103
reader.close();
75-
76104
} catch (FileNotFoundException fnfe) {
77-
throw new Exception("File not found");
105+
notice = "File not found";
106+
notifyViews();
78107
} catch (Exception ex) {
79-
throw new Exception("There was a problem reading the event file");
108+
notice = "There was a problem reading the event file";
109+
notifyViews();
80110
}
81111

82112
return response;

example/views/EventListView.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package views;
22

33
import java.awt.BorderLayout;
4+
5+
import javax.swing.JOptionPane;
46
import javax.swing.JPanel;
57
import javax.swing.JScrollPane;
68
import javax.swing.JTable;
@@ -46,8 +48,12 @@ public EventListView(EventListController eventListController, JTable table)
4648
//-----------------------------------------------------------------------
4749
@Override
4850
public void update(Model model, Object data)
49-
{}
50-
51+
{
52+
if (data != null) {
53+
String notice = (String) data;
54+
JOptionPane.showMessageDialog(this, notice);
55+
}
56+
}
5157

5258
/**
5359
* Creates view's frame.

example/views/NewEventView.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import javax.swing.JCheckBox;
1111
import javax.swing.JFormattedTextField;
1212
import javax.swing.JLabel;
13+
import javax.swing.JOptionPane;
1314
import javax.swing.JPanel;
1415
import javax.swing.JRadioButton;
1516
import javax.swing.JTextField;
@@ -68,7 +69,12 @@ public NewEventView(NewEventController newEventController)
6869
//-----------------------------------------------------------------------
6970
@Override
7071
public void update(Model model, Object data)
71-
{}
72+
{
73+
if (data != null) {
74+
String notice = (String) data;
75+
JOptionPane.showMessageDialog(null, notice);
76+
}
77+
}
7278

7379
/**
7480
* Reset all fields.

0 commit comments

Comments
 (0)