Commit a1c233f6 authored by lafabregue's avatar lafabregue
Browse files

update currentview to ArrayList

parent fd23a4b8
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.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
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.List;
import java.util.Random;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
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.SwingConstants;
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.io.RawImage;
import mustic.utils.CSVUtils;
import mustic.utils.filters.CSVFileFilter;
/**
* Dialog window that allow the user to generate a set of constraints
*
* @author Baptiste LAFABREGUE
*
*/
public class ConstraintsSelectionDialog extends JInternalFrame {
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;
private BufferedImage imagePoints = null;
private BufferedImage imageSource = null;
private JLabel labelImage;
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>();
/** colours linked to each constraints */
private Vector<Color> constraintsColors = new Vector<Color>();
/** 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;
/**
* Constructor to a dialog window to visualise 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.labelImage = new JLabel();
this.labelImage.setHorizontalAlignment(SwingConstants.CENTER);
panelImage.add(this.labelImage, BorderLayout.CENTER);
this.labelImage.addMouseWheelListener(new MouseWheelListener() {
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
if (e.getUnitsToScroll() > 0) {
zoomIn();
} else {
zoomOut();
}
}
});
this.labelImage.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int x = (int) ((e.getX() - (int) ((labelImage.getWidth() - (labelImage.getIcon().getIconWidth())) / 2.0)) / mZoomRate);
int y = (int) ((e.getY() - (int) ((labelImage.getHeight() - (labelImage.getIcon().getIconHeight())) / 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();
}
}
});
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) {
ConstraintsSelectionDialog.this.dispose();
}
});
panelButton.add(buttonOk);
otherComponentList.add(buttonOk);
JButton importButton = new JButton(Messages.getString("ConstraintsSelectionDialog.51"));
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"));
exportButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
exportConstraints();
}
});
panelButton.add(exportButton);
otherComponentList.add(exportButton);
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);
this.setContentPane(mainPanel);
this.setTitle(Messages.getString("ConstraintsSelectionDialog.8")); //$NON-NLS-1$
this.setVisible(true);
}
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();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
protected void importConstraints() {
try {
JFileChooser fileChooser = new JFileChooser();
CSVFileFilter filter = new CSVFileFilter();
fileChooser.addChoosableFileFilter(filter);
fileChooser.setFileFilter(filter);
fileChooser.setAcceptAllFileFilterUsed(true);
fileChooser.setMultiSelectionEnabled(false);
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
this.setCursor(new Cursor(Cursor.WAIT_CURSOR));
boolean imageOuvre = openFile(file);
if (!imageOuvre)
JOptionPane.showMessageDialog(this, Messages.getString("ConstraintsSelectionDialog.53"),
Messages.getString("ConstraintsSelectionDialog.54"), JOptionPane.ERROR_MESSAGE);
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, Messages.getString("ConstraintsSelectionDialog.53"),
Messages.getString("ConstraintsSelectionDialog.54"), JOptionPane.ERROR_MESSAGE);
ex.printStackTrace();
}
this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
private boolean openFile(File file) {
System.out.println("__________________________________________________________________________________________\n"); //$NON-NLS-1$
System.out.println("Opening file : " + file.getPath()); //$NON-NLS-1$
System.out.println("__________________________________________________________________________________________"); //$NON-NLS-1$
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.524"),
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;
}
}
CSVUtils reader = null;
try {
reader = new CSVUtils(file);
clear();
List<String> line;
while((line = reader.readNext()) != null) {
addConstraint(line, isImageRelated);
}
enableAllComponents();
updateUI();
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
return true;
}
private void addConstraint(List<String> line, boolean isImageRelated) {
switch (Integer.parseInt(line.get(0))) {
case Constraint.MUST_LINK_TYPE :
try {
createMustLinkConstraint();
int index1 = getIndex(Integer.parseInt(line.get(1)), isImageRelated);
int index2 = getIndex(Integer.parseInt(line.get(2)), isImageRelated);
constraints.add(new MustLinkConstraint(index1, index2));
currentStep = -1;
// we update text fields
((JTextField) constraintsPanels.get(constraintsPanels.size()-1)
.getComponent(1)).setText(""+index1);
((JTextField) constraintsPanels.get(constraintsPanels.size()-1)
.getComponent(3)).setText(""+index2);
} catch(Exception e) {
e.printStackTrace();
}
break;
case Constraint.CANNOT_LINK_TYPE :
try {
int index1 = getIndex(Integer.parseInt(line.get(1)), isImageRelated);
int index2 = getIndex(Integer.parseInt(line.get(2)), isImageRelated);
createCannotLinkConstraint();
constraints.add(new CannotLinkConstraint(index1, index2));
currentStep = -1;
// we update text fields
((JTextField) constraintsPanels.get(constraintsPanels.size()-1)
.getComponent(1)).setText(""+index1);
((JTextField) constraintsPanels.get(constraintsPanels.size()-1)
.getComponent(3)).setText(""+index2);
} catch(Exception e) {
e.printStackTrace();
}
break;
case Constraint.LABEL_TYPE :
try {
int index = getIndex(Integer.parseInt(line.get(1)), isImageRelated);
createLabelConstraint();
constraints.add(new LabelConstraint(index,
Integer.parseInt(line.get(2))));
currentStep = -1;
// we update text fields
((JTextField) constraintsPanels.get(constraintsPanels.size()-1)
.getComponent(1)).setText(""+index);
} catch(Exception e) {
e.printStackTrace();
}
break;
}
}
/**
* This method allow to get the image related index
* given a data or an image related index
*
* @param index
* the index
* @param isImageRelated
* true if related
*
* to the image, false if related to the data
* @return the image related index
*/
private int getIndex(int index, boolean isImageRelated) {
if(isImageRelated || data.getSampler() == null)
return index;
//TODo take in consideration the geotranslate vectors to have the coorest index
return data.getSampler().getSampleIndexes().get(index);
}
private JPanel buildInfoPanel() {
JPanel panelInfos = new JPanel(new BorderLayout());
panelInfos.setBorder(BorderFactory.createTitledBorder(Messages.getString("ConstraintsSelectionDialog.0"))); //$NON-NLS-1$
panelInfos.setPreferredSize(new Dimension(370, 300));
JTaskPane taskPane = new JTaskPane();
JTaskPaneGroup mustLinkPane = new JTaskPaneGroup();
mustLinkPane.setLayout(new BorderLayout());
mustLinkPane.setTitle(Messages.getString("ConstraintsSelectionDialog.100")); //$NON-NLS-1$
JButton mlAddbutton = new JButton(Messages.getString("ConstraintsSelectionDialog.13"));
mlAddbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
createMustLinkConstraint();
}
});
otherComponentList.add(mlAddbutton);
mustLinkContainer.setLayout(new GridLayout(0, 1));
mustLinkPane.add(mustLinkContainer, BorderLayout.CENTER);
mustLinkPane.add(mlAddbutton, BorderLayout.SOUTH);
mustLinkPane.setBackground(Color.green);
taskPane.add(mustLinkPane);
JTaskPaneGroup cannotLinkPane = new JTaskPaneGroup();
cannotLinkPane.setLayout(new BorderLayout());
cannotLinkPane.setTitle(Messages.getString("ConstraintsSelectionDialog.101")); //$NON-NLS-1$
JButton clAddbutton = new JButton(Messages.getString("ConstraintsSelectionDialog.13"));
clAddbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
createCannotLinkConstraint();
}
});
otherComponentList.add(clAddbutton);
cannotLinkContainer.setLayout(new GridLayout(0, 1));
cannotLinkPane.add(cannotLinkContainer, BorderLayout.CENTER);
cannotLinkPane.add(clAddbutton, BorderLayout.SOUTH);
taskPane.add(cannotLinkPane);
JTaskPaneGroup labelPane = new JTaskPaneGroup();
labelPane.setLayout(new BorderLayout());
labelPane.setTitle(Messages.getString("ConstraintsSelectionDialog.102")); //$NON-NLS-1$
JButton laAddbutton = new JButton(Messages.getString("ConstraintsSelectionDialog.13"));
laAddbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
createLabelConstraint();
}
});
otherComponentList.add(laAddbutton);
labelContainer.setLayout(new GridLayout(0, 1));
labelPane.add(labelContainer, BorderLayout.CENTER);
labelPane.add(laAddbutton, BorderLayout.SOUTH);
taskPane.add(labelPane);
JTaskPaneGroup cluserRelatedPane = new JTaskPaneGroup();
cluserRelatedPane.setLayout(new BorderLayout());
cluserRelatedPane.setTitle(Messages.getString("ConstraintsSelectionDialog.103")); //$NON-NLS-1$
cluserRelatedPane.add(buildClusterRelatedPane(), BorderLayout.CENTER);
taskPane.add(cluserRelatedPane);
panelInfos.removeAll();
panelInfos.add(taskPane, BorderLayout.CENTER);
JButton buttonApply = new JButton(Messages.getString("ConstraintsSelectionDialog.21")); //$NON-NLS-1$
JButton buttonClear = new JButton(Messages.getString("ConstraintsSelectionDialog.22")); //$NON-NLS-1$
JPanel paneBouton = new JPanel();
paneBouton.add(buttonApply);
paneBouton.add(buttonClear);
panelInfos.add(paneBouton, BorderLayout.SOUTH);
return panelInfos;
}
private JPanel buildClusterRelatedPane() {
JPanel container = new JPanel();
container.setLayout(new GridLayout(0,1));
// add the nb cluster part
JPanel nbCLusterPane = new JPanel();
nbCLusterPane.setLayout(new GridLayout(0,1));
final JPanel ncInner = new JPanel();
ncInner.setLayout(new FlowLayout());
ncInner.add(new JLabel(Messages.getString("ConstraintsSelectionDialog.16")));
nbClusterMinTextField = new JTextField(3);
nbClusterMinTextField.setText("1");
nbClusterMinTextField.setEnabled(false);
ncInner.add(nbClusterMinTextField);
ncInner.add(new JLabel(Messages.getString("ConstraintsSelectionDialog.17")));
nbClusterMaxTextField = new JTextField(3);
nbClusterMaxTextField.setText("1");
nbClusterMaxTextField.setEnabled(false);
ncInner.add(nbClusterMaxTextField);
nbClusterCheckBox = new JCheckBox(Messages.getString("ConstraintsSelectionDialog.104"));
nbClusterCheckBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for(Component c : ncInner.getComponents()) {
c.setEnabled(nbClusterCheckBox.isSelected());
}
}
});
otherComponentList.add(nbClusterCheckBox);
for(Component c : ncInner.getComponents()) {
otherComponentList.addElement(c);
}
nbCLusterPane.add(nbClusterCheckBox);
nbCLusterPane.add(ncInner);
container.add(nbCLusterPane);
// add the diameter cluster part
JPanel diameterPane = new JPanel();
diameterPane.setLayout(new GridLayout(0,1));
JPanel dInner = new JPanel();
dInner.setLayout(new GridLayout(0,1));
final JPanel maxPane = new JPanel();
maxPane.setLayout(new FlowLayout());
maxPane.add(new JLabel(Messages.getString("ConstraintsSelectionDialog.17")));
diameterTextField = new JTextField(7);
diameterTextField.setEnabled(false);
maxPane.add(diameterTextField);
dInner.add(maxPane);
final JSlider slider = new JSlider(JSlider.HORIZONTAL, (int) minDistance,
(int) maxDistance, (int) maxDistance);
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
diameterTextField.setText(slider.getValue()+"");
}
});
slider.setEnabled(false);
dInner.add(slider);
diameterTextField.setText(slider.getValue()+"");
diameterTextField.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
diameterTextFieldChanged(slider);
}
@Override
public void focusGained(FocusEvent e) {
}
});
diameterTextField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
slider.setValue((int) Double.parseDouble(diameterTextField.getText()));
}
});
clusterDiameterCheckBox = new JCheckBox(Messages.getString("ConstraintsSelectionDialog.105"));
clusterDiameterCheckBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {