Skip to content

Commit d22f790

Browse files
Tabs schließen und UX; Papierkorb löscht Markierung; CodeGen/Java-Scope
- StrTabbedPane: Tabs schließen (Mittelklick, Menü, Strg+W), Tab-Wechsel ohne JButton-Konflikt - AuswahlPanel: Linksklick auf Papierkorb löscht markiertes Element (Dialog wie Kontextmenü) - Struktogramm: markiertesElementLoeschen, Markierung nach Löschen bereinigen - CodeGenRules, CodeErzeuger, StruktogrammElementListe: Codegenerierung/Java-Scope erweitert - i18n: palette.trashClick für Papierkorb-Tooltip Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 314db35 commit d22f790

11 files changed

Lines changed: 585 additions & 37 deletions

File tree

‎src/main/java/de/visustruct/control/Struktogramm.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,12 +833,27 @@ public void elementLoeschen(StruktogrammElement zuLoeschen, boolean vorherFragen
833833
tmp.entfernen(zuLoeschen); //das Element wird entfernt
834834
}
835835

836+
if (zuLoeschen == markiertesElement) {
837+
elementAuswaehlen(null);
838+
}
839+
836840
zeichenbereichAktualisieren();
837841
zeichne();
838842
rueckgaengigPunktSetzen();
839843
}
840844
}
841845

846+
/**
847+
* Löscht das gelb markierte Element mit Bestätigungsdialog (wie Kontextmenü „Löschen“).
848+
* Platzhalter-LeerElemente werden wie im Kontextmenü nicht gelöscht.
849+
*/
850+
public void markiertesElementLoeschen() {
851+
if (markiertesElement == null || markiertesElement instanceof LeerElement) {
852+
return;
853+
}
854+
elementLoeschen(markiertesElement, true);
855+
}
856+
842857

843858

844859
//Rechtsklick an der Position (x/y) wurde registriert, also Popup-Menü anzeigen

‎src/main/java/de/visustruct/struktogrammelemente/CodeGenRules.java‎

Lines changed: 308 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,111 @@ public final class CodeGenRules {
2626
"\\b(?:final\\s+)?(?:byte|short|int|long|float|double|boolean|char|String)\\s*\\[\\s*\\]\\s+([A-Za-z_$][A-Za-z0-9_$]*)\\b");
2727
private static final Pattern JAVA_ARRAY_DECL_2 = Pattern.compile(
2828
"\\b(?:final\\s+)?(?:byte|short|int|long|float|double|boolean|char|String)\\s+([A-Za-z_$][A-Za-z0-9_$]*)\\s*\\[\\s*\\]");
29-
private static final Set<String> JAVA_ARRAY_LIKE_LENGTH_NAMES = Set.of(
30-
"a", "a1", "a2", "arr", "arr1", "arr2", "array", "data", "feld", "i", "j", "k", "m", "n",
31-
"nums", "werte", "zahlen", "student", "students");
29+
/** Während der Java-Quelltext-Erzeugung: bereits deklarierte einfache Bezeichner (z. B. kein zweites {@code int} bei {@code input:}). */
30+
private static final ThreadLocal<Integer> JAVA_SCOPE_DEPTH = new ThreadLocal<>();
31+
private static final ThreadLocal<Set<String>> JAVA_DECLARED_NAMES = new ThreadLocal<>();
32+
private static final Pattern JAVA_EMITTED_TYPED_ASSIGN = Pattern.compile(
33+
"(?i)^\\s*(?:final\\s+)?(int|long|float|double|boolean|byte|short|char|String)(?:\\s*\\[\\s*\\])?\\s+([A-Za-z_$][\\w]*)\\s*=");
34+
private static final Pattern JAVA_EMITTED_TYPED_ASSIGN_BRACKETS_AFTER = Pattern.compile(
35+
"(?i)^\\s*(?:final\\s+)?(int|long|float|double|boolean|byte|short|char|String)\\s+([A-Za-z_$][\\w]*)\\s*\\[\\s*\\]\\s*=");
36+
/** Während der JS-Quelltext-Erzeugung: für {@link #javaScriptIntegerDivisionIfNeeded} (Swift-Parität). */
37+
private static final ThreadLocal<Set<String>> JS_INTEGER_VARIABLES = new ThreadLocal<>();
38+
39+
private static final Pattern MATH_RANDOM_NO_PARENS = Pattern.compile("(?i)\\bMath\\.random\\b(?!\\s*\\()");
40+
private static final Pattern JS_INT_ASSIGN_DIV = Pattern
41+
.compile("^\\s*([A-Za-z_$][A-Za-z0-9_$]*)\\s*=\\s*([A-Za-z_$][A-Za-z0-9_$]*|\\d+)\\s*/\\s*([A-Za-z_$][A-Za-z0-9_$]*|\\d+)\\s*$");
42+
private static final Pattern JS_INT_DIV_EQ = Pattern.compile("^\\s*([A-Za-z_$][A-Za-z0-9_$]*)\\s*/=\\s*([A-Za-z_$][A-Za-z0-9_$]*|\\d+)\\s*$");
43+
private static final Pattern JS_INT_DECL_ASSIGN = Pattern
44+
.compile("(?i)^\\s*(integer|int|long|short|byte)\\s+([A-Za-z_$][A-Za-z0-9_$]*)\\s*=");
45+
/** Struktogramm-Kurzschreibweisen ohne „=“: {@code zahl / 2}, Collatz {@code zahl * 3 + 1} / {@code 3 * zahl + 1}. */
46+
private static final Pattern SHORTHAND_SELF_DIV = Pattern
47+
.compile("^\\s*([A-Za-z_$][A-Za-z0-9_$]*)\\s*/\\s*([A-Za-z_$][A-Za-z0-9_$]*|\\d+)\\s*$");
48+
private static final Pattern SHORTHAND_VAR_MUL_ADD = Pattern
49+
.compile("^\\s*([A-Za-z_$][A-Za-z0-9_$]*)\\s*\\*\\s*(\\d+)\\s*\\+\\s*(\\d+)\\s*$");
50+
private static final Pattern SHORTHAND_LIT_MUL_VAR_ADD = Pattern
51+
.compile("^\\s*(\\d+)\\s*\\*\\s*([A-Za-z_$][A-Za-z0-9_$]*)\\s*\\+\\s*(\\d+)\\s*$");
3252

