package editor;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import engine.Board;
import engine.Scheme;
import generator.BoardManager;
import generator.OutlineGenerator;
/**
* Generates new board using OutlineGenerator with given parameters.
*/
public class SchemeGenerateOutlineDialog extends SchemeDialog implements ActionListener {
private static final long serialVersionUID = 11404200914042009L;
private JLabel lbSolid, lbElevator, lbPainter, lbLava, lbIce, lbHeight, lbWidth, lbSolidConcrete;
private JSpinner spSolid, spElevator, spPainter, spLava, spIce, spHeight, spWidth, spSolidConcrete;
private JButton btOutlineOK, btOutlineCancel, btOutlineGenerate;
private JCheckBox chCohesion, chRemoveSurroundedSolids;
private boolean generated;
public SchemeGenerateOutlineDialog(
Frame owner,SchemeDialogListener schemeDialogListener) {
super(owner,"Generuj szkic...",schemeDialogListener);
lbSolid = new JLabel("Współczynnik kamieni: ");
spSolid = new JSpinner(new SpinnerNumberModel(0.2,0.0,1.0,0.1));
lbElevator = new JLabel("Współczynnik wind: ");
spElevator = new JSpinner(new SpinnerNumberModel(0.0,0.0,1.0,0.1));
lbPainter = new JLabel("Współczynnik malarzy: ");
spPainter = new JSpinner(new SpinnerNumberModel(0.0,0.0,1.0,0.1));
lbLava = new JLabel("Współczynnik lawy: ");
spLava = new JSpinner(new SpinnerNumberModel(0.0,0.0,1.0,0.1));
lbIce = new JLabel("Współczynnik lodu: ");
spIce = new JSpinner(new SpinnerNumberModel(0.0,0.0,1.0,0.1));
lbSolidConcrete = new JLabel("Współczynnik cementu: ");
spSolidConcrete = new JSpinner(new SpinnerNumberModel(0.0,0.0,1.0,0.1));
lbHeight = new JLabel("Wysokość: ");
spHeight = new JSpinner(new SpinnerNumberModel(0,0,Board.MAX_SIZE,1));
lbWidth = new JLabel("Szerokość: ");
spWidth = new JSpinner(new SpinnerNumberModel(0,0,Board.MAX_SIZE,1));
chCohesion = new JCheckBox("Spójność",true);
chRemoveSurroundedSolids = new JCheckBox("Bez otoczonych kamieni",false);
JPanel pWidth = new JPanel();
pWidth.add(lbWidth);pWidth.add(spWidth);
JPanel pHeight = new JPanel();
pHeight.add(lbHeight);pHeight.add(spHeight);
JPanel panel = new JPanel(new GridLayout(7,2,2,5));
panel.add(pWidth);panel.add(pHeight);
panel.add(lbSolid);panel.add(spSolid);
panel.add(lbElevator);panel.add(spElevator);
panel.add(lbPainter);panel.add(spPainter);
panel.add(lbLava);panel.add(spLava);
panel.add(lbIce);panel.add(spIce);
panel.add(lbSolidConcrete);panel.add(spSolidConcrete);
JPanel panelCenter = new JPanel();
panelCenter.add(panel, BorderLayout.CENTER);
panelCenter.add(chCohesion);
panelCenter.add(chRemoveSurroundedSolids);
this.getContentPane().add(panelCenter,BorderLayout.CENTER);
panel = new JPanel();
btOutlineGenerate = new JButton("Generuj");
btOutlineGenerate.addActionListener(this);
panel.add(btOutlineGenerate);
btOutlineOK = new JButton("OK");
btOutlineOK.addActionListener(this);
panel.add(btOutlineOK);
btOutlineCancel = new JButton("Anuluj");
btOutlineCancel.addActionListener(this);
panel.add(btOutlineCancel);
this.getContentPane().add(panel,BorderLayout.SOUTH);
this.setBounds(100,50,345,345);
this.setResizable(false);
}
private void generate() {
Board newBoard = OutlineGenerator.generateOutline(
(Integer)spWidth.getValue(), (Integer)spHeight.getValue(),
(Double)spSolid.getValue(), (Double)spIce.getValue(),
(Double)spLava.getValue(), (Double)spElevator.getValue(),
(Double)spPainter.getValue(), (Double) spSolidConcrete.getValue());
if (chRemoveSurroundedSolids.isSelected()) BoardManager.removeSurroundedSolids(newBoard);
if (chCohesion.isSelected()) BoardManager.makeConnected(newBoard);
generated = true;
dialogResult(new Scheme(newBoard));
}
@Override
public void setScheme(Scheme originalScheme) {
super.setScheme(originalScheme);
if (originalScheme.start.width == 0)
spWidth.setValue(12); else
spWidth.setValue(originalScheme.start.width);
if (originalScheme.start.height == 0)
spHeight.setValue(8); else
spHeight.setValue(originalScheme.start.height);
generated = false;
}
@Override
public void actionPerformed(ActionEvent ev) {
if (ev.getSource() == btOutlineGenerate) {
generate();
} else
if (ev.getSource() == btOutlineOK) {
if (!generated)
generate();
dialogDone();
} else
if (ev.getSource() == btOutlineCancel) {
dialogCanceled();
}
}
}