Package editor

Source Code of editor.SchemeResizeDialog

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 java.util.ListIterator;

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 javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import engine.Board;
import engine.Scheme;
import engine.Scheme.Move;
import generator.BoardManager;

/**
* Resize dialog changes size of the given board,
* optionally moves the elements.
*/
public class SchemeResizeDialog extends SchemeDialog implements ActionListener, ChangeListener {
  private static final long serialVersionUID = 21004200910042009L;

  // Interface
  private JLabel lbWidth,lbHeight,lbShiftX,lbShiftY;
  private JSpinner spWidth,spHeight,spShiftX,spShiftY;
  private JButton btResizeOK,btResizeCancel;
  private JCheckBox chPreview;
 
  public SchemeResizeDialog(Frame owner,SchemeDialogListener schemeDialogListener) {
    super(owner,"Zmień rozmiar...",schemeDialogListener);
   
    lbWidth = new JLabel("Szerokość: ");
    spWidth = new JSpinner(new SpinnerNumberModel(0,0,Board.MAX_SIZE,1));
    spWidth.addChangeListener(this);
    lbHeight = new JLabel("Wysokość:  ");
    spHeight = new JSpinner(new SpinnerNumberModel(0,0,Board.MAX_SIZE,1));
    spHeight.addChangeListener(this);
    lbShiftX = new JLabel("Przesunięcie x: ");
    spShiftX = new JSpinner(new SpinnerNumberModel(0,-Board.MAX_SIZE,Board.MAX_SIZE,1));
    spShiftX.addChangeListener(this);
    lbShiftY = new JLabel("Przesunięcie y: ");
    spShiftY = new JSpinner(new SpinnerNumberModel(0,-Board.MAX_SIZE,Board.MAX_SIZE,1));
    spShiftY.addChangeListener(this);
    chPreview = new JCheckBox("Podgląd",true);chPreview.addActionListener(this);
    btResizeOK = new JButton("OK");btResizeOK.addActionListener(this);   
    btResizeCancel = new JButton("Cancel");btResizeCancel.addActionListener(this);
   
    JPanel plParameters = new JPanel();
    plParameters.setLayout(new GridLayout(2,4,10,5));
   
    plParameters.add(lbWidth);
    plParameters.add(spWidth);
    plParameters.add(lbShiftX);
    plParameters.add(spShiftX);
   
    plParameters.add(lbHeight);
    plParameters.add(spHeight);
    plParameters.add(lbShiftY);
    plParameters.add(spShiftY);
   
    JPanel panel2 = new JPanel();
    panel2.add(plParameters,BorderLayout.CENTER);
    this.add(panel2,BorderLayout.CENTER);
    JPanel plButtons = new JPanel();

    plButtons.add(chPreview);
    plButtons.add(btResizeOK);
    plButtons.add(btResizeCancel);
    this.add(plButtons,BorderLayout.SOUTH);

    this.setResizable(false);
    this.setBounds(100,50,420,125);
  }

  private void performResize() {
    // Construct new board
    int shiftx = (Integer)spShiftX.getValue();
    int shifty = (Integer)spShiftY.getValue();
    Board newBoard = BoardManager.getResized(originalScheme.start,
        (Integer)spWidth.getValue(),(Integer)spHeight.getValue(),
        shiftx,shifty);
    newBoard.doFramesToStability();
    // Construct new scheme
    Scheme newScheme = new Scheme(originalScheme);
    ListIterator<Move> iterator = newScheme.moves.listIterator();
    while (iterator.hasNext()) {
      Move move = iterator.next();
      move.x += shiftx;
      move.y += shifty;
    }
    newScheme.setStart(newBoard);
    // Set as a new result
    this.dialogResult(newScheme);
  }
 
  @Override
  public void setScheme(Scheme originalScheme) {
    super.setScheme(originalScheme);
    spWidth.removeChangeListener(this);
    spWidth.setValue(originalScheme.start.width);
    spWidth.addChangeListener(this);
    spHeight.removeChangeListener(this);
    spHeight.setValue(originalScheme.start.height);       
    spHeight.addChangeListener(this);
    spShiftX.removeChangeListener(this);spShiftX.setValue(0);spShiftX.addChangeListener(this);
    spShiftY.removeChangeListener(this);spShiftY.setValue(0);spShiftY.addChangeListener(this);
  }
 
  @Override
  public void actionPerformed(ActionEvent ev) {
    if (ev.getSource() == btResizeOK) {
      if (!chPreview.isSelected())
        performResize();
      this.dialogDone();
    } else
    if (ev.getSource() == btResizeCancel) {
      this.dialogCanceled();
    } else
    if (ev.getSource() == chPreview) {
      if (chPreview.isSelected())
        performResize();
    }
  }

  @Override
  public void stateChanged(ChangeEvent ev) {
    if (chPreview.isSelected())
      performResize();
  }

}
TOP

Related Classes of editor.SchemeResizeDialog

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.