-
Notifications
You must be signed in to change notification settings - Fork 109
Adds save dirty checking event #647
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package edu.wpi.grip.core.events; | ||
|
|
||
| /** | ||
| * An event that can potentially dirty the save file. | ||
| * | ||
| * <p>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 |
|---|---|---|
|
|
@@ -2,8 +2,10 @@ | |
|
|
||
| import edu.wpi.grip.core.Pipeline; | ||
| import edu.wpi.grip.core.PipelineRunner; | ||
| import edu.wpi.grip.core.events.DirtiesSaveEvent; | ||
|
|
||
| import com.google.common.annotations.VisibleForTesting; | ||
| import com.google.common.eventbus.Subscribe; | ||
| import com.google.common.reflect.ClassPath; | ||
| import com.thoughtworks.xstream.XStream; | ||
| import com.thoughtworks.xstream.annotations.XStreamAlias; | ||
|
|
@@ -20,7 +22,8 @@ | |
| import java.io.Writer; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Optional; | ||
|
|
||
| import javafx.beans.property.SimpleBooleanProperty; | ||
| import javafx.beans.value.ChangeListener; | ||
|
||
| import javax.inject.Inject; | ||
| import javax.inject.Singleton; | ||
|
|
||
|
|
@@ -36,6 +39,7 @@ public class Project { | |
| @Inject | ||
| private PipelineRunner pipelineRunner; | ||
| private Optional<File> file = Optional.empty(); | ||
| private final SimpleBooleanProperty saveIsDirty = new SimpleBooleanProperty(false); | ||
|
|
||
| @Inject | ||
| public void initialize(StepConverter stepConverter, | ||
|
|
@@ -109,6 +113,7 @@ void open(Reader reader) { | |
| this.pipeline.clear(); | ||
| this.xstream.fromXML(reader); | ||
| pipelineRunner.startAsync(); | ||
| saveIsDirty.set(false); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -124,5 +129,27 @@ public void save(File file) throws IOException { | |
|
|
||
| public void save(Writer writer) { | ||
| this.xstream.toXML(this.pipeline, writer); | ||
| saveIsDirty.set(false); | ||
| } | ||
|
|
||
| public boolean isSaveDirty() { | ||
| return saveIsDirty.get(); | ||
| } | ||
|
|
||
| public void addDirtyListener(ChangeListener<Boolean> changeListener) { | ||
| saveIsDirty.addListener(changeListener); | ||
| } | ||
|
|
||
| public void removeDirtyListener(ChangeListener<Boolean> changeListener) { | ||
| saveIsDirty.removeListener(changeListener); | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this pattern but we can't do this using |
||
|
|
||
| @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.get() && dirtySaveEvent.doesDirtySave()) { | ||
| saveIsDirty.set(true); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the same code as the method below it.
Can you refactor it into one method that is called by both methods?
Or just have one of these methods call the other one.