mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
Moved boat annotations into their own class. Implemented observer pattern.
Observer pattern appears to have caused issues with updating Text objects. Made annotations look nicer. Kinda. #refactor
This commit is contained in:
@@ -1,9 +1,17 @@
|
||||
package seng302.models;
|
||||
|
||||
import javafx.beans.InvalidationListener;
|
||||
import javafx.beans.property.*;
|
||||
import javafx.beans.value.ChangeListener;
|
||||
import javafx.beans.value.ObservableValue;
|
||||
import javafx.fxml.Initializable;
|
||||
import javafx.scene.paint.Color;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Year;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Yacht class for the racing boat.
|
||||
@@ -14,7 +22,237 @@ import java.text.SimpleDateFormat;
|
||||
public class Yacht {
|
||||
// Used in boat group
|
||||
private Color colour;
|
||||
private double velocity;
|
||||
|
||||
private DoubleProperty velocityProperty = new DoubleProperty() {
|
||||
|
||||
private ObservableValue<? extends Number> boundValue;
|
||||
private List<ChangeListener> changeListeners = new ArrayList<>();
|
||||
private List<InvalidationListener> invalidationListeners = new ArrayList<>();
|
||||
private double velocity;
|
||||
|
||||
@Override
|
||||
public void bind(ObservableValue<? extends Number> observable) {
|
||||
boundValue = observable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unbind() {
|
||||
boundValue = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBound() {
|
||||
if (boundValue == null) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getBean() {
|
||||
return Yacht.this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "velocity property of " + boatName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double get() {
|
||||
return velocity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addListener(ChangeListener<? super Number> listener) {
|
||||
changeListeners.add(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeListener(ChangeListener<? super Number> listener) {
|
||||
changeListeners.remove(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addListener(InvalidationListener listener) {
|
||||
invalidationListeners.add(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeListener(InvalidationListener listener) {
|
||||
invalidationListeners.remove(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set (double newVelocity) {
|
||||
double oldVelocity = velocity;
|
||||
velocity = newVelocity;
|
||||
if (newVelocity >= 0)
|
||||
for (ChangeListener cl : changeListeners) {
|
||||
cl.changed(this, oldVelocity, newVelocity);
|
||||
}
|
||||
else
|
||||
for (InvalidationListener il : invalidationListeners) {
|
||||
il.invalidated(this);
|
||||
}
|
||||
if (isBound())
|
||||
boundValue.notify();
|
||||
}
|
||||
};
|
||||
private LongProperty timeAtNextProperty = new LongProperty() {
|
||||
|
||||
private ObservableValue<? extends Number> boundValue;
|
||||
private List<ChangeListener> changeListeners = new ArrayList<>();
|
||||
private List<InvalidationListener> invalidationListeners = new ArrayList<>();
|
||||
private long estimate;
|
||||
|
||||
@Override
|
||||
public void bind(ObservableValue<? extends Number> observable) {
|
||||
boundValue = observable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unbind() {
|
||||
boundValue = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBound() {
|
||||
if (boundValue == null) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getBean() {
|
||||
return Yacht.this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "estimated time to next mark property of " + boatName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long get() {
|
||||
return estimate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addListener(ChangeListener<? super Number> listener) {
|
||||
changeListeners.add(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeListener(ChangeListener<? super Number> listener) {
|
||||
changeListeners.remove(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addListener(InvalidationListener listener) {
|
||||
invalidationListeners.add(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeListener(InvalidationListener listener) {
|
||||
invalidationListeners.remove(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set (long newEstimate) {
|
||||
long oldEstimate = estimate;
|
||||
estimate = newEstimate;
|
||||
if (newEstimate >= 0)
|
||||
for (ChangeListener cl : changeListeners) {
|
||||
cl.changed(this, oldEstimate, newEstimate);
|
||||
}
|
||||
else
|
||||
for (InvalidationListener il : invalidationListeners) {
|
||||
il.invalidated(this);
|
||||
}
|
||||
if (isBound())
|
||||
boundValue.notify();
|
||||
}
|
||||
};
|
||||
private LongProperty markRoundingTimeProperty = new LongProperty() {
|
||||
private ObservableValue<? extends Number> boundValue;
|
||||
private List<ChangeListener> changeListeners = new ArrayList<>();
|
||||
private List<InvalidationListener> invalidationListeners = new ArrayList<>();
|
||||
private long roundingTime;
|
||||
|
||||
@Override
|
||||
public void bind(ObservableValue<? extends Number> observable) {
|
||||
boundValue = observable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unbind() {
|
||||
boundValue = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBound() {
|
||||
if (boundValue == null) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getBean() {
|
||||
return Yacht.this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "time from last mark property of " + boatName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long get() {
|
||||
return roundingTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addListener(ChangeListener<? super Number> listener) {
|
||||
changeListeners.add(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeListener(ChangeListener<? super Number> listener) {
|
||||
changeListeners.remove(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addListener(InvalidationListener listener) {
|
||||
invalidationListeners.add(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeListener(InvalidationListener listener) {
|
||||
invalidationListeners.remove(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set (long newTime) {
|
||||
long oldTime = newTime;
|
||||
roundingTime = newTime;
|
||||
if (newTime >= 0)
|
||||
for (ChangeListener cl : changeListeners) {
|
||||
cl.changed(this, oldTime, newTime);
|
||||
}
|
||||
else
|
||||
for (InvalidationListener il : invalidationListeners) {
|
||||
il.invalidated(this);
|
||||
}
|
||||
if (isBound())
|
||||
boundValue.notify();
|
||||
}
|
||||
};
|
||||
|
||||
private String boatType;
|
||||
private Integer sourceID;
|
||||
@@ -27,11 +265,8 @@ public class Yacht {
|
||||
private Integer legNumber;
|
||||
private Integer penaltiesAwarded;
|
||||
private Integer penaltiesServed;
|
||||
private Long estimateTimeAtNextMark;
|
||||
private Long estimateTimeAtFinish;
|
||||
private String position;
|
||||
// Mark rounding
|
||||
private Long markRoundingTime;
|
||||
|
||||
/**
|
||||
* Used in EventTest and RaceTest.
|
||||
@@ -51,7 +286,7 @@ public class Yacht {
|
||||
*/
|
||||
public Yacht(String boatName, double boatVelocity, String shortName, int id) {
|
||||
this.boatName = boatName;
|
||||
this.velocity = boatVelocity;
|
||||
this.velocityProperty.set(boatVelocity);
|
||||
this.shortName = shortName;
|
||||
this.sourceID = id;
|
||||
}
|
||||
@@ -116,14 +351,8 @@ public class Yacht {
|
||||
this.penaltiesServed = penaltiesServed;
|
||||
}
|
||||
|
||||
public Long getEstimateTimeAtNextMark() {
|
||||
// DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
|
||||
// return format.format(estimateTimeAtNextMark);
|
||||
return estimateTimeAtNextMark;
|
||||
}
|
||||
|
||||
public void setEstimateTimeAtNextMark(Long estimateTimeAtNextMark) {
|
||||
this.estimateTimeAtNextMark = estimateTimeAtNextMark;
|
||||
timeAtNextProperty.set(estimateTimeAtNextMark);
|
||||
}
|
||||
|
||||
public String getEstimateTimeAtFinish() {
|
||||
@@ -151,24 +380,29 @@ public class Yacht {
|
||||
this.colour = colour;
|
||||
}
|
||||
|
||||
public Double getVelocity() {
|
||||
return velocity;
|
||||
}
|
||||
|
||||
public void setVelocity(double velocity) {
|
||||
this.velocity = velocity;
|
||||
velocityProperty.set(velocity);
|
||||
}
|
||||
|
||||
public Long getMarkRoundingTime() {
|
||||
return markRoundingTime;
|
||||
}
|
||||
|
||||
public void setMarkRoundingTime(Long markRoundingTime) {
|
||||
this.markRoundingTime = markRoundingTime;
|
||||
markRoundingTimeProperty.set(markRoundingTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return boatName;
|
||||
}
|
||||
|
||||
public ReadOnlyDoubleProperty getReadOnlyVelocityProperty () {
|
||||
return velocityProperty;
|
||||
}
|
||||
|
||||
public ReadOnlyLongProperty getReadOnlyNextMarkProperty() {
|
||||
return timeAtNextProperty;
|
||||
}
|
||||
|
||||
public ReadOnlyLongProperty getReadOnlyMarkRoundingProperty() {
|
||||
return markRoundingTimeProperty;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user