Commit 645e82ae authored by lafabregue's avatar lafabregue

Add the constraints creation tool in the toolbar

parent 74d5ac3e
......@@ -13,6 +13,9 @@ import jcl.jcld.database.SSHParameters;
import mustic.gui.panels.*;
import mustic.gui.segmentation.SegmentationPanel;
import mustic.io.RawImage;
import net.infonode.tabbedpanel.TabFactory;
import net.infonode.tabbedpanel.TabbedPanel;
import net.infonode.tabbedpanel.titledtab.TitledTab;
/**
* Classe qui a toutes les donnees concernant l'image ouverte .... - RawImage - ImageData -
......@@ -51,7 +54,7 @@ public class ImageSession {
public String mTitle;
/** Contains image, classifier, results */
protected JPanel mWorskSpace;
protected TabbedPanel mWorskSpace;
/** The Data que decrit une imqge */
private RawImage rawImage;
......@@ -63,7 +66,7 @@ public class ImageSession {
public SSHParameters sshparameters = null;
/** enable the image to display the image or not, for a memory usage matter */
private boolean displayEnable = false;
private boolean displayEnable = true;
/**
* @param aImage
......@@ -117,10 +120,16 @@ public class ImageSession {
if (MainFrame.DEBUG_MODE)
System.out.println(Messages.getString("ImageSession.6")); //$NON-NLS-1$
this.mWorskSpace = new JPanel();
this.mWorskSpace = new TabbedPanel();
JScrollPane sp = new JScrollPane(this.mImagePanel);
mWorskSpace = new JPanel(new BorderLayout());
mWorskSpace.add(sp, BorderLayout.CENTER);
final TitledTab tabImage = TabFactory.createTitledTab(
"Image",
new ImageIcon(getClass().getResource(
"/icons/mimetypes/image-x-generic.png")),
sp);
// mWorskSpace = new JPanel(new BorderLayout());
mWorskSpace.addTab(tabImage);
}
......@@ -214,7 +223,7 @@ public class ImageSession {
return this.segPanels;
}
public JPanel getWorkSpace() {
public TabbedPanel getWorkSpace() {
return this.mWorskSpace;
}
......
......@@ -27,6 +27,7 @@ import jcl.learning.methods.monostrategy.SingleClassification;
import jcl.weights.ClassificationWeights;
import jsl.Segmentation;
import mustic.gui.dialog.*;
import mustic.gui.dialog.classifier.ConstraintsSelectionDialog;
import mustic.gui.dialog.consoleHandler.ConsoleFrame;
import mustic.gui.panels.*;
import mustic.gui.panels.data.DataConstructFrame;
......@@ -568,7 +569,17 @@ public class MainFrame extends JFrame {
}
});
toolsMenu.add(menuTransformationBmp);
/** Menu to generate constraints from current image session */
menuConstraints = new JMenuItem(Messages.getString("MainFrame.301"));
menuConstraints.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
createConstraints();
}
});
toolsMenu.add(menuConstraints);
menuBar.add(toolsMenu);
ArffMenu = new JMenu(Messages.getString("MainFrame.32")); //$NON-NLS-1$
......@@ -963,6 +974,8 @@ public class MainFrame extends JFrame {
private Boolean actionCrop;
private JMenuItem menuTransformationBmp;
private Boolean actionTransformationBmp;
private JMenuItem menuConstraints;
private Boolean actionConstraints;
private JMenuItem menuItemArff;
private Boolean actionArff;
private JMenuItem menuItemRaw;
......@@ -1073,6 +1086,7 @@ public class MainFrame extends JFrame {
this.actionContrastM = false;
this.actionContratsP = false;
this.actionCrop = false;
this.actionConstraints = false;
this.actionFilter = false;
this.actionMask = false;
this.actionRefresh = false;
......@@ -1102,6 +1116,7 @@ public class MainFrame extends JFrame {
this.actionContrastM = true;
this.actionContratsP = true;
this.actionCrop = true;
this.actionConstraints = true;
this.actionFilter = true;
this.actionMask = true;
this.actionNew = true;
......@@ -1139,6 +1154,7 @@ public class MainFrame extends JFrame {
menuItemRoiColor.setEnabled(this.actionRoiColor);
menuItemMask.setEnabled(this.actionMask);
menuCrop.setEnabled(this.actionCrop);
menuConstraints.setEnabled(this.actionConstraints);
menuTransformationBmp.setEnabled(this.actionTransformationBmp);
menuItemArff.setEnabled(this.actionArff);
menuItemRaw.setEnabled(this.actionRaw);
......@@ -1198,6 +1214,7 @@ public class MainFrame extends JFrame {
menuItemMask.setText(Messages.getString("MainFrame.29"));
menuCrop.setText(Messages.getString("MainFrame.0"));
menuTransformationBmp.setText(Messages.getString("MainFrame.30"));
menuConstraints.setText(Messages.getString("MainFrame.301"));
menuItemArff.setText(Messages.getString("MainFrame.33"));
menuItemRaw.setText("RawImage to Arff");
menuItemLoadArff.setText(Messages.getString("MainFrame.34"));
......@@ -2111,6 +2128,21 @@ public class MainFrame extends JFrame {
JOptionPane.WARNING_MESSAGE);
}
}
/**
* Launch the dialog panel to create constraints
*/
protected void createConstraints() {
try {
ConstraintsSelectionDialog dialog = new ConstraintsSelectionDialog();
dialog.setImage(this.currentImageSession.getRawImage());
MainFrame.getInstance().getDesktop().addFrame(dialog);
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, Messages.getString("MainFrame.128"), Messages.getString("MainFrame.129"), //$NON-NLS-1$ //$NON-NLS-2$
JOptionPane.WARNING_MESSAGE);
}
}
public void doAfterSegmentation(Segmentation seg, RawImage img) {
// fenetre et panel du resultat
......@@ -3271,8 +3303,15 @@ public class MainFrame extends JFrame {
}
});
tab.setHighlightedStateTitleComponent(closeButton);
MainFrame.getInstance().getCurrentSession().getWorkSpace().addTab(tab);
//MainFrame.getInstance().getCurrentImageSession().getWorkSpace().addTab(tab);
final TitledTab tabSegmentation = TabFactory.createTitledTab(
"Segementation",
new ImageIcon(getClass().getResource(
"/icons/outlines.png")),
sp);
// mWorskSpace = new JPanel(new BorderLayout());
this.currentImageSession.getWorkSpace().addTab(tabSegmentation);
this.currentImageSession.getWorkSpace().setSelectedTab(this.currentImageSession.getWorkSpace()
.getTabAt(this.currentImageSession.getWorkSpace().getTabCount() - 1));
}
......
......@@ -612,7 +612,7 @@ public class PanneauAffichageParam extends JPanel {
//GridBagLayout layout = new GridBagLayout();
//this.setLayout(layout);
for (int index = 0; index < _attributes.getLength(); index++) { // on parcourt la liste des attributs
String attribut = _attributes.getLocalName(index);
String attribut = _attributes.getQName(index);
switch (attribut)
{
case "methode":
......
......@@ -26,6 +26,7 @@ import javax.swing.JSpinner;
import javax.swing.JTextField;
import mustic.gui.DesktopFrame;
import mustic.gui.ImageDesktopFrame;
import mustic.gui.MainFrame;
import mustic.io.RawImage;
......@@ -55,8 +56,8 @@ public class ImageToArffDialog extends JInternalFrame {
JPanel panelData = new JPanel(new GridLayout(4, 2));
panelData.add(new JLabel(Messages.getString("ImageToArffDialog.0"))); //$NON-NLS-1$
DesktopFrame[] desktopFrames = MainFrame.getInstance().getDesktop()
.getAllDesktopFrames();
ImageDesktopFrame[] desktopFrames = MainFrame.getInstance().getDesktop()
.getAllImageDesktopFrames();
int nbSession = desktopFrames.length;
for (int i = 0; i < nbSession; i++) {
String path = desktopFrames[i].getImageSession().getRawImage()
......@@ -79,12 +80,12 @@ public class ImageToArffDialog extends JInternalFrame {
if (ImageToArffDialog.this.comboImage.getSelectedIndex() == ImageToArffDialog.this.comboImage
.getItemCount() - 1) {
int nbSession = MainFrame.getInstance().getDesktop()
.getAllDesktopFrames().length;
.getAllImageDesktopFrames().length;
System.out.println(Messages.getString("ImageToArffDialog.6") //$NON-NLS-1$
+ nbSession);
MainFrame.getInstance().openNewFile();
DesktopFrame[] desktopFrames = MainFrame.getInstance()
.getDesktop().getAllDesktopFrames();
ImageDesktopFrame[] desktopFrames = MainFrame.getInstance()
.getDesktop().getAllImageDesktopFrames();
System.out.println(Messages.getString("ImageToArffDialog.7") //$NON-NLS-1$
+ desktopFrames.length);
if (MainFrame.getInstance().getDesktop()
......@@ -123,8 +124,8 @@ public class ImageToArffDialog extends JInternalFrame {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
DesktopFrame[] desktopFrames = MainFrame.getInstance()
.getDesktop().getAllDesktopFrames();
ImageDesktopFrame[] desktopFrames = MainFrame.getInstance()
.getDesktop().getAllImageDesktopFrames();
int nbSessions = desktopFrames.length;
if (nbSessions > 0) {
String path = desktopFrames[comboImage.getSelectedIndex()]
......@@ -191,8 +192,8 @@ public class ImageToArffDialog extends JInternalFrame {
* lance la création du fichier arff
*/
protected void buttonOk_actionPerformed() {
DesktopFrame[] desktopFrames = MainFrame.getInstance().getDesktop()
.getAllDesktopFrames();
ImageDesktopFrame[] desktopFrames = MainFrame.getInstance().getDesktop()
.getAllImageDesktopFrames();
int nbSessions = desktopFrames.length;
if (nbSessions == 0) {
JOptionPane.showMessageDialog(this,
......
package mustic.gui.dialog.classifier;
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.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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.JFileChooser;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import com.l2fprod.common.swing.JTaskPane;
import com.l2fprod.common.swing.JTaskPaneGroup;
import jcl.clustering.constraints.CannotLinkConstraint;
import jcl.clustering.constraints.Constraint;
import jcl.clustering.constraints.LabelConstraint;
import jcl.clustering.constraints.MustLinkConstraint;
import mustic.gui.panels.classifier.ClassifierPanel;
import mustic.io.RawImage;
import mustic.utils.CSVUtils;
import mustic.utils.filters.CSVFileFilter;
/**
* Fenetre permettant de choisir les centres pour K-Means
* en les selectionnant dans l'image
*
*/
public class ConstraintsSelectionDialog extends JInternalFrame {
private static final long serialVersionUID = 1L;
private int width;
private RawImage image = null;
private BufferedImage imagePoints = null;
private BufferedImage imageSource = null;
private JLabel labelImage;
public JPanel mainPanel;
private JTaskPane taskPane = new JTaskPane();
/** 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>();
private Vector<Component> otherComponentList = new Vector<Component>();
/** 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;
public ConstraintsSelectionDialog() {
super();
mainPanel = new JPanel(new BorderLayout());
// affichage de l'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;
}
affichePixel();
}
}
});
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$
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() {
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 {
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 : constraints) {
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$
CSVUtils reader = null;
try {
reader = new CSVUtils(file);
clear();
List<String> line;
while((line = reader.readNext()) != null) {
addConstraint(line);
}
enableAllComponents();
updateUI();
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
return true;
}
private void addConstraint(List<String> line) {
switch (Integer.parseInt(line.get(0))) {
case Constraint.MUST_LINK_TYPE :
try {
createMustLinkConstraint();
constraints.add(new MustLinkConstraint(Integer.parseInt(line.get(1)),
Integer.parseInt(line.get(2))));
currentStep = -1;
// we update text fields
((JTextField) constraintsPanels.get(constraintsPanels.size()-1)
.getComponent(1)).setText(""+Integer.parseInt(line.get(1)));
((JTextField) constraintsPanels.get(constraintsPanels.size()-1)
.getComponent(3)).setText(""+Integer.parseInt(line.get(2)));
} catch(Exception e) {
e.printStackTrace();
}
break;
case Constraint.CANOT_LINK_TYPE :
try {
createCannotLinkConstraint();
constraints.add(new CannotLinkConstraint(Integer.parseInt(line.get(1)),
Integer.parseInt(line.get(2))));
currentStep = -1;
// we update text fields
((JTextField) constraintsPanels.get(constraintsPanels.size()-1)
.getComponent(1)).setText(""+Integer.parseInt(line.get(1)));
((JTextField) constraintsPanels.get(constraintsPanels.size()-1)
.getComponent(3)).setText(""+Integer.parseInt(line.get(2)));
} catch(Exception e) {
e.printStackTrace();
}
break;
case Constraint.LABEL_TYPE :
try {
createLabelConstraint();
constraints.add(new LabelConstraint(Integer.parseInt(line.get(1)),
Integer.parseInt(line.get(2))));
currentStep = -1;
// we update text fields
((JTextField) constraintsPanels.get(constraintsPanels.size()-1)
.getComponent(1)).setText(""+Integer.parseInt(line.get(1)));
} catch(Exception e) {
e.printStackTrace();
}
break;
}
}
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));
JTaskPaneGroup mustLinkPane = new JTaskPaneGroup();
mustLinkPane.setLayout(new BorderLayout());
mustLinkPane.setTitle("ggggggggggg"); //$NON-NLS-1$
mustLinkPane.setTitle(Messages.getString("ConstraintsSelectionDialog.10")); //$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);
this.taskPane.add(mustLinkPane);
JTaskPaneGroup cannotLinkPane = new JTaskPaneGroup();
cannotLinkPane.setLayout(new BorderLayout());
cannotLinkPane.setTitle(Messages.getString("ConstraintsSelectionDialog.11")); //$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);
this.taskPane.add(cannotLinkPane);
JTaskPaneGroup labelPane = new JTaskPaneGroup();
labelPane.setLayout(new BorderLayout());
labelPane.setTitle(Messages.getString("ConstraintsSelectionDialog.12")); //$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);
this.taskPane.add(labelPane);
JScrollPane jpane = new JScrollPane(taskPane);
panelInfos.removeAll();