Add button to suspend events

This commit is contained in:
0xb00bface 2021-12-11 17:07:50 +01:00
parent 92fa017b6b
commit bf9e23bdfa
4 changed files with 46 additions and 37 deletions

View File

@ -1,6 +1,7 @@
package ctbrec.ui.settings;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
@ -8,7 +9,9 @@ import java.time.format.FormatStyle;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javafx.scene.control.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -42,15 +45,6 @@ import javafx.geometry.Orientation;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.Separator;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
@ -64,20 +58,21 @@ public class ActionSettingsPanel extends GridPane {
private static final Logger LOG = LoggerFactory.getLogger(ActionSettingsPanel.class);
private ListView<EventHandlerConfiguration> actionTable;
private TextField name = new TextField();
private ComboBox<Event.Type> event = new ComboBox<>();
private ComboBox<Model.State> modelState = new ComboBox<>();
private ComboBox<Recording.State> recordingState = new ComboBox<>();
private final TextField name = new TextField();
private final ComboBox<Event.Type> event = new ComboBox<>();
private final ComboBox<Model.State> modelState = new ComboBox<>();
private final ComboBox<Recording.State> recordingState = new ComboBox<>();
private CheckBox playSound = new CheckBox("Play sound");
private FileSelectionBox sound = new FileSelectionBox();
private CheckBox showNotification = new CheckBox("Notify me");
private Button testNotification = new Button("Test");
private CheckBox executeProgram = new CheckBox("Execute program");
private ProgramSelectionBox program = new ProgramSelectionBox();
private final CheckBox playSound = new CheckBox("Play sound");
private final FileSelectionBox sound = new FileSelectionBox();
private final CheckBox showNotification = new CheckBox("Notify me");
private final Button testNotification = new Button("Test");
private final ToggleButton toggleEvents = new ToggleButton();
private final CheckBox executeProgram = new CheckBox("Execute program");
private final ProgramSelectionBox program = new ProgramSelectionBox();
private ListSelectionPane<Model> modelSelectionPane;
private Recorder recorder;
private final Recorder recorder;
public ActionSettingsPanel(Recorder recorder) {
this.recorder = recorder;
@ -111,20 +106,35 @@ public class ActionSettingsPanel extends GridPane {
var delete = new Button("Delete");
delete.setOnAction(this::delete);
delete.setDisable(true);
var buttons = new HBox(5, add, delete);
toggleEvents.setOnAction(this::toggleEvents);
toggleEvents.setSelected(Config.getInstance().getSettings().eventsSuspended);
toggleEvents.setText(toggleEvents.isSelected() ? "Resume Events" : "Suspend Events");
toggleEvents.setTooltip(new Tooltip(toggleEvents.isSelected() ? "Events are currently suspended" : "Events are currently active"));
var buttons = new HBox(5, add, delete, toggleEvents);
buttons.setStyle("-fx-background-color: -fx-background"); // workaround so that the buttons don't shrink
add(buttons, 0, 2);
actionTable.getSelectionModel().getSelectedItems().addListener((ListChangeListener<EventHandlerConfiguration>) change -> delete.setDisable(change.getList().isEmpty()));
}
private void toggleEvents(ActionEvent actionEvent) {
Config.getInstance().getSettings().eventsSuspended = toggleEvents.isSelected();
toggleEvents.setText(toggleEvents.isSelected() ? "Resume Events" : "Suspend Events");
toggleEvents.setTooltip(new Tooltip(toggleEvents.isSelected() ? "Events are currently suspended" : "Events are currently active"));
try {
Config.getInstance().save();
} catch (IOException e) {
LOG.error("Couldn't save config", e);
}
}
private void add(ActionEvent evt) {
var actionPane = createActionPane();
var dialog = new Stage();
dialog.initModality(Modality.APPLICATION_MODAL);
dialog.initOwner(getScene().getWindow());
dialog.setTitle("New Action");
InputStream icon = getClass().getResourceAsStream("/icon.png");
InputStream icon = Objects.requireNonNull(getClass().getResourceAsStream("/icon.png"), "/icon.png not found in classpath");
dialog.getIcons().add(new Image(icon));
var root = new Wizard(dialog, this::validateSettings, actionPane);
var scene = new Scene(root, 800, 540);

View File

@ -71,6 +71,7 @@ public class Settings {
public List<String> disabledSites = new ArrayList<>();
public String downloadFilename = "${modelSanitizedName}-${localDateTime}";
public List<EventHandlerConfiguration> eventHandlers = new ArrayList<>();
public boolean eventsSuspended = false;
public boolean fastScrollSpeed = true;
public String fc2livePassword = "";
public String fc2liveUsername = "";

View File

@ -7,6 +7,7 @@ import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
import ctbrec.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -17,16 +18,15 @@ import ctbrec.event.EventHandlerConfiguration.ActionConfiguration;
import ctbrec.event.EventHandlerConfiguration.PredicateConfiguration;
public class EventHandler {
private static final transient Logger LOG = LoggerFactory.getLogger(EventHandler.class);
private static final Logger LOG = LoggerFactory.getLogger(EventHandler.class);
private List<EventPredicate> predicates = new ArrayList<>();
private List<Action> actions;
private Type event;
private final List<Action> actions;
private String id;
public EventHandler(EventHandlerConfiguration config) {
id = config.getId();
event = config.getEvent();
Type event = config.getEvent();
actions = createActions(config);
predicates = createPredicates(config);
predicates.add(new EventTypePredicate(event));
@ -51,6 +51,11 @@ public class EventHandler {
@Subscribe
public void reactToEvent(Event evt) {
if (Config.getInstance().getSettings().eventsSuspended) {
LOG.debug("Events are suspended. Ignoring {}", evt);
return;
}
try {
boolean matches = true;
for (Predicate<Event> predicate : predicates) {

View File

@ -1,13 +1,9 @@
package ctbrec.event;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import ctbrec.Model;
import java.util.*;
public class EventHandlerConfiguration {
private String id;
@ -154,10 +150,7 @@ public class EventHandlerConfiguration {
return false;
EventHandlerConfiguration other = (EventHandlerConfiguration) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
return other.id == null;
} else return id.equals(other.id);
}
}