Skip to content

Commit

Permalink
Core Editor: Show notification banner in case of error or outdated file
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadelessFox committed Apr 23, 2024
1 parent 075e4f1 commit ec53e52
Show file tree
Hide file tree
Showing 21 changed files with 360 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ public interface Archive extends Closeable {
@Nullable
ArchiveFile findFile(@NotNull String identifier);

@Nullable
ArchiveFile findFile(long identifier);

@NotNull
default ArchiveFile getFile(@NotNull String identifier) {
final ArchiveFile file = findFile(identifier);
if (file == null) {
throw new IllegalArgumentException("Can't find file '%s' in archive %s".formatted(identifier, getName()));
}
return file;
}

@NotNull
ArchiveFile getFile(@NotNull String identifier);
default ArchiveFile getFile(long identifier) {
final ArchiveFile file = findFile(identifier);
if (file == null) {
throw new IllegalArgumentException("Can't find file '%#018x' in archive %s".formatted(identifier, getName()));
}
return file;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@
import java.io.InputStream;

public interface ArchiveFile {
@NotNull
String getName();

@NotNull
String getPath();
long getIdentifier();

@NotNull
Archive getArchive();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,26 @@ public interface ArchiveManager extends Closeable {
@Nullable
ArchiveFile findFile(@NotNull String identifier);

@Nullable
ArchiveFile findFile(long identifier);

@NotNull
default ArchiveFile getFile(@NotNull String identifier) {
final ArchiveFile file = findFile(identifier);
if (file == null) {
throw new IllegalArgumentException("Can't find file '%s'".formatted(identifier));
}
return file;
}

@NotNull
ArchiveFile getFile(@NotNull String identifier);
default ArchiveFile getFile(long identifier) {
final ArchiveFile file = findFile(identifier);
if (file == null) {
throw new IllegalArgumentException("Can't find file '%#018x'".formatted(identifier));
}
return file;
}

@NotNull
Collection<? extends Archive> getArchives();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.shade.decima.model.archive.Archive;
import com.shade.decima.model.archive.ArchiveFile;
import com.shade.decima.model.archive.ArchiveManager;
import com.shade.decima.model.packfile.edit.Change;
import com.shade.decima.model.packfile.resource.Resource;
import com.shade.decima.model.util.FilePath;
Expand Down Expand Up @@ -59,7 +58,7 @@ public class Packfile implements Archive, Comparable<Packfile> {

@NotNull
@Override
public ArchiveManager getManager() {
public PackfileManager getManager() {
return manager;
}

Expand Down Expand Up @@ -283,21 +282,12 @@ public ArchiveFile findFile(@NotNull String identifier) {
return new PackfileFile(this, entry);
}

@NotNull
@Nullable
@Override
public ArchiveFile getFile(@NotNull String identifier) {
public ArchiveFile findFile(long identifier) {
final FileEntry entry = getFileEntry(identifier);
if (entry == null) {
throw new IllegalArgumentException("Can't find file '%s' in archive %s".formatted(identifier, getName()));
}
return new PackfileFile(this, entry);
}

@NotNull
public ArchiveFile getFile(long hash) {
final FileEntry entry = getFileEntry(hash);
if (entry == null) {
throw new IllegalArgumentException("Can't find file '%#018x' in archive %s".formatted(hash, getName()));
return null;
}
return new PackfileFile(this, entry);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,10 @@
import java.io.IOException;
import java.io.InputStream;

public class PackfileFile implements ArchiveFile {
private final Packfile packfile;
private final Packfile.FileEntry entry;

public PackfileFile(@NotNull Packfile packfile, @NotNull Packfile.FileEntry entry) {
this.packfile = packfile;
this.entry = entry;
}

@NotNull
@Override
public String getName() {
return "%#018x".formatted(entry.hash());
}

@NotNull
public record PackfileFile(@NotNull Packfile packfile, @NotNull Packfile.FileEntry entry) implements ArchiveFile {
@Override
public String getPath() {
return getName();
public long getIdentifier() {
return entry.hash();
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ public ArchiveFile findFile(@NotNull String identifier) {
return archive.getFile(identifier);
}

@NotNull
@Nullable
@Override
public ArchiveFile getFile(@NotNull String identifier) {
public ArchiveFile findFile(long identifier) {
final Packfile archive = findFirst(identifier);
if (archive == null) {
throw new IllegalArgumentException("Can't find file '%s'".formatted(identifier));
return null;
}
return archive.getFile(identifier);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.shade.decima.ui.editor;

import com.shade.decima.model.app.ProjectContainer;
import com.shade.decima.model.packfile.Packfile;
import com.shade.decima.model.archive.Archive;
import com.shade.decima.model.util.FilePath;
import com.shade.decima.ui.Application;
import com.shade.decima.ui.navigator.NavigatorPath;
Expand All @@ -27,8 +27,8 @@ public NodeEditorInputLazy(@NotNull String container, @NotNull String packfile,
this(UUID.fromString(container), packfile, FilePath.of(path, false, false));
}

public NodeEditorInputLazy(@NotNull ProjectContainer container, @NotNull Packfile packfile, @NotNull String path) {
this(container.getId(), packfile.getPath().getFileName().toString(), FilePath.of(path, false, false));
public NodeEditorInputLazy(@NotNull ProjectContainer container, @NotNull Archive archive, @NotNull String path) {
this(container.getId(), archive.getId(), FilePath.of(path, false, false));
}

@NotNull
Expand Down Expand Up @@ -86,7 +86,7 @@ public String getDescription() {
public boolean representsSameResource(@NotNull EditorInput other) {
if (other instanceof NodeEditorInputSimple o) {
return container().equals(o.getNode().getProjectContainer().getId())
&& packfile().equals(o.getNode().getPackfile().getPath().getFileName().toString())
&& packfile().equals(o.getNode().getPackfile().getId())
&& path().equals(o.getNode().getPath());
}
if (other instanceof NodeEditorInputLazy o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class CoreEditor extends JSplitPane implements SaveableEditor, StatefulEd
private final ProjectEditorInput input;
private final RTTICoreFile file;
private final MessageBusConnection connection;
private final int errors;

// Initialized in CoreEditor#createComponent
private CoreTree tree;
Expand All @@ -67,16 +68,17 @@ public class CoreEditor extends JSplitPane implements SaveableEditor, StatefulEd
private boolean sortingEnabled;

public CoreEditor(@NotNull FileEditorInput input) {
this(input, loadCoreFile(input));
this(input, loadFile(input));
}

public CoreEditor(@NotNull NodeEditorInput input) {
this(input, loadCoreFile(input));
this(input, loadFile(input));
}

private CoreEditor(@NotNull ProjectEditorInput input, @NotNull RTTICoreFile file) {
private CoreEditor(@NotNull ProjectEditorInput input, @NotNull FileLoadResult result) {
this.input = input;
this.file = file;
this.file = result.file;
this.errors = result.errors;
this.connection = MessageBus.getInstance().connect();

connection.subscribe(CoreEditorSettings.SETTINGS, () -> {
Expand Down Expand Up @@ -350,6 +352,10 @@ public RTTICoreFile getCoreFile() {
return file;
}

public int getErrorCount() {
return errors;
}

private void fireDirtyStateChange() {
firePropertyChange("dirty", null, isDirty());
}
Expand Down Expand Up @@ -421,23 +427,30 @@ private void fitValueViewer(@NotNull JComponent component) {
}

@NotNull
private static RTTICoreFile loadCoreFile(@NotNull NodeEditorInput input) {
try {
return input.getProject().getCoreFileReader().read(input.getNode().getFile(), LoggingErrorHandlingStrategy.getInstance());
private static FileLoadResult loadFile(@NotNull NodeEditorInput input) {
try (InputStream is = input.getNode().getFile().newInputStream()) {
return loadFile(input.getProject(), is);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

@NotNull
private static RTTICoreFile loadCoreFile(@NotNull FileEditorInput input) {
private static FileLoadResult loadFile(@NotNull FileEditorInput input) {
try (InputStream is = Files.newInputStream(input.getPath())) {
return input.getProject().getCoreFileReader().read(is, LoggingErrorHandlingStrategy.getInstance());
return loadFile(input.getProject(), is);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

@NotNull
private static FileLoadResult loadFile(@NotNull Project project, @NotNull InputStream is) throws IOException {
final MetricLoggingErrorHandlingStrategy strategy = new MetricLoggingErrorHandlingStrategy();
final RTTICoreFile file = project.getCoreFileReader().read(is, strategy);
return new FileLoadResult(file, strategy.errors);
}

@NotNull
private static String serializePath(@NotNull RTTIPath path) {
final StringBuilder selection = new StringBuilder();
Expand Down Expand Up @@ -512,4 +525,16 @@ private static RTTIPath deserializePath(@NotNull Object object) {

return new RTTIPath(elements.toArray(RTTIPathElement[]::new));
}

private record FileLoadResult(@NotNull RTTICoreFile file, int errors) {}

private static class MetricLoggingErrorHandlingStrategy extends LoggingErrorHandlingStrategy {
private int errors = 0;

@Override
public void handle(@NotNull Exception e) {
super.handle(e);
errors++;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.shade.decima.ui.menu.menus.EditMenu;
import com.shade.decima.ui.menu.menus.FileMenu;
import com.shade.decima.ui.menu.menus.ViewMenu;
import com.shade.platform.ui.editors.spi.EditorOnboarding;
import com.shade.platform.ui.editors.EditorOnboarding;
import com.shade.platform.ui.editors.spi.EditorOnboardingProvider;
import com.shade.util.NotNull;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.shade.decima.ui.editor.impl.notifications;

import com.shade.decima.ui.editor.core.CoreEditor;
import com.shade.platform.ui.editors.Editor;
import com.shade.platform.ui.editors.EditorNotification;
import com.shade.platform.ui.editors.spi.EditorNotificationProvider;
import com.shade.util.NotNull;

import java.text.MessageFormat;
import java.util.Collection;
import java.util.List;

public class LoadedWithErrorsEditorNotificationProvider implements EditorNotificationProvider {
@NotNull
@Override
public Collection<EditorNotification> getNotifications(@NotNull Editor editor) {
if (!(editor instanceof CoreEditor coreEditor) || coreEditor.getErrorCount() == 0) {
return List.of();
}

return List.of(new EditorNotification(
EditorNotification.Status.ERROR,
MessageFormat.format(
"<html>This file was loaded with <b>{0}</b> {0,choice,1#error|1<errors}. "
+ "Refer to the console output for more information</html>",
coreEditor.getErrorCount()
),
List.of()
));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.shade.decima.ui.editor.impl.notifications;

import com.shade.decima.model.archive.ArchiveFile;
import com.shade.decima.ui.Application;
import com.shade.decima.ui.editor.NodeEditorInput;
import com.shade.decima.ui.editor.NodeEditorInputSimple;
import com.shade.decima.ui.navigator.NavigatorPath;
import com.shade.decima.ui.navigator.impl.NavigatorFileNode;
import com.shade.platform.model.runtime.VoidProgressMonitor;
import com.shade.platform.ui.editors.Editor;
import com.shade.platform.ui.editors.EditorManager;
import com.shade.platform.ui.editors.EditorNotification;
import com.shade.platform.ui.editors.spi.EditorNotificationProvider;
import com.shade.util.NotNull;

import java.util.Collection;
import java.util.List;

public class SupersededByPatchEditorNotificationProvider implements EditorNotificationProvider {
@NotNull
@Override
public Collection<EditorNotification> getNotifications(@NotNull Editor editor) {
if (!(editor.getInput() instanceof NodeEditorInput input)) {
return List.of();
}

final NavigatorFileNode node = input.getNode();
final ArchiveFile currentFile = node.getFile();
final ArchiveFile actualFile = node.getArchive().getManager().getFile(currentFile.getIdentifier());

if (currentFile.equals(actualFile)) {
return List.of();
}

return List.of(new EditorNotification(
EditorNotification.Status.WARNING,
"<html>This file has been superseded by a patch file. The displayed content may be outdated</html>",
List.of(
new EditorNotification.Action("Open File", () -> Application.getNavigator().getModel()
.findFileNode(new VoidProgressMonitor(), NavigatorPath.of(
node.getProject().getContainer(),
actualFile.getArchive(),
node.getPath()
))
.thenApply(n -> EditorManager.getInstance().openEditor(new NodeEditorInputSimple(n), true)))
)
));
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.shade.decima.ui.navigator;

import com.shade.decima.model.app.ProjectContainer;
import com.shade.decima.model.packfile.Packfile;
import com.shade.decima.model.archive.Archive;
import com.shade.decima.model.util.FilePath;
import com.shade.util.NotNull;

public record NavigatorPath(@NotNull String projectId, @NotNull String packfileId, @NotNull FilePath filePath) {
@NotNull
public static NavigatorPath of(@NotNull ProjectContainer container, @NotNull Packfile packfile, @NotNull FilePath filePath) {
return new NavigatorPath(container.getId().toString(), packfile.getPath().getFileName().toString(), filePath);
public static NavigatorPath of(@NotNull ProjectContainer container, @NotNull Archive archive, @NotNull FilePath filePath) {
return new NavigatorPath(container.getId().toString(), archive.getId(), filePath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public Packfile getPackfile() {

@Override
public boolean contains(@NotNull NavigatorPath path) {
return packfile.getPath().getFileName().toString().equals(path.packfileId());
return packfile.getId().equals(path.packfileId());
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
com.shade.decima.ui.editor.impl.notifications.LoadedWithErrorsEditorNotificationProvider
com.shade.decima.ui.editor.impl.notifications.SupersededByPatchEditorNotificationProvider
Loading

0 comments on commit ec53e52

Please sign in to comment.