/
GUIHelpers.java
104 lines (89 loc) · 3.79 KB
/
GUIHelpers.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
/*
Copyright 2016-2018 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.i18n.Localization;
import com.willwinder.universalgcodesender.model.BackendAPI;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
/**
*
* @author wwinder
*/
public class GUIHelpers {
private static final Logger LOGGER = Logger.getLogger(GUIHelpers.class.getName());
// Set with reflection in unit tests.
private static boolean unitTestMode = false;
public static void invokeLater(Runnable r) {
if (unitTestMode) {
r.run();
} else {
java.awt.EventQueue.invokeLater(r);
}
}
/**
* Displays an error message to the user which will not block the current thread.
* @param errorMessage message to display in the dialog.
*/
public static void displayErrorDialog(final String errorMessage) {
displayErrorDialog(errorMessage, false);
}
/**
* Displays an error message to the user.
* @param errorMessage message to display in the dialog.
* @param modal toggle whether the message should block or fire and forget.
*/
public static void displayErrorDialog(final String errorMessage, boolean modal) {
if (StringUtils.isEmpty(errorMessage)) {
LOGGER.warning("Something tried to display an error message with an empty message: " + ExceptionUtils.getStackTrace(new Throwable()));
return;
}
Runnable r = () -> {
//JOptionPane.showMessageDialog(new JFrame(), errorMessage,
// Localization.getString("error"), JOptionPane.ERROR_MESSAGE);
NarrowOptionPane.showNarrowDialog(250, errorMessage.replaceAll("\\.\\.", "\\."),
Localization.getString("error"),
JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
};
if (modal) {
r.run();
} else {
java.awt.EventQueue.invokeLater(r);
}
}
public static void displayHelpDialog(final String helpMessage) {
java.awt.EventQueue.invokeLater(() -> {
NarrowOptionPane.showNarrowConfirmDialog(250, helpMessage, Localization.getString("help"),
JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
//JOptionPane.showMessageDialog(new JFrame(), helpMessage,
// Localization.getString("help"), JOptionPane.INFORMATION_MESSAGE);
});
}
public static void openGcodeFile(File f, BackendAPI backend) {
ThreadHelper.invokeLater(() -> {
try {
backend.setGcodeFile(f);
Settings settings = backend.getSettings();
settings.setLastOpenedFilename(f.getAbsolutePath());
SettingsFactory.saveSettings(settings);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Couldn't set gcode-file" + e.getMessage(), e);
}
});
}
}