Commit 417627d4 authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

fix #280 nullpointer in gui while simulation setup

The problem occurred because the OnlineVisualization model is setup
during gui startup but was only valid after the first call to
OnlineVisualization#postUpdate where the DrawData object is created.
Only then was the OnlineVisualization valid and capable of answering
all callbacks.

This commit will fix the problem but does not fix the design the problem.
The OnlineVisualization is still created at gui startup but all
callbacks to the model are removed util the OnlineVisualization model is
fully initialized.
parent 801b7818
......@@ -9,7 +9,6 @@ import org.vadere.gui.components.view.ISelectScenarioElementListener;
import org.vadere.state.scenario.ScenarioElement;
import org.vadere.state.types.ScenarioElementType;
import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.geometry.shapes.VPolygon;
import org.vadere.util.geometry.shapes.VShape;
import org.vadere.util.logging.Logger;
import org.vadere.util.voronoi.VoronoiDiagram;
......
package org.vadere.gui.onlinevisualization;
import javax.swing.JPanel;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.vadere.gui.onlinevisualization.model.OnlineVisualizationModel;
......@@ -93,6 +91,15 @@ public class OnlineVisualization implements PassiveCallback {
@Override
public void preLoop(double simTimeInSec) {
// [issue 280] ensure OnlineVisualisation model is completely setup before
// OnlineVisualisation renderer is initialized in window.preLoop()
// push pop DrawData once at the beginning. This will completely initialize the model
// (i.e. set Topography to correct value. Before this call it is null....)
pushDrawData(simTimeInSec);
model.popDrawData();
// [issue 280] activate mouse listeners to allow zoom action in OnlineVisualisation
window.addListener();
onlineVisualisationPanel.setVisible(this.enableVisualization);
window.preLoop();
}
......@@ -101,6 +108,9 @@ public class OnlineVisualization implements PassiveCallback {
public void postLoop(double simTimeInSec) {
onlineVisualisationPanel.setVisible(false);
model.reset();
// [issue 280] deactivate mouse listeners because model is not valid anymore
window.removeListeners();
}
@Override
......@@ -142,7 +152,17 @@ public class OnlineVisualization implements PassiveCallback {
}
public JPanel getVisualizationPanel() {
// [issue 280] show OnlineVisualization Window and remove mouse Listeners. This is necessary to ensure
// that no null pointer exception is thrown in the awt thread due to not completely
// initialized OnlineVisualisation model. A better fix would be to only initialize the
// OnlineVisualisation AFTER the model is loaded completely but this would need more changes
// in the gui setup.
public void showVisualization(){
window.setVisible(true);
window.removeListeners();
}
public OnlineVisualisationWindow getVisualizationPanel() {
return onlineVisualisationPanel;
}
......
package org.vadere.gui.onlinevisualization.view;
import java.util.*;
import org.vadere.gui.components.control.IMode;
import org.vadere.gui.components.control.RectangleSelectionMode;
import org.vadere.gui.components.utils.Resources;
import org.vadere.gui.components.view.ScaleablePanel;
import org.vadere.gui.onlinevisualization.control.OnlineVisSelectionMode;
import org.vadere.gui.onlinevisualization.model.OnlineVisualizationModel;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
/**
* Main panel of the visualization window, used to display the simulation
......@@ -31,7 +33,8 @@ public class MainPanel extends ScaleablePanel implements Observer {
private final OnlineVisualizationModel model;
private List<IRendererChangeListener> rendererChangeListeners;
private static Resources resources = Resources.getInstance("global");
private IMode selectionMode = null;
private IMode selectionMode;
/** Creates a new main panel. */
public MainPanel(final OnlineVisualizationModel model) {
......@@ -39,12 +42,31 @@ public class MainPanel extends ScaleablePanel implements Observer {
this.model = model;
this.rendererChangeListeners = new ArrayList<>();
RectangleSelectionMode selectionMode = new OnlineVisSelectionMode(model);
this.selectionMode = new OnlineVisSelectionMode(model);
addMouseListener(selectionMode);
addMouseMotionListener(selectionMode);
addMouseWheelListener(selectionMode);
}
// [issue 280] remove listener if model is not valid.
public void addListener(){
if (selectionMode == null)
selectionMode = new OnlineVisSelectionMode(model);
addMouseListener(selectionMode);
addMouseMotionListener(selectionMode);
addMouseWheelListener(selectionMode);
}
// [issue 280] add listener if model is valid.
public void removeListeners(){
if (selectionMode != null){
removeMouseListener(this.selectionMode);
removeMouseMotionListener(this.selectionMode);
removeMouseWheelListener(this.selectionMode);
}
}
public void addRendererChangeListener(final IRendererChangeListener listener) {
rendererChangeListeners.add(listener);
}
......
......@@ -40,12 +40,20 @@ public class OnlineVisualisationWindow extends JPanel implements Observer {
private static final long serialVersionUID = 3522170593760789565L;
private static final Resources resources = Resources.getInstance("global");
private static final Configuration CONFIG = VadereConfig.getConfig(); private ScenarioElementView jsonPanel;
private static final Configuration CONFIG = VadereConfig.getConfig();
private JToolBar toolbar;
private SimulationInfoPanel infoPanel;
private JToolBar toolbar; // top
private SimulationInfoPanel infoPanel; // footer
private JScrollPane scenarioScrollPane; // left
private ScenarioElementView jsonPanel; // right
private JSplitPane splitPaneForTopographyAndJsonPane; // container left/right
private MainPanel mainPanel;
private OnlineVisualizationModel model;
public OnlineVisualisationWindow(final MainPanel mainPanel, final OnlineVisualizationModel model) {
this.mainPanel = mainPanel;
this.model = model;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int windowHeight = screenSize.height - 250;
......@@ -53,15 +61,15 @@ public class OnlineVisualisationWindow extends JPanel implements Observer {
FormLayout spiltLayout = new FormLayout("2dlu, default:grow(0.75), 2dlu, default:grow(0.25), 2dlu", // col
"2dlu, default, 2dlu, fill:default:grow, 2dlu, default, 2dlu"); // rows
JScrollPane scrollPane = new ScenarioScrollPane(mainPanel, model);
scenarioScrollPane = new ScenarioScrollPane(mainPanel, model);
model.addScaleChangeListener(mainPanel);
mainPanel.addComponentListener(new PanelResizeListener(model));
mainPanel.setScrollPane(scrollPane);
scrollPane.getViewport()
.addChangeListener(new JViewportChangeListener(model, scrollPane.getVerticalScrollBar()));
model.addScrollPane(scrollPane);
mainPanel.setScrollPane(scenarioScrollPane);
scenarioScrollPane.getViewport()
.addChangeListener(new JViewportChangeListener(model, scenarioScrollPane.getVerticalScrollBar()));
model.addScrollPane(scenarioScrollPane);
IViewportChangeListener viewportChangeListener = new ViewportChangeListener(model, scrollPane);
IViewportChangeListener viewportChangeListener = new ViewportChangeListener(model, scenarioScrollPane);
model.addViewportChangeListener(viewportChangeListener);
jsonPanel = new ScenarioElementView(model);
......@@ -256,13 +264,13 @@ public class OnlineVisualisationWindow extends JPanel implements Observer {
SwingUtils.addActionToToolbar(toolbar, openSettingsDialog,
Messages.getString("ProjectView.btnSettings.tooltip"));
JSplitPane splitPaneForTopographyAndJsonPane = new JSplitPane();
splitPaneForTopographyAndJsonPane = new JSplitPane();
splitPaneForTopographyAndJsonPane.setResizeWeight(0.8);
splitPaneForTopographyAndJsonPane.resetToPreferredSizes();
splitPaneForTopographyAndJsonPane.setLeftComponent(scrollPane);
splitPaneForTopographyAndJsonPane.setLeftComponent(scenarioScrollPane);
splitPaneForTopographyAndJsonPane.setRightComponent(jsonPanel);
scrollPane.setPreferredSize(new Dimension(1, windowHeight));
scenarioScrollPane.setPreferredSize(new Dimension(1, windowHeight));
add(toolbar, cc.xyw(2, 2, 3));
add(splitPaneForTopographyAndJsonPane, cc.xywh(2, 4, 4, 1));
......
......@@ -219,7 +219,9 @@ public class ScenarioPanel extends JPanel implements IProjectChangeListener, Pro
public void showVisualization() {
CardLayout cl = (CardLayout) this.getLayout();
cl.show(this, visualizationCardName);
onlineVisualization.getMainPanel().setVisible(true);
// [issue 280] set visibility and remove mouse listeners during setup. Will be added
// after setup of simulation run is complete.
onlineVisualization.showVisualization();
}
public void showEditScenario() {
......
Supports Markdown
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