Skip to content

Commit

Permalink
bump version; code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
xzel23 committed Nov 3, 2024
1 parent 905bad5 commit 4bcb8fc
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ public abstract class FxApplication<A extends FxApplication<A, C>, C extends FxC
/**
* Preferences
*/
protected Preferences preferences = null;
protected @Nullable Preferences preferences;
/**
* The controller instance.
*/
protected C controller;
protected @Nullable C controller;

// - UI -

Expand All @@ -116,7 +116,7 @@ public abstract class FxApplication<A extends FxApplication<A, C>, C extends FxC
/**
* The main stage.
*/
private Stage mainStage;
private @Nullable Stage mainStage;

/**
* Constructor.
Expand Down Expand Up @@ -158,8 +158,8 @@ public static String asText(@Nullable URI uri) {
*
* @return the resource bundle or {@code null}
*/
protected ResourceBundle getResourceBundle() {
return null;
protected Optional<ResourceBundle> getResourceBundle() {
return Optional.empty();
}

/**
Expand Down Expand Up @@ -287,7 +287,7 @@ protected void updateApplicationTitle() {
StringBuilder title = new StringBuilder();
title.append(i18n.get("fx.application.name"));

FxDocument document = controller.getCurrentDocument();
FxDocument document = getController().getCurrentDocument().orElse(null);

if (document != null) {
String locStr = document.hasLocation() ?
Expand All @@ -304,7 +304,9 @@ protected void updateApplicationTitle() {
title.append(marker).append(locStr);
}

mainStage.setTitle(title.toString());
if (mainStage != null) {
mainStage.setTitle(title.toString());
}
}

/**
Expand Down Expand Up @@ -341,7 +343,9 @@ public void closeApplicationWindow() {
}
}

mainStage.close();
if (mainStage != null) {
mainStage.close();
}

mainStage = null; // make it garbage collectable
}
Expand Down Expand Up @@ -377,6 +381,7 @@ public final Preferences getPreferences() {
* @return the application's primary stage, or null if the application has been closed
*/
public Stage getStage() {
LangUtil.check(mainStage != null, "no main stage");
return mainStage;
}

Expand Down Expand Up @@ -433,6 +438,7 @@ public Path getDataDir() {
* @return the controller
*/
protected C getController() {
LangUtil.check(controller != null, "controller not set");
return controller;
}

Expand Down Expand Up @@ -498,15 +504,6 @@ public FileChooser.ExtensionFilter getExtensionFilterAllFiles() {
return new FileChooser.ExtensionFilter(i18n.get("fx.application.filter.all_files"), "*.*");
}

/**
* Get the user's home directory.
*
* @return the user's home directory
*/
public Path getUserHome() {
return USER_HOME;
}

/**
* Show this application's about dialog.
*/
Expand All @@ -519,7 +516,7 @@ public void showAboutDialog() {
*
* @param css URL to the CSS data
*/
protected void showAboutDialog(URL css) {
protected void showAboutDialog(@Nullable URL css) {
Dialogs.about(mainStage)
.title(i18n.format("fx.application.about.title.{0.name}", i18n.get("fx.application.name")))
.name(i18n.get("fx.application.name"))
Expand Down Expand Up @@ -571,4 +568,13 @@ public void addCleanupAction(Runnable task) {
public void removeCleanupAction(Runnable task) {
cleanupActions.remove(task);
}

/**
* Get the user home path.
*
* @return the user home path
*/
public static Path getUserHome() {
return USER_HOME;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;


/**
* Abstract controller class for handling JavaFX applications with documents.
*
Expand All @@ -67,7 +66,7 @@ public abstract class FxController<A extends FxApplication<A, C>, C extends FxCo
/**
* The URI of the currently opened document.
*/
protected final ObjectProperty<D> currentDocumentProperty = new SimpleObjectProperty<>();
protected final ObjectProperty<@Nullable D> currentDocumentProperty = new SimpleObjectProperty<>();

/**
* The {@link I18N} instance.
Expand All @@ -77,7 +76,7 @@ public abstract class FxController<A extends FxApplication<A, C>, C extends FxCo
/**
* The application instance.
*/
private A app;
private @Nullable A app;

/**
* The Default constructor. Just declared here to reduce visibility.
Expand All @@ -101,6 +100,7 @@ public void closeApplicationWindow() {
LOG.debug("close aborted because of dirty state");
return;
}
assert app != null;
app.closeApplicationWindow();
}

Expand Down Expand Up @@ -176,20 +176,19 @@ protected boolean handleDirtyState() {
/**
* Get current document location.
*
* @return URI of the current document or {@link FxDocument#VOID_URI}
* @return URI of the current document
*/
public URI getCurrentDocumentLocation() {
D doc = getCurrentDocument();
return doc != null ? doc.getLocation() : FxDocument.VOID_URI;
public Optional<URI> getCurrentDocumentLocation() {
return getCurrentDocument().map(FxDocument::getLocation);
}

/**
* Get current document.
*
* @return the current document or {@code null}
* @return the current document
*/
public D getCurrentDocument() {
return currentDocumentProperty.get();
public Optional<D> getCurrentDocument() {
return Optional.ofNullable(currentDocumentProperty.get());
}

/**
Expand Down Expand Up @@ -265,11 +264,10 @@ public boolean open() {
return false;
}

D document = getCurrentDocument();
Path initialDir = initialDir(document);
Path initialDir = initialDir(getCurrentDocument().orElse(null));

if (initialDir == null || !Files.isDirectory(initialDir)) {
initialDir = getApp().getUserHome();
initialDir = FxApplication.getUserHome();
}

Optional<Path> file = Dialogs
Expand Down Expand Up @@ -325,17 +323,20 @@ protected boolean open(URI uri) {
* @return true if the document was successfully saved, false otherwise
*/
public boolean save() {
if (!hasCurrentDocument()) {
//noinspection DataFlowIssue - false positive
D d = getCurrentDocument().orElse(null);

if (d == null) {
LOG.info("no document; not saving");
return false;
}

if (!getCurrentDocument().hasLocation()) {
if (!d.hasLocation()) {
LOG.debug("save: no URI set, delegating to saveAs()");
return saveAs();
}

return saveDocumentAndHandleErrors(getCurrentDocument());
return saveDocumentAndHandleErrors(d);
}

private boolean saveDocumentAndHandleErrors(D document) {
Expand All @@ -361,12 +362,14 @@ protected List<FileChooser.ExtensionFilter> saveFilters() {
* @return true if the document was successfully saved, false otherwise
*/
public boolean saveAs() {
if (!hasCurrentDocument()) {
//noinspection DataFlowIssue - false positive
D document = getCurrentDocument().orElse(null);

if (document == null) {
LOG.info("no document; not saving as new document");
return false;
}

D document = getCurrentDocument();
Path initialDir = initialDir(document);

Optional<Path> file = Dialogs
Expand Down Expand Up @@ -400,7 +403,8 @@ public boolean saveAs() {
*/
private Path initialDir(@Nullable D document) {
if (document == null) {
return getApp().getUserHome();
getApp();
return FxApplication.getUserHome();
}

Path parent = null;
Expand All @@ -411,7 +415,8 @@ private Path initialDir(@Nullable D document) {
} else {
String lastDocument = getApp().getPreference(PREF_DOCUMENT, "");
if (lastDocument.isBlank()) {
parent = getApp().getUserHome();
getApp();
parent = FxApplication.getUserHome();
LOG.debug("initialDir() - last document location not set, using user home as parent: {}", parent);
} else {
try {
Expand All @@ -420,7 +425,7 @@ private Path initialDir(@Nullable D document) {
LOG.debug("initialDir() - using last document location as parent: {}", parent);
} catch (IllegalArgumentException e) {
LOG.warn("could not retrieve last document location", e);
parent = app.getUserHome();
parent = FxApplication.getUserHome();
}
}
}
Expand All @@ -433,7 +438,8 @@ private Path initialDir(@Nullable D document) {

if (initialDir == null || !Files.isDirectory(initialDir)) {
LOG.warn("initialDir() - initial directory invalid, using user home instead: {}", initialDir);
initialDir = getApp().getUserHome();
getApp();
initialDir = FxApplication.getUserHome();
}

return initialDir;
Expand Down Expand Up @@ -480,9 +486,10 @@ public void setStatusText(String s) {
* @return the current directory if available, otherwise the user's home directory
*/
public Path getCurrentDir() {
if (hasCurrentDocument() && getCurrentDocument().hasLocation()) {
FxDocument document = getCurrentDocument().orElse(null);
if (document != null && document.hasLocation()) {
try {
Path parent = getCurrentDocument().getPath().getParent();
Path parent = document.getPath().getParent();
if (parent != null) {
return parent;
}
Expand All @@ -491,7 +498,7 @@ public Path getCurrentDir() {
}
}
LOG.warn("using user home");
return getApp().getUserHome();
return FxApplication.getUserHome();
}

/**
Expand Down
6 changes: 3 additions & 3 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
rootProject.name = "dua3-fx"
val projectVersion = "0.45-BETA-1"
val projectVersion = "0.45-beta-2-SNAPSHOT"

include("fx-application")
include("fx-application:fx-application-fxml")
Expand All @@ -15,10 +15,10 @@ dependencyResolutionManagement {
plugin("versions", "com.github.ben-manes.versions").version("0.51.0")
plugin("test-logger", "com.adarshr.test-logger").version("4.0.0")
plugin("spotbugs", "com.github.spotbugs").version("6.0.25")
plugin("cabe", "com.dua3.cabe").version("3.0-beta-7")
plugin("cabe", "com.dua3.cabe").version("3.0-beta-9")

version("commons-logging", "1.3.1")
version("dua3-utility", "14.0-BETA-1")
version("dua3-utility", "14-beta-2-SNAPSHOT")
version("javafx", "23")
version("jspecify", "1.0.0")
version("log4j", "2.24.1")
Expand Down

0 comments on commit 4bcb8fc

Please sign in to comment.