use of java.awt.HeadlessException in project jmeter by apache.
the class JMeterUtils method reportErrorToUser.
/**
* Report an error through a dialog box in GUI mode
* or in logs and stdout in Non GUI mode
*
* @param errorMsg - the error message.
* @param titleMsg - title string
* @param exception Exception
*/
public static void reportErrorToUser(String errorMsg, String titleMsg, Exception exception) {
if (errorMsg == null) {
errorMsg = "Unknown error - see log file";
log.warn("Unknown error", new Throwable("errorMsg == null"));
}
GuiPackage instance = GuiPackage.getInstance();
if (instance == null) {
if (exception != null) {
log.error(errorMsg, exception);
} else {
log.error(errorMsg);
}
// NOSONAR intentional
System.out.println(errorMsg);
// Done
return;
}
try {
JOptionPane.showMessageDialog(instance.getMainFrame(), errorMsg, titleMsg, JOptionPane.ERROR_MESSAGE);
} catch (HeadlessException e) {
log.warn("reportErrorToUser(\"" + errorMsg + "\") caused", e);
}
}
use of java.awt.HeadlessException in project JMRI by JMRI.
the class RosterEntry method writeFile.
/**
* Write the contents of this RosterEntry to a file.
* <p>
* Information on the contents is passed through the parameters,
* as the actual XML creation is done in the LocoFile class.
*
* @param cvModel CV contents to include in file
* @param iCvModel indexed CV contents to include in file
* @param variableModel Variable contents to include in file
*
*/
public void writeFile(CvTableModel cvModel, IndexedCvTableModel iCvModel, VariableTableModel variableModel) {
LocoFile df = new LocoFile();
// do I/O
FileUtil.createDirectory(LocoFile.getFileLocation());
try {
String fullFilename = LocoFile.getFileLocation() + getFileName();
File f = new File(fullFilename);
// do backup
df.makeBackupFile(LocoFile.getFileLocation() + getFileName());
// changed
changeDateUpdated();
// and finally write the file
df.writeFile(f, cvModel, iCvModel, variableModel, this);
} catch (Exception e) {
log.error("error during locomotive file output", e);
try {
JOptionPane.showMessageDialog(null, ResourceBundle.getBundle("jmri.jmrit.roster.JmritRosterBundle").getString("ErrorSavingText") + "\n" + e.getMessage(), ResourceBundle.getBundle("jmri.jmrit.roster.JmritRosterBundle").getString("ErrorSavingTitle"), JOptionPane.ERROR_MESSAGE);
} catch (HeadlessException he) {
// silently ignore inability to display dialog
}
}
}
use of java.awt.HeadlessException in project JMRI by JMRI.
the class NameCheckAction method actionPerformed.
@SuppressWarnings("unchecked")
@Override
public void actionPerformed(ActionEvent e) {
if (fci == null) {
fci = jmri.jmrit.XmlFile.userFileChooser("XML files", "xml");
}
// request the filename from an open dialog
fci.rescanCurrentDirectory();
int retVal = fci.showOpenDialog(_who);
// handle selection or cancel
if (retVal == JFileChooser.APPROVE_OPTION) {
File file = fci.getSelectedFile();
if (log.isDebugEnabled()) {
log.debug("located file " + file + " for XML processing");
}
// handle the file (later should be outside this thread?)
try {
Element root = readFile(file);
if (log.isDebugEnabled()) {
log.debug("parsing complete");
}
// check to see if there's a decoder element
if (root.getChild("decoder") == null) {
log.warn("Does not appear to be a decoder file");
return;
}
Iterator<Element> iter = root.getChild("decoder").getChild("variables").getDescendants(new ElementFilter("variable"));
jmri.jmrit.symbolicprog.NameFile nfile = jmri.jmrit.symbolicprog.NameFile.instance();
String warnings = "";
while (iter.hasNext()) {
Element varElement = iter.next();
// for each variable, see if can find in names file
Attribute labelAttr = varElement.getAttribute("label");
String label = null;
if (labelAttr != null) {
label = labelAttr.getValue();
}
Attribute itemAttr = varElement.getAttribute("item");
String item = null;
if (itemAttr != null) {
item = itemAttr.getValue();
}
if (log.isDebugEnabled()) {
log.debug("Variable called \"" + ((label != null) ? label : "<none>") + "\" \"" + ((item != null) ? item : "<none>"));
}
if (!(label == null ? false : nfile.checkName(label)) && !(item == null ? false : nfile.checkName(item))) {
log.warn("Variable not found: label=\"" + ((label != null) ? label : "<none>") + "\" item=\"" + ((item != null) ? label : "<none>") + "\"");
warnings += "Variable not found: label=\"" + ((label != null) ? label : "<none>") + "\" item=\"" + ((item != null) ? item : "<none>") + "\"\n";
}
}
if (!warnings.equals("")) {
JOptionPane.showMessageDialog(_who, warnings);
} else {
JOptionPane.showMessageDialog(_who, "No mismatched items found");
}
} catch (HeadlessException | IOException | JDOMException ex) {
JOptionPane.showMessageDialog(_who, "Error parsing decoder file: " + ex);
}
} else {
log.debug("XmlFileCheckAction cancelled in open dialog");
}
}
use of java.awt.HeadlessException in project JMRI by JMRI.
the class EcosLocoAddressManager method checkLocoList.
/* This is used after an event update form the ecos informing us of a change in the
* loco list, we have to determine if it is an addition or delete.
* We should only ever do either a remove or an add in one go, if we are adding the loco
* to the roster otherwise this causes a problem with the roster list.
*/
void checkLocoList(String[] ecoslines) {
log.info("Checking loco list");
String loco;
for (int i = 0; i < ecoslines.length; i++) {
loco = ecoslines[i];
loco = loco.replaceAll("[\\n\\r]", "");
if (getByEcosObject(loco) == null) {
log.debug("We are to add loco " + loco + " to the Ecos Loco List");
EcosMessage mout = new EcosMessage("get(" + loco + ", addr, name, protocol)");
tc.sendEcosMessage(mout, this);
}
}
String[] jmrilist = getEcosObjectArray();
boolean nomatch = true;
for (int i = 0; i < jmrilist.length; i++) {
nomatch = true;
for (int k = 0; k < ecoslines.length; k++) {
loco = ecoslines[k];
loco = loco.replaceAll("[\\n\\r]", "");
if (loco.equals(jmrilist[i])) {
nomatch = false;
break;
}
}
if (nomatch) {
//System.out.println("We do not have a match, therefore this should be deleted from the Ecos loco Manager " + jmrilist[i]);
log.debug("Loco not found so need to remove from register");
if (getByEcosObject(jmrilist[i]).getRosterId() != null) {
final String rosterid = getByEcosObject(jmrilist[i]).getRosterId();
final Roster _roster = Roster.getDefault();
final RosterEntry re = _roster.entryFromTitle(rosterid);
re.deleteAttribute(p.getRosterAttribute());
re.writeFile(null, null, null);
Roster.getDefault().writeRoster();
if (p.getRemoveLocoFromJMRI() == EcosPreferences.YES) {
_roster.removeEntry(re);
Roster.getDefault().writeRoster();
} else if (p.getRemoveLocoFromJMRI() == EcosPreferences.ASK) {
try {
final JDialog dialog = new JDialog();
dialog.setTitle("Remove Roster Entry From JMRI?");
dialog.setLocation(300, 200);
dialog.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
JPanel container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
container.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JLabel question = new JLabel(rosterid + " has been removed from the Ecos do you want to remove it from JMRI?");
question.setAlignmentX(Component.CENTER_ALIGNMENT);
container.add(question);
final JCheckBox remember = new JCheckBox("Remember this setting for next time?");
remember.setFont(remember.getFont().deriveFont(10f));
remember.setAlignmentX(Component.CENTER_ALIGNMENT);
//user preferences do not have the save option, but once complete the following line can be removed
//Need to get the method to save connection configuration.
remember.setVisible(true);
JButton yesButton = new JButton("Yes");
JButton noButton = new JButton("No");
JPanel button = new JPanel();
button.setAlignmentX(Component.CENTER_ALIGNMENT);
button.add(yesButton);
button.add(noButton);
container.add(button);
noButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (remember.isSelected()) {
p.setRemoveLocoFromJMRI(EcosPreferences.ASK);
}
dialog.dispose();
}
});
yesButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (remember.isSelected()) {
p.setRemoveLocoFromJMRI(EcosPreferences.YES);
}
setLocoToRoster();
_roster.removeEntry(re);
Roster.getDefault().writeRoster();
dialog.dispose();
}
});
container.add(remember);
container.setAlignmentX(Component.CENTER_ALIGNMENT);
container.setAlignmentY(Component.CENTER_ALIGNMENT);
dialog.getContentPane().add(container);
dialog.pack();
dialog.setModal(true);
dialog.setVisible(true);
} catch (HeadlessException he) {
// silently ignore inability to display dialog
}
}
}
//Even if we do not delete the loco from the roster, we need to remove it from the ecos list.
deregister(getByEcosObject(jmrilist[i]));
}
}
}
use of java.awt.HeadlessException in project jdk8u_jdk by JetBrains.
the class Desktop method getDesktop.
/**
* Returns the <code>Desktop</code> instance of the current
* browser context. On some platforms the Desktop API may not be
* supported; use the {@link #isDesktopSupported} method to
* determine if the current desktop is supported.
* @return the Desktop instance of the current browser context
* @throws HeadlessException if {@link
* GraphicsEnvironment#isHeadless()} returns {@code true}
* @throws UnsupportedOperationException if this class is not
* supported on the current platform
* @see #isDesktopSupported()
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public static synchronized Desktop getDesktop() {
if (GraphicsEnvironment.isHeadless())
throw new HeadlessException();
if (!Desktop.isDesktopSupported()) {
throw new UnsupportedOperationException("Desktop API is not " + "supported on the current platform");
}
sun.awt.AppContext context = sun.awt.AppContext.getAppContext();
Desktop desktop = (Desktop) context.get(Desktop.class);
if (desktop == null) {
desktop = new Desktop();
context.put(Desktop.class, desktop);
}
return desktop;
}
Aggregations