1 | sclass G22LAScriptIDE<A extends G22LAScript> is Swingable { |
2 | settable G22Utils g22utils; |
3 | settable S noScriptSelectedMsg = "Please select or create a script to edit it"; |
4 | |
5 | A script; |
6 | |
7 | transient SingleComponentPanel scp; |
8 | transient JLeftArrowScriptIDE[] ides; |
9 | transient JTabbedPane tabs; |
10 | transient JButton btnSave, btnDiscardChanges, btnClearForAutoRun; |
11 | |
12 | abstract class Mode { |
13 | G22ScriptMode modeEnum; |
14 | S name; |
15 | gettable bool editable; |
16 | |
17 | *(G22ScriptMode *modeEnum, S *name, bool *editable) {} |
18 | |
19 | simplyCached abstract IVarWithNotify<S> scriptVar(); |
20 | |
21 | void addButtons(JPanel panel) {} |
22 | |
23 | toString { ret name; } |
24 | |
25 | S tabName() { ret scriptVar().has() ? name : "Not " + firstToLower(name); } |
26 | } |
27 | |
28 | class ModeClearedForAutoRun > Mode { |
29 | *() { super(G22ScriptMode.autoRunnable, "Cleared for auto-run", false); } |
30 | |
31 | IVarWithNotify<S> scriptVar_load() { |
32 | var var = new VirtualVar<S>( |
33 | -> script.codeForAutoRun(), |
34 | null /*text -> script.setClearedForAutoRun(text == null ?: new ClearForAutoRun(text))*/); |
35 | addWeakChangeListener(script.varClearedForAutoRun(), var); |
36 | ret var; |
37 | } |
38 | |
39 | void addButtons(JPanel panel) { |
40 | panel.add(jbutton("Forget auto-run code", rThread forgetAutoRunCode)); |
41 | } |
42 | } |
43 | |
44 | class ModeSaved > Mode { |
45 | *() { super(G22ScriptMode.saved, "Saved", false); } |
46 | |
47 | IVarWithNotify<S> scriptVar_load() { |
48 | ret getterVarOnly(script.varText()); |
49 | } |
50 | |
51 | void addButtons(JPanel panel) { |
52 | panel.add(btnClearForAutoRun = jbutton("Clear for auto-run", rThread clearForAutoRun)); |
53 | panel.add(jbutton("Forget code", rThread forgetSaved)); |
54 | } |
55 | } |
56 | |
57 | class ModeEdit > Mode { |
58 | *() { super(G22ScriptMode.edit, "Edit", true); } |
59 | |
60 | IVarWithNotify<S> scriptVar_load() { |
61 | var var = new VirtualVar<S>( |
62 | -> script.textForEditing(), |
63 | text -> script.receiveEditingText(text) |
64 | ); |
65 | addWeakChangeListener(script, var); |
66 | ret var; |
67 | } |
68 | |
69 | void addButtons(JPanel panel) { |
70 | panel.add(btnSave = jbutton("Save", rThread saveEdit)); |
71 | panel.add(btnDiscardChanges = jbutton("Discard changes", rThread discardEdit)); |
72 | } |
73 | } |
74 | |
75 | transient new ModeEdit modeEdit; |
76 | transient new ModeSaved modeSaved; |
77 | transient new ModeClearedForAutoRun modeClearedForAutoRun; |
78 | transient L<Mode> modes = ll( |
79 | modeEdit, |
80 | modeSaved, |
81 | modeClearedForAutoRun |
82 | ); |
83 | |
84 | *(G22Utils *g22utils) {} |
85 | |
86 | cachedVisualize { |
87 | ides = new JLeftArrowScriptIDE[l(modes)]; |
88 | if (scp == null) scp = singleComponentPanel(); |
89 | loadScript(script); |
90 | ret scp; |
91 | } |
92 | |
93 | void setScript(A script) { |
94 | if (this.script != script) |
95 | if (this.script != null) |
96 | fail("Can't set script after initialisation"); |
97 | else |
98 | loadScript(script); |
99 | } |
100 | |
101 | void loadScript(A script) { |
102 | this.script = script; |
103 | if (scp == null) ret; |
104 | if (script == null) |
105 | scp.set(jcenteredlabel(noScriptSelectedMsg())); |
106 | else { |
107 | tabs = jtabs(); |
108 | |
109 | // This places tabs vertically at the right hand side... |
110 | // (not what we want) |
111 | //setTabPlacement(JTabbedPane.RIGHT, tabs); |
112 | |
113 | for (int i, Mode mode : unpair iterateWithIndex(modes)) { |
114 | var ide = ides[i] = g22utils.leftArrowIDE(); |
115 | ide.wrapSection = c -> wrapEditorSection(mode, c); |
116 | ide.newCompileResult = -> script.newCompileResult(); |
117 | ide.makeParser = -> script.makeParser(); |
118 | modifyIDE(ide); |
119 | var varScript = mode.scriptVar(); |
120 | ide.lvScript(varWithNotifyToLiveValue(S.class, varScript)); |
121 | addTab(tabs, str(mode)); |
122 | mode.addButtons(ide.buttons()); |
123 | ide.visualize(); |
124 | ide.setEditable(mode.editable()); |
125 | varScript.onChangeAndNow(text -> |
126 | setTab(tabs, i, text == null ? jcenteredlabel("Empty") : wrapIDE(mode, ide)); |
127 | ide.popDownButton.onFillingMenu(menu -> |
128 | addMenuItem(menu, "Show History", rThread showHistory)); |
129 | } |
130 | |
131 | script.onChangeAndNow(r { |
132 | for (int i, Mode mode : unpair iterateWithIndex(modes)) |
133 | setTabTitle(tabs, i, mode.tabName()); |
134 | setEnabled(script.isEditing(), btnSave, btnDiscardChanges); |
135 | setEnabled(btnClearForAutoRun, script.isSavedDistinctFromAutoRunVersion()); |
136 | }); |
137 | |
138 | setMode(script.isEditing() ? modeEdit : modeSaved); |
139 | scp.set(tabs); |
140 | } |
141 | } |
142 | |
143 | swappable JComponent wrapEditorSection(Mode mode, JComponent editorSection) { |
144 | if (mode == modeSaved) |
145 | ret withTopMargin(northAndCenterWithMargin( |
146 | withRightMargin(jline(jbutton("Edit script", -> setMode(modeEdit)))), |
147 | editorSection)); |
148 | else |
149 | ret editorSection; |
150 | } |
151 | |
152 | swappable JComponent wrapIDE(Mode mode, JLeftArrowScriptIDE ide) { |
153 | ret ide.visualize(); |
154 | } |
155 | |
156 | event settingUpIDE(JLeftArrowScriptIDE ide); |
157 | |
158 | swappable void modifyIDE(JLeftArrowScriptIDE ide) { |
159 | ide.showTitle(false); |
160 | settingUpIDE(ide); |
161 | } |
162 | |
163 | void saveEdit { |
164 | script.completeEdit(); |
165 | setMode(modeSaved); |
166 | } |
167 | |
168 | JLeftArrowScriptIDE ide(Mode mode) { |
169 | if (ides == null) visualize(); |
170 | ret _get(ides, indexOf(modes, mode)); |
171 | } |
172 | |
173 | void setMode(Mode mode) { |
174 | selectTab(tabs, indexOf(modes, mode)); |
175 | } |
176 | |
177 | void discardEdit { |
178 | setMode(modeSaved); |
179 | script.editingText(null); |
180 | } |
181 | |
182 | void forgetSaved { |
183 | script.setTextWithHistory(null); |
184 | } |
185 | |
186 | void clearForAutoRun { |
187 | script.clearForAutoRun(); |
188 | setMode(modeClearedForAutoRun); |
189 | } |
190 | |
191 | void forgetAutoRunCode { |
192 | script.forgetAutoRunCode(); |
193 | } |
194 | |
195 | private void selfTest_impl { |
196 | new G22LAScript script; |
197 | setScript((A) script); |
198 | ide(modeEdit).setText("hello"); |
199 | assertEqualsVerbose(null, script.text()); |
200 | assertEqualsVerbose("hello", script.editedText()); |
201 | saveEdit(); |
202 | assertEqualsVerbose("hello", script.text()); |
203 | assertEqualsVerbose(null, script.editedText()); |
204 | } |
205 | |
206 | static void selfTest(G22Utils g22utils) { |
207 | new G22LAScriptIDE(g22utils).selfTest_impl(); |
208 | } |
209 | |
210 | void showHistory { |
211 | showText("Edit history of " + script, loadTextFile(script.historyFile())); |
212 | } |
213 | } |
Began life as a copy of #1034345
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1034760 |
Snippet name: | G22LAScriptIDE - IDE for a G22LAScript (with the 3 modes, backup with old layout) |
Eternal ID of this version: | #1034760/1 |
Text MD5: | f7d050d5ffaf7e2432b6abcd5c7726a3 |
Author: | stefan |
Category: | javax / gazelle 22 |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-03-08 05:34:08 |
Source code size: | 6306 bytes / 213 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 170 / 156 |
Referenced in: | [show references] |