Commit c9f5661d authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

add DocumentLister to DataProcessorView to auto update new filename.

With this change the user does not need to hit enter to see the new
file name on the left hand side of the ProcessorView. With focus
loss the filename is validated and reverted to a default name if
invalid.
parent aff6c656
Pipeline #67136 failed with stages
in 55 minutes and 41 seconds
package org.vadere.gui.projectview.utils;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public abstract class SimpleDocumentListener implements DocumentListener {
@Override
public void insertUpdate(DocumentEvent e) {
update(e);
}
@Override
public void removeUpdate(DocumentEvent e) {
update(e);
}
@Override
public void changedUpdate(DocumentEvent e) {
update(e);
}
public abstract void update(DocumentEvent e);
}
......@@ -12,6 +12,7 @@ import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import org.fife.ui.rsyntaxtextarea.Theme;
import org.vadere.gui.components.utils.Messages;
import org.vadere.gui.components.view.JComboCheckBox;
import org.vadere.gui.projectview.utils.SimpleDocumentListener;
import org.vadere.simulator.projects.Scenario;
import org.vadere.simulator.projects.dataprocessing.DataProcessingJsonManager;
import org.vadere.simulator.projects.dataprocessing.outputfile.OutputFile;
......@@ -25,6 +26,8 @@ import org.vadere.util.io.IOUtils;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
......@@ -214,11 +217,7 @@ class DataProcessingView extends JPanel implements IJsonView {
@Override
public void actionPerformed(ActionEvent e) {
String filename = "out.txt";
int count = 1;
while (outputFileNameAlreadyExists(filename)) { // ensure unique suggested filename
filename = "out" + (count ++) + ".txt";
}
String filename = getDefaultFilename();
OutputFileStore outputFileStore = new OutputFileStore();
outputFileStore.setFilename(filename);
currentScenario.getDataProcessingJsonManager().addOutputFile(outputFileStore);
......@@ -327,6 +326,15 @@ class DataProcessingView extends JPanel implements IJsonView {
ProjectView.getMainWindow().refreshScenarioNames();
}
private String getDefaultFilename(){
String filename = "out.txt";
int count = 1;
while (outputFileNameAlreadyExists(filename) > 0) { // ensure unique suggested filename
filename = "out" + (count ++) + ".txt";
}
return filename;
}
@Override
public void setVadereScenario(Scenario scenario) {
this.currentScenario = scenario;
......@@ -476,29 +484,52 @@ class DataProcessingView extends JPanel implements IJsonView {
c.gridx = 1;
c.gridy = 0;
JTextField nameField = new JTextField(outputFile.toString());
nameField.addActionListener(ae -> {
String oldName = outputFile.toString();
String newName = nameField.getText();
if (!oldName.equals(newName)) {
String msg = "";
if (newName.isEmpty()) {
msg = Messages.getString("DataProcessingView.msgFileEmpty");
}
if (outputFileNameAlreadyExists(newName)) {
msg = Messages.getString("DataProcessingView.msgFileInUse");
}
if (msg.isEmpty()) {
outputFile.setRelativeFileName(newName);
outputFilesTable.repaint();
refreshGUI();
} else {
nameField.setText(oldName);
JOptionPane.showMessageDialog(ProjectView.getMainWindow(), msg,
Messages.getString("DataProcessingView.dialogInvalidFile.label"), JOptionPane.WARNING_MESSAGE);
}
// add DocumentLister to update outputFiles character at a time
nameField.getDocument().addDocumentListener(new SimpleDocumentListener() {
@Override
public void update(DocumentEvent e){
SwingUtilities.invokeLater(() -> {
String oldName = outputFile.toString();
String newName = nameField.getText();
if (!oldName.equals(newName)) {
outputFile.setRelativeFileName(newName);
outputFilesTable.repaint();
refreshGUI();
}
});
}
});
// check at focus loss for invalid file names. If found reset to a default free value.
nameField.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
SwingUtilities.invokeLater(() -> {
String newName = nameField.getText();
String msg = "";
if (newName.isEmpty()) {
msg = Messages.getString("DataProcessingView.msgFileEmpty");
newName = getDefaultFilename();
} else if (outputFileNameAlreadyExists(newName) > 1) { // is automatically updated.
msg = Messages.getString("DataProcessingView.msgFileInUse");
newName = getDefaultFilename();
}
if (!msg.isEmpty()) {
nameField.setText(newName);
JOptionPane.showMessageDialog(ProjectView.getMainWindow(), msg,
Messages.getString("DataProcessingView.dialogInvalidFile.label"), JOptionPane.WARNING_MESSAGE);
}
});
}
});
// got to next element. Note: this will trigger the focus loss event above.
nameField.addActionListener(ae -> {
passFocusOn();
});
addEditableComponent(nameField);
panel.add(nameField, c);
......@@ -568,6 +599,7 @@ class DataProcessingView extends JPanel implements IJsonView {
repaint(); // inelegantly, it needs both revalidate() and repaint() stackoverflow.com/a/5812780
}
private void handleDataProcessorSelected(DataProcessor dataProcessor) {
selectedDataProcessor = dataProcessor;
dataProcessorsDetailsPanel.removeAll();
......@@ -689,9 +721,9 @@ class DataProcessingView extends JPanel implements IJsonView {
return type.getTypeName().substring(type.getTypeName().lastIndexOf(".") + 1);
}
private boolean outputFileNameAlreadyExists(String filename) {
private long outputFileNameAlreadyExists(String filename) {
return currentScenario.getDataProcessingJsonManager().getOutputFiles().stream()
.anyMatch(oFile -> oFile.getFileName().equals(filename));
.filter(oFile -> oFile.getFileName().equals(filename)).count();
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment