From 6c17b559fb43b72b9ff2218868a419f254e43605 Mon Sep 17 00:00:00 2001 From: Luka Kresoja <57074484+LukeOnuke@users.noreply.github.com> Date: Tue, 2 Nov 2021 01:21:09 +0100 Subject: [PATCH] Fix active directory with virtual active directory and numerous small improvements Prepared for version 1.0 --- .gitignore | 1 - .../com/lukeonuke/lmark/LMarkApplication.java | 6 +++ .../java/com/lukeonuke/lmark/Registry.java | 15 +++++- .../lmark/event/SimpleScrollEvent.java | 2 +- .../com/lukeonuke/lmark/gui/DebugWindow.java | 41 +++++++++++++++ .../lukeonuke/lmark/gui/MainAppWindow.java | 48 +++++++++++------- .../lukeonuke/lmark/gui/SettingsWindow.java | 3 +- .../com/lukeonuke/lmark/gui/StartWindow.java | 4 +- .../lmark/gui/elements/Markdown.java | 1 + .../com/lukeonuke/lmark/util/FileUtils.java | 27 +++------- .../com/lukeonuke/lmark/util/FxUtils.java | 17 +++++++ src/main/resources/gui/mainstyle-light.css | 3 ++ target/classes/module-info.class | Bin 760 -> 759 bytes .../compile/default-compile/inputFiles.lst | 10 ++-- 14 files changed, 129 insertions(+), 49 deletions(-) create mode 100644 src/main/java/com/lukeonuke/lmark/gui/DebugWindow.java create mode 100644 src/main/java/com/lukeonuke/lmark/util/FxUtils.java diff --git a/.gitignore b/.gitignore index 39c0560..a162640 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ - # java .idea/ *.iml diff --git a/src/main/java/com/lukeonuke/lmark/LMarkApplication.java b/src/main/java/com/lukeonuke/lmark/LMarkApplication.java index 08db411..d7e6554 100644 --- a/src/main/java/com/lukeonuke/lmark/LMarkApplication.java +++ b/src/main/java/com/lukeonuke/lmark/LMarkApplication.java @@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory; import java.io.File; +import java.net.URISyntaxException; import java.util.Arrays; import java.util.List; @@ -24,6 +25,7 @@ public static void launchApp(String[] args) { @Override public void start(Stage primaryStage) { + logger.info("Virtual working directory " + FileUtils.getRelativeFile("").getPath()); if (!arguments.isEmpty()) { logger.info("Found arguments"); @@ -47,4 +49,8 @@ public void start(Stage primaryStage) { new StartWindow(primaryStage).show(); } } + + public static List getArguments() { + return arguments; + } } diff --git a/src/main/java/com/lukeonuke/lmark/Registry.java b/src/main/java/com/lukeonuke/lmark/Registry.java index 6d62e2d..52a0cc2 100644 --- a/src/main/java/com/lukeonuke/lmark/Registry.java +++ b/src/main/java/com/lukeonuke/lmark/Registry.java @@ -1,13 +1,18 @@ package com.lukeonuke.lmark; import com.lukeonuke.lmark.util.FileUtils; +import org.slf4j.LoggerFactory; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.beans.PropertyChangeSupport; import java.io.*; import java.util.Properties; public class Registry { Properties prop = new Properties(); String fileName = FileUtils.getRelativeFile("app.properties").getPath(); + PropertyChangeSupport registryChangeSupport = new PropertyChangeSupport(this); private static Registry instance = null; private Registry() { @@ -50,7 +55,11 @@ public String readOption(String key){ return prop.getProperty(key); } - public void write(String key, String value) {prop.setProperty(key, value);} + public void write(String key, String value) { + registryChangeSupport.firePropertyChange(key, prop.getProperty(key), value); + prop.setProperty(key, value); + } + public void write(String key, boolean value){write(key, Boolean.toString(value));} public boolean readOptionAsBoolean(String key){ @@ -64,4 +73,8 @@ public void save(){ e.printStackTrace(); } } + + public void registerRegistryChangeEvent(String propertyName, PropertyChangeListener propertyChangeEvent){ + registryChangeSupport.addPropertyChangeListener(propertyName, propertyChangeEvent); + } } diff --git a/src/main/java/com/lukeonuke/lmark/event/SimpleScrollEvent.java b/src/main/java/com/lukeonuke/lmark/event/SimpleScrollEvent.java index 57985ae..8ee82dc 100644 --- a/src/main/java/com/lukeonuke/lmark/event/SimpleScrollEvent.java +++ b/src/main/java/com/lukeonuke/lmark/event/SimpleScrollEvent.java @@ -3,7 +3,7 @@ import javafx.event.EventType; public class SimpleScrollEvent extends CustomEvent { - public static final EventType SIMPLE_SCROLL_EVENT_TYPE = new EventType(CUSTOM_EVENT_TYPE, "CustomEvent1"); + public static final EventType SIMPLE_SCROLL_EVENT_TYPE = new EventType(CUSTOM_EVENT_TYPE, "SIMPLE_SCROLL_EVENT"); private double scrollPercentage; public SimpleScrollEvent(double scrollPercentage) { super(SIMPLE_SCROLL_EVENT_TYPE); diff --git a/src/main/java/com/lukeonuke/lmark/gui/DebugWindow.java b/src/main/java/com/lukeonuke/lmark/gui/DebugWindow.java new file mode 100644 index 0000000..665446e --- /dev/null +++ b/src/main/java/com/lukeonuke/lmark/gui/DebugWindow.java @@ -0,0 +1,41 @@ +package com.lukeonuke.lmark.gui; + +import com.lukeonuke.lmark.ApplicationConstants; +import com.lukeonuke.lmark.util.ThemeManager; +import javafx.scene.Scene; +import javafx.scene.control.TextArea; +import javafx.scene.image.Image; +import javafx.stage.Stage; + +public class DebugWindow implements AppWindow{ + Stage stage; + TextArea textArea = new TextArea(); + + public DebugWindow(Stage stage) { + this.stage = stage; + } + + @Override + public void show() { + Scene scene = new Scene(textArea); + ThemeManager.getInstance().addCss(scene); + stage.getIcons().add(new Image(ApplicationConstants.ICON)); + stage.setTitle("lmark - Debug window"); + stage.setScene(scene); + stage.show(); + } + + @Override + public void hide() { + stage.hide(); + } + + @Override + public String getDescription() { + return null; + } + + public void setText(String string){ + textArea.setText(string); + } +} diff --git a/src/main/java/com/lukeonuke/lmark/gui/MainAppWindow.java b/src/main/java/com/lukeonuke/lmark/gui/MainAppWindow.java index f576c88..4ee049a 100644 --- a/src/main/java/com/lukeonuke/lmark/gui/MainAppWindow.java +++ b/src/main/java/com/lukeonuke/lmark/gui/MainAppWindow.java @@ -1,15 +1,13 @@ package com.lukeonuke.lmark.gui; import com.lukeonuke.lmark.ApplicationConstants; +import com.lukeonuke.lmark.LMarkApplication; import com.lukeonuke.lmark.Registry; import com.lukeonuke.lmark.event.CustomEvent; import com.lukeonuke.lmark.event.SimpleScrollEvent; import com.lukeonuke.lmark.gui.elements.FileCell; import com.lukeonuke.lmark.gui.elements.Markdown; -import com.lukeonuke.lmark.util.AnchorUtils; -import com.lukeonuke.lmark.util.FileUtils; -import com.lukeonuke.lmark.util.OSIntegration; -import com.lukeonuke.lmark.util.ThemeManager; +import com.lukeonuke.lmark.util.*; import javafx.application.Platform; import javafx.geometry.Orientation; import javafx.geometry.Pos; @@ -169,18 +167,21 @@ public void show() { MenuItem showNonRenderedHTML = new MenuItem("Show non rendered HTML"); showNonRenderedHTML.setOnAction(actionEvent -> { - TextArea nonRenderedHTML = new TextArea(); - nonRenderedHTML.setEditable(false); - nonRenderedHTML.setText(markdown.getContents()); Stage stage = new Stage(); - Scene scene = new Scene(nonRenderedHTML); - stage.setScene(scene); - stage.setTitle("Debug - non rendered html"); - stage.setIconified(false); - stage.getIcons().add(new Image(ApplicationConstants.ICON)); - stage.show(); + DebugWindow debugWindow = new DebugWindow(stage); + debugWindow.setText(markdown.getContents()); + debugWindow.show(); }); - debug.getItems().add(showNonRenderedHTML); + + MenuItem showArguments = new MenuItem("Show arguments"); + showArguments.setOnAction(actionEvent -> { + Stage stage = new Stage(); + DebugWindow debugWindow = new DebugWindow(stage); + debugWindow.setText(LMarkApplication.getArguments().toString()); + debugWindow.show(); + }); + + debug.getItems().addAll(showNonRenderedHTML, showArguments); optionsMenu.getItems().add(debug); @@ -320,9 +321,7 @@ public void show() { stage.getIcons().add(new Image(ApplicationConstants.ICON)); stage.setTitle(ApplicationConstants.MAIN_WINDOW_TITLE + " - " + fileUtils.getFile().getName()); - if (autosaveEnabled) { - stage.setTitle(stage.getTitle() + " | autosave enabled"); - } + updateTitle(); stage.setScene(scene); stage.setOnCloseRequest((event) -> { @@ -362,6 +361,11 @@ public void show() { } }); + registry.registerRegistryChangeEvent(ApplicationConstants.PROPERTIES_AUTOSAVE_ENABLED, autosaveEvent -> { + autosaveEnabled = Boolean.parseBoolean( (String) autosaveEvent.getNewValue()); + FxUtils.lazyRunOnPlatform(this::updateTitle); + }); + stage.show(); } @@ -376,12 +380,18 @@ public String getDescription() { } private void save(String text) { - if (!autosaveEnabled) - stage.setTitle(ApplicationConstants.MAIN_WINDOW_TITLE + " - " + fileUtils.getFile().getName()); + fileUtils.saveFile(fileUtils.getFile(), text); logger.info("Saved hash = " + text.hashCode()); } + private void updateTitle(){ + stage.setTitle(ApplicationConstants.MAIN_WINDOW_TITLE + " - " + fileUtils.getFile().getPath()); + if (autosaveEnabled){ + stage.setTitle(stage.getTitle() + " | autosave enabled"); + } + } + private void formatItalicize(TextArea textArea, int count) { formatSelection(textArea, count, '*'); } diff --git a/src/main/java/com/lukeonuke/lmark/gui/SettingsWindow.java b/src/main/java/com/lukeonuke/lmark/gui/SettingsWindow.java index efacf14..0a3afc2 100644 --- a/src/main/java/com/lukeonuke/lmark/gui/SettingsWindow.java +++ b/src/main/java/com/lukeonuke/lmark/gui/SettingsWindow.java @@ -77,8 +77,7 @@ public void show() { stage.show(); - stage.setAlwaysOnTop(true); - stage.setAlwaysOnTop(false); + stage.toFront(); } @Override diff --git a/src/main/java/com/lukeonuke/lmark/gui/StartWindow.java b/src/main/java/com/lukeonuke/lmark/gui/StartWindow.java index 83e6bad..ae0193b 100644 --- a/src/main/java/com/lukeonuke/lmark/gui/StartWindow.java +++ b/src/main/java/com/lukeonuke/lmark/gui/StartWindow.java @@ -48,7 +48,7 @@ public void show() { ArrayList recentFilesList = new ArrayList<>(); - File recentFilesStorage = new File(ApplicationConstants.RECENT_FILES_STORAGE); + File recentFilesStorage = FileUtils.getRelativeFile(ApplicationConstants.RECENT_FILES_STORAGE); try { if (!recentFilesStorage.exists()) { @@ -56,7 +56,7 @@ public void show() { FileUtils.writeJSON(recentFilesList, recentFilesStorage); } - recentFilesList = FileUtils.readJSON(ApplicationConstants.RECENT_FILES_STORAGE, + recentFilesList = FileUtils.readJSON(recentFilesStorage.getPath(), new TypeToken>() { }.getType()); diff --git a/src/main/java/com/lukeonuke/lmark/gui/elements/Markdown.java b/src/main/java/com/lukeonuke/lmark/gui/elements/Markdown.java index c049544..e945884 100644 --- a/src/main/java/com/lukeonuke/lmark/gui/elements/Markdown.java +++ b/src/main/java/com/lukeonuke/lmark/gui/elements/Markdown.java @@ -46,6 +46,7 @@ public Markdown() { webView.getStyleClass().add("markdown"); webView.getEngine().getHistory().getEntries().clear(); webView.contextMenuEnabledProperty().setValue(false); + webView.getEngine().setUserDataDirectory(FileUtils.getRelativeFile("webview-memory")); webView.getEngine().setOnError(errorEvent -> { logger.error(errorEvent.getMessage()); diff --git a/src/main/java/com/lukeonuke/lmark/util/FileUtils.java b/src/main/java/com/lukeonuke/lmark/util/FileUtils.java index 60ee9a6..f2e50ba 100644 --- a/src/main/java/com/lukeonuke/lmark/util/FileUtils.java +++ b/src/main/java/com/lukeonuke/lmark/util/FileUtils.java @@ -3,6 +3,7 @@ import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.lukeonuke.lmark.ApplicationConstants; +import com.lukeonuke.lmark.LMark; import javafx.application.Platform; import org.mozilla.universalchardet.UniversalDetector; @@ -33,7 +34,7 @@ public static FileUtils getInstance() { } private FileUtils(String path) throws FileNotFoundException { - file = new File(path).getAbsoluteFile(); + setFile(new File(path).getAbsoluteFile()); if (!file.exists()) { throw new FileNotFoundException("File not found"); @@ -49,10 +50,8 @@ public static String getResourceAsString(String path) throws IOException, NullPo String line; - int count = 0; while ((line = reader.readLine()) != null) { sb.append(line); - count++; } } catch (IOException e) { @@ -68,8 +67,8 @@ public File getFile() { public void setFile(File file) { File oldFile = this.file; this.file = file; - fileChangeSupport.firePropertyChange("file", oldFile, file); addToRecents(file); + fileChangeSupport.firePropertyChange("file", oldFile, file); } public String readFile() throws IOException { @@ -97,19 +96,6 @@ public static String detectCharset(File file) { return charset; } - public static String stripProtocolFromPath(String path) { - String reFormatted = path; - if(path.indexOf(':') != -1){ - reFormatted = path.substring(path.indexOf(':') + 1); - } - - while (reFormatted.startsWith("/") || reFormatted.startsWith("\\")) { - reFormatted = reFormatted.substring(1); - } - - return reFormatted; - } - public static void writeJSON(Object source, File file) throws IOException { FileWriter fileWriter = new FileWriter(file); Gson gson = new Gson(); @@ -135,6 +121,7 @@ public void registerFileListener(PropertyChangeListener propertyChangeListener){ } public static void addToRecents(File recentFile){ + recentFile = recentFile.getAbsoluteFile(); File recentFilesStorage = FileUtils.getRelativeFile(ApplicationConstants.RECENT_FILES_STORAGE); ArrayList recent; try { @@ -147,8 +134,8 @@ public static void addToRecents(File recentFile){ } if(recent.contains(recentFile.getAbsolutePath())){ - recent.add(0, recentFile.getAbsolutePath()); recent.remove(recentFile.getAbsolutePath()); + recent.add(0, recentFile.getAbsolutePath()); } if(!recent.contains(recentFile.getAbsolutePath())){ @@ -167,7 +154,9 @@ public static void addToRecents(File recentFile){ public static File getRelativeFile(String path){ try { - return new File(new File(".").getCanonicalPath() + File.separator + path); + return new File(new File(LMark.class.getResource("").getFile()) + .getParentFile().getParentFile().getParentFile().getParentFile().getCanonicalPath() + + File.separator + path); } catch (IOException e) { Platform.exit(); System.exit(1); diff --git a/src/main/java/com/lukeonuke/lmark/util/FxUtils.java b/src/main/java/com/lukeonuke/lmark/util/FxUtils.java new file mode 100644 index 0000000..c5c31b1 --- /dev/null +++ b/src/main/java/com/lukeonuke/lmark/util/FxUtils.java @@ -0,0 +1,17 @@ +package com.lukeonuke.lmark.util; + +import javafx.application.Platform; + +public class FxUtils { + /** + * Will lazy run stuff that needs to be ran on the jfx main thread. Lazy run means that it will add it to the + * Platform.runLater() queue only if its not on the jfx main thread. + * */ + public static void lazyRunOnPlatform(Runnable runnable){ + if(Platform.isFxApplicationThread()){ + runnable.run(); + }else{ + Platform.runLater(runnable); + } + } +} diff --git a/src/main/resources/gui/mainstyle-light.css b/src/main/resources/gui/mainstyle-light.css index 1b0a647..2eae3f1 100644 --- a/src/main/resources/gui/mainstyle-light.css +++ b/src/main/resources/gui/mainstyle-light.css @@ -231,6 +231,9 @@ menu-bar { .split-pane-divider { -fx-background-color: -background; + -fx-border-style: hidden solid hidden solid; + -fx-border-width: 1; + -fx-border-color: derive(-wintergreen, 0.85); } .split-pane{ diff --git a/target/classes/module-info.class b/target/classes/module-info.class index f06ac7647b32deaebb55c422a8fd0c4e52125f5f..6143695b7bab7fbce14a34e0a23749191c99b4a2 100644 GIT binary patch literal 759 zcmZva+j7$|5QcvRnzm`7r3$4STh0`SlC*{L1l)1O6DYQPiY$3#yUlRbN8ow5-~o6j zhV{}=X23K4B>%hK)xY+SpWnU%IK{hF^aM6aAGg+wa+i34!Druw$b8JL5!n7KqzZke z1^S;jTVhpU#g;nECDsMjyQrG!n%?S9->Zq5N^A;jb$YT;k#|k-wwAa_Juby_Gn3dB z*i5WhbfuKGR$@nB=a14hxmB@lw8T(gZK+G_3hXVPR26(eCsvh7V}I@>_Wr3+=5-`- zkNg(#TrEnf>Pv|S)Sk~Cn|j{(O5#zcP)bqzSz)3^;$WF$eVXPjm3TsqeMnVplaqp? zM`U<1(zeRI0kM4m literal 760 zcmZvaS#r}r5QhITB+dp7ZWF?quoJ-JC85Fzc;kr^jHJ=#kw!I=5*JT70_Wj@18^vc z)?fow;3|L9-P6a)Fc37Bb12v<+Bi5NlVeGSZ)A-j>;g zcC|8Ls$8VG%)sYbo3OZ~wybQug2bsR%|c$m!JjdrxLXa^Xtd)YY^BSzDGUqrH;z|0 z>?-C>ste5>SgYks;^4%z3vElrWVQL@|3oKAn@zOg*JV>*0fN7!U%wuDZ1ivq*ZKT{ z4Id_KUIBe