use of mudmap2.backend.PlaceGroup in project mudmap2 by Neop.
the class PlaceDialog method create.
/**
* Creates the dialog
*/
@Override
void create() {
setLayout(new GridLayout(0, 2, 4, 4));
add(new JLabel("Name"));
if (place != null)
textfield_name = new JTextField(place.getName());
else
textfield_name = new JTextField();
add(textfield_name);
place_group_null = new PlaceGroup("none", null);
add(new JLabel("Place group"));
combobox_place_group = new JComboBox<>();
combobox_place_group.addItem(place_group_null);
for (PlaceGroup a : world.getPlaceGroups()) combobox_place_group.addItem(a);
if (place != null && place.getPlaceGroup() != null)
combobox_place_group.setSelectedItem(place.getPlaceGroup());
add(combobox_place_group);
add(new JLabel("Risk level"));
combobox_risk = new JComboBox<>();
for (RiskLevel rl : world.getRiskLevels()) combobox_risk.addItem(rl);
if (place != null && place.getRiskLevel() != null)
combobox_risk.setSelectedItem(place.getRiskLevel());
add(combobox_risk);
Integer min_lvl = 0, max_lvl = 0;
if (place != null) {
min_lvl = place.getRecLevelMin();
max_lvl = place.getRecLevelMax();
}
add(new JLabel("Minimal level"));
spinner_rec_lvl_min = new JSpinner();
spinner_rec_lvl_min.setModel(new SpinnerNumberModel(max(min_lvl, 0), 0, 1000, 1));
add(spinner_rec_lvl_min);
if (min_lvl < 0)
spinner_rec_lvl_min.setEnabled(false);
add(new JLabel());
add(checkbox_lvl_min = new JCheckBox("Show min level"));
checkbox_lvl_min.setSelected(min_lvl >= 0);
checkbox_lvl_min.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
spinner_rec_lvl_min.setEnabled(((JCheckBox) ae.getSource()).isSelected());
}
});
add(new JLabel("Maximum level"));
spinner_rec_lvl_max = new JSpinner();
spinner_rec_lvl_max.setModel(new SpinnerNumberModel(max(max_lvl, 0), 0, 1000, 1));
add(spinner_rec_lvl_max);
if (max_lvl < 0)
spinner_rec_lvl_max.setEnabled(false);
add(new JLabel());
add(checkbox_lvl_max = new JCheckBox("Show max level"));
checkbox_lvl_max.setSelected(max_lvl >= 0);
checkbox_lvl_max.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
spinner_rec_lvl_max.setEnabled(((JCheckBox) ae.getSource()).isSelected());
}
});
JButton button_cancel = new JButton("Cancel");
add(button_cancel);
button_cancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
dispose();
}
});
JButton button_ok = new JButton("Ok");
add(button_ok);
getRootPane().setDefaultButton(button_ok);
button_ok.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
save();
dispose();
}
});
// listener for escape key
getRootPane().registerKeyboardAction(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
dispose();
}
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
pack();
setLocation(getParent().getX() + (getParent().getWidth() - getWidth()) / 2, getParent().getY() + (getParent().getHeight() - getHeight()) / 2);
}
use of mudmap2.backend.PlaceGroup in project mudmap2 by Neop.
the class PlaceDialog method save.
/**
* Saves the place data
*/
public void save() {
// if(!textfield_name.getText().isEmpty()){ // name not empty
try {
if (layer == null)
layer = world.getNewLayer();
// create place if it doesn't exist else just set the name
if (place == null)
layer.put(place = new Place(textfield_name.getText(), px, py, layer));
else
place.setName(textfield_name.getText());
PlaceGroup a = (PlaceGroup) combobox_place_group.getSelectedItem();
// replace null group with null
place.setPlaceGroup(a != place_group_null ? a : null);
place.setRiskLevel((RiskLevel) combobox_risk.getSelectedItem());
if (checkbox_lvl_min.isSelected()) {
place.setRecLevelMin((Integer) spinner_rec_lvl_min.getValue());
} else {
place.setRecLevelMin(-1);
}
if (checkbox_lvl_max.isSelected()) {
place.setRecLevelMax((Integer) spinner_rec_lvl_max.getValue());
} else {
place.setRecLevelMax(-1);
}
} catch (Exception ex) {
Logger.getLogger(PlaceDialog.class.getName()).log(Level.SEVERE, null, ex);
}
// }
getParent().repaint();
}
use of mudmap2.backend.PlaceGroup in project mudmap2 by Neop.
the class PlaceGroupDialog method save.
private void save() {
// add new PlaceGroup to world and place
if (new_group) {
placeGroup = new PlaceGroup(textfield_name.getText(), colorchooserbutton.getColor());
world.addPlaceGroup(placeGroup);
if (place != null && place_group == null)
place.setPlaceGroup(placeGroup);
} else {
// modify PlaceGroup
placeGroup.setName(textfield_name.getText());
placeGroup.setColor(colorchooserbutton.getColor());
}
// assign to all places
if (place_group != null)
for (Place pl : place_group) pl.setPlaceGroup(placeGroup);
getParent().repaint();
}
use of mudmap2.backend.PlaceGroup in project mudmap2 by Neop.
the class PlaceGroupDialog method create.
/**
* Creates the GUI
*/
@Override
void create() {
setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.insets = new Insets(2, 2, 2, 2);
constraints.gridx = 0;
constraints.gridy = 0;
constraints.fill = GridBagConstraints.HORIZONTAL;
add(new JLabel("Name"), constraints);
constraints.gridx = 1;
constraints.gridwidth = 2;
if (placeGroup != null)
textfield_name = new JTextField(placeGroup.toString());
else
textfield_name = new JTextField();
add(textfield_name, constraints);
textfield_name.setColumns(20);
constraints.gridwidth = 1;
constraints.gridx = 0;
constraints.gridy++;
add(new JLabel("Color"), constraints);
constraints.weighty = 4.0;
constraints.gridx = 1;
constraints.fill = GridBagConstraints.BOTH;
constraints.gridwidth = 2;
if (placeGroup != null)
colorchooserbutton = new ColorChooserButton(getParent(), placeGroup.getColor());
else
colorchooserbutton = new ColorChooserButton(getParent());
add(colorchooserbutton, constraints);
constraints.weighty = 1.0;
constraints.gridwidth = 1;
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridx = 0;
constraints.gridy++;
JButton button_cancel = new JButton("Cancel");
add(button_cancel, constraints);
button_cancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
dispose();
}
});
constraints.gridx++;
JButton button_new = new JButton("Add");
button_new.setToolTipText("Create a new place group");
add(button_new, constraints);
getRootPane().setDefaultButton(button_new);
button_new.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
new_group = true;
save();
dispose();
}
});
if (!new_group) {
// don't show edit button when creating a new place
constraints.gridx++;
JButton button_edit = new JButton("Edit");
button_edit.setToolTipText("Edit the current place group");
add(button_edit, constraints);
getRootPane().setDefaultButton(button_edit);
button_edit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
save();
dispose();
}
});
}
pack();
setResizable(false);
setLocation(getParent().getX() + (getParent().getWidth() - getWidth()) / 2, getParent().getY() + (getParent().getHeight() - getHeight()) / 2);
}
use of mudmap2.backend.PlaceGroup in project mudmap2 by Neop.
the class WorldFileDefaultTest method testReadWriteFile.
/**
* Test of readFile an writeFile methods, of class WorldFileDefault.
* @throws java.lang.Exception
*/
@Test
public void testReadWriteFile() throws Exception {
System.out.println("writeFile");
String worldName = "FooBar";
World world = new World(worldName);
Layer layer1 = world.getNewLayer();
Layer layer2 = world.getNewLayer();
String[] placeNames = { "Foo", "Foo Bar", "Baz" };
Integer[] placeX = { 0, 1, 6 };
Integer[] placeY = { 0, 0, 4 };
Place pl0 = new Place(placeNames[0], placeX[0], placeY[0], layer1);
Place pl1 = new Place(placeNames[1], placeX[1], placeY[1], layer1);
Place pl2 = new Place(placeNames[2], placeX[2], placeY[2], layer2);
layer1.put(pl0);
layer1.put(pl1);
layer2.put(pl2);
Path path0 = new Path(pl0, "n", pl1, "s");
Path path1 = new Path(pl1, "e", pl0, "w");
pl0.connectPath(path0);
pl1.connectPath(path1);
pl0.connectChild(pl2);
String pgName0 = "myGroup";
String pgName1 = "my second area";
Color pgCol0 = Color.orange;
Color pgCol1 = Color.blue;
PlaceGroup pg0 = new PlaceGroup(pgName0, pgCol0);
PlaceGroup pg1 = new PlaceGroup(pgName1, pgCol1);
pl0.setPlaceGroup(pg0);
pl1.setPlaceGroup(pg1);
String flag0 = "a";
String flag1 = "cd e";
String flag2 = "fgh";
pl0.setFlag(flag0, true);
pl0.setFlag(flag1, true);
pl0.setFlag(flag2, false);
String comment0 = "This is a test comment";
String comment1 = "and a second comment line";
String comment2 = "and another one";
pl0.addComment(comment0);
pl0.addComment(comment1);
pl0.addComment(comment2);
String wfFile = folder.getRoot() + "/wfj";
WorldFileDefault instanceWriter = new WorldFileDefault(wfFile);
instanceWriter.writeFile(world);
WorldFileDefault instanceReader = new WorldFileDefault(wfFile);
World result = instanceReader.readFile();
assertEquals(worldName, result.getName());
assertEquals(2, result.getLayers().size());
int placeNum = 0;
Layer layer1new = null;
Layer layer2new = null;
for (Layer layer : result.getLayers()) {
placeNum += layer.getPlaces().size();
// find corresponding layers
if (layer.getPlaces().size() == 1)
layer2new = layer;
else if (layer.getPlaces().size() == 2)
layer1new = layer;
}
assertEquals(3, placeNum);
assertNotNull(layer1new);
assertNotNull(layer2new);
Place pl0r = layer1new.get(placeX[0], placeY[0]);
Place pl1r = layer1new.get(placeX[1], placeY[1]);
Place pl2r = layer2new.get(placeX[2], placeY[2]);
assertNotNull(pl0r);
assertNotNull(pl1r);
assertNotNull(pl2r);
assertEquals(pl0.getName(), pl0r.getName());
assertEquals(pl1.getName(), pl1r.getName());
assertEquals(pl2.getName(), pl2r.getName());
assertEquals(pl0.getX(), pl0r.getX());
assertEquals(pl0.getY(), pl0r.getY());
assertEquals(pl1.getX(), pl1r.getX());
assertEquals(pl1.getY(), pl1r.getY());
assertEquals(pl2.getX(), pl2r.getX());
assertEquals(pl2.getY(), pl2r.getY());
assertEquals(pl0r.getLayer(), pl1r.getLayer());
assertNotSame(pl0r.getLayer().getId(), pl2r.getLayer().getId());
assertEquals(2, pl0r.getPaths().size());
assertEquals(2, pl1r.getPaths().size());
assertEquals(0, pl2r.getPaths().size());
Path path0r = pl0r.getPaths().toArray(new Path[2])[0];
Path path1r = pl0r.getPaths().toArray(new Path[2])[1];
Path path2r = pl1r.getPaths().toArray(new Path[2])[0];
Path path3r = pl1r.getPaths().toArray(new Path[2])[1];
assertTrue(path0r != path1r);
assertTrue(path0r == path2r || path0r == path3r);
assertTrue(path1r == path2r || path1r == path3r);
assertEquals(pl0r.getPathTo("n"), pl1r.getPathTo("s"));
assertEquals(pl0r.getPathTo("w"), pl1r.getPathTo("e"));
assertEquals(1, pl0r.getChildren().size());
assertEquals(0, pl1r.getChildren().size());
assertEquals(0, pl2r.getChildren().size());
assertEquals(0, pl0r.getParents().size());
assertEquals(0, pl1r.getParents().size());
assertEquals(1, pl2r.getParents().size());
assertEquals(pl0r.getChildren().toArray(new Place[1])[0], pl2r);
assertEquals(pl2r.getParents().toArray(new Place[1])[0], pl0r);
assertNotNull(pl0r.getPlaceGroup());
assertNotNull(pl1r.getPlaceGroup());
assertNull(pl2r.getPlaceGroup());
assertEquals(pl0.getPlaceGroup().getName(), pl0r.getPlaceGroup().getName());
assertEquals(pl1.getPlaceGroup().getName(), pl1r.getPlaceGroup().getName());
assertEquals(2, pl0r.getFlags().size());
assertEquals(0, pl1r.getFlags().size());
assertEquals(0, pl2r.getFlags().size());
assertTrue(pl0r.getFlag(flag0));
assertTrue(pl0r.getFlag(flag1));
assertFalse(pl0r.getFlag(flag2));
assertEquals(3, pl0r.getComments().size());
assertEquals(0, pl1r.getComments().size());
assertEquals(0, pl2r.getComments().size());
assertTrue(pl0r.getComments().contains(comment0));
assertTrue(pl0r.getComments().contains(comment1));
assertTrue(pl0r.getComments().contains(comment2));
}
Aggregations