Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* An event that occurs when a new connection is added to the pipeline. This is triggered by the user adding a
* connection with the GUI.
*/
public class ConnectionAddedEvent implements RunPipelineEvent {
public class ConnectionAddedEvent implements RunPipelineEvent, DirtiesSaveEvent {
private final Connection connection;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* An event that occurs when a connection is removed from the pipeline. This is triggered by the user deleting a
* connection with the GUI.
*/
public class ConnectionRemovedEvent {
public class ConnectionRemovedEvent implements DirtiesSaveEvent {
private final Connection connection;

/**
Expand Down
18 changes: 18 additions & 0 deletions core/src/main/java/edu/wpi/grip/core/events/DirtiesSaveEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package edu.wpi.grip.core.events;

/**
* An event that can potentially dirty the save file.
* These events ensure that anything that changes causes the save file to be flagged as dirty and in need of being
* saved for the project to be deemed "clean" again.
*/
public interface DirtiesSaveEvent {

/**
* Some events may have more logic regarding whether they make the save dirty or not.
*
* @return True if this event should dirty the project save
*/
default boolean doesDirtySave() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* An event that occurs when the value stored in a socket changes. This can happen, for example, as the result of an
* operation completing, or as a response to user input.
*/
public class SocketChangedEvent implements RunPipelineEvent {
public class SocketChangedEvent implements RunPipelineEvent, DirtiesSaveEvent {
private final Socket socket;

/**
Expand All @@ -27,6 +27,18 @@ public Socket getSocket() {
return this.socket;
}

/**
* This event will only dirty the save if the InputSocket does not have connections.
* Thus the value can only have been changed by a UI component.
* If the socket has connections then the value change is triggered by another socket's change.
*
* @return True if this should dirty the save.
*/
@Override
public boolean doesDirtySave() {
return socket.getDirection() == Socket.Direction.INPUT && socket.getConnections().isEmpty();
}

@Override
public boolean pipelineShouldRun() {
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* An event that occurs when a {@link OutputSocket} is set to be either previewed or not previewed. The GUI listens for these events
* so it knows which sockets to show previews for.
*/
public class SocketPreviewChangedEvent {
public class SocketPreviewChangedEvent implements DirtiesSaveEvent {
private OutputSocket socket;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @see Source
*/
public class SourceAddedEvent {
public class SourceAddedEvent implements DirtiesSaveEvent {
private final Source source;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* @see Source
*/
public class SourceRemovedEvent {
public class SourceRemovedEvent implements DirtiesSaveEvent {
private final Source source;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* An event that occurs when a new step is added to the pipeline. This is triggered by the user adding a step with the
* GUI.
*/
public class StepAddedEvent {
public class StepAddedEvent implements DirtiesSaveEvent {
private final Step step;
private final OptionalInt index;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* An event that occurs when a new step is moved from one position to another in the pipeline
*/
public class StepMovedEvent {
public class StepMovedEvent implements DirtiesSaveEvent {
private final Step step;
private final int distance;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* An event that occurs when a new step is removed from the pipeline. This is triggered by the user deleting a step
* from the GUI.
*/
public class StepRemovedEvent {
public class StepRemovedEvent implements DirtiesSaveEvent {
private final Step step;

/**
Expand Down
23 changes: 22 additions & 1 deletion core/src/main/java/edu/wpi/grip/core/serialization/Project.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package edu.wpi.grip.core.serialization;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.eventbus.Subscribe;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider;
import edu.wpi.grip.core.*;
import edu.wpi.grip.core.Connection;
import edu.wpi.grip.core.Pipeline;
import edu.wpi.grip.core.Step;
import edu.wpi.grip.core.events.DirtiesSaveEvent;
import edu.wpi.grip.core.sockets.InputSocket;
import edu.wpi.grip.core.sockets.OutputSocket;
import edu.wpi.grip.core.sources.CameraSource;
Expand All @@ -27,6 +31,7 @@ public class Project {

protected final XStream xstream = new XStream(new PureJavaReflectionProvider());
private Optional<File> file = Optional.empty();
private boolean saveIsDirty = false;

@Inject
public void initialize(StepConverter stepConverter,
Expand Down Expand Up @@ -69,6 +74,7 @@ public void open(File file) throws IOException {
void open(Reader reader) {
this.pipeline.clear();
this.xstream.fromXML(reader);
saveIsDirty = false;
}

/**
Expand All @@ -81,7 +87,22 @@ public void save(File file) throws IOException {
this.file = Optional.of(file);
}


public void save(Writer writer) {
this.xstream.toXML(this.pipeline, writer);
saveIsDirty = false;
}

public boolean isSaveDirty() {
return saveIsDirty;
}

@Subscribe
public void onDirtiesSaveEvent(DirtiesSaveEvent dirtySaveEvent) {
// Only update the flag the save isn't already dirty
// We don't need to be redundantly checking if the event dirties the save.
if (!saveIsDirty && dirtySaveEvent.doesDirtySave()) {
saveIsDirty = true;
}
}
}
2 changes: 1 addition & 1 deletion ui/src/main/java/edu/wpi/grip/ui/MainWindowController.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void initialize() {
* @return true If the user has not chosen to
*/
private boolean showConfirmationDialogAndWait() {
if (!pipeline.getSteps().isEmpty()) {
if (!pipeline.getSteps().isEmpty() && project.isSaveDirty()) {
final ButtonType save = new ButtonType("Save");
final ButtonType dontSave = ButtonType.NO;
final ButtonType cancel = ButtonType.CANCEL;
Expand Down