-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathPreferencesWindow.java
More file actions
571 lines (503 loc) · 20.5 KB
/
PreferencesWindow.java
File metadata and controls
571 lines (503 loc) · 20.5 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
/**
*
*/
package replicatorg.app.ui;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Vector;
import java.util.logging.Level;
import javax.swing.AbstractAction;
import javax.swing.ButtonGroup;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import net.miginfocom.swing.MigLayout;
import replicatorg.app.Base;
import replicatorg.app.Base.InitialOpenBehavior;
import replicatorg.app.util.PythonUtils;
import replicatorg.app.util.SwingPythonSelector;
import replicatorg.machine.MachineInterface;
import replicatorg.machine.model.MachineType;
import replicatorg.uploader.FirmwareUploader;
/**
* Edit the major preference settings.
* @author phooky
*
*/
public class PreferencesWindow extends JFrame implements GuiConstants {
// gui elements
// the calling editor, so updates can be applied
MainWindow editor;
JFormattedTextField fontSizeField;
JTextField firmwareUpdateUrlField;
JTextField logPathField;
private void showCurrentSettings() {
Font editorFont = Base.getFontPref("editor.font","Monospaced,plain,12");
fontSizeField.setText(String.valueOf(editorFont.getSize()));
String firmwareUrl = Base.preferences.get("replicatorg.updates.url", FirmwareUploader.DEFAULT_UPDATES_URL);
firmwareUpdateUrlField.setText(firmwareUrl);
String logPath = Base.preferences.get("replicatorg.logpath", "");
logPathField.setText(logPath);
}
private JCheckBox addCheckboxForPref(Container c, String text, final String pref, boolean defaultVal) {
JCheckBox cb = new JCheckBox(text);
cb.setSelected(Base.preferences.getBoolean(pref,defaultVal));
c.add(cb,"wrap");
cb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JCheckBox box = (JCheckBox)e.getSource();
Base.preferences.putBoolean(pref,box.isSelected());
}
});
return cb;
}
private void addInitialFilePrefs(Container c) {
final String prefName = "replicatorg.initialopenbehavior";
int defaultBehavior = InitialOpenBehavior.OPEN_LAST.ordinal();
int ordinal = Base.preferences.getInt(prefName, defaultBehavior);
if (ordinal >= InitialOpenBehavior.values().length) {
ordinal = defaultBehavior;
}
final InitialOpenBehavior openBehavior = InitialOpenBehavior.values()[ordinal];
ButtonGroup bg = new ButtonGroup();
class RadioAction extends AbstractAction {
private InitialOpenBehavior behavior;
public RadioAction(String text, InitialOpenBehavior behavior) {
super(text);
this.behavior = behavior;
}
public void actionPerformed(ActionEvent e) {
Base.preferences.putInt(prefName,behavior.ordinal());
}
}
c.add(new JLabel("On ReplicatorG launch:"),"split");
// We don't have SELECTED_KEY in Java 1.5, so we'll do things the old-fashioned, ugly way.
JRadioButton b;
b = new JRadioButton(new RadioAction("Open last opened or saved file",InitialOpenBehavior.OPEN_LAST));
if (InitialOpenBehavior.OPEN_LAST == openBehavior) { b.setSelected(true); }
bg.add(b);
c.add(b,"split");
b = new JRadioButton(new RadioAction("Open new file",InitialOpenBehavior.OPEN_NEW));
if (InitialOpenBehavior.OPEN_NEW == openBehavior) { b.setSelected(true); }
bg.add(b);
c.add(b,"wrap 10px");
}
JComboBox makeDebugLevelDropdown() {
String levelName = Base.preferences.get("replicatorg.debuglevel", Level.INFO.getName());
Level l = Level.parse(levelName);
if (l == null) { l = Level.INFO; }
Vector<Level> levels = new Vector<Level>();
levels.add(Level.ALL);
levels.add(Level.FINEST);
levels.add(Level.FINER);
levels.add(Level.FINE);
levels.add(Level.INFO);
levels.add(Level.WARNING);
final ComboBoxModel model = new DefaultComboBoxModel(levels);
model.setSelectedItem(l);
JComboBox cb = new JComboBox(model);
cb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Level level = (Level)(model.getSelectedItem());
Base.preferences.put("replicatorg.debuglevel", level.getName());
Base.logger.setLevel(level);
}
});
return cb;
}
// Copied from app.ui.controlpanel.ExtruderPanel
private double confirmTemperature(double target, String limitPrefName, double defaultLimit) {
double limit = Base.preferences.getDouble("temperature.acceptedLimit", defaultLimit);
if (target > limit){
// Temperature warning dialog!
int n = JOptionPane.showConfirmDialog(this,
"<html>Setting the temperature to <b>" + Double.toString(target) + "\u00b0C</b> may<br>"+
"involve health and/or safety risks or cause damage to your machine!<br>"+
"The maximum recommended temperature is <b>"+Double.toString(limit)+"</b>.<br>"+
"Do you accept the risk and still want to set this temperature?",
"Danger",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
if (n == JOptionPane.YES_OPTION) {
return target;
} else if (n == JOptionPane.NO_OPTION) {
return Double.MIN_VALUE;
} else { // Cancel or whatnot
return Double.MIN_VALUE;
}
} else {
return target;
}
}
/**
*
* @param driver Needed for the Replicator-specific options
*/
public PreferencesWindow(final MachineInterface machine) {
super("Preferences");
setResizable(true);
Image icon = Base.getImage("images/icon.gif", this);
setIconImage(icon);
JTabbedPane prefTabs = new JTabbedPane();
JPanel basic = new JPanel();
// Container content = this.getContentPane();
Container content = basic;
content.setLayout(new MigLayout("fill"));
content.add(new JLabel("MainWindow font size: "), "split");
fontSizeField = new JFormattedTextField(Base.getLocalFormat());
fontSizeField.setColumns(4);
content.add(fontSizeField);
content.add(new JLabel(" (requires restart of ReplicatorG)"), "wrap");
boolean checkTempDuringBuild = Base.preferences.getBoolean("build.monitor_temp", true);
addCheckboxForPref(content,"Monitor temperature during builds","build.monitor_temp", checkTempDuringBuild);
addCheckboxForPref(content,"Automatically connect to machine at startup","replicatorg.autoconnect",true);
addCheckboxForPref(content,"Show experimental machine profiles","machine.showExperimental",false);
addCheckboxForPref(content,"Review GCode for potential toolhead problems before building","build.safetyChecks",true);
addCheckboxForPref(content,"Break Z motion into separate moves (normally false)","replicatorg.parser.breakzmoves",false);
addCheckboxForPref(content,"Show starfield in model preview window","ui.show_starfield",false);
addCheckboxForPref(content,"Notifications in System tray","ui.preferSystemTrayNotifications",false);
addCheckboxForPref(content,"Automatically regenerate gcode when building from model view.","build.autoGenerateGcode",true);
addCheckboxForPref(content,"Use native avrdude for uploading code","uploader.useNative",false);
JPanel advanced = new JPanel();
content = advanced;
content.setLayout(new MigLayout("fill"));
JButton modelColorButton;
modelColorButton = new JButton("Choose model color");
modelColorButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Note that this color is also defined in EditingModel.java
Color modelColor = new Color(Base.preferences.getInt("ui.modelColor",-19635));
modelColor = JColorChooser.showDialog(
null,
"Choose Model Color",
modelColor);
if(modelColor == null)
return;
Base.preferences.putInt("ui.modelColor", modelColor.getRGB());
Base.getEditor().refreshPreviewPanel();
}
});
modelColorButton.setVisible(true);
content.add(modelColorButton,"split");
JButton backgroundColorButton;
backgroundColorButton = new JButton("Choose background color");
backgroundColorButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Note that this color is also defined in EditingModel.java
Color backgroundColor = new Color(Base.preferences.getInt("ui.backgroundColor", 0));
backgroundColor = JColorChooser.showDialog(
null,
"Choose Background Color",
backgroundColor);
if(backgroundColor == null)
return;
Base.preferences.putInt("ui.backgroundColor", backgroundColor.getRGB());
Base.getEditor().refreshPreviewPanel();
}
});
backgroundColorButton.setVisible(true);
content.add(backgroundColorButton,"wrap");
content.add(new JLabel("Firmware update URL: "),"split");
firmwareUpdateUrlField = new JTextField(34);
content.add(firmwareUpdateUrlField,"growx, wrap");
{
JLabel arcResolutionLabel = new JLabel("Arc resolution (in mm): ");
content.add(arcResolutionLabel,"split");
double value = Base.preferences.getDouble("replicatorg.parser.curve_segment_mm", 1.0);
JFormattedTextField arcResolutionField = new JFormattedTextField(Base.getLocalFormat());
arcResolutionField.setValue(new Double(value));
content.add(arcResolutionField);
String arcResolutionHelp = "<html><small><em>" +
"The arc resolution is the default segment length that the gcode parser will break arc codes <br>"+
"like G2 and G3 into. Drivers that natively handle arcs will ignore this setting." +
"</em></small></html>";
arcResolutionField.setToolTipText(arcResolutionHelp);
arcResolutionLabel.setToolTipText(arcResolutionHelp);
// content.add(new JLabel(arcResolutionHelp),"growx,wrap");
arcResolutionField.setColumns(10);
arcResolutionField.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("value")) {
try {
Double v = (Double)evt.getNewValue();
if (v == null) return;
Base.preferences.putDouble("replicatorg.parser.curve_segment_mm", v.doubleValue());
} catch (ClassCastException cce) {
Base.logger.warning("Unexpected value type: "+evt.getNewValue().getClass().toString());
}
}
}
});
}
{
JLabel sfTimeoutLabel = new JLabel("Skeinforge timeout: ");
content.add(sfTimeoutLabel,"split, gap unrelated");
int value = Base.preferences.getInt("replicatorg.skeinforge.timeout", -1);
JFormattedTextField sfTimeoutField = new JFormattedTextField(Base.getLocalFormat());
sfTimeoutField.setValue(new Integer(value));
content.add(sfTimeoutField,"wrap 10px, growx");
String sfTimeoutHelp = "<html><small><em>" +
"The Skeinforge timeout is the number of seconds that replicatorg will wait while the<br>" +
"Skeinforge preferences window is open. If you find that RepG freezes after editing profiles<br>" +
"you can set this number greater than -1 (-1 means no timeout)." +
"</em></small></html>";
sfTimeoutField.setToolTipText(sfTimeoutHelp);
sfTimeoutLabel.setToolTipText(sfTimeoutHelp);
// content.add(new JLabel(sfTimeoutHelp),"growx,wrap");
sfTimeoutField.setColumns(10);
sfTimeoutField.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("value")) {
try {
Integer v = ((Number)evt.getNewValue()).intValue();
if (v == null) return;
Base.preferences.putInt("replicatorg.skeinforge.timeout", v.intValue());
} catch (ClassCastException cce) {
Base.logger.warning("Unexpected value type: "+evt.getNewValue().getClass().toString());
}
}
}
});
}
{
content.add(new JLabel("Debugging level (default INFO):"),"split");
content.add(makeDebugLevelDropdown(), "wrap");
final JCheckBox logCb = new JCheckBox("Log to file");
logCb.setSelected(Base.preferences.getBoolean("replicatorg.useLogFile",false));
content.add(logCb, "split");
logCb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Base.preferences.putBoolean("replicatorg.useLogFile",logCb.isSelected());
}
});
final JLabel logPathLabel = new JLabel("Log file name: ");
content.add(logPathLabel,"split");
logPathField = new JTextField(34);
content.add(logPathField,"growx, wrap 10px");
logPathField.setEnabled(logCb.isSelected());
logPathLabel.setEnabled(logCb.isSelected());
logCb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JCheckBox box = (JCheckBox)e.getSource();
logPathField.setEnabled(box.isSelected());
logPathLabel.setEnabled(box.isSelected());
}
});
}
{
final int defaultTemp = 75;
final String tooltipGeneral = "When enabled, starting all builds heats components to this temperature";
final String tooltipHead = "Set preheat temperature for the specified toolhead";
final String tooltipPlatform = "Set preheat temperature for the build platfom";
final JCheckBox preheatCb = new JCheckBox("Preheat builds");
preheatCb.setToolTipText(tooltipGeneral);
content.add(preheatCb, "split");
preheatCb.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
Base.preferences.putBoolean("build.doPreheat", preheatCb.isSelected());
}
});
preheatCb.setSelected(Base.preferences.getBoolean("build.doPreheat", false));
final JLabel t0Label = new JLabel("Toolhead Right: ");
final JLabel t1Label = new JLabel("Toolhead Left: ");
final JLabel pLabel = new JLabel("Platform: ");
Integer t0Value = Base.preferences.getInt("build.preheatTool0", defaultTemp);
Integer t1Value = Base.preferences.getInt("build.preheatTool1", defaultTemp);
Integer pValue = Base.preferences.getInt("build.preheatPlatform", defaultTemp);
final JFormattedTextField t0Field = new JFormattedTextField(Base.getLocalFormat());
final JFormattedTextField t1Field = new JFormattedTextField(Base.getLocalFormat());
final JFormattedTextField pField = new JFormattedTextField(Base.getLocalFormat());
t0Field.setToolTipText(tooltipHead);
t0Label.setToolTipText(tooltipHead);
t1Field.setToolTipText(tooltipHead);
t1Label.setToolTipText(tooltipHead);
pField.setToolTipText(tooltipPlatform);
pLabel.setToolTipText(tooltipPlatform);
t0Field.setValue(t0Value);
t1Field.setValue(t1Value);
pField.setValue(pValue);
// let's avoid creating too many Anon. inner Listeners, also is fewer lines (and just as clear)!
PropertyChangeListener p = new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("value")) {
double target;
if(evt.getSource() == t0Field)
{
target = ((Number)t0Field.getValue()).doubleValue();
target = confirmTemperature(target,"temperature.acceptedLimit",200.0);
if (target == Double.MIN_VALUE) {
t0Field.setValue(Base.preferences.getInt("build.preheatTool0", defaultTemp));
return;
}
Base.preferences.putInt("build.preheatTool0", (int)target);
}
else if(evt.getSource() == t1Field)
{
target = ((Number)t1Field.getValue()).doubleValue();
target = confirmTemperature(target,"temperature.acceptedLimit",200.0);
if (target == Double.MIN_VALUE) {
t0Field.setValue(Base.preferences.getInt("build.preheatTool1", defaultTemp));
return;
}
Base.preferences.putInt("build.preheatTool1", (int)target);
}
else if(evt.getSource() == pField)
{
target = ((Number)pField.getValue()).doubleValue();
target = confirmTemperature(target,"temperature.acceptedLimit.bed",110.0);
if (target == Double.MIN_VALUE) {
t0Field.setValue(Base.preferences.getInt("build.preheatPlatform", defaultTemp));
return;
}
Base.preferences.putInt("build.preheatPlatform", (int)target);
}
}
}
};
t0Field.addPropertyChangeListener(p);
t1Field.addPropertyChangeListener(p);
pField.addPropertyChangeListener(p);
content.add(t0Label, "split, gap 20px");
content.add(t0Field, "split, growx");
content.add(t1Label, "split, gap unrelated");
content.add(t1Field, "split, growx");
content.add(pLabel, "split, gap unrelated");
content.add(pField, "split, growx, wrap 10px");
}
{
JButton b = new JButton("Select Python interpreter...");
content.add(b,"spanx,wrap 10px");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SwingPythonSelector sps = new SwingPythonSelector(PreferencesWindow.this);
String path = sps.selectFreeformPath();
if (path != null) {
PythonUtils.setPythonPath(path);
}
}
});
}
addInitialFilePrefs(content);
prefTabs.add(basic, "Basic");
prefTabs.add(advanced, "Advanced");
content = getContentPane();
content.setLayout(new MigLayout());
content.add(prefTabs, "wrap");
JButton allPrefs = new JButton("View Preferences Table");
content.add(allPrefs, "split");
allPrefs.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFrame advancedPrefs = new AdvancedPrefs();
advancedPrefs.setVisible(true);
}
});
// Also available as a menu item in the main gui.
JButton delPrefs = new JButton("Reset all preferences");
content.add(delPrefs);
delPrefs.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
editor.resetPreferences();
}
});
JButton button;
button = new JButton("Close");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
applyFrame();
dispose();
}
});
content.add(button, "tag ok");
showCurrentSettings();
// closing the window is same as hitting cancel button
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
ActionListener disposer = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
dispose();
}
};
Base.registerWindowCloseKeys(getRootPane(), disposer);
pack();
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screen.width - getWidth()) / 2,
(screen.height - getHeight()) / 2);
// handle window closing commands for ctrl/cmd-W or hitting ESC.
getContentPane().addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
KeyStroke wc = MainWindow.WINDOW_CLOSE_KEYSTROKE;
if ((e.getKeyCode() == KeyEvent.VK_ESCAPE)
|| (KeyStroke.getKeyStrokeForEvent(e).equals(wc))) {
dispose();
}
}
});
}
/**
* Change internal settings based on what was chosen in the prefs, then send
* a message to the editor saying that it's time to do the same.
*/
public void applyFrame() {
// put each of the settings into the table
String newSizeText = fontSizeField.getText();
try {
int newSize = Integer.parseInt(newSizeText.trim());
String fontName = Base.preferences.get("editor.font","Monospaced,plain,12");
if (fontName != null) {
String pieces[] = fontName.split(",");
pieces[2] = String.valueOf(newSize);
StringBuffer buf = new StringBuffer();
for (String piece : pieces) {
if (buf.length() > 0) buf.append(",");
buf.append(piece);
}
Base.preferences.put("editor.font", buf.toString());
}
} catch (Exception e) {
Base.logger.warning("ignoring invalid font size " + newSizeText);
}
String origUpdateUrl = Base.preferences.get("replicatorg.updates.url", "");
if (!origUpdateUrl.equals(firmwareUpdateUrlField.getText())) {
FirmwareUploader.invalidateFirmware();
Base.preferences.put("replicatorg.updates.url",firmwareUpdateUrlField.getText());
FirmwareUploader.checkFirmware(); // Initiate a new firmware check
}
String logPath = logPathField.getText();
Base.preferences.put("replicatorg.logpath", logPath);
Base.setLogFile(logPath);
editor.applyPreferences();
}
public void showFrame(MainWindow editor) {
this.editor = editor;
// set all settings entry boxes to their actual status
setVisible(true);
}
}