OSPE_QuickJoinSlitsPanel.java 3.3 KB
package osp.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import osp.Mask;
import osp.MaskHistory;

public class OSPE_QuickJoinSlitsPanel extends JDialog {
	private static final long serialVersionUID = 1L;

	private JComboBox<String> startSlit = new JComboBox<String>();
	private JComboBox<String> stopSlit = new JComboBox<String>();
	private JComboBox<String> modSlit = new JComboBox<String>();

	private JButton jButtonOK;
	private JButton jButtonCancel;

	private Mask currentMask;
	private OspeControl control;

	public OSPE_QuickJoinSlitsPanel(Mask msk, OspeControl ctrl, int n, int m) {
		currentMask = msk;
		control=ctrl;
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setAlwaysOnTop(true);

		for (int i = 0; i < Mask._NBRSLITS_; i++) {
			String texte = new String();
			texte = "" + (i + 1);
			startSlit.addItem(texte);
			stopSlit.addItem(texte);
			modSlit.addItem(texte);
		}
		startSlit.setSelectedIndex(n);
		stopSlit.setSelectedIndex(m);
		modSlit.setSelectedIndex(n);

		JPanel firstPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		firstPanel.add(new JLabel("Join Slits from"));
		firstPanel.add(startSlit);
		firstPanel.add(new JLabel("to"));
		firstPanel.add(stopSlit);
		this.getContentPane().add(firstPanel, BorderLayout.NORTH);

		JPanel secondPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		secondPanel.add(new JLabel("Keeping values of slit"));
		secondPanel.add(modSlit);
		this.getContentPane().add(secondPanel, BorderLayout.CENTER);

		JPanel thirdPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		thirdPanel.add(getOkButton());
		thirdPanel.add(getCancelButton());
		this.getContentPane().add(thirdPanel, BorderLayout.SOUTH);

		pack();
		setVisible(true);
	}

	private JButton getCancelButton() {
		if(jButtonCancel == null){
			jButtonCancel = new JButton("Cancel");
			jButtonCancel.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					dispose();
				}
			});
		}
		return jButtonCancel;
	}

	private JButton getOkButton() {
		if(jButtonOK == null){
			jButtonOK = new JButton("OK");
			jButtonOK.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					int startSlitNum = startSlit.getSelectedIndex();
					int stopSlitNum = stopSlit.getSelectedIndex();

					final MaskHistory history = currentMask.getHistory();
					history.addEntry(
							"Join slits " + (startSlitNum + 1) + " to " + (stopSlitNum + 1));
					history.addAction("splitSlits " + startSlitNum + " " + stopSlitNum,
							null);

					for (int i = startSlitNum; i <= stopSlitNum; i++) {
						Double position = new Double(currentMask.getSlit(i).getPosition());
						Double aperture = new Double(currentMask.getSlit(i).getAperture());
						history.addAction("setPosition " + i, position);
						history.addAction("setAperture " + i, aperture);
					}

					currentMask.joinSlits(startSlitNum, stopSlitNum, modSlit.getSelectedIndex());
					dispose();
					
				}
			});
		}
		return jButtonOK;
	}
}