-
-
Notifications
You must be signed in to change notification settings - Fork 818
Expand file tree
/
Copy pathSettings.java
More file actions
635 lines (509 loc) · 18 KB
/
Settings.java
File metadata and controls
635 lines (509 loc) · 18 KB
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/*
Copyright 2014-2024 Will Winder
This file is part of Universal Gcode Sender (UGS).
UGS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
UGS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with UGS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.willwinder.universalgcodesender.utils;
import com.willwinder.universalgcodesender.connection.ConnectionDriver;
import com.willwinder.universalgcodesender.model.Axis;
import com.willwinder.universalgcodesender.model.Position;
import com.willwinder.universalgcodesender.model.UnitUtils;
import com.willwinder.universalgcodesender.model.UnitUtils.Units;
import com.willwinder.universalgcodesender.types.FxSettings;
import com.willwinder.universalgcodesender.types.Macro;
import com.willwinder.universalgcodesender.types.WindowSettings;
import org.apache.commons.lang3.StringUtils;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.logging.Logger;
public class Settings {
private static final Logger logger = Logger.getLogger(Settings.class.getName());
// Transient, don't serialize or deserialize.
transient private SettingChangeListener listener = null;
transient public static int HISTORY_SIZE = 10;
private String firmwareVersion = "GRBL";
private String fileName = System.getProperty("user.home");
// Welcome screen
private Deque<String> fileHistory = new ArrayDeque<>();
private Deque<String> dirHistory = new ArrayDeque<>();
// Connection
private String port = "";
private String portRate = "115200";
// Jogging / JogService
private boolean manualModeEnabled = false;
private double manualModeStepSize = 1;
private boolean useZStepSize = true;
private boolean showABCStepSize = true;
private double zJogStepSize = 1;
private double abcJogStepSize = 1;
private double jogFeedRate = 100;
// Console
private boolean scrollWindowEnabled = true;
private boolean verboseOutputEnabled = false;
private boolean commandTableEnabled = false;
// Sender Settings
private WindowSettings mainWindowSettings = new WindowSettings(0,0,640,520);
private WindowSettings visualizerWindowSettings = new WindowSettings(0,0,640,480);
private boolean singleStepMode = false;
private boolean statusUpdatesEnabled = true;
private int statusUpdateRate = 200;
private Units preferredUnits = Units.MM;
private Set<Axis> disabledAxes = new HashSet<>();
private boolean showMachinePosition = false;
private boolean showNightlyWarning = true;
private boolean showSerialPortWarning = true;
private boolean autoStartPendant = false;
private int pendantPort = 8080;
private boolean autoConnect = false;
private boolean autoReconnect = false;
private final AutoLevelSettings autoLevelSettings = new AutoLevelSettings();
private FileStats fileStats = new FileStats();
//vvv deprecated fields, still here to not break the old save files
// Transient, don't serialize or deserialize.
transient private String customGcode1 = null;
transient private String customGcode2 = null;
transient private String customGcode3 = null;
transient private String customGcode4 = null;
transient private String customGcode5 = null;
//^^^ deprecated fields, still here to not break the old save files
private Map<Integer, Macro> macros = new HashMap<>();
private String language = "en_US";
private String connectionDriver;
/**
* A directory with gcode files for easy access through pendant
*/
private String workspaceDirectory;
/**
* The safety height to clear when returning to home given in mm.
*/
private Double safetyHeight = 5d;
/**
* If the mouse zoom should be inverted
*/
private boolean invertMouseZoom = false;
/**
* Should show an information dialog if the current language isn't
* fully translated
*/
private boolean showTranslationsWarning = true;
/**
* The last working directory used by the file browser
*/
private String lastWorkingDirectory = System.getProperty("user.home");
private FxSettings fxSettings = new FxSettings();
private String machineDecimalFormat = "0.000";
/**
* The GSON deserialization doesn't do anything beyond initialize what's in the json document. Call finalizeInitialization() before using the Settings.
*/
public Settings() {
logger.fine("Initializing...");
// Initialize macros with a default macro
macros.put(1, new Macro(UUID.randomUUID().toString(), "Go to XY zero", null, "G90 X0 Y0;"));
}
/**
* Null legacy fields and move data to current data structures
*/
public void finalizeInitialization() {
if (customGcode1 != null) {
updateMacro("customGcode1", 1, null, null, customGcode1);
customGcode1 = null;
}
if (customGcode2 != null) {
updateMacro("customGcode2",2, null, null, customGcode2);
customGcode2 = null;
}
if (customGcode3 != null) {
updateMacro("customGcode3",3, null, null, customGcode3);
customGcode3 = null;
}
if (customGcode4 != null) {
updateMacro("customGcode4", 4, null, null, customGcode4);
customGcode4 = null;
}
if (customGcode5 != null) {
updateMacro("customGcode5", 5, null, null, customGcode5);
customGcode5 = null;
}
}
/**
* This method should only be called once during setup, a runtime exception
* will be thrown if that contract is violated.
*/
public void setSettingChangeListener(SettingChangeListener listener) {
this.listener = listener;
if (this.autoLevelSettings != null) {
autoLevelSettings.setSettingChangeListener(listener);
}
}
private void changed() {
if (listener != null) {
listener.settingChanged();
}
}
public String getFirmwareVersion() {
return firmwareVersion;
}
public void setFirmwareVersion(String firmwareVersion) {
if (StringUtils.equals(firmwareVersion, this.firmwareVersion)) return;
this.firmwareVersion = firmwareVersion;
changed();
}
public String getLastOpenedFilename() {
return fileName;
}
public void setLastOpenedFilename(String absolutePath) {
Path p = Paths.get(absolutePath).toAbsolutePath();
this.fileName = p.toString();
updateRecentFiles(p.toString());
updateRecentDirectory(p.getParent().toString());
changed();
}
public Collection<String> getRecentFiles() {
return Collections.unmodifiableCollection(fileHistory);
}
public void updateRecentFiles(String absolutePath) {
updateRecent(this.fileHistory, HISTORY_SIZE, absolutePath);
}
public Collection<String> getRecentDirectories() {
return Collections.unmodifiableCollection(dirHistory);
}
public void updateRecentDirectory(String absolutePath) {
updateRecent(this.dirHistory, HISTORY_SIZE, absolutePath);
}
private static void updateRecent(Deque<String> stack, int maxSize, String element) {
stack.remove(element);
stack.push(element);
while( stack.size() > maxSize)
stack.removeLast();
}
public String getPort() {
return port;
}
public void setPort(String port) {
if (StringUtils.equals(port, this.port)) return;
this.port = port;
changed();
}
public String getPortRate() {
return portRate;
}
public void setPortRate(String portRate) {
if (StringUtils.equals(portRate, this.portRate)) return;
this.portRate = portRate;
changed();
}
public boolean isManualModeEnabled() {
return manualModeEnabled;
}
public void setManualModeEnabled(boolean manualModeEnabled) {
this.manualModeEnabled = manualModeEnabled;
changed();
}
public double getManualModeStepSize() {
return manualModeStepSize;
}
public void setManualModeStepSize(double manualModeStepSize) {
this.manualModeStepSize = manualModeStepSize;
changed();
}
public boolean useZStepSize() {
return this.useZStepSize;
}
public void setUseZStepSize(boolean useZStepSize) {
this.useZStepSize = useZStepSize;
changed();
}
public boolean showABCStepSize() {
return this.showABCStepSize;
}
public void setShowABCStepSize(boolean showABCStepSize) {
this.showABCStepSize = showABCStepSize;
changed();
}
public double getZJogStepSize() {
return zJogStepSize;
}
public void setZJogStepSize(double zJogStepSize) {
this.zJogStepSize = zJogStepSize;
changed();
}
public double getABCJogStepSize() {
return abcJogStepSize;
}
public void setABCJogStepSize(double abcJogStepSize) {
this.abcJogStepSize = abcJogStepSize;
changed();
}
public double getJogFeedRate() {
return jogFeedRate;
}
public void setJogFeedRate(double jogFeedRate) {
this.jogFeedRate = jogFeedRate;
changed();
}
public boolean isScrollWindowEnabled() {
return scrollWindowEnabled;
}
public void setScrollWindowEnabled(boolean scrollWindowEnabled) {
this.scrollWindowEnabled = scrollWindowEnabled;
changed();
}
public boolean isVerboseOutputEnabled() {
return verboseOutputEnabled;
}
public void setVerboseOutputEnabled(boolean verboseOutputEnabled) {
this.verboseOutputEnabled = verboseOutputEnabled;
changed();
}
public boolean isCommandTableEnabled() {
return commandTableEnabled;
}
public void setCommandTableEnabled(boolean enabled) {
commandTableEnabled = enabled;
changed();
}
public WindowSettings getMainWindowSettings() {
return this.mainWindowSettings;
}
public void setMainWindowSettings(WindowSettings ws) {
this.mainWindowSettings = ws;
changed();
}
public void setFxSettings(FxSettings fxSettings) {
this.fxSettings = fxSettings;
changed();
}
public FxSettings getFxSettings() {
return this.fxSettings;
}
public WindowSettings getVisualizerWindowSettings() {
return this.visualizerWindowSettings;
}
public void setVisualizerWindowSettings(WindowSettings vw) {
this.visualizerWindowSettings = vw;
changed();
}
public boolean isSingleStepMode() {
return singleStepMode;
}
public void setSingleStepMode(boolean singleStepMode) {
this.singleStepMode = singleStepMode;
changed();
}
public boolean isStatusUpdatesEnabled() {
return statusUpdatesEnabled;
}
public void setStatusUpdatesEnabled(boolean statusUpdatesEnabled) {
this.statusUpdatesEnabled = statusUpdatesEnabled;
changed();
}
public int getStatusUpdateRate() {
return statusUpdateRate;
}
public void setStatusUpdateRate(int statusUpdateRate) {
this.statusUpdateRate = statusUpdateRate;
changed();
}
public Units getPreferredUnits() {
return (preferredUnits == null) ? Units.MM : preferredUnits;
}
public boolean isAxisEnabled(Axis a) {
return !this.disabledAxes.contains(a);
}
public void setAxisEnabled(Axis a, boolean enabled) {
if (enabled ? this.disabledAxes.remove(a) : this.disabledAxes.add(a)) {
changed();
}
}
public boolean isShowMachinePosition() {
return showMachinePosition;
}
public void setShowMachinePosition(boolean showMachinePosition) {
this.showMachinePosition = showMachinePosition;
changed();
}
public void setPreferredUnits(Units units) {
if (units != null) {
double scaleUnits = UnitUtils.scaleUnits(preferredUnits, units);
preferredUnits = units;
changed();
// Change
setManualModeStepSize(manualModeStepSize * scaleUnits);
setZJogStepSize(zJogStepSize * scaleUnits);
setJogFeedRate(Math.round(jogFeedRate * scaleUnits));
}
}
public boolean isShowNightlyWarning() {
return showNightlyWarning;
}
public void setShowNightlyWarning(boolean showNightlyWarning) {
this.showNightlyWarning = showNightlyWarning;
changed();
}
public boolean isShowSerialPortWarning() {
return showSerialPortWarning;
}
public void setShowSerialPortWarning(boolean showSerialPortWarning) {
this.showSerialPortWarning = showSerialPortWarning;
changed();
}
public List<Macro> getMacros() {
return Collections.unmodifiableList(new ArrayList<>(macros.values()));
}
public void updateMacro(String uuid, Integer index, String name, String description, String gcode) {
if (gcode == null) {
macros.remove(index);
} else {
if (name == null) {
name = index.toString();
}
macros.put(index, new Macro(uuid, name, description, gcode));
}
changed();
}
public String getLanguage() {
// "zh_CHS" is a legacy format that has been renamed.
if ("zh_CHS".equals(this.language)) {
this.language = "zh_CN";
}
return this.language;
}
public void setLanguage (String language) {
this.language = language;
changed();
}
public boolean isAutoConnectEnabled() {
return autoConnect;
}
public boolean isAutoReconnect() {
return autoReconnect;
}
public void setAutoLevelSettings(AutoLevelSettings settings) {
if (! settings.equals(this.autoLevelSettings)) {
this.autoLevelSettings.apply(settings);
changed();
}
}
public AutoLevelSettings getAutoLevelSettings() {
return this.autoLevelSettings;
}
public void setFileStats(FileStats settings) {
this.fileStats = settings;
changed();
}
public FileStats getFileStats() {
return this.fileStats;
}
public ConnectionDriver getConnectionDriver() {
if (StringUtils.isNotEmpty(this.connectionDriver)) {
try {
return ConnectionDriver.valueOf(this.connectionDriver);
} catch (IllegalArgumentException | NullPointerException ignored) {
// Never mind, we'll use the default
}
}
return ConnectionDriver.JSERIALCOMM;
}
public void setConnectionDriver(ConnectionDriver connectionDriver) {
this.connectionDriver = connectionDriver.name();
changed();
}
public void setAutoStartPendant(boolean autoStartPendant) {
this.autoStartPendant = autoStartPendant;
changed();
}
public boolean isAutoStartPendant() {
return this.autoStartPendant;
}
public void setWorkspaceDirectory(String workspaceDirectory) {
this.workspaceDirectory = workspaceDirectory;
}
public String getWorkspaceDirectory() {
return this.workspaceDirectory;
}
public void addMacro(Macro macro) {
int newIndex = macros.keySet().stream().max(Integer::compareTo).orElse(0) + 1;
macros.put(newIndex, macro);
changed();
}
public void setMacros(List<Macro> macros) {
this.macros.clear();
macros.forEach(this::addMacro);
changed();
}
public double getSafetyHeight() {
return this.safetyHeight;
}
public void setSafetyHeight(double safetyHeight) {
this.safetyHeight = safetyHeight;
}
public int getPendantPort() {
return pendantPort;
}
public void setPendantPort(int pendantPort) {
this.pendantPort = pendantPort;
changed();
}
public boolean isInvertMouseZoom() {
return invertMouseZoom;
}
public void setInvertMouseZoom(boolean invertMouseZoom) {
this.invertMouseZoom = invertMouseZoom;
changed();
}
public boolean isShowTranslationsWarning() {
return showTranslationsWarning;
}
public void setShowTranslationsWarning(boolean showTranslationsWarning) {
this.showTranslationsWarning = showTranslationsWarning;
}
public String getLastWorkingDirectory() {
return lastWorkingDirectory;
}
public void setLastWorkingDirectory(String lastWorkingDirectory) {
this.lastWorkingDirectory = lastWorkingDirectory;
}
public String getMachineDecimalFormat() {
return this.machineDecimalFormat;
}
public void setMachineDecimalFormat(String aValue) {
this.machineDecimalFormat=aValue;
this.changed();
}
public static class FileStats {
public Position minCoordinate;
public Position maxCoordinate;
public long numCommands;
public FileStats() {
this.minCoordinate = new Position(0, 0, 0, Units.MM);
this.maxCoordinate = new Position(0, 0, 0, Units.MM);
this.numCommands = 0;
}
public FileStats(Position min, Position max, long num) {
this.minCoordinate = min;
this.maxCoordinate = max;
this.numCommands = num;
}
}
}