3353
private CodeGenRules() {
3454
}
3555

56+
/**
57+
* Vor {@code quellcodeAllerUnterelementeGenerieren(…, typJavaScript, …)} aufrufen, danach
58+
* {@link #endJavaScriptCodeGeneration()} in {@code finally}.
59+
*/
60+
public static void beginJavaScriptCodeGeneration(StruktogrammElementListe root) {
61+
JS_INTEGER_VARIABLES.set(inferIntegerVariablesForJavaScript(root));
62+
}
63+
64+
public static void endJavaScriptCodeGeneration() {
65+
JS_INTEGER_VARIABLES.remove();
66+
}
67+
68+
/**
69+
* Pro Wurzel-{@code quellcodeAllerUnterelementeGenerieren}-Lauf (verschachtelte Aufrufe per Zähler).
70+
* Ermittelt u. a. doppelte {@code int}-Deklarationen bei {@code input:} nach vorherigem {@code int zahl = …}.
71+
*/
72+
public static void enterJavaGenerationScope() {
73+
Integer d = JAVA_SCOPE_DEPTH.get();
74+
int n = d == null ? 0 : d;
75+
if (n == 0) {
76+
JAVA_DECLARED_NAMES.set(new HashSet<>());
77+
}
78+
JAVA_SCOPE_DEPTH.set(n + 1);
79+
}
80+
81+
public static void leaveJavaGenerationScope() {
82+
Integer d = JAVA_SCOPE_DEPTH.get();
83+
if (d == null) {
84+
return;
85+
}
86+
int n = d - 1;
87+
if (n <= 0) {
88+
JAVA_SCOPE_DEPTH.remove();
89+
JAVA_DECLARED_NAMES.remove();
90+
} else {
91+
JAVA_SCOPE_DEPTH.set(n);
92+
}
93+
}
94+
95+
private static Set<String> currentJavaDeclaredNames() {
96+
Set<String> s = JAVA_DECLARED_NAMES.get();
97+
return s != null ? s : Set.of();
98+
}
99+
100+
private static void registerJavaNamesFromEmitted(String block) {
101+
Set<String> s = JAVA_DECLARED_NAMES.get();
102+
if (s == null || block == null || block.isEmpty()) {
103+
return;
104+
}
105+
for (String rawLine : block.split("\\R", -1)) {
106+
String ln = rawLine.trim();
107+
if (ln.isEmpty()) {
108+
continue;
109+
}
110+
Matcher m1 = JAVA_EMITTED_TYPED_ASSIGN.matcher(ln);
111+
if (m1.find()) {
112+
s.add(m1.group(2));
113+
continue;
114+
}
115+
Matcher m2 = JAVA_EMITTED_TYPED_ASSIGN_BRACKETS_AFTER.matcher(ln);
116+
if (m2.find()) {
117+
s.add(m2.group(2));
118+
}
119+
}
120+
}
121+
122+
private static boolean javaVariableAlreadyDeclared(String simpleName) {
123+
if (simpleName == null || simpleName.isEmpty()) {
124+
return false;
125+
}
126+
return currentJavaDeclaredNames().contains(simpleName);
127+
}
128+
129+
private static Set<String> currentJsIntegerVariables() {
130+
Set<String> s = JS_INTEGER_VARIABLES.get();
131+
return s != null ? s : Set.of();
132+
}
133+
36134
public static String indent(String codeLine, int spaces) {
37135
if (spaces <= 0) {
38136
return codeLine;
@@ -46,6 +144,9 @@ public static String generateInstructionLine(String raw, int target, int indent)
46144
return "";
47145
}
48146
String translated = translateInstructionLine(line, target);
147+
if (target == CodeErzeuger.typJava) {
148+
registerJavaNamesFromEmitted(translated);
149+
}
49150
return indentMultiline(translated, indent);
50151
}
51152

