Commit 0f66b211 authored by lafabregue's avatar lafabregue
Browse files

clean merge files

parent afdfc085
package mustic.gui.dialog.classifier;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.InputEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.Vector;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import com.l2fprod.common.swing.JTaskPane;
import com.l2fprod.common.swing.JTaskPaneGroup;
import jcl.clustering.constraints.CannotLinkConstraint;
import jcl.clustering.constraints.ClusterDiameterConstraint;
import jcl.clustering.constraints.Constraint;
import jcl.clustering.constraints.LabelConstraint;
import jcl.clustering.constraints.MustLinkConstraint;
import jcl.clustering.constraints.NbClusterConstraint;
import jcl.data.Data;
import jcl.utils.Images.StreamedImageReaderWrapper;
import mustic.gui.tools.ZoomBoxPanel;
import mustic.gui.tools.Zoomable;
import mustic.io.RawImage;
import mustic.utils.filters.CSVFileFilter;
import mustic.utils.io.CSVUtils;
/**
* Dialog window that allows the user to generate a set of constraints
*
* @author Baptiste LAFABREGUE
*
*/
public class ConstraintsSelectionDialog extends JInternalFrame implements Zoomable {
private static final long serialVersionUID = 1L;
/** data related fields */
private RawImage image = null;
private int width = -1;
private double minDistance = 0;
private double maxDistance = 100;
private Data data = null;
/** Source image */
private BufferedImage imageSource = null;
/** Image with constraints displayed */
private BufferedImage imagePoints = null;
/** Image displayed */
private BufferedImage mNew = null;
<<<<<<< HEAD
=======
>>>>>>> colorsClass
/** Panel that contains the displayed image */
private JPanel displayPanel = null;
public JPanel mainPanel;
protected int labelClassesCount = 1;
/** inner panel of task groups per constraint types */
private JPanel mustLinkContainer = new JPanel();
private JPanel cannotLinkContainer = new JPanel();
private JPanel labelContainer = new JPanel();
/** zoom level */
private float mZoomRate = 1.0f;
/** zoom step */
private float mZoomStep = 0.1f;
private JPanel panelImage = new JPanel(new BorderLayout());
/** set of created constraints */
private Vector<Constraint> constraints = new Vector<Constraint>();
/** panels linked to each constraints */
private Vector<JPanel> constraintsPanels = new Vector<JPanel>();
/** other components that must be disable or enabled */
private Vector<Component> otherComponentList = new Vector<Component>();
/** Cluster related components */
private JCheckBox nbClusterCheckBox;
private JTextField nbClusterMaxTextField;
private JTextField nbClusterMinTextField;
private JCheckBox clusterDiameterCheckBox;
private JTextField diameterTextField;
/** Record steps of constraint construction */
private int currentStep = 0;
static final private int MustLink_step1 = 1;
static final private int MustLink_step2 = 2;
static final private int CannotLink_step1 = 3;
static final private int CannotLink_step2 = 4;
static final private int Label_step1 = 5;
private int previousIndex = -1;
/**
* Colors and buttons linked to Must Link, Cannot Link and Label constraints
**/
/** Hide/Display constraints related components */
private JCheckBox check;
/** Dialog for choosing colors for the constraints*/
JDialog jd_colorsChooser;
/** constraints colors */
private Color mustLinkColor = new Color(124, 252, 0);
private Color cannotLinkColor = new Color(255, 0, 255);
private Vector<Color> labelConstraintColor = new Vector<Color>();
<<<<<<< HEAD
private Vector<BufferedImage> imageLabelPixel = new Vector<BufferedImage>();
private Vector<BufferedImage> imageLabelPixelTemp = new Vector<BufferedImage>();
/** temporary constraints colors */
private Color mustLinkColorTemp;
private Color cannotLinkColorTemp;
private Vector<Color> labelConstraintColorTemp = new Vector<Color>();
/** constraints buttons */
private JButton jb_mustLinkChooseColor = new JButton();
private JButton jb_cannotLinkChooseColor = new JButton();
private Vector<JButton> jb_labelConstraintClassChooseColor = new Vector<JButton>();
/** dynamic JPanel Colors of Label Constraints Classes*/
JPanel labelColorConstraintPanel = new JPanel(new GridLayout(0,1));
=======
>>>>>>> colorsClass
/** Images and constraints zoom */
/** Constraint images related components */
File inputImage = new File(getClass().getResource("/icons/icon-pince.png").getFile());
private BufferedImage imagePixel;
private BufferedImage imagePixelML;
private BufferedImage imagePixelCL;
<<<<<<< HEAD
private BufferedImage imagePixelMLTemp;
private BufferedImage imagePixelCLTemp;
=======
private Vector<BufferedImage> imageLabelPixel = new Vector<BufferedImage>();
>>>>>>> colorsClass
private int classID;
private JPanel container;
/**
* Constructor to a dialog window to visualize and build a set of
* constraints
*
* @param img
* the image on which the constraints have to be build
* @param data
* (optional) the data related to the constraints
*/
public ConstraintsSelectionDialog(RawImage img, Data data) {
super();
mainPanel = new JPanel(new BorderLayout());
if (data != null) {
// case of Data provided
minDistance = data.getEstimatedMinDistance();
maxDistance = data.getEstimatedMaxDistance();
this.data = data;
} else {
// otherwise we base our estimation on the image
StreamedImageReaderWrapper reader = new StreamedImageReaderWrapper(img.getPath());
minDistance = 0;
maxDistance = reader.getMaxValue() - reader.getMinValue();
}
// display the image
this.displayPanel = (JPanel) new ZoomBoxPanel(this);
this.displayPanel.addMouseWheelListener(new MouseWheelListener() {
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
<<<<<<< HEAD
=======
>>>>>>> colorsClass
// zoom in zoom box
if((e.getModifiers() & InputEvent.CTRL_MASK) == InputEvent.CTRL_MASK) {
if (e.getUnitsToScroll() > 0) {
((ZoomBoxPanel) displayPanel).zoomInBox();
} else {
((ZoomBoxPanel) displayPanel).zoomOutBox();
}
}
// resize zoom box area
else if((e.getModifiers() & InputEvent.ALT_MASK) == InputEvent.ALT_MASK) {
if (e.getUnitsToScroll() > 0) {
((ZoomBoxPanel) displayPanel).increaseZoomBoxArea();
} else {
((ZoomBoxPanel) displayPanel).decreaseZoomBoxArea();
}
<<<<<<< HEAD
=======
}
// zoom in image
else {
if (e.getUnitsToScroll() > 0) {
zoomOut();
} else {
zoomIn();
}
>>>>>>> colorsClass
}
// zoom in image
else {
if (e.getUnitsToScroll() > 0) {
zoomOut();
} else {
zoomIn();
}
}
}
});
this.displayPanel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// mouse focus on the window
displayPanel.requestFocusInWindow();
// point coordinates relative to img
int x = (int) ((e.getX()
- (int) ((displayPanel.getWidth() - (mNew.getWidth())) / 2.0)) / mZoomRate);
int y = (int) ((e.getY()
- (int) ((displayPanel.getHeight() - (mNew.getHeight())) / 2.0)) / mZoomRate);
if (x >= 0 && y >= 0 && x < imageSource.getWidth() * mZoomRate
&& y < imageSource.getHeight() /* mZoomRate*/) {
switch (currentStep) {
case MustLink_step1:
case CannotLink_step1:
case Label_step1:
addFirstPixel(x, y);
break;
case MustLink_step2:
case CannotLink_step2:
addSecondPixel(x, y);
break;
default:
// we don't refresh if not a valid step
return;
}
displayPixels();
}
}
@Override
public void mouseEntered(MouseEvent e) {
displayPanel.requestFocusInWindow();
}
});
panelImage.add(this.displayPanel, BorderLayout.CENTER);
setImage(img);
JPanel panelButton = new JPanel();
JButton buttonOk = new JButton(Messages.getString("ConstraintsSelectionDialog.50")); //$NON-NLS-1$
buttonOk.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (ConstraintsSelectionDialog.this.data != null) {
ConstraintsSelectionDialog.this.data.updateAndSetConstraintsToSample(getConstraints());
}
ConstraintsSelectionDialog.this.dispose();
}
});
panelButton.add(buttonOk);
otherComponentList.add(buttonOk);
JButton buttonCancel = new JButton(Messages.getString("ConstraintsSelectionDialog.55")); //$NON-NLS-1$
buttonCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ConstraintsSelectionDialog.this.dispose();
}
});
panelButton.add(buttonCancel);
otherComponentList.add(buttonCancel);
JButton importButton = new JButton(Messages.getString("ConstraintsSelectionDialog.51")); //$NON-NLS-1$
importButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
importConstraints();
}
});
panelButton.add(importButton);
otherComponentList.add(importButton);
JButton exportButton = new JButton(Messages.getString("ConstraintsSelectionDialog.52")); //$NON-NLS-1$
exportButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
exportConstraints();
}
});
panelButton.add(exportButton);
otherComponentList.add(exportButton);
JButton constraintsColorChooser = new JButton(
Messages.getString("ConstraintsSelectionDialog.56")); //$NON-NLS-1$
constraintsColorChooser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// action for color chooser
new ConstraintsColor(ConstraintsSelectionDialog.this,mustLinkColor,cannotLinkColor,labelConstraintColor);
displayPixels();
}
});
panelButton.add(constraintsColorChooser);
<<<<<<< HEAD
=======
>>>>>>> colorsClass
/** Displays or hides current constraints on the image */
check = new JCheckBox(Messages.getString("ConstraintsSelectionDialog.57")); //$NON-NLS-1$
check.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(check.isSelected()) {
doZoomedOp();
}
else {
displayPixels();
}
}
});
panelButton.add(check);
initializeConstraintsColors();
otherComponentList.add(constraintsColorChooser);
JCheckBox zoomBoxChkBox = new JCheckBox(Messages.getString("ConstraintsSelectionDialog.58"));
<<<<<<< HEAD
=======
// key listener for the keyJCheckBox
>>>>>>> colorsClass
this.displayPanel.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
// enable zoom box
if(e.getKeyCode() == KeyEvent.VK_Z) {
zoomBoxChkBox.doClick();
}
// hide constraints
if(e.getKeyCode() == KeyEvent.VK_H) {
check.doClick();
}
}
@Override
public void keyReleased(KeyEvent e) {
}
});
this.displayPanel.setFocusable(true);
zoomBoxChkBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
((ZoomBoxPanel) displayPanel).setZoomBoxEnabled(true);
} else {
((ZoomBoxPanel) displayPanel).setZoomBoxEnabled(false);
}
}
});
panelButton.add(zoomBoxChkBox);
otherComponentList.add(zoomBoxChkBox);
JScrollPane jpane = new JScrollPane(panelImage);
jpane.setBorder(BorderFactory.createTitledBorder(Messages.getString("ConstraintsSelectionDialog.7"))); //$NON-NLS-1$
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(buildInfoPanel()), jpane);
splitPane.setDividerLocation(390);
mainPanel.add(splitPane, BorderLayout.CENTER);
// mainPanel.add(buildInfoPanel(), BorderLayout.WEST);
// mainPanel.add(jpane, BorderLayout.CENTER);
mainPanel.add(panelButton, BorderLayout.SOUTH);
if (data != null && data.getConstraints() != null) {
setConstraints(data.getConstraints(), false);
}
doZoomedOp();
this.setContentPane(mainPanel);
this.setTitle(Messages.getString("ConstraintsSelectionDialog.8")); //$NON-NLS-1$
this.setVisible(true);
}
/**
* Initialize button colors add actions for the
* must-link and cannot link chooser color buttons
*/
private void initializeConstraintsColors() {
try {
imagePixel = ImageIO.read(inputImage);
imagePixelML = clone(imagePixel);
imagePixelCL = clone(imagePixel);
} catch (IOException e1) {
e1.printStackTrace();
}
<<<<<<< HEAD
jb_mustLinkChooseColor.setBackground(mustLinkColor);
jb_cannotLinkChooseColor.setBackground(cannotLinkColor);
changeColorPixel(imagePixelML, mustLinkColor);
jb_mustLinkChooseColor.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
chooseMustLinkColor();
}
});
changeColorPixel(imagePixelCL, cannotLinkColor);
jb_cannotLinkChooseColor.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
chooseCannotLinkColor();
}
});
=======
// paints the image with the color of the mustlink and cannotlink
changeColorPixel(imagePixelML, mustLinkColor);
changeColorPixel(imagePixelCL, cannotLinkColor);
>>>>>>> colorsClass
// initializes the color,
// panel containing the class name + it's button for the first label
// Constraint Class
Random rand = new Random();
float r = rand.nextFloat();
float g = rand.nextFloat();
float b = rand.nextFloat();
labelConstraintColor.add(new Color(r, g, b));
BufferedImage copy = clone(imagePixel);
changeColorPixelLabel(copy, 0,labelConstraintColor);
imageLabelPixel.add(copy);
<<<<<<< HEAD
imageLabelPixelTemp.add(clone(imagePixel));
imagePixelCLTemp = clone(imagePixel);
imagePixelMLTemp = clone(imagePixel);
jb_labelConstraintClassChooseColor.add(new JButton());
jb_labelConstraintClassChooseColor.get(0).putClientProperty("index", 0);
jb_labelConstraintClassChooseColor.get(0).setBackground(labelConstraintColor.get(0));
createLabelColorConstraintsPanel();
=======
>>>>>>> colorsClass
}
protected float getmZoomRate() {
return mZoomRate;
}
protected JPanel getImgPanel() {
return panelImage;
}
@Override
public BufferedImage getDisplayedImage() {
return mNew;
}
protected void exportConstraints() {
boolean isImageRelated = true;
if (data != null) {
// choose between image or data indexes
String[] possibilities = { Messages.getString("ConstraintsSelectionDialog.521"),
Messages.getString("ConstraintsSelectionDialog.522") };
String s = (String) JOptionPane.showInputDialog(this, Messages.getString("ConstraintsSelectionDialog.520"),
Messages.getString("ConstraintsSelectionDialog.523"), JOptionPane.PLAIN_MESSAGE, null,
possibilities, Messages.getString("ConstraintsSelectionDialog.522"));
// if data is selected
if (s.equals(Messages.getString("ConstraintsSelectionDialog.522"))) {
isImageRelated = false;
}
}
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new CSVFileFilter());
String path = "~/constraints.csv";
fileChooser.setSelectedFile(new File(path));
int returnVal = fileChooser.showDialog(this, Messages.getString("ConstraintsSelectionDialog.52")); //$NON-NLS-1$
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
Vector<Constraint> constrainsToExport = null;
if (isImageRelated) {
constrainsToExport = getConstraints();
} else {
constrainsToExport = data.updateAndSetConstraintsToSample(getConstraints());
}
String filename = fileChooser.getSelectedFile().getPath();
if (!filename.endsWith(".csv") && !filename.endsWith(".CSV"))
filename += ".csv"; //$NON-NLS-1$
FileOutputStream out = new FileOutputStream(filename);
PrintWriter writer = new PrintWriter(out);
for (Constraint c : constrainsToExport) {
writer.println(c);
}
writer.close();
out.close();
JOptionPane.showMessageDialog(this, Messages.getString("ConstraintsSelectionDialog.525"),
Messages.getString("ConstraintsSelectionDialog.526"), //$NON-NLS-1$ //$NON-NLS-2$
JOptionPane.INFORMATION_MESSAGE);
} catch (FileNotFoundException e) {
e.printStackTrace();