Improved SystemCommand mechanism, added Alert- and ShutdownHelper, and

... added askForConfirmation option
This commit is contained in:
2020-09-23 17:03:32 +02:00
parent 758e52e030
commit 2d9283551a
13 changed files with 260 additions and 49 deletions

View File

@ -212,6 +212,13 @@ public final class LocalDB implements EventListener {
@Event
private void onNewAuthToken(NewAuthToken evt) { authToken = evt.get(); }
/**
* Deletes the file that stores the "remember me" feature.
*
* @since Envoy Client v0.2-beta
*/
public void deleteLoginFile() { lastLoginFile.delete(); }
/**
* @return a {@code Map<String, User>} of all users stored locally with their
* user names as keys

View File

@ -92,6 +92,8 @@ public final class Settings implements EventListener {
new SettingsItem<>(new File(System.getProperty("user.home") + "/Downloads/"), "Download location",
"The location where files will be saved to"));
items.putIfAbsent("autoSaveDownloads", new SettingsItem<>(false, "Save without asking?", "Should downloads be saved without asking?"));
items.putIfAbsent("askForConfirmation",
new SettingsItem<>(true, "Ask for confirmation", "Will ask for confirmation before doing certain things"));
}
/**
@ -182,6 +184,25 @@ public final class Settings implements EventListener {
*/
public void setHideOnClose(boolean hideOnClose) { ((SettingsItem<Boolean>) items.get("hideOnClose")).set(hideOnClose); }
/**
* @return whether a confirmation dialog should be displayed before certain
* actions
* @since Envoy Client v0.2-alpha
*/
public Boolean isAskForConfirmation() { return (Boolean) items.get("askForConfirmation").get(); }
/**
* Changes the behavior of calling certain functionality by displaying a
* confirmation dialog before executing it.
*
* @param askForConfirmation whether confirmation dialogs should be displayed
* before certain actions
* @since Envoy Client v0.2-alpha
*/
public void setAskForConfirmation(boolean askForConfirmation) {
((SettingsItem<Boolean>) items.get("askForConfirmation")).set(askForConfirmation);
}
/**
* @return the items
*/

View File

@ -1,7 +1,6 @@
package envoy.client.data.commands;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
import java.util.function.Consumer;
/**
@ -22,6 +21,22 @@ public final class SystemCommandBuilder {
private String description;
private int relevance;
private final SystemCommandMap commandsMap;
/**
* Creates a new {@code SystemCommandsBuilder} without underlying
* {@link SystemCommandMap}.
*
* @since Envoy Client v0.2-beta
*/
public SystemCommandBuilder() { this(null); }
/**
* @param commandsMap the map to use when calling build (optional)
* @since Envoy Client v0.2-beta
*/
public SystemCommandBuilder(SystemCommandMap commandsMap) { this.commandsMap = commandsMap; }
/**
* @param numberOfArguments the numberOfArguments to set
* @return this {@code SystemCommandBuilder}
@ -125,6 +140,7 @@ public final class SystemCommandBuilder {
/**
* Builds a {@code SystemCommand} based upon the previously entered data.<br>
* Automatically adds the built object to the given map.
* At the end, this {@code SystemCommandBuilder} <b>can</b> be reset but must
* not be.
*
@ -141,4 +157,78 @@ public final class SystemCommandBuilder {
if (reset) reset();
return sc;
}
/**
* Builds a {@code SystemCommand} based upon the previously entered data.
* Automatically adds the built object to the given map.
*
* @param command the command under which to store the SystemCommand in the
* {@link SystemCommandMap}
* @return the built {@code SystemCommand}
* @throws NullPointerException if no map has been assigned to this builder
* @since Envoy Client v0.2-beta
*/
public SystemCommand build(String command) { return build(command, true); }
/**
* Builds a {@code SystemCommand} based upon the previously entered data.<br>
* Automatically adds the built object to the given map.
* {@code SystemCommand#numberOfArguments} will be set to 0, regardless of the
* previous value.<br>
* At the end, this {@code SystemCommandBuilder} will be reset.
*
* @param command the command under which to store the SystemCommand in the
* {@link SystemCommandMap}
* @return the built {@code SystemCommand}
* @throws NullPointerException if no map has been assigned to this builder
* @since Envoy Client v0.2-beta
*/
public SystemCommand buildNoArg(String command) {
numberOfArguments = 0;
return build(command, true);
}
/**
* Builds a {@code SystemCommand} based upon the previously entered data.<br>
* Automatically adds the built object to the given map.
* {@code SystemCommand#numberOfArguments} will be set to use the rest of the
* string as argument, regardless of the previous value.<br>
* At the end, this {@code SystemCommandBuilder} will be reset.
*
* @param command the command under which to store the SystemCommand in the
* {@link SystemCommandMap}
* @return the built {@code SystemCommand}
* @throws NullPointerException if no map has been assigned to this builder
* @since Envoy Client v0.2-beta
*/
public SystemCommand buildRemainingArg(String command) {
numberOfArguments = -1;
return build(command, true);
}
/**
* Builds a {@code SystemCommand} based upon the previously entered data.<br>
* Automatically adds the built object to the given map.
* At the end, this {@code SystemCommandBuilder} <b>can</b> be reset but must
* not be.
*
* @param command the command under which to store the SystemCommand in the
* {@link SystemCommandMap}
* @param reset whether this {@code SystemCommandBuilder} should be reset
* afterwards.<br>
* This can be useful if another command wants to execute
* something
* similar
* @return the built {@code SystemCommand}
* @throws NullPointerException if no map has been assigned to this builder
* @since Envoy Client v0.2-beta
*/
public SystemCommand build(String command, boolean reset) {
final var sc = new SystemCommand(action, numberOfArguments, defaults, description);
sc.setRelevance(relevance);
if (commandsMap != null) commandsMap.add(command, sc);
else throw new NullPointerException("No map in SystemCommandsBuilder present");
if (reset) reset();
return sc;
}
}

