Add warning text if simulation listener not recognized as class

This commit is contained in:
SiboVG 2022-11-14 13:57:56 +01:00
parent 1021bbc8a9
commit fa27fd0b9a
2 changed files with 57 additions and 12 deletions

View File

@ -4,6 +4,8 @@ import java.awt.Dialog.ModalityType;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JComponent;
@ -50,17 +52,21 @@ public abstract class AbstractSwingSimulationExtensionConfigurator<E extends Sim
close.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
close();
}
});
panel.add(close, "right");
dialog.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
close();
}
});
dialog.add(panel);
GUIUtil.setDisposableDialogOptions(dialog, close);
dialog.setVisible(true);
close();
GUIUtil.setNullModels(dialog);
dialog = null;
}
/**
@ -78,10 +84,12 @@ public abstract class AbstractSwingSimulationExtensionConfigurator<E extends Sim
}
/**
* Called when the default dialog is closed. By default does nothing.
* Called when the default dialog is closed. By default hides the dialog and cleans up the component models.
*/
protected void close() {
dialog.setVisible(false);
GUIUtil.setNullModels(dialog);
dialog = null;
}
protected abstract JComponent getConfigurationComponent(E extension, Simulation simulation, JPanel panel);

View File

@ -8,22 +8,37 @@ import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import net.sf.openrocket.document.Simulation;
import net.sf.openrocket.gui.components.StyledLabel;
import net.sf.openrocket.l10n.Translator;
import net.sf.openrocket.plugin.Plugin;
import net.sf.openrocket.simulation.extension.AbstractSwingSimulationExtensionConfigurator;
import net.sf.openrocket.startup.Application;
import net.sf.openrocket.util.Color;
@Plugin
public class JavaCodeConfigurator extends AbstractSwingSimulationExtensionConfigurator<JavaCode> {
private JavaCode extension;
private JTextField classNameField;
private static final Translator trans = Application.getTranslator();
public JavaCodeConfigurator() {
super(JavaCode.class);
}
@Override
protected JComponent getConfigurationComponent(final JavaCode extension, Simulation simulation, JPanel panel) {
this.extension = extension;
panel.add(new JLabel(trans.get("SimulationExtension.javacode.desc")), "wrap para");
panel.add(new JLabel(trans.get("SimulationExtension.javacode.className")), "wrap rel");
final JTextField textField = new JTextField(extension.getClassName());
textField.getDocument().addDocumentListener(new DocumentListener() {
classNameField = new JTextField(extension.getClassName());
panel.add(classNameField, "growx, wrap");
StyledLabel errorMsg = new StyledLabel();
errorMsg.setFontColor(Color.DARK_RED.toAWTColor());
errorMsg.setVisible(false);
panel.add(errorMsg, "growx, wrap");
classNameField.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
update();
}
@ -37,11 +52,33 @@ public class JavaCodeConfigurator extends AbstractSwingSimulationExtensionConfig
}
public void update() {
extension.setClassName(textField.getText());
// Display error message if the class name is invalid
String text = classNameField.getText().trim();
try {
Class.forName(text);
errorMsg.setVisible(false);
} catch (ClassNotFoundException e) {
// Don't display an error message for an empty field
if (text.length() == 0) {
errorMsg.setVisible(false);
return;
}
errorMsg.setText(trans.get("SimulationExtension.javacode.classnotfound"));
errorMsg.setVisible(true);
}
}
});
panel.add(textField, "growx");
return panel;
}
@Override
protected void close() {
if (this.extension != null && this.classNameField != null) {
this.extension.setClassName(this.classNameField.getText().trim());
}
super.close();
}
}