Skip to content

Commit

Permalink
Fix active directory with virtual active directory and numerous small…
Browse files Browse the repository at this point in the history
… improvements

Prepared for version 1.0
  • Loading branch information
LukeOnuke committed Nov 2, 2021
1 parent fd08504 commit 6c17b55
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 49 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# java
.idea/
*.iml
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/lukeonuke/lmark/LMarkApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.slf4j.LoggerFactory;

import java.io.File;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.List;

Expand All @@ -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");
Expand All @@ -47,4 +49,8 @@ public void start(Stage primaryStage) {
new StartWindow(primaryStage).show();
}
}

public static List<String> getArguments() {
return arguments;
}
}
15 changes: 14 additions & 1 deletion src/main/java/com/lukeonuke/lmark/Registry.java
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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){
Expand All @@ -64,4 +73,8 @@ public void save(){
e.printStackTrace();
}
}

public void registerRegistryChangeEvent(String propertyName, PropertyChangeListener propertyChangeEvent){
registryChangeSupport.addPropertyChangeListener(propertyName, propertyChangeEvent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import javafx.event.EventType;

public class SimpleScrollEvent extends CustomEvent {
public static final EventType<CustomEvent> SIMPLE_SCROLL_EVENT_TYPE = new EventType(CUSTOM_EVENT_TYPE, "CustomEvent1");
public static final EventType<CustomEvent> SIMPLE_SCROLL_EVENT_TYPE = new EventType(CUSTOM_EVENT_TYPE, "SIMPLE_SCROLL_EVENT");
private double scrollPercentage;
public SimpleScrollEvent(double scrollPercentage) {
super(SIMPLE_SCROLL_EVENT_TYPE);
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/com/lukeonuke/lmark/gui/DebugWindow.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
48 changes: 29 additions & 19 deletions src/main/java/com/lukeonuke/lmark/gui/MainAppWindow.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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) -> {
Expand Down Expand Up @@ -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();
}

Expand All @@ -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, '*');
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/lukeonuke/lmark/gui/SettingsWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ public void show() {

stage.show();

stage.setAlwaysOnTop(true);
stage.setAlwaysOnTop(false);
stage.toFront();
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/lukeonuke/lmark/gui/StartWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ public void show() {


ArrayList<String> recentFilesList = new ArrayList<>();
File recentFilesStorage = new File(ApplicationConstants.RECENT_FILES_STORAGE);
File recentFilesStorage = FileUtils.getRelativeFile(ApplicationConstants.RECENT_FILES_STORAGE);
try {
if (!recentFilesStorage.exists()) {

recentFilesStorage.createNewFile();
FileUtils.writeJSON(recentFilesList, recentFilesStorage);

}
recentFilesList = FileUtils.readJSON(ApplicationConstants.RECENT_FILES_STORAGE,
recentFilesList = FileUtils.readJSON(recentFilesStorage.getPath(),
new TypeToken<ArrayList<String>>() {
}.getType());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
27 changes: 8 additions & 19 deletions src/main/java/com/lukeonuke/lmark/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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");
Expand All @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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();
Expand All @@ -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<String> recent;
try {
Expand All @@ -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())){
Expand All @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/lukeonuke/lmark/util/FxUtils.java
Original file line number Diff line number Diff line change
@@ -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
* <code>Platform.runLater()<code/> 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);
}
}
}
3 changes: 3 additions & 0 deletions src/main/resources/gui/mainstyle-light.css
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Binary file modified target/classes/module-info.class
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
C:\Users\lukak\Documents\GitHub\mdedit\src\main\java\com\lukeonuke\lmark\gui\util\OSIntegration.java
C:\Users\lukak\Documents\GitHub\mdedit\src\main\java\com\lukeonuke\lmark\gui\util\ThemeManager.java
C:\Users\lukak\Documents\GitHub\mdedit\src\main\java\com\lukeonuke\lmark\util\AnchorUtils.java
C:\Users\lukak\Documents\GitHub\mdedit\src\main\java\com\lukeonuke\lmark\gui\elements\Markdown.java
C:\Users\lukak\Documents\GitHub\mdedit\src\main\java\com\lukeonuke\lmark\gui\util\FileUtils.java
C:\Users\lukak\Documents\GitHub\mdedit\src\main\java\com\lukeonuke\lmark\util\FxUtils.java
C:\Users\lukak\Documents\GitHub\mdedit\src\main\java\com\lukeonuke\lmark\util\FileUtils.java
C:\Users\lukak\Documents\GitHub\mdedit\src\main\java\com\lukeonuke\lmark\event\CustomEvent.java
C:\Users\lukak\Documents\GitHub\mdedit\src\main\java\com\lukeonuke\lmark\gui\SettingsWindow.java
C:\Users\lukak\Documents\GitHub\mdedit\src\main\java\com\lukeonuke\lmark\event\CustomEventHandler.java
C:\Users\lukak\Documents\GitHub\mdedit\src\main\java\com\lukeonuke\lmark\ApplicationConstants.java
C:\Users\lukak\Documents\GitHub\mdedit\src\main\java\com\lukeonuke\lmark\Registry.java
C:\Users\lukak\Documents\GitHub\mdedit\src\main\java\com\lukeonuke\lmark\LMark.java
C:\Users\lukak\Documents\GitHub\mdedit\src\main\java\com\lukeonuke\lmark\LMarkApplication.java
C:\Users\lukak\Documents\GitHub\mdedit\src\main\java\com\lukeonuke\lmark\gui\DebugWindow.java
C:\Users\lukak\Documents\GitHub\mdedit\src\main\java\com\lukeonuke\lmark\util\OSIntegration.java
C:\Users\lukak\Documents\GitHub\mdedit\src\main\java\com\lukeonuke\lmark\gui\StartWindow.java
C:\Users\lukak\Documents\GitHub\mdedit\src\main\java\com\lukeonuke\lmark\util\ThemeManager.java
C:\Users\lukak\Documents\GitHub\mdedit\src\main\java\com\lukeonuke\lmark\gui\elements\FileCell.java
C:\Users\lukak\Documents\GitHub\mdedit\src\main\java\com\lukeonuke\lmark\gui\MainAppWindow.java
C:\Users\lukak\Documents\GitHub\mdedit\src\main\java\com\lukeonuke\lmark\gui\util\AnchorUtils.java
C:\Users\lukak\Documents\GitHub\mdedit\src\main\java\module-info.java
C:\Users\lukak\Documents\GitHub\mdedit\src\main\java\com\lukeonuke\lmark\gui\elements\Title.java
C:\Users\lukak\Documents\GitHub\mdedit\src\main\java\com\lukeonuke\lmark\gui\AppWindow.java
Expand Down

0 comments on commit 6c17b55

Please sign in to comment.