OSPE_PickObjectFrame.java 3.96 KB
package osp.ui;

import java.awt.Container;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Point2D;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import jsky.image.gui.PickObjectFrame;
import osp.utils.CustomJDialog;

/**
 * Jsky utility to choose objects on the image with theirs characteristics and
 * their center
 * 
 * @author SB Update : EB
 */
public class OSPE_PickObjectFrame extends PickObjectFrame {

	private static final long serialVersionUID = 1L;

	protected OSPE_VisuPanel visuPanel;
	protected JButton closeButton;
	protected int addedObjects;

	private JButton btCancel;

	public OSPE_PickObjectFrame(OSPE_VisuPanel v) {
		super(v.getNavigatorImageDisplay());
		visuPanel = v;
		this.setLocationRelativeTo(visuPanel);
		Container c = getContentPane();

		JPanel p0 = (JPanel) c.getComponent(0); // Main Panel
		/*
		 * Il n'ya qu'un seul panel dans le Container p0 a 3 panels : 0 : image
		 * zoom de l'objet qd bt pick selectionne 1 : Image statistics. 2 :
		 * Boutons panel 3 : Multi-pick option (checkBox)
		 */
			
		// Multi-Pick option
		JPanel p3 = (JPanel) getPickObject().getComponent(3);
		JPanel p2 = (JPanel) p0.getComponent(2); // buttons panel
		JButton addButton = (JButton) p2.getComponent(1);
		addButton.setVisible(false);
		
		JButton pickButton = (JButton) p2.getComponent(0);
		pickButton.setVisible(false);
		
		closeButton = (JButton) p2.getComponent(2); // Close button

		getPickObject().remove(3); // enleve (aussi) le panel Multi-Pick option

		this.setAlwaysOnTop(true);
		this.setVisible(true);
		pickButton.doClick();

		// keep the windows on first plan during pick operation
		// Active le bouton Pick
		// btP.doClick();
		// selection of the multi-Pick option
		this.setTitle("Add Objects");
		((JCheckBox) p3.getComponent(0)).setSelected(true);
		addedObjects = 0;

		p2.add(getCancelButton(), 0);

		// Add objects
		getPickObject().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				Point2D.Double pos = new Point2D.Double(getPickObject().getStatistics().getImageX(),
						getPickObject().getStatistics().getImageY());

				visuPanel.addObject("-1",-1," ", pos,0);

				if (visuPanel.getFov().getCurrentImage().getNbrObjects() > 0 && 
					visuPanel.getFov().getCurrentImage().getLastObject().getPosition().equals(pos))
					addedObjects++;
				
			}
		});

		closeButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				close();
			}
		});
		
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				closeButton.doClick();
			}
		});		
	}

	
	private JButton getCancelButton() {
		if (btCancel == null){
			btCancel = new JButton("Cancel");
			btCancel.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					if (addedObjects > 0) {
						String mess = "You are going to cancel and remove all the objects you added. Are you sure?";
						CustomJDialog dialog = new CustomJDialog(mess, CustomJDialog._WARNING_,
								CustomJDialog._YESNO_BUTTON_, OSPE_PickObjectFrame.this);

						if (dialog.getValue() == JOptionPane.YES_OPTION) {
							for (int i = 0; i < addedObjects; i++)
								visuPanel.getFov().getCurrentImage().removeLastObject();

							visuPanel.getNavigatorImageDisplay().updateImage();
							visuPanel.updateByHand();
							visuPanel.getOspeControl().getActionControl().setSaveNeeded(false);
							closeButton.doClick();
						}
					} else{
						closeButton.doClick();
					}
				}
			});
		}
		
		return btCancel;
	}
	
	/**
	 * 
	 */
	private void close() {
		visuPanel.closePickObjFrame();
	}
}