-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathFxDockSchema.java
397 lines (334 loc) · 8.08 KB
/
FxDockSchema.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// Copyright © 2016-2025 Andy Goryachev <[email protected]>
package goryachev.fxdock;
import goryachev.common.log.Log;
import goryachev.common.util.ASettingsStore;
import goryachev.common.util.SB;
import goryachev.common.util.SStream;
import goryachev.fx.settings.FxSettingsSchema;
import goryachev.fx.settings.WindowMonitor;
import goryachev.fxdock.internal.DockTools;
import goryachev.fxdock.internal.FxDockEmptyPane;
import goryachev.fxdock.internal.FxDockRootPane;
import goryachev.fxdock.internal.FxDockSplitPane;
import goryachev.fxdock.internal.FxDockTabPane;
import javafx.application.Platform;
import javafx.geometry.Orientation;
import javafx.scene.Node;
import javafx.stage.Stage;
import javafx.stage.Window;
/**
* FxDock framework schema for the layout storage.
*/
public abstract class FxDockSchema
extends FxSettingsSchema
{
@Override
public abstract Stage createWindow(String name);
@Override
public abstract Stage createDefaultWindow();
public abstract FxDockPane createPane(String type);
//
private static final String NAME_PANE = ".P";
private static final String NAME_TAB = ".T";
private static final String NAME_SPLIT = ".S";
private static final String SUFFIX_CONTENT = ".layout";
private static final String SUFFIX_SELECTED_TAB = ".tab";
private static final String SUFFIX_SPLITS = ".splits";
private static final String TYPE_EMPTY = "E";
private static final String TYPE_PANE = "P";
private static final String TYPE_HSPLIT = "H";
private static final String TYPE_VSPLIT = "V";
private static final String TYPE_TAB = "T";
private static final Log log = Log.get("FxDockSchema");
public FxDockSchema(ASettingsStore s)
{
super(s);
}
@Override
protected void restoreWindowLocal(Window w, WindowMonitor m)
{
super.restoreWindowLocal(w, m);
if(w instanceof FxDockWindow dw)
{
String prefix = FX_PREFIX + m.getID();
// if(dw.getContent() == null)
{
Node n = loadDockWindowContent(prefix);
if(n != null)
{
dw.setContent(n);
restoreContent(prefix, n);
}
}
}
}
@Override
protected void storeWindowLocal(Window w, WindowMonitor m)
{
super.storeWindowLocal(w, m);
if(w instanceof FxDockWindow dw)
{
String prefix = FX_PREFIX + m.getID();
Node n = dw.getContent();
saveDockWindowContent(prefix, n);
storeContent(prefix, n);
}
}
protected Node loadDockWindowContent(String prefix)
{
SStream s = store().getStream(prefix + SUFFIX_CONTENT);
return loadContentRecursively(s);
}
protected void saveDockWindowContent(String prefix, Node n)
{
SStream s = new SStream();
saveContentRecursively(s, n);
store().setStream(prefix + SUFFIX_CONTENT, s);
}
protected FxDockSplitPane loadSplit(SStream s, Orientation or)
{
FxDockSplitPane sp = new FxDockSplitPane();
sp.setOrientation(or);
int sz = s.nextInt();
for(int i=0; i<sz; i++)
{
Node ch = loadContentRecursively(s);
sp.addPane(ch);
}
return sp;
}
protected Node loadContentRecursively(SStream s)
{
String t = s.nextString();
if(t == null)
{
return null;
}
else if(TYPE_PANE.equals(t))
{
String type = s.nextString();
return createPane(type);
}
else if(TYPE_HSPLIT.equals(t))
{
return loadSplit(s, Orientation.HORIZONTAL);
}
else if(TYPE_VSPLIT.equals(t))
{
return loadSplit(s, Orientation.VERTICAL);
}
else if(TYPE_TAB.equals(t))
{
FxDockTabPane tp = new FxDockTabPane();
int sz = s.nextInt();
for(int i=0; i<sz; i++)
{
Node ch = loadContentRecursively(s);
tp.addTab(ch);
}
return tp;
}
else if(TYPE_EMPTY.equals(t))
{
return new FxDockEmptyPane();
}
else
{
return null;
}
}
protected void saveContentRecursively(SStream s, Node n)
{
if(n == null)
{
return;
}
else if(n instanceof FxDockPane p)
{
String type = p.getDockPaneType();
s.add(TYPE_PANE);
s.add(type);
}
else if(n instanceof FxDockSplitPane p)
{
int ct = p.getPaneCount();
Orientation or = p.getOrientation();
s.add(or == Orientation.HORIZONTAL ? TYPE_HSPLIT : TYPE_VSPLIT);
s.add(ct);
for(Node ch: p.getPanes())
{
saveContentRecursively(s, ch);
}
}
else if(n instanceof FxDockTabPane p)
{
int ct = p.getTabCount();
s.add(TYPE_TAB);
s.add(ct);
for(Node ch: p.getPanes())
{
saveContentRecursively(s, ch);
}
}
else if(n instanceof FxDockEmptyPane p)
{
s.add(TYPE_EMPTY);
}
else
{
throw new Error("?" + n);
}
}
protected String getPath(String prefix, Node n, String suffix)
{
SB sb = new SB(128);
sb.append(prefix);
getPathRecursive(sb, n);
if(suffix != null)
{
sb.a(suffix);
}
return sb.toString();
}
protected void getPathRecursive(SB sb, Node node)
{
Node n = DockTools.getParent(node);
if(n != null)
{
getPathRecursive(sb, n);
if(n instanceof FxDockSplitPane p)
{
int ix = p.indexOfPane(node);
sb.a('.');
sb.a(ix);
}
else if(n instanceof FxDockTabPane p)
{
int ix = p.indexOfTab(node);
sb.a('.');
sb.a(ix);
}
}
if(node instanceof FxDockRootPane)
{
return;
}
else if(node instanceof FxDockSplitPane)
{
sb.a(NAME_SPLIT);
}
else if(node instanceof FxDockTabPane)
{
sb.a(NAME_TAB);
}
else if(node instanceof FxDockPane)
{
sb.a(NAME_PANE);
}
else
{
throw new Error("?" + node);
}
}
/**
* default functionality provided by the docking framework to load window content settings.
* what's being stored:
* - split positions
* - settings bindings
*/
protected void restoreContent(String prefix, Node n)
{
if(n != null)
{
if(n instanceof FxDockPane p)
{
//loadPaneSettings(prefix, p);
}
else if(n instanceof FxDockSplitPane p)
{
for(Node ch: p.getPanes())
{
restoreContent(prefix, ch);
}
// because of the split pane idiosyncrasy with layout
Platform.runLater(() -> loadSplitPaneSettings(prefix, p));
}
else if(n instanceof FxDockTabPane p)
{
loadTabPaneSettings(prefix, p);
for(Node ch: p.getPanes())
{
restoreContent(prefix, ch);
}
}
restoreNode(n);
}
}
/**
* default functionality provided by the docking framework to store window content settings.
* what's being stored:
* - split positions
* - settings bindings
*/
protected void storeContent(String prefix, Node n)
{
if(n != null)
{
storeNode(n);
if(n instanceof FxDockPane p)
{
//savePaneSettings(prefix, p);
}
else if(n instanceof FxDockSplitPane p)
{
saveSplitPaneSettings(prefix, p);
for(Node ch: p.getPanes())
{
storeContent(prefix, ch);
}
}
else if(n instanceof FxDockTabPane p)
{
saveTabPaneSettings(prefix, p);
for(Node ch: p.getPanes())
{
storeContent(prefix, ch);
}
}
}
}
protected void saveSplitPaneSettings(String prefix, FxDockSplitPane p)
{
double[] divs = p.getDividerPositions();
SStream s = new SStream();
s.addAll(divs);
String k = getPath(prefix, p, SUFFIX_SPLITS);
store().setStream(k, s);
}
protected void loadSplitPaneSettings(String prefix, FxDockSplitPane p)
{
String k = getPath(prefix, p, SUFFIX_SPLITS);
SStream s = store().getStream(k);
int ct = s.size();
if(p.getDividers().size() == ct)
{
for(int i=0; i<ct; i++)
{
double pos = s.nextDouble();
p.setDividerPosition(i, pos);
}
}
}
protected void saveTabPaneSettings(String prefix, FxDockTabPane p)
{
int ix = p.getSelectedTabIndex();
String k = getPath(prefix, p, SUFFIX_SELECTED_TAB);
store().setInt(k, ix);
}
protected void loadTabPaneSettings(String prefix, FxDockTabPane p)
{
String k = getPath(prefix, p, SUFFIX_SELECTED_TAB);
int ix = store().getInt(k, 0);
p.select(ix);
}
}