Anzeige von Area und Location Namen korrigiert
This commit is contained in:
@@ -58,7 +58,7 @@ public class AreaEditorView extends BorderPane {
|
||||
current = saved;
|
||||
reloading = false;
|
||||
persist();
|
||||
writeDefaultLocalisationIfAbsent(saved.id());
|
||||
writeDefaultLocalisation(saved.id());
|
||||
}
|
||||
|
||||
// ── Form panel ────────────────────────────────────────────────────────────
|
||||
@@ -187,6 +187,7 @@ public class AreaEditorView extends BorderPane {
|
||||
private void createArea() {
|
||||
AreaDefinition d = new AreaDefinition(PREFIX + "neu_" + System.currentTimeMillis(), "", "", "");
|
||||
areas.add(d);
|
||||
writeDefaultLocalisation(d.id());
|
||||
onSelected(current, d);
|
||||
}
|
||||
|
||||
@@ -233,12 +234,12 @@ public class AreaEditorView extends BorderPane {
|
||||
|
||||
// ── Localisation defaults ──────────────────────────────────────────────────
|
||||
|
||||
private void writeDefaultLocalisationIfAbsent(String fullId) {
|
||||
private void writeDefaultLocalisation(String fullId) {
|
||||
if (fullId == null || fullId.isBlank()) return;
|
||||
String shortId = fullId.startsWith(PREFIX) ? fullId.substring(PREFIX.length()) : fullId;
|
||||
if (shortId.isBlank()) return;
|
||||
String key = fullId + ".name";
|
||||
String defaultName = Character.toUpperCase(shortId.charAt(0)) + shortId.substring(1);
|
||||
String defaultName = EditorNaming.toDisplayName(shortId);
|
||||
|
||||
Path langSrc = ProjectRoot.resolve("blight-lang", "src", "main", "resources", "lang");
|
||||
Path[] langMirrors = {
|
||||
@@ -248,12 +249,11 @@ public class AreaEditorView extends BorderPane {
|
||||
for (String locale : new String[]{"de", "en"}) {
|
||||
Path src = langSrc.resolve("messages_" + locale + ".properties");
|
||||
try {
|
||||
if (appendKeyIfAbsent(src, key, defaultName)) {
|
||||
for (Path mirrorDir : langMirrors) {
|
||||
Path mirror = mirrorDir.resolve("messages_" + locale + ".properties");
|
||||
if (Files.exists(mirror)) {
|
||||
Files.copy(src, mirror, StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
writeOrUpdateKey(src, key, defaultName);
|
||||
for (Path mirrorDir : langMirrors) {
|
||||
Path mirror = mirrorDir.resolve("messages_" + locale + ".properties");
|
||||
if (Files.exists(mirror)) {
|
||||
Files.copy(src, mirror, StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
@@ -262,20 +262,22 @@ public class AreaEditorView extends BorderPane {
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns true if the key was absent and has been appended. */
|
||||
private static boolean appendKeyIfAbsent(Path file, String key, String value) throws IOException {
|
||||
if (!Files.exists(file)) return false;
|
||||
private static void writeOrUpdateKey(Path file, String key, String value) throws IOException {
|
||||
if (!Files.exists(file)) return;
|
||||
String needle = key + "=";
|
||||
String needleSpace = key + " =";
|
||||
for (String line : Files.readAllLines(file, StandardCharsets.UTF_8)) {
|
||||
String s = line.stripLeading();
|
||||
if (s.startsWith(needle) || s.startsWith(needleSpace)) return false;
|
||||
java.util.List<String> lines = new java.util.ArrayList<>(Files.readAllLines(file, StandardCharsets.UTF_8));
|
||||
boolean found = false;
|
||||
for (int i = 0; i < lines.size(); i++) {
|
||||
if (lines.get(i).stripLeading().startsWith(needle) || lines.get(i).stripLeading().startsWith(needleSpace)) {
|
||||
lines.set(i, key + "=" + value);
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
String existing = Files.readString(file, StandardCharsets.UTF_8);
|
||||
String sep = existing.endsWith("\n") ? "" : "\n";
|
||||
Files.writeString(file, existing + sep + key + "=" + value + "\n",
|
||||
if (!found) lines.add(key + "=" + value);
|
||||
Files.writeString(file, String.join("\n", lines) + "\n",
|
||||
StandardCharsets.UTF_8, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
|
||||
return true;
|
||||
}
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package de.blight.editor.ui;
|
||||
|
||||
public final class EditorNaming {
|
||||
|
||||
private EditorNaming() {}
|
||||
|
||||
/**
|
||||
* Wandelt einen underscore_id-Bezeichner in einen lesbaren Anzeigenamen um:
|
||||
* Unterstriche werden durch Leerzeichen ersetzt, jedes Wort beginnt mit Großbuchstaben.
|
||||
*
|
||||
* Beispiele:
|
||||
* "wald" → "Wald"
|
||||
* "dark_forest" → "Dark Forest"
|
||||
* "neu_1234567890" → "Neu 1234567890"
|
||||
*/
|
||||
public static String toDisplayName(String id) {
|
||||
if (id == null || id.isBlank()) return id;
|
||||
String[] parts = id.split("_", -1);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String part : parts) {
|
||||
if (sb.length() > 0) sb.append(' ');
|
||||
if (!part.isEmpty()) {
|
||||
sb.append(Character.toUpperCase(part.charAt(0)));
|
||||
sb.append(part.substring(1));
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package de.blight.editor.ui;
|
||||
import de.blight.common.LocationIO;
|
||||
import de.blight.common.model.Location;
|
||||
import de.blight.common.model.TextReference;
|
||||
import de.blight.editor.ProjectRoot;
|
||||
import javafx.animation.PauseTransition;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
@@ -14,6 +15,8 @@ import javafx.scene.layout.*;
|
||||
import javafx.util.Duration;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.*;
|
||||
import java.util.List;
|
||||
|
||||
public class LocationEditorView extends BorderPane {
|
||||
@@ -49,6 +52,7 @@ public class LocationEditorView extends BorderPane {
|
||||
saveFormToLocation(current);
|
||||
if (current.getId() == null || current.getId().isBlank()) return;
|
||||
persist();
|
||||
writeDefaultLocalisation(current.getId());
|
||||
}
|
||||
|
||||
// ── Form panel ────────────────────────────────────────────────────────────
|
||||
@@ -133,6 +137,7 @@ public class LocationEditorView extends BorderPane {
|
||||
Location loc = new Location();
|
||||
loc.setName(new TextReference(PREFIX + "neu_" + System.currentTimeMillis()));
|
||||
locations.add(loc);
|
||||
writeDefaultLocalisation(loc.getId());
|
||||
onSelected(current, loc);
|
||||
}
|
||||
|
||||
@@ -177,6 +182,54 @@ public class LocationEditorView extends BorderPane {
|
||||
catch (IOException e) { locations.clear(); }
|
||||
}
|
||||
|
||||
// ── Localisation defaults ──────────────────────────────────────────────────
|
||||
|
||||
private void writeDefaultLocalisation(String fullId) {
|
||||
if (fullId == null || fullId.isBlank()) return;
|
||||
String shortId = fullId.startsWith(PREFIX) ? fullId.substring(PREFIX.length()) : fullId;
|
||||
if (shortId.isBlank()) return;
|
||||
String key = fullId + ".name";
|
||||
String defaultName = EditorNaming.toDisplayName(shortId);
|
||||
|
||||
Path langSrc = ProjectRoot.resolve("blight-lang", "src", "main", "resources", "lang");
|
||||
Path[] langMirrors = {
|
||||
ProjectRoot.resolve("blight-lang", "bin", "main", "lang"),
|
||||
ProjectRoot.resolve("blight-lang", "build", "resources", "main", "lang"),
|
||||
};
|
||||
for (String locale : new String[]{"de", "en"}) {
|
||||
Path src = langSrc.resolve("messages_" + locale + ".properties");
|
||||
try {
|
||||
writeOrUpdateKey(src, key, defaultName);
|
||||
for (Path mirrorDir : langMirrors) {
|
||||
Path mirror = mirrorDir.resolve("messages_" + locale + ".properties");
|
||||
if (Files.exists(mirror)) {
|
||||
Files.copy(src, mirror, StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// non-critical
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void writeOrUpdateKey(Path file, String key, String value) throws IOException {
|
||||
if (!Files.exists(file)) return;
|
||||
String needle = key + "=";
|
||||
String needleSpace = key + " =";
|
||||
java.util.List<String> lines = new java.util.ArrayList<>(Files.readAllLines(file, StandardCharsets.UTF_8));
|
||||
boolean found = false;
|
||||
for (int i = 0; i < lines.size(); i++) {
|
||||
if (lines.get(i).stripLeading().startsWith(needle) || lines.get(i).stripLeading().startsWith(needleSpace)) {
|
||||
lines.set(i, key + "=" + value);
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) lines.add(key + "=" + value);
|
||||
Files.writeString(file, String.join("\n", lines) + "\n",
|
||||
StandardCharsets.UTF_8, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
|
||||
}
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────────────────
|
||||
|
||||
private static Label sectionTitle(String text) {
|
||||
|
||||
Reference in New Issue
Block a user