Skip to content

Commit 64170ad

Browse files
committed
Task 4: Complete the Query class
1 parent 1535600 commit 64170ad

File tree

6 files changed

+22
-6
lines changed

6 files changed

+22
-6
lines changed
829 Bytes
Binary file not shown.
-118 Bytes
Binary file not shown.
131 Bytes
Binary file not shown.

SoftwareConstruction_ObjectOrientedDesign/finalProject/final-project-starter/TwitterMapperStarter/src/query/Query.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@
33
import filters.Filter;
44
import org.openstreetmap.gui.jmapviewer.JMapViewer;
55
import org.openstreetmap.gui.jmapviewer.Layer;
6+
import twitter4j.Status;
7+
import ui.MapMarkerSimple;
8+
import util.Util;
69

710
import javax.swing.*;
811
import java.awt.*;
12+
import java.util.Observable;
13+
import java.util.Observer;
914

1015
/**
1116
* A query over the twitter stream.
12-
* TODO: Task 4: you are to complete this class.
1317
*/
14-
public class Query {
18+
public class Query implements Observer {
19+
1520
// The map on which to display markers when the query matches
1621
private final JMapViewer map;
1722
// Each query has its own "layer" so they can be turned on and off all at once
@@ -24,6 +29,8 @@ public class Query {
2429
private final Filter filter;
2530
// The checkBox in the UI corresponding to this query (so we can turn it on and off and delete it)
2631
private JCheckBox checkBox;
32+
// For the statues of the observable (TwitterSource)
33+
private Status status;
2734

2835
public Color getColor() {
2936
return color;
@@ -61,13 +68,21 @@ public String toString() {
6168
return "Query: " + queryString;
6269
}
6370

71+
@Override
72+
public void update(Observable o, Object arg) {
73+
this.status = (Status) arg;
74+
if (filter.matches((Status) arg)) {
75+
map.addMapMarker(new MapMarkerSimple(layer, Util.statusCoordinate((Status) arg)));
76+
}
77+
}
78+
6479
/**
6580
* This query is no longer interesting, so terminate it and remove all traces of its existence.
6681
*
67-
* TODO: Implement this method
6882
*/
6983
public void terminate() {
70-
84+
// map.removeMapMarker(new MapMarkerSimple(layer, Util.statusCoordinate(this.status)));
85+
map.removeAllMapMarkers();
7186
}
7287
}
7388

SoftwareConstruction_ObjectOrientedDesign/finalProject/final-project-starter/TwitterMapperStarter/src/twitter/TwitterSource.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ public abstract class TwitterSource extends Observable {
1111
protected boolean doLogging = true;
1212
// The set of terms to look for in the stream of tweets
1313
protected Set<String> terms = new HashSet<>();
14-
private List<Observer> observers = new ArrayList<>();
1514

1615
// Called each time a new set of filter terms has been established
1716
abstract protected void sync();

SoftwareConstruction_ObjectOrientedDesign/finalProject/final-project-starter/TwitterMapperStarter/src/ui/Application.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void addQuery(Query query) {
5555
Set<String> allterms = getQueryTerms();
5656
twitterSource.setFilterTerms(allterms);
5757
contentPanel.addQuery(query);
58-
// TODO: This is the place where you should connect the new query to the twitter source
58+
twitterSource.addObserver(query);
5959
}
6060

6161
/**
@@ -189,6 +189,8 @@ public void run() {
189189
// A query has been deleted, remove all traces of it
190190
public void terminateQuery(Query query) {
191191
// TODO: This is the place where you should disconnect the expiring query from the twitter source
192+
query.terminate();
193+
twitterSource.deleteObserver(query);
192194
queries.remove(query);
193195
Set<String> allterms = getQueryTerms();
194196
twitterSource.setFilterTerms(allterms);

0 commit comments

Comments
 (0)