Start work on editing window
This commit is contained in:
1 parent
fd124c828d
commit
92e10965bd
4 files changed
+105
No files matched your search
@@ -142,6 +142,8 @@ public class BefatorWeb {
|
||||
}
|
||||
});
|
||||
LinkDatabase.LINKS_TABLE.getAll().forEach(BefatorWeb::addLinkToGraph);
|
||||
|
||||
new FileBrowserWindow().setVisible(true);
|
||||
}
|
||||
|
||||
public static void addLinkToGraph(LinkDBEntry link) {
|
||||
|
||||
@@ -47,6 +47,10 @@ public record SavedPaged(String url, ContentType type, StatusCode status) {
|
||||
throw new IllegalArgumentException("Invalid content type: " + contentType);
|
||||
}
|
||||
|
||||
public boolean isText() {
|
||||
return this != GIF && this != JPEG && this != PNG && this != ICO && this != BINARY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return value;
|
||||
|
||||
@@ -116,4 +116,22 @@ public class SiteCache {
|
||||
}
|
||||
}
|
||||
|
||||
private static String fixUrlPath(String url, SavedPaged.ContentType type) {
|
||||
if (url.contains("?")) {
|
||||
url = url.substring(0, url.indexOf("?"));
|
||||
}
|
||||
|
||||
if (url.endsWith("/")) {
|
||||
url = url + "index";
|
||||
}
|
||||
if (!url.substring(url.indexOf('/') + 1).contains(".")) {
|
||||
return url + switch (type) {
|
||||
case BINARY, PLAIN -> "";
|
||||
case REDIRECT -> ".REDIRECT";
|
||||
default -> "." + type.name().toLowerCase();
|
||||
};
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package de.bommels05.befatorweb.gui;
|
||||
|
||||
import de.bommels05.befatorweb.SavedPaged;
|
||||
import de.bommels05.befatorweb.SiteCache;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.tree.DefaultMutableTreeNode;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.awt.*;
|
||||
import java.util.StringJoiner;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class FileBrowserWindow extends JFrame {
|
||||
|
||||
public FileBrowserWindow() {
|
||||
super("Befator Web");
|
||||
|
||||
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Websites");
|
||||
List<String> pages = new ArrayList<>(SiteCache.getPages().stream().map(SavedPaged::url).toList());
|
||||
addPages(pages, root);
|
||||
|
||||
JPanel editorPane = new JPanel();
|
||||
JTextArea textArea = new JTextArea();
|
||||
textArea.setEditable(true);
|
||||
editorPane.add(textArea);
|
||||
JTree files = new JTree(root);
|
||||
files.addTreeSelectionListener(e -> {
|
||||
StringJoiner joiner = new StringJoiner("/");
|
||||
for (Object o : e.getPath().getPath()) {
|
||||
if (!o.toString().equals("Websites")) {
|
||||
joiner.add(o.toString());
|
||||
}
|
||||
}
|
||||
SavedPaged page = SiteCache.getPage(joiner.toString());
|
||||
if (page == null && joiner.toString().endsWith("/index.html")) {
|
||||
page = SiteCache.getPage(joiner.toString().substring(0, joiner.toString().lastIndexOf('/') + 1));
|
||||
}
|
||||
if (page != null) {
|
||||
if (page.type().isText()) {
|
||||
textArea.setText(new String(page.getContent()));
|
||||
} else {
|
||||
textArea.setText("Binary File");
|
||||
}
|
||||
} else {
|
||||
textArea.setText("");
|
||||
}
|
||||
});
|
||||
JPopupMenu menu = new JPopupMenu();
|
||||
JMenuItem notepad = new JMenuItem("Notepad++");
|
||||
menu.add(notepad);
|
||||
files.setComponentPopupMenu(menu);
|
||||
JSplitPane content = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(files), new JScrollPane(editorPane));
|
||||
this.add(content);
|
||||
|
||||
this.pack();
|
||||
}
|
||||
|
||||
private void addPages(List<String> pages, DefaultMutableTreeNode root) {
|
||||
while (!pages.isEmpty()) {
|
||||
String url = pages.get(0);
|
||||
if (url.contains("/")) {
|
||||
String parent = url.substring(0, url.indexOf('/'));
|
||||
List<String> children = pages.stream().filter(s -> s.startsWith(parent + "/")).toList();
|
||||
List<String> subPages = children.stream().map(s -> s.substring(url.indexOf("/") + 1)).toList();
|
||||
DefaultMutableTreeNode parentNode = new DefaultMutableTreeNode(parent);
|
||||
addPages(new ArrayList<>(subPages), parentNode);
|
||||
root.add(parentNode);
|
||||
pages.removeAll(children);
|
||||
} else {
|
||||
if (!url.isEmpty()) {
|
||||
root.add(new DefaultMutableTreeNode(url));
|
||||
} else {
|
||||
root.add(new DefaultMutableTreeNode("index.html"));
|
||||
}
|
||||
pages.remove(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user