package editor;
import generator.BlocksGenerator;
import generator.MixGenerator;
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.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import engine.Scheme;
/**
* The blocks generator dialog, uses BlocksGenerator and MixGenerator to
* create and mix blocks on the board.
*/
public class SchemeGenerateBlocksDialog extends SchemeDialog implements
ActionListener {
private static final long serialVersionUID = 12104200921042009L;
private JLabel lbIterationNumber, lbEquivalentPbb, lbPreviousPbb;
private JSpinner spIterationNumber,spEquivalentPbb, spPreviouPbb;
private JButton btGenerateBlocks, btGenerateEquivalent, btGeneratePrevious;
private JButton btOriginalScheme, btOK,btCancel;
private Scheme currentScheme;
public SchemeGenerateBlocksDialog(
Frame owner,SchemeDialogListener schemeDialogListener) {
super(owner,"Generuj bloki...",schemeDialogListener);
lbIterationNumber = new JLabel(" iteracje: ");
spIterationNumber = new JSpinner(new SpinnerNumberModel(5,1,200,1));
lbEquivalentPbb = new JLabel(" ppb.: ");
spEquivalentPbb = new JSpinner(new SpinnerNumberModel(0.0,0.0,1.0,0.1));
lbPreviousPbb = new JLabel(" ppb.: ");
spPreviouPbb = new JSpinner(new SpinnerNumberModel(0.0,0.0,1.0,0.1));
btGenerateBlocks = new JButton("Generuj bloki");
btGenerateBlocks.addActionListener(this);
btGenerateEquivalent = new JButton("Mieszaj równoważnie");
btGenerateEquivalent.addActionListener(this);
btGeneratePrevious = new JButton("Mieszaj ze skokami");
btGeneratePrevious.addActionListener(this);
btOriginalScheme = new JButton("Przywróć orginalny");
btOriginalScheme.addActionListener(this);
btOK = new JButton("OK");btOK.addActionListener(this);
btCancel = new JButton("Anuluj");btCancel.addActionListener(this);
JPanel pEq = new JPanel(new GridLayout(1,2,2,3));
pEq.add(lbEquivalentPbb); pEq.add(spEquivalentPbb);
JPanel pPrv = new JPanel(new GridLayout(1,2,2,3));
pPrv.add(lbPreviousPbb); pPrv.add(spPreviouPbb);
JPanel pIter = new JPanel(new GridLayout(1,2,2,3));
pIter.add(lbIterationNumber); pIter.add(spIterationNumber);
JPanel panel = new JPanel(new GridLayout(3,2,5,5));
panel.add(btGenerateBlocks); panel.add(pIter);
panel.add(btGenerateEquivalent); panel.add(pEq);
panel.add(btGeneratePrevious); panel.add(pPrv);
JPanel pOKCancel = new JPanel();
pOKCancel.add(btOriginalScheme); pOKCancel.add(btOK); pOKCancel.add(btCancel);
JPanel panelCenter = new JPanel();
panelCenter.add(panel, BorderLayout.CENTER);
this.getContentPane().add(panelCenter, BorderLayout.CENTER);
this.getContentPane().add(pOKCancel,BorderLayout.SOUTH);
this.setBounds(100,50,375,165);
this.setResizable(false);
}
@Override
public void setScheme(Scheme originalScheme) {
super.setScheme(originalScheme);
currentScheme = new Scheme(originalScheme);
}
@Override
public void actionPerformed(ActionEvent ev) {
if (ev.getSource() == btGenerateBlocks) {
for (int i = 0; i < (Integer)spIterationNumber.getValue(); i++) {
Scheme newScheme = BlocksGenerator.generateBlocks(currentScheme.start);
newScheme.appendMoves(currentScheme);currentScheme = newScheme;
newScheme = MixGenerator.generatePrevious(
(Double)spPreviouPbb.getValue(), currentScheme);
newScheme.appendMoves(currentScheme); currentScheme = newScheme;
newScheme = MixGenerator.generateEquivalent(
(Double)spEquivalentPbb.getValue(), currentScheme);
newScheme.appendMoves(currentScheme); currentScheme = newScheme;
}
currentScheme.optimizeMoves();
dialogResult(currentScheme);
} else
if (ev.getSource() == btGenerateEquivalent) {
Scheme mixedScheme = MixGenerator.generateEquivalent(
(Double)spEquivalentPbb.getValue(), currentScheme);
mixedScheme.appendMoves(currentScheme);
currentScheme = mixedScheme;
dialogResult(currentScheme);
} else
if (ev.getSource() == btGeneratePrevious) {
Scheme mixedScheme = MixGenerator.generatePrevious(
(Double)spPreviouPbb.getValue(), currentScheme);
mixedScheme.appendMoves(currentScheme);
currentScheme = mixedScheme;
dialogResult(currentScheme);
} else
if (ev.getSource() == btOriginalScheme) {
currentScheme = originalScheme;
dialogResult(originalScheme);
} else
if (ev.getSource() == btOK) {
dialogDone();
} else
if (ev.getSource() == btCancel) {
dialogCanceled();
}
}
}