Commit 30a8af67 authored by Benedikt Kleinmeier's avatar Benedikt Kleinmeier

In "Events" tab, added menu to load presettings.

parent bd787ec5
......@@ -340,6 +340,7 @@ Tab.Event.title=Events
Tab.OutputProcessors.title=Data output
Tab.TopographyCreator.title=Topography creator
Tab.PostVisualization.title=Post-Visualization
TextView.Button.LoadPresettings=Load presettings
# Info Panel at bottom of Topography creator
InfoPanel.ScenarioSize.label=Scenario Size
......
......@@ -334,6 +334,7 @@ Tab.Event.title=Events
Tab.OutputProcessors.title=Datenausgabe
Tab.TopographyCreator.title=Topographie-Designer
Tab.PostVisualization.title=Post-Visualisierung
TextView.Button.LoadPresettings=Voreinstellungen laden
# Info Panel at bottom of Topography creator
InfoPanel.ScenarioSize.label=Szenariogr\u00f6\u00dfe
......
......@@ -14,6 +14,7 @@ import org.vadere.simulator.projects.dataprocessing.DataProcessingJsonManager;
import org.vadere.simulator.projects.io.JsonConverter;
import org.vadere.state.attributes.ModelDefinition;
import org.vadere.state.events.json.EventInfoStore;
import org.vadere.state.events.presettings.EventPresettings;
import org.vadere.state.scenario.Topography;
import org.vadere.state.util.StateJsonConverter;
import org.vadere.util.io.IOUtils;
......@@ -110,15 +111,18 @@ public class TextView extends JPanel implements IJsonView {
add(panelTop, BorderLayout.NORTH);
panelTop.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
generatePresettingsMenu(attributeType);
JButton btnSaveToFile = new JButton(Messages.getString("TextFileView.btnSaveToFile.text"));
btnSaveToFile.addActionListener(saveToFileActionListener);
btnSaveToFile.setIcon(new ImageIcon(Resources.class.getResource("/icons/floppy.gif")));
panelTop.add(btnSaveToFile);
btnLoadFromFile = new JButton(Messages.getString("TextView.btnLoadFromFile.text"));
btnLoadFromFile.setIcon(new ImageIcon(Resources.class.getResource("/icons/floppy.gif")));
panelTop.add(btnLoadFromFile);
jsonValidIndicator = new JsonValidIndicator();
jsonValidIndicator = new JsonValidIndicator();
panelTop.add(jsonValidIndicator);
JScrollPane scrollPane = new JScrollPane();
add(scrollPane, BorderLayout.CENTER);
......@@ -205,6 +209,35 @@ public class TextView extends JPanel implements IJsonView {
jsonValidIndicator.setValid();
}
private void generatePresettingsMenu(final AttributeType attributeType) {
if (attributeType == AttributeType.EVENT) {
JMenuBar presetMenuBar = new JMenuBar();
JMenu mnPresetMenu = new JMenu(Messages.getString("TextView.Button.LoadPresettings"));
presetMenuBar.add(mnPresetMenu);
EventPresettings.PRESETTINGS_MAP.forEach(
(clazz, jsonString) -> mnPresetMenu.add(new JMenuItem(new AbstractAction(clazz.getSimpleName()) {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
if (JOptionPane.showConfirmDialog(ProjectView.getMainWindow(),
Messages.getString("Tab.Model.confirmLoadTemplate.text"),
Messages.getString("Tab.Model.confirmLoadTemplate.title"),
JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
try {
txtrTextfiletextarea.setText(jsonString);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
})));
panelTop.add(presetMenuBar);
}
}
@Override
public void setVadereScenario(Scenario scenario) { // in order to avoid passing the exception upwards. might not be the best solution
try {
......
package org.vadere.state.events.presettings;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.vadere.state.events.json.EventInfo;
import org.vadere.state.events.json.EventInfoStore;
import org.vadere.state.events.types.*;
import org.vadere.state.util.JacksonObjectMapper;
import org.vadere.util.geometry.shapes.VRectangle;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Provide JSON presettings for commonly used events.
*
* This class can be used as helper for GUI elements.
*/
public class EventPresettings {
/** Map an event class (e.g., BangEvent) to a JSON string. */
public static Map<Class, String> PRESETTINGS_MAP;
// Static initializer for "PRESETTINGS_MAP".
static {
PRESETTINGS_MAP = new HashMap<>();
Event[] eventsToUse = new Event[] {
new BangEvent(),
new WaitEvent(),
new WaitInAreaEvent(0, new VRectangle(0, 0, 10, 10)),
};
for (Event event : eventsToUse) {
// Container for a timeframe and the corresponding events.
EventInfo eventInfo = new EventInfo();
List<Event> events = new ArrayList<>();
events.add(event);
eventInfo.setEventTimeframe(new EventTimeframe(0, 10, false, 0));
eventInfo.setEvents(events);
// Container for multiple event infos.
List<EventInfo> eventInfos = new ArrayList<>();
eventInfos.add(eventInfo);
EventInfoStore eventInfoStore = new EventInfoStore();
eventInfoStore.setEventInfos(eventInfos);
try {
ObjectMapper mapper = new JacksonObjectMapper();
// String jsonDataString = mapper.writeValueAsString(eventInfoStore);
String jsonDataString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(eventInfoStore);
PRESETTINGS_MAP.put(event.getClass(), jsonDataString);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
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