Commit 7ecf36a2 authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

issue #43. Refactor Dialog to own class. Add new Icon

Show illegal entry by changing color to red. It is still possible to
click OK even if the input is invalid. The invalid input will be ignored
without an error is thrown.
parent c30de7dc
...@@ -16,7 +16,6 @@ import org.vadere.state.scenario.Topography; ...@@ -16,7 +16,6 @@ import org.vadere.state.scenario.Topography;
import org.vadere.state.util.StateJsonConverter; import org.vadere.state.util.StateJsonConverter;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.beans.IntrospectionException;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -224,7 +223,7 @@ public class ScenarioPanel extends JPanel implements IProjectChangeListener, Pro ...@@ -224,7 +223,7 @@ public class ScenarioPanel extends JPanel implements IProjectChangeListener, Pro
if (isEditable) { if (isEditable) {
menusInTabs.forEach(menu -> menu.setEnabled(true)); menusInTabs.forEach(menu -> menu.setEnabled(true));
try {
int index = tabbedPane.getSelectedIndex(); int index = tabbedPane.getSelectedIndex();
if (topographyCreatorView != null && tabbedPane.indexOfComponent(topographyCreatorView) >= 0) { if (topographyCreatorView != null && tabbedPane.indexOfComponent(topographyCreatorView) >= 0) {
tabbedPane.removeTabAt(tabbedPane.indexOfComponent(topographyCreatorView)); tabbedPane.removeTabAt(tabbedPane.indexOfComponent(topographyCreatorView));
...@@ -237,10 +236,6 @@ public class ScenarioPanel extends JPanel implements IProjectChangeListener, Pro ...@@ -237,10 +236,6 @@ public class ScenarioPanel extends JPanel implements IProjectChangeListener, Pro
tabbedPane.setSelectedIndex(index); tabbedPane.setSelectedIndex(index);
setTopography(scenario.getTopography()); setTopography(scenario.getTopography());
} catch (IOException | IntrospectionException e) {
e.printStackTrace();
logger.error(e.getLocalizedMessage());
}
} else { } else {
menusInTabs.forEach(menu -> menu.setEnabled(false)); menusInTabs.forEach(menu -> menu.setEnabled(false));
boolean topoWasSelected = false; boolean topoWasSelected = false;
......
package org.vadere.gui.topographycreator.control; package org.vadere.gui.topographycreator.control;
import org.vadere.gui.postvisualization.view.DialogFactory;
import org.vadere.gui.projectview.view.ProjectView;
import org.vadere.gui.topographycreator.model.IDrawPanelModel; import org.vadere.gui.topographycreator.model.IDrawPanelModel;
import org.vadere.gui.topographycreator.model.TopographyCreatorModel; import org.vadere.gui.topographycreator.model.TopographyCreatorModel;
import org.vadere.state.scenario.Topography;
import org.vadere.util.geometry.shapes.VRectangle;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.*; import javax.swing.*;
...@@ -35,27 +29,13 @@ public class ActionResizeTopographyBound extends TopographyAction { ...@@ -35,27 +29,13 @@ public class ActionResizeTopographyBound extends TopographyAction {
action.actionPerformed(e); action.actionPerformed(e);
TopographyCreatorModel model = (TopographyCreatorModel) getScenarioPanelModel(); TopographyCreatorModel model = (TopographyCreatorModel) getScenarioPanelModel();
JTextField textField = new JTextField(); ActionResizeTopographyBoundDialog dialog = new ActionResizeTopographyBoundDialog(
textField.setText(String.format("%.3f x %.3f",
model.getTopography().getBounds().width, model.getTopography().getBounds().width,
model.getTopography().getBounds().height)); model.getTopography().getBounds().height
if (JOptionPane.showConfirmDialog(ProjectView.getMainWindow(), textField, );
"Set width x height", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION){
String input = textField.getText().trim().replace(" ", ""); if (dialog.getValue()){
String[] in = input.split("x"); model.setTopographyBound(dialog.getBound());
double width;
double height;
VRectangle bound = null;
try{
width = Double.valueOf(in[0]);
height = Double.valueOf(in[1]);
bound = new VRectangle(0.0,0.0,width, height);
}catch (NumberFormatException exp){
System.out.println("wrong format");
}
if (bound != null){
model.setTopographyBound(bound);
}
} }
getScenarioPanelModel().notifyObservers(); getScenarioPanelModel().notifyObservers();
} }
......
package org.vadere.gui.topographycreator.control;
import org.vadere.gui.projectview.view.ProjectView;
import org.vadere.util.geometry.shapes.VRectangle;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class ActionResizeTopographyBoundDialog {
JTextField textField;
VRectangle bound;
VRectangle boundOld;
boolean valid;
public ActionResizeTopographyBoundDialog(double width, double height){
textField = new JTextField();
textField.setText(String.format("%.3f x %.3f", width, height));
textField.getDocument().addDocumentListener(new DialogListener());
bound = new VRectangle(0.0,0.0, width, height);
boundOld = new VRectangle(0.0,0.0, width, height);
valid = false;
}
public VRectangle getBound() {
return valid ? bound : boundOld;
}
public boolean getValue(){
return JOptionPane.showConfirmDialog(
ProjectView.getMainWindow(),
textField,
"width x heigt",
JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION;
}
private class DialogListener implements DocumentListener{
JTextField textField;
String text;
DialogListener(){
textField = ActionResizeTopographyBoundDialog.this.textField;
}
@Override
public void insertUpdate(DocumentEvent e) {
handle(e);
}
@Override
public void removeUpdate(DocumentEvent e) {
handle(e);
}
@Override
public void changedUpdate(DocumentEvent e) {
handle(e);
}
private void handle(DocumentEvent e){
text = textField.getText().replace(" ", "");
String[] tmp = text.split("x");
double width;
double height;
try {
width = Double.valueOf(tmp[0]);
height = Double.valueOf(tmp[1]);
ActionResizeTopographyBoundDialog.this.bound =
new VRectangle(0.0,0.0, width, height);
ActionResizeTopographyBoundDialog.this.valid = true;
textField.setForeground(Color.BLACK);
}catch (Exception ex){
ActionResizeTopographyBoundDialog.this.valid = false;
textField.setForeground(Color.RED);
}
}
}
}
...@@ -297,7 +297,7 @@ public class TopographyWindow extends JPanel { ...@@ -297,7 +297,7 @@ public class TopographyWindow extends JPanel {
/* resize Topography */ /* resize Topography */
TopographyAction resizeTopographyBound =new ActionResizeTopographyBound("SetTopograpyBound", TopographyAction resizeTopographyBound =new ActionResizeTopographyBound("SetTopograpyBound",
new ImageIcon(Resources.class.getResource("/icons/paint_method_rectangle_icon.png")), new ImageIcon(Resources.class.getResource("/icons/topography_icon.png")),
panelModel, selectShape); panelModel, selectShape);
addActionToToolbar(toolbar, selectShape, "select_shape_tooltip"); addActionToToolbar(toolbar, selectShape, "select_shape_tooltip");
...@@ -317,7 +317,6 @@ public class TopographyWindow extends JPanel { ...@@ -317,7 +317,6 @@ public class TopographyWindow extends JPanel {
addActionToToolbar(toolbar, openObstacleDialog, "TopographyCreator.btnInsertObstacle.tooltip", addActionToToolbar(toolbar, openObstacleDialog, "TopographyCreator.btnInsertObstacle.tooltip",
obsButton); obsButton);
addActionToToolbar(toolbar, closeDialogAction, "TopographyCreator.btnInsertPedestrian.tooltip"); addActionToToolbar(toolbar, closeDialogAction, "TopographyCreator.btnInsertPedestrian.tooltip");
addActionToToolbar(toolbar, resizeTopographyBound, "TopographyCreator.btnTopographyBound.tooltip");
addActionToToolbar(toolbar, openStairsDialog, "TopographyCreator.btnInsertStairs.tooltip", addActionToToolbar(toolbar, openStairsDialog, "TopographyCreator.btnInsertStairs.tooltip",
stairsButton); stairsButton);
toolbar.addSeparator(new Dimension(5, 50)); toolbar.addSeparator(new Dimension(5, 50));
...@@ -327,6 +326,7 @@ public class TopographyWindow extends JPanel { ...@@ -327,6 +326,7 @@ public class TopographyWindow extends JPanel {
// addActionToToolbar(toolbar, minimizeAction, // addActionToToolbar(toolbar, minimizeAction,
// "TopographyCreator.btnMinimizeTopography.tooltip"); // "TopographyCreator.btnMinimizeTopography.tooltip");
addActionToToolbar(toolbar, maximizeAction, "TopographyCreator.btnMaximizeTopography.tooltip"); addActionToToolbar(toolbar, maximizeAction, "TopographyCreator.btnMaximizeTopography.tooltip");
addActionToToolbar(toolbar, resizeTopographyBound, "TopographyCreator.btnTopographyBound.tooltip");
toolbar.addSeparator(new Dimension(5, 50)); toolbar.addSeparator(new Dimension(5, 50));
addActionToToolbar(toolbar, selectCutAction, "TopographyCreator.btnCutTopography.tooltip"); addActionToToolbar(toolbar, selectCutAction, "TopographyCreator.btnCutTopography.tooltip");
addActionToToolbar(toolbar, resetScenarioAction, "TopographyCreator.btnNewTopography.tooltip"); addActionToToolbar(toolbar, resetScenarioAction, "TopographyCreator.btnNewTopography.tooltip");
......
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