View File

@ -12,19 +12,19 @@ import envoy.util.EnvoyLog;
* This class stores all {@link SystemCommand}s used.
* <p>
* Project: <strong>envoy-client</strong><br>
* File: <strong>SystemCommandsMap.java</strong><br>
* File: <strong>SystemCommandMap.java</strong><br>
* Created: <strong>17.07.2020</strong><br>
*
* @author Leon Hofmeister
* @since Envoy Client v0.2-beta
*/
public final class SystemCommandsMap {
public final class SystemCommandMap {
private final Map<String, SystemCommand> systemCommands = new HashMap<>();
private final Pattern commandPattern = Pattern.compile("^[a-zA-Z0-9_:!\\(\\)\\?\\.\\,\\;\\-]+$");
private static final Logger logger = EnvoyLog.getLogger(SystemCommandsMap.class);
private static final Logger logger = EnvoyLog.getLogger(SystemCommandMap.class);
/**
* Adds a new command to the map if the command name is valid.
@ -33,7 +33,7 @@ public final class SystemCommandsMap {
* given action
* @param systemCommand the command to add - can be built using
* {@link SystemCommandBuilder}
* @see SystemCommandsMap#isValidKey(String)
* @see SystemCommandMap#isValidKey(String)
* @since Envoy Client v0.2-beta
*/
public void add(String command, SystemCommand systemCommand) {
@ -48,7 +48,7 @@ public final class SystemCommandsMap {
* map).
* <p>
* Usage example:<br>
* {@code SystemCommandsMap systemCommands = new SystemCommandsMap();}<br>
* {@code SystemCommandMap systemCommands = new SystemCommandMap();}<br>
* {@code Button button = new Button();}
* {@code systemCommands.add("example", text -> button.setText(text.get(0), 1);}<br>
* {@code ....}<br>
@ -132,7 +132,7 @@ public final class SystemCommandsMap {
* map).
* <p>
* Usage example:<br>
* {@code SystemCommandsMap systemCommands = new SystemCommandsMap();}<br>
* {@code SystemCommandMap systemCommands = new SystemCommandMap();}<br>
* {@code Button button = new Button();}<br>
* {@code systemCommands.add("example", (words)-> button.setText(words.get(0), 1);}<br>
* {@code ....}<br>