Commit 98c46d20 authored by smorgenthaler's avatar smorgenthaler

Add progress bar update while exporting/importing constraints

parent 316e16a4
package mustic.gui;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
public class ProgressBarPropertyChangeListener implements PropertyChangeListener
{
// The loading bar progresses from 10 to 10
static public int incrementation = 10;
static public int jalon = incrementation;
/**
*
* Invoked when task's progress property changes.
* (each time progress' value changes)
*
*/
@Override
public void propertyChange(PropertyChangeEvent evt)
{
if (evt.getPropertyName() == "progress")
{
int progress = (Integer) evt.getNewValue();
if(progress >= ProgressBarPropertyChangeListener.jalon)
{
MainFrame.getProgressBar().setValue((Math.min(/*ProgressBarPropertyChangeListener.jalon*/progress, 100)));
ProgressBarPropertyChangeListener.jalon += ProgressBarPropertyChangeListener.incrementation;
}
}
}
}
......@@ -21,11 +21,13 @@ import java.awt.event.MouseWheelListener;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.LineNumberReader;
import java.util.List;
import java.util.Random;
import java.util.Vector;
......@@ -34,7 +36,6 @@ 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;
......@@ -59,9 +60,8 @@ import jcl.clustering.constraints.MustLinkConstraint;
import jcl.clustering.constraints.NbClusterConstraint;
import jcl.data.Data;
import jcl.utils.Images.StreamedImageReaderWrapper;
import mustic.gui.ProgressBarPropertyChangeListener;
import mustic.io.RawImage;
import mustic.utils.filters.CSVFileFilter;
import mustic.utils.io.CSVUtils;
/**
* Dialog window that allow the user to generate a set of constraints
......@@ -72,6 +72,8 @@ import mustic.utils.io.CSVUtils;
public class ConstraintsSelectionDialog extends JInternalFrame {
private static final long serialVersionUID = 1L;
ImportConstraintsTask importConstraintsTask = null;
/** data related fields */
private RawImage image = null;
......@@ -260,140 +262,95 @@ public class ConstraintsSelectionDialog extends JInternalFrame {
}
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();
} catch (IOException e) {
e.printStackTrace();
}
}
ExportConstraintsTask exportConstraintsTask = new ExportConstraintsTask(this);
exportConstraintsTask.addPropertyChangeListener(new ProgressBarPropertyChangeListener());
exportConstraintsTask.execute();
}
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);
} else {
JOptionPane.showMessageDialog(this,
Messages.getString("ConstraintsSelectionDialog.510"),
Messages.getString("ConstraintsSelectionDialog.511"), //$NON-NLS-1$ //$NON-NLS-2$
JOptionPane.INFORMATION_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));
protected void importConstraints()
{
ImportConstraintsTask importConstraintsTask = new ImportConstraintsTask(this);
importConstraintsTask.addPropertyChangeListener(new ProgressBarPropertyChangeListener());
importConstraintsTask.execute();
}
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();
displayPixels();
updateUI();
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
return true;
public int countLines(File aFile) throws IOException {
LineNumberReader reader = null;
try {
reader = new LineNumberReader(new FileReader(aFile));
while ((reader.readLine()) != null);
return reader.getLineNumber();
} catch (Exception ex) {
return -1;
} finally {
if(reader != null)
reader.close();
}
}
public Data getData()
{
return this.data;
}
// boolean openFile(File file) throws IOException {
//
// 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(this.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;
// int i=1;
// int nbrL = countLines(file);
//
//
// while((line = reader.readNext()) != null)
// {
// this.addConstraint(line, isImageRelated);
// try {
// Thread.sleep(500);
// System.out.println(i + " sur " + nbrL);
// i++;
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// this.enableAllComponents();
// this.displayPixels();
// this.updateUI();
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// MainFrame.getProgressBar().setValue(0);
// return false;
// }
//
// MainFrame.getProgressBar().setValue(0);
// return true;
// }
private void addConstraint(List<String> line, boolean isImageRelated) {
void addConstraint(List<String> line, boolean isImageRelated) {
switch (Integer.parseInt(line.get(line.size()-1))) {
case Constraint.MUST_LINK_TYPE :
try {
......@@ -862,7 +819,7 @@ public class ConstraintsSelectionDialog extends JInternalFrame {
.getComponent(1)).setText(""+index);
}
private void displayPixels() {
void displayPixels() {
for (int x=0 ; x<this.imageSource.getWidth() ; x++) {
for (int y=0 ; y<this.imageSource.getHeight() ; y++) {
this.imagePoints.setRGB(x, y, this.imageSource.getRGB(x, y));
......@@ -936,7 +893,7 @@ public class ConstraintsSelectionDialog extends JInternalFrame {
g.drawLine(x, y-7, x, y+7);
}
private void clear() {
public void clear() {
constraints = new Vector<Constraint>();
constraintsColors = new Vector<Color>();
}
......@@ -1102,7 +1059,7 @@ public class ConstraintsSelectionDialog extends JInternalFrame {
}
}
private void enableAllComponents() {
void enableAllComponents() {
for(Component c : otherComponentList) {
c.setEnabled(true);
}
......
package mustic.gui.dialog.classifier;
import java.awt.Cursor;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
import jcl.clustering.constraints.Constraint;
import mustic.gui.MainFrame;
import mustic.gui.ProgressBarPropertyChangeListener;
import mustic.utils.filters.CSVFileFilter;
/**
*
* @author Adrien MEYER
*
*/
public class ExportConstraintsTask extends SwingWorker<Void, Void>
{
ConstraintsSelectionDialog dialog = null;
public ExportConstraintsTask (ConstraintsSelectionDialog dialog)
{
this.dialog = dialog;
}
static public int progress = 0;
/*
* Main task. Executed in background thread.
*/
@Override
public Void doInBackground()
{
boolean isImageRelated = true;
if(dialog.getData() != null)
{
// choose between image or data indexes
String[] possibilities = { Messages.getString("ConstraintsSelectionDialog.521"),
Messages.getString("ConstraintsSelectionDialog.522")};
String s = (String)JOptionPane.showInputDialog(
dialog,
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(dialog, Messages.getString("ConstraintsSelectionDialog.52")); //$NON-NLS-1$
if (returnVal == JFileChooser.APPROVE_OPTION)
{
try
{
Vector<Constraint> constrainsToExport = null;
if(isImageRelated)
{
constrainsToExport = dialog.getConstraints();
}
else
{
constrainsToExport = dialog.getData().updateAndSetConstraintsToSample(dialog.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);
int nbrL = constrainsToExport.size();
int progressImprove = 100/nbrL;
dialog.setCursor(new Cursor(Cursor.WAIT_CURSOR));
for(Constraint c : constrainsToExport)
{
writer.println(c);
progress += progressImprove;
////
if(!(progress >= ProgressBarPropertyChangeListener.jalon))
{
continue;
}
////
try
{
Thread.sleep(250);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
// progress += progressImprove;
setProgress(Math.min(progress, 100));
}
if(progress<100)
{
try
{
Thread.sleep(250);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
setProgress(100);
}
setProgress(100);
writer.close();
out.close();
JOptionPane.showMessageDialog(dialog,
Messages.getString("ConstraintsSelectionDialog.525"),
Messages.getString("ConstraintsSelectionDialog.526"), //$NON-NLS-1$ //$NON-NLS-2$
JOptionPane.INFORMATION_MESSAGE);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
dialog.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
ProgressBarPropertyChangeListener.jalon=10;
progress=0;
MainFrame.getProgressBar().setValue(0);
setProgress(0);
return null;
}
}
package mustic.gui.dialog.classifier;
import java.awt.Cursor;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
import mustic.gui.MainFrame;
import mustic.gui.ProgressBarPropertyChangeListener;
import mustic.utils.filters.CSVFileFilter;
import mustic.utils.io.CSVUtils;
/**
*
* @author Adrien MEYER
*
*/
class ImportConstraintsTask extends SwingWorker<Void, Void>
{
ConstraintsSelectionDialog dialog = null;
public ImportConstraintsTask (ConstraintsSelectionDialog dialog)
{
this.dialog = dialog;
}
static public int progress = 0;
/*
* Main task. Executed in background thread.
*/
@Override
public Void doInBackground()
{
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(dialog);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
dialog.setCursor(new Cursor(Cursor.WAIT_CURSOR));
boolean imageOuvre;
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(dialog.getData() != null)
{
// choose between image or data indexes
String[] possibilities = { Messages.getString("ConstraintsSelectionDialog.521"),
Messages.getString("ConstraintsSelectionDialog.522")};
String s = (String)JOptionPane.showInputDialog(
dialog,
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);
dialog.clear();
List<String> line;
int nbrL = dialog.countLines(file);
int progressImprove = 100/nbrL;
while((line = reader.readNext()) != null)
{
dialog.addConstraint(line, isImageRelated);
progress += progressImprove;
if(!(progress >= ProgressBarPropertyChangeListener.jalon))
{
continue;
}
try
{
Thread.sleep(250);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
setProgress(Math.min(progress, 100));
}