@@ -91,6 +192,26 @@ private static String translateInstructionLine(String line, int target) {
91192
if (bareOutput != null) {
92193
return bareOutput;
93194
}
195+
if (target == CodeErzeuger.typJavaScript) {
196+
String shortAssign = javaScriptShorthandSelfAssignIfNeeded(line, currentJsIntegerVariables());
197+
if (shortAssign != null) {
198+
return formatCStyleStatementLine(shortAssign);
199+
}
200+
String div = javaScriptIntegerDivisionIfNeeded(line, currentJsIntegerVariables());
201+
if (div != null) {
202+
return formatCStyleStatementLine(div);
203+
}
204+
} else if (target == CodeErzeuger.typJava) {
205+
String shortAssign = javaShorthandSelfAssignIfNeeded(line);
206+
if (shortAssign != null) {
207+
return formatCStyleStatementLine(shortAssign);
208+
}
209+
} else if (target == CodeErzeuger.typPython) {
210+
String shortAssign = pythonShorthandSelfAssignIfNeeded(line);
211+
if (shortAssign != null) {
212+
return shortAssign;
213+
}
214+
}
94215
if (isCStyleTarget(target)) {
95216
return formatCStyleStatementLine(line);
96217
}
@@ -227,7 +348,15 @@ private static String javaInputTarget(InputLineParse p) {
227348
if (p.isIndexedTarget()) {
228349
return p.name;
229350
}
230-
return p.type + " " + p.name;
351+
String simple = p.name.replace(" ", "");
352+
int lb = simple.indexOf('[');
353+
if (lb >= 0) {
354+
simple = simple.substring(0, lb);
355+
}
356+
if (javaVariableAlreadyDeclared(simple)) {
357+
return p.name.replace(" ", "");
358+
}
359+
return p.type + " " + p.name.replace(" ", "");
231360
}
232361

233362
private static String jsInputTarget(InputLineParse p) {
@@ -457,16 +586,187 @@ private static boolean isSimpleIncrement(String step, String var) {
457586
|| step.matches("\\s*" + Pattern.quote(var) + "\\s*\\+=\\s*1\\s*");
458587
}
459588

589+
/** Wie Swift {@code inferIntegerVariablesForJavaScript}: für {@code Math.floor}-Division in JS. */
590+
public static Set<String> inferIntegerVariablesForJavaScript(StruktogrammElementListe root) {
591+
Set<String> out = new HashSet<>();
592+
if (root != null) {
593+
walkStruktogramForJsIntInference(root, out);
594+
}
595+
return out;
596+
}
597+
598+
private static void walkStruktogramForJsIntInference(StruktogrammElementListe list, Set<String> out) {
599+
for (StruktogrammElement e : list) {
600+
for (String line : e.text) {
601+
addFromStatementLineForJsInt(line, out);
602+
}
603+
if (e instanceof Schleife schleife) {
604+
walkStruktogramForJsIntInference(schleife.gibListe(), out);
605+
} else if (e instanceof Fallauswahl fall) {
606+
for (StruktogrammElementListe sub : fall.listen) {
607+
walkStruktogramForJsIntInference(sub, out);
608+
}
609+
}
610+
}
611+
}
612+
613+
private static void addFromStatementLineForJsInt(String raw, Set<String> set) {
614+
String t = raw != null ? raw.trim() : "";
615+
if (t.isEmpty()) {
616+
return;
617+
}
618+
String lower = t.toLowerCase();
619+
if (lower.startsWith("input:")) {
620+
InputLineParse p = parseInputLine(t);
621+
if (p != null && isDiagramIntegerType(p.type) && !p.isIndexedTarget()) {
622+
set.add(p.name);
623+
}
624+
return;
625+
}
626+
Matcher constM = CONST_TYPED.matcher(stripTrailingSemicolon(t));
627+
if (constM.matches() && isDiagramIntegerType(constM.group(1))) {
628+
set.add(constM.group(2));
629+
return;
630+
}
631+
Matcher decl = JS_INT_DECL_ASSIGN.matcher(t);
632+
if (decl.find()) {
633+
set.add(decl.group(2).trim());
634+
}
635+
}
636+
637+
private static boolean isDiagramIntegerType(String rawType) {
638+
String x = rawType != null ? rawType.trim().toLowerCase() : "";
639+
return x.equals("int") || x.equals("integer") || x.equals("long") || x.equals("short") || x.equals("byte");
640+
}
641+
642+
/**
643+
* Wie Swift {@code javaScriptIntegerDivisionIfNeeded}: einfache {@code x = x / y} bzw. {@code x /= y} für
644+
* als Ganzzahl erkannte Variablen → {@code Math.floor}.
645+
*/
646+
static String javaScriptIntegerDivisionIfNeeded(String raw, Set<String> intVariables) {
647+
if (intVariables == null || intVariables.isEmpty()) {
648+
return null;
649+
}
650+
String t = stripTrailingSemicolon(raw).trim();
651+
if (t.isEmpty()) {
652+
return null;
653+
}
654+
Matcher assign = JS_INT_ASSIGN_DIV.matcher(t);
655+
if (assign.matches()) {
656+
String lhs = assign.group(1);
657+
String a = assign.group(2);
658+
String b = assign.group(3);
659+
if (intVariables.contains(lhs) && lhs.equals(a)) {
660+
return lhs + " = Math.floor(" + a + " / " + b + ")";
661+
}
662+
return null;
663+
}
664+
Matcher divEq = JS_INT_DIV_EQ.matcher(t);
665+
if (divEq.matches()) {
666+
String lhs = divEq.group(1);
667+
String b = divEq.group(2);
668+
if (intVariables.contains(lhs)) {
669+
return lhs + " = Math.floor(" + lhs + " / " + b + ")";
670+
}
671+
}
672+
return null;
673+
}
674+
675+
/**
676+
* Diagrammzeilen ohne {@code =}: z. B. {@code zahl / 2} oder Collatz {@code zahl * 3 + 1} — werden zu gültigen
677+
* Zuweisungen (sonst kein Effekt im Browser/Interpreter).
678+
*/
679+
private static String javaScriptShorthandSelfAssignIfNeeded(String raw, Set<String> intVariables) {
680+
String t = stripTrailingSemicolon(raw).trim();
681+
if (t.isEmpty()) {
682+
return null;
683+
}
684+
Matcher mDiv = SHORTHAND_SELF_DIV.matcher(t);
685+
if (mDiv.matches()) {
686+
String lhs = mDiv.group(1);
687+
String rhs = mDiv.group(2);
688+
if (intVariables != null && intVariables.contains(lhs)) {
689+
return lhs + " = Math.floor(" + lhs + " / " + rhs + ")";
690+
}
691+
return lhs + " = " + lhs + " / " + rhs;
692+
}
693+
Matcher mMul = SHORTHAND_VAR_MUL_ADD.matcher(t);
694+
if (mMul.matches()) {
695+
String v = mMul.group(1);
696+
return v + " = " + v + " * " + mMul.group(2) + " + " + mMul.group(3);
697+
}
698+
Matcher m3 = SHORTHAND_LIT_MUL_VAR_ADD.matcher(t);
699+
if (m3.matches()) {
700+
String v = m3.group(2);
701+
return v + " = " + m3.group(1) + " * " + v + " + " + m3.group(3);
702+
}
703+
return null;
704+
}
705+
706+
private static String javaShorthandSelfAssignIfNeeded(String raw) {
707+
String t = stripTrailingSemicolon(raw).trim();
708+
if (t.isEmpty()) {
709+
return null;
710+
}
711+
Matcher mDiv = SHORTHAND_SELF_DIV.matcher(t);
712+
if (mDiv.matches()) {
713+
String lhs = mDiv.group(1);
714+
return lhs + " = " + lhs + " / " + mDiv.group(2);
715+
}
716+
Matcher mMul = SHORTHAND_VAR_MUL_ADD.matcher(t);
717+
if (mMul.matches()) {
718+
String v = mMul.group(1);
719+
return v + " = " + v + " * " + mMul.group(2) + " + " + mMul.group(3);
720+
}
721+
Matcher m3 = SHORTHAND_LIT_MUL_VAR_ADD.matcher(t);
722+
if (m3.matches()) {
723+
String v = m3.group(2);
724+
return v + " = " + m3.group(1) + " * " + v + " + " + m3.group(3);
725+
}
726+
return null;
727+
}
728+
729+
private static String pythonShorthandSelfAssignIfNeeded(String raw) {
730+
String t = stripTrailingSemicolon(raw).trim();
731+
if (t.isEmpty()) {
732+
return null;
733+
}
734+
Matcher mDiv = SHORTHAND_SELF_DIV.matcher(t);
735+
if (mDiv.matches()) {
736+
String lhs = mDiv.group(1);
737+
return lhs + " = " + lhs + " // " + mDiv.group(2);
738+
}
739+
Matcher mMul = SHORTHAND_VAR_MUL_ADD.matcher(t);
740+
if (mMul.matches()) {
741+
String v = mMul.group(1);
742+
return v + " = " + v + " * " + mMul.group(2) + " + " + mMul.group(3);
743+
}
744+
Matcher m3 = SHORTHAND_LIT_MUL_VAR_ADD.matcher(t);
745+
if (m3.matches()) {
746+
String v = m3.group(2);
747+
return v + " = " + m3.group(1) + " * " + v + " + " + m3.group(3);
748+
}
749+
return null;
750+
}
751+
752+
private static String normalizeJavaScriptMathRandomCall(String s) {
753+
Matcher m = MATH_RANDOM_NO_PARENS.matcher(s);
754+
StringBuffer out = new StringBuffer();
755+
while (m.find()) {
756+
m.appendReplacement(out, "Math.random()");
757+
}
758+
m.appendTail(out);
759+
return out.toString();
760+
}
761+
460762
private static String replaceIdentifierDotLengthWithParensInJava(String code) {
461763
Set<String> arrayNames = javaArrayDeclarationNames(code);
462764
Matcher m = JAVA_LENGTH.matcher(code);
463765
StringBuffer out = new StringBuffer();
464766
while (m.find()) {
465767
String id = m.group(1);
466768
String lower = id.toLowerCase();
467-
String replacement = JAVA_ARRAY_LIKE_LENGTH_NAMES.contains(lower) || arrayNames.contains(lower)
468-
? m.group(0)
469-
: id + ".length()";
769+
String replacement = arrayNames.contains(lower) ? m.group(0) : id + ".length()";
470770
m.appendReplacement(out, Matcher.quoteReplacement(replacement));
471771
}
472772
m.appendTail(out);
@@ -500,7 +800,7 @@ private static String javaScriptRhsFromJavaRhs(String rhs, boolean array) {
500800
if (cast.matches()) {
501801
return "Math.floor(" + javaScriptRhsFromJavaRhs(cast.group(1), false) + ")";
502802
}
503-
return t;
803+
return normalizeJavaScriptMathRandomCall(t);
504804
}
505805

506806
private static String pythonRhsFromJavaRhs(String rhs, boolean array) {

0 commit comments

Comments
 (0)