patternjavaMinor
Note Management Program
Viewed 0 times
managementnoteprogram
Problem
Good night everyone. I spent the last week or so building this pretty cool note management program written with JavaFX. . Buttons are the method used to switch b/w open documents, and the "notes" are converted to HTML documents using a simple syntax:
The button with the
I've done some refactoring on my own, but I'm not sure how else I can reasonably clean up my code. Also, the program is rather RAM heavy, and I read that it's partially a javafx problem, but I'm sure there's some way I can write this better.
main.fxml:
Notey.java
```
package notey;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Parent;
import javafx.fxml.FXMLLoader;
import java.io.IOException;
import java.net.URL;
public class Notey extends Application{
public static void main(String[] args){
Application.launch(args);
}
public void star
- Everything after
HEADINGon a single line is converted to ah1element.
- Everything after
IMAGEon a single line is converted to animgelement.
- Everything else is treated as a
pelement.
The button with the
= sign is used to show/hide the sidepane. The save/save all buttons do what they say. The view button switches the right pane's focus between the WebView used to view the current HTML version of the current note and the TextField and TextArea used for giving notes a title and text respectively. The last feature is that it scans the notes directory and loads any notes it finds on startup. I've done some refactoring on my own, but I'm not sure how else I can reasonably clean up my code. Also, the program is rather RAM heavy, and I read that it's partially a javafx problem, but I'm sure there's some way I can write this better.
main.fxml:
Notey.java
```
package notey;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Parent;
import javafx.fxml.FXMLLoader;
import java.io.IOException;
import java.net.URL;
public class Notey extends Application{
public static void main(String[] args){
Application.launch(args);
}
public void star
Solution
public void toggleWebView(){
if(webViewBox.isVisible()){
webViewBox.setVisible(false);
documentText.setVisible(true);
documentTitle.setVisible(true);
webViewBox.setPrefWidth(0);
webViewBox.setPrefHeight(0);
}
else{
webViewBox.setVisible(true);
documentText.setVisible(false);
documentTitle.setVisible(false);
webViewBox.setPrefWidth(1500);
webViewBox.setPrefHeight(1500);
WebEngine engine = webViewBox.getEngine();
System.out.println("Loading " + saveDocument(documentText, documentTitle, false));
engine.loadContent(saveDocument(documentText, documentTitle, false));
}
}Split it up into "showWebView" and "hideWebView". Right now you're doing multiple things in the same function and they're completely unrelated.
File output;
String validated;
String validChars = "(\\W)";
if(title.length()>10){
validated = title.replaceAll(validChars, "").substring(0, 10);
output= new File("notes/" + validated + ".txt");
}
else{
validated = title.replaceAll(validChars, "");
output= new File("notes/" + validated + ".txt");
}Here you have a possible bug: if stripping certain parts of the string makes the length drop from 11 to 9, then you'll get an exception when making the substring. Instead, first replace, then measure:
File output;
String validated;
String validChars = "(\\W)";
String strippedTitle = title.replaceAll(validChars, "");
if(strippedTitle.length()>10){
validated = strippedTitle.substring(0, 10);
} else {
validated = strippedTitle;
}
output= new File("notes/" + validated + ".txt");And of course, there's no need to have the same file object creation in the true and false cases.
public void sidePaneVisibility(ActionEvent event){
if(!showSidePane){
dividerPosition = mainViewPane.getDividerPositions()[0];
mainViewPane.setDividerPositions(0.0);
//sidePane.setVisible(false);
showSidePane = true;
}
else{
//sidePane.setVisible(true);
mainViewPane.setDividerPositions(dividerPosition);
showSidePane = false;
}
}If you're going to set it to it's inverse in both cases (if false, set true, if true, set false), then you don't need duplicate code:
public void sidePaneVisibility(ActionEvent event){
if(!showSidePane){
dividerPosition = mainViewPane.getDividerPositions()[0];
mainViewPane.setDividerPositions(0.0);
} else {
mainViewPane.setDividerPositions(dividerPosition);
}
showSidePane = !showSidePane;
}And commented out code can always go, that's what source control is for. If the code that is commented out was wrong, but seemed like obvious to write, then leave it in in commented form, but describe WHY it's wrong. Even in that case, you're usually better off describing just the function ("cannot use functionX here because reasons"), as it is better at pinpointing the issue than leaving the line in full.
Code Snippets
public void toggleWebView(){
if(webViewBox.isVisible()){
webViewBox.setVisible(false);
documentText.setVisible(true);
documentTitle.setVisible(true);
webViewBox.setPrefWidth(0);
webViewBox.setPrefHeight(0);
}
else{
webViewBox.setVisible(true);
documentText.setVisible(false);
documentTitle.setVisible(false);
webViewBox.setPrefWidth(1500);
webViewBox.setPrefHeight(1500);
WebEngine engine = webViewBox.getEngine();
System.out.println("Loading " + saveDocument(documentText, documentTitle, false));
engine.loadContent(saveDocument(documentText, documentTitle, false));
}
}File output;
String validated;
String validChars = "(\\W)";
if(title.length()>10){
validated = title.replaceAll(validChars, "").substring(0, 10);
output= new File("notes/" + validated + ".txt");
}
else{
validated = title.replaceAll(validChars, "");
output= new File("notes/" + validated + ".txt");
}File output;
String validated;
String validChars = "(\\W)";
String strippedTitle = title.replaceAll(validChars, "");
if(strippedTitle.length()>10){
validated = strippedTitle.substring(0, 10);
} else {
validated = strippedTitle;
}
output= new File("notes/" + validated + ".txt");public void sidePaneVisibility(ActionEvent event){
if(!showSidePane){
dividerPosition = mainViewPane.getDividerPositions()[0];
mainViewPane.setDividerPositions(0.0);
//sidePane.setVisible(false);
showSidePane = true;
}
else{
//sidePane.setVisible(true);
mainViewPane.setDividerPositions(dividerPosition);
showSidePane = false;
}
}public void sidePaneVisibility(ActionEvent event){
if(!showSidePane){
dividerPosition = mainViewPane.getDividerPositions()[0];
mainViewPane.setDividerPositions(0.0);
} else {
mainViewPane.setDividerPositions(dividerPosition);
}
showSidePane = !showSidePane;
}Context
StackExchange Code Review Q#118947, answer score: 2
Revisions (0)
No revisions yet.