More rework

This commit is contained in:
crschnick
2024-10-24 13:46:34 +00:00
parent 1c80db936c
commit 263eb811f4
11 changed files with 34 additions and 97 deletions
@@ -17,7 +17,7 @@ public class ConnectionRefreshExchangeImpl extends ConnectionRefreshExchange {
if (e.getStore() instanceof FixedHierarchyStore) {
DataStorage.get().refreshChildren(e, true);
} else {
e.validateOrThrowAndClose(null);
e.validateOrThrow();
}
return Response.builder().build();
}
@@ -174,11 +174,8 @@ public class StoreCreationComp extends DialogComp {
e.getProvider(),
e.getStore(),
v -> true,
(newE, context, validated) -> {
(newE, validated) -> {
ThreadHelper.runAsync(() -> {
if (context != null) {
context.close();
}
if (!DataStorage.get().getStoreEntries().contains(e)) {
DataStorage.get().addStoreEntryIfNotPresent(newE);
} else {
@@ -205,16 +202,15 @@ public class StoreCreationComp extends DialogComp {
base != null ? DataStoreProviders.byStore(base) : null,
base,
dataStoreProvider -> category.equals(dataStoreProvider.getCreationCategory()),
(e, context, validated) -> {
(e, validated) -> {
try {
DataStorage.get().addStoreEntryIfNotPresent(e);
if (context != null
&& validated
if (validated
&& e.getProvider().shouldShowScan()
&& AppPrefs.get()
.openConnectionSearchWindowOnConnectionCreation()
.get()) {
ScanAlert.showAsync(e, context);
ScanAlert.showAsync(e);
}
} catch (Exception ex) {
ErrorEvent.fromThrowable(ex).handle();
@@ -226,7 +222,7 @@ public class StoreCreationComp extends DialogComp {
public interface CreationConsumer {
void consume(DataStoreEntry entry, ValidationContext<?> validationContext, boolean validated);
void consume(DataStoreEntry entry, boolean validated);
}
private static void show(
@@ -265,7 +261,7 @@ public class StoreCreationComp extends DialogComp {
return List.of(
new ButtonComp(AppI18n.observable("skipValidation"), null, () -> {
if (showInvalidConfirmAlert()) {
commit(null, false);
commit(false);
} else {
finish();
}
@@ -308,7 +304,7 @@ public class StoreCreationComp extends DialogComp {
// We didn't change anything
if (existingEntry != null && existingEntry.getStore().equals(store.getValue())) {
commit(null, false);
commit(false);
return;
}
@@ -338,8 +334,8 @@ public class StoreCreationComp extends DialogComp {
try (var ignored = new BooleanScope(busy).start()) {
DataStorage.get().addStoreEntryInProgress(entry.getValue());
var context = entry.getValue().validateAndKeepOpenOrThrowAndClose(null);
commit(context, true);
entry.getValue().validateOrThrow();
commit(true);
} catch (Throwable ex) {
if (ex instanceof ValidationException) {
ErrorEvent.expected(ex);
@@ -420,14 +416,14 @@ public class StoreCreationComp extends DialogComp {
.createRegion();
}
private void commit(ValidationContext<?> validationContext, boolean validated) {
private void commit(boolean validated) {
if (finished.get()) {
return;
}
finished.setValue(true);
if (entry.getValue() != null) {
consumer.consume(entry.getValue(), validationContext, validated);
consumer.consume(entry.getValue(), validated);
}
PlatformThread.runLaterIfNeeded(() -> {
@@ -22,7 +22,7 @@ public class StoreCreationMenu {
automatically.setGraphic(new FontIcon("mdi2e-eye-plus-outline"));
automatically.textProperty().bind(AppI18n.observable("addAutomatically"));
automatically.setOnAction(event -> {
ScanAlert.showAsync(null, null);
ScanAlert.showAsync(null);
event.consume();
});
menu.getItems().add(automatically);
@@ -39,7 +39,7 @@ public class StoreIntroComp extends SimpleComp {
var scanButton = new Button(null, new FontIcon("mdi2m-magnify"));
scanButton.textProperty().bind(AppI18n.observable("detectConnections"));
scanButton.setOnAction(event -> ScanAlert.showAsync(DataStorage.get().local(), null));
scanButton.setOnAction(event -> ScanAlert.showAsync(DataStorage.get().local()));
scanButton.setDefaultButton(true);
var scanPane = new StackPane(scanButton);
scanPane.setAlignment(Pos.CENTER);
@@ -46,7 +46,7 @@ public interface ShellStore extends DataStore, FileSystemStore, ValidatableStore
ShellControlFunction shellFunction();
@Override
default ShellValidationContext validate(ShellValidationContext context) throws Exception {
default ShellValidationContext validate() throws Exception {
var func = shellFunction();
var c = func instanceof ShellControlParentStoreFunction s ? s.control(s.getParentStore().getOrStartSession()) :
func instanceof ShellControlParentFunction p ? p.control(p.parentControl()) : func.control();
@@ -138,6 +138,9 @@ public class ErrorHandlerComp extends SimpleComp {
} catch (Throwable t) {
t.printStackTrace();
}
} else {
showLatch.countDown();
finishLatch.countDown();
}
});
@@ -516,47 +516,18 @@ public class DataStoreEntry extends StorageElement {
}
public void validateOrThrow() throws Throwable {
validateOrThrowAndClose(null);
}
public boolean validateOrThrowAndClose(ValidationContext<?> existingContext) throws Throwable {
var subContext = validateAndKeepOpenOrThrowAndClose(existingContext);
if (subContext != null) {
subContext.close();
return true;
} else {
return false;
}
}
@SuppressWarnings("unchecked")
public <T> ValidationContext<?> validateAndKeepOpenOrThrowAndClose(ValidationContext<?> existingContext)
throws Throwable {
if (store == null) {
return null;
return;
}
if (!(store instanceof ValidatableStore<?> l)) {
return null;
return;
}
try {
store.checkComplete();
incrementBusyCounter();
ValidationContext<T> context = existingContext != null
? (ValidationContext<T>) existingContext
: (ValidationContext<T>) l.createContext();
if (context == null) {
return null;
}
try {
var r = ((ValidatableStore<ValidationContext<T>>) l).validate(context);
return r;
} catch (Throwable t) {
context.close();
throw t;
}
l.validate();
} finally {
decrementBusyCounter();
}
@@ -17,7 +17,7 @@ import java.util.function.BiFunction;
public class ScanAlert {
public static void showAsync(DataStoreEntry entry, ValidationContext<?> context) {
public static void showAsync(DataStoreEntry entry) {
ThreadHelper.runAsync(() -> {
var showForCon = entry == null
|| (entry.getStore() instanceof ShellStore
@@ -25,12 +25,12 @@ public class ScanAlert {
|| shellStoreState.getTtyState() == null
|| shellStoreState.getTtyState() == ShellTtyState.NONE));
if (showForCon) {
showForShellStore(entry, (ShellValidationContext) context);
showForShellStore(entry);
}
});
}
public static void showForShellStore(DataStoreEntry initial, ShellValidationContext context) {
public static void showForShellStore(DataStoreEntry initial) {
show(
initial,
(DataStoreEntry entry, ShellControl sc) -> {
@@ -61,17 +61,15 @@ public class ScanAlert {
}
}
return applicable;
},
context);
});
}
private static void show(
DataStoreEntry initialStore,
BiFunction<DataStoreEntry, ShellControl, List<ScanProvider.ScanOperation>> applicable,
ShellValidationContext shellValidationContext) {
BiFunction<DataStoreEntry, ShellControl, List<ScanProvider.ScanOperation>> applicable) {
DialogComp.showWindow(
"scanAlertTitle",
stage -> new ScanDialog(
stage, initialStore != null ? initialStore.ref() : null, applicable, shellValidationContext));
stage, initialStore != null ? initialStore.ref() : null, applicable));
}
}
@@ -40,18 +40,15 @@ class ScanDialog extends DialogComp {
private final ListProperty<ScanProvider.ScanOperation> selected =
new SimpleListProperty<>(FXCollections.observableArrayList());
private final BooleanProperty busy = new SimpleBooleanProperty();
private ShellValidationContext shellValidationContext;
ScanDialog(
Stage window,
DataStoreEntryRef<ShellStore> entry,
BiFunction<DataStoreEntry, ShellControl, List<ScanProvider.ScanOperation>> applicable,
ShellValidationContext shellValidationContext) {
BiFunction<DataStoreEntry, ShellControl, List<ScanProvider.ScanOperation>> applicable) {
this.window = window;
this.initialStore = entry;
this.entry = new SimpleObjectProperty<>(entry);
this.applicable = applicable;
this.shellValidationContext = shellValidationContext;
}
@Override
@@ -62,7 +59,6 @@ class ScanDialog extends DialogComp {
@Override
protected void finish() {
ThreadHelper.runFailableAsync(() -> {
try {
if (entry.get() == null) {
return;
}
@@ -84,7 +80,7 @@ class ScanDialog extends DialogComp {
}
// Previous scan operation could have exited the shell
shellValidationContext.get().start();
initialStore.getStore().getOrStartSession();
try {
a.getScanner().run();
@@ -93,24 +89,11 @@ class ScanDialog extends DialogComp {
}
}
});
} finally {
if (shellValidationContext != null) {
shellValidationContext.close();
shellValidationContext = null;
}
}
});
}
@Override
protected void discard() {
ThreadHelper.runAsync(() -> {
if (shellValidationContext != null) {
shellValidationContext.close();
shellValidationContext = null;
}
});
}
protected void discard() {}
@Override
protected Comp<?> pane(Comp<?> content) {
@@ -161,22 +144,8 @@ class ScanDialog extends DialogComp {
ThreadHelper.runFailableAsync(() -> {
BooleanScope.executeExclusive(busy, () -> {
if (shellValidationContext != null) {
shellValidationContext.close();
shellValidationContext = null;
}
shellValidationContext = new ShellValidationContext(
newValue.getStore().getOrStartSession().withoutLicenseCheck().start());
// Handle window close while connection is established
if (!window.isShowing()) {
discard();
return;
}
var a = applicable.apply(entry.get().get(), shellValidationContext.get());
var sc = initialStore.getStore().getOrStartSession().withoutLicenseCheck();
var a = applicable.apply(entry.get().get(), sc);
Platform.runLater(() -> {
if (a == null) {
window.close();
@@ -18,7 +18,7 @@ public interface ValidatableStore<T extends ValidationContext<?>> extends DataSt
*
* @throws Exception if any part of the validation went wrong
*/
T validate(T context) throws Exception;
T validate() throws Exception;
T createContext() throws Exception;
}
@@ -70,7 +70,7 @@ public class ScanStoreAction implements ActionProvider {
@Override
public void execute() {
if (entry == null || entry.getStore() instanceof ShellStore) {
ScanAlert.showForShellStore(entry, null);
ScanAlert.showForShellStore(entry);
}
}
}