CSVToDataDialog.java.orig 6.98 KB
Newer Older
balanche's avatar
balanche committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
package mustic.gui.dialog.csv;

import java.awt.BorderLayout;
import java.awt.Dimension;
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.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

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.JTextField;

import jcl.data.Data;
import jcl.data.DataObject;
import jcl.data.DistanceModel;
import jcl.data.SimpleData;
import jcl.data.attribute.AttributeMultiDimSequence;
<<<<<<< HEAD
import jcl.data.distance.DistanceParameter;
import jcl.weights.GlobalWeights;
=======
import multiCube.tools.image.ImageHelper;
>>>>>>> master
import mustic.gui.MainFrame;
import mustic.gui.dialog.Messages;
import mustic.utils.documentFilter.ToUpdateObject;
import mustic.utils.io.CSVUtils;


/**
 * Dialog that allow the user to generate a Data from an arff file
 * 
 * @author Baptiste LAFABREGUE
 *
 */
public class CSVToDataDialog extends JInternalFrame implements ToUpdateObject {

	private static final long serialVersionUID = 1L;

	private JTextField 			textFile 			= new JTextField();

	private JTextField 			textData 			= new JTextField();
	
	private JTextField 			textSeparator		= new JTextField();
	
	public CSVToDataDialog() {
		super();

		JPanel mainPanel = new JPanel(new BorderLayout());

		JPanel panel = new JPanel(new GridLayout(3, 2));
		JPanel pInpout = new JPanel(new BorderLayout());
		panel.add(new JLabel(Messages.getString("CSVToDataDialog.0")), BorderLayout.WEST); //$NON-NLS-1$
		pInpout.add(this.textFile, BorderLayout.CENTER);
		this.textFile.addFocusListener(new FocusListener() {
			@Override
			public void focusLost(FocusEvent e) {
				updateOutputName();
			}

			@Override
			public void focusGained(FocusEvent e) {
			}
		});
		
		JButton buttonBrowse = new JButton("..."); //$NON-NLS-1$
		buttonBrowse.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser fileChooser = new JFileChooser();
				int returnVal = fileChooser
						.showOpenDialog(CSVToDataDialog.this);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					CSVToDataDialog.this.textFile.setText(fileChooser
							.getSelectedFile().getPath());
					updateOutputName();
					if (!fileChooser.getSelectedFile().getPath() 
							.endsWith(".csv")) { //$NON-NLS-1$
						JOptionPane.showMessageDialog(CSVToDataDialog.this,
								Messages.getString("CSVToDataDialog.3"), //$NON-NLS-1$
								Messages.getString("CSVToDataDialog.4"), //$NON-NLS-1$ 
								JOptionPane.WARNING_MESSAGE);
					}
				}
			}
		});
		pInpout.add(buttonBrowse, BorderLayout.EAST);
		panel.add(pInpout);
		
		JPanel pSeparator = new JPanel(new BorderLayout());
		panel.add(new JLabel(Messages.getString("CSVToDataDialog.2")), BorderLayout.WEST); //$NON-NLS-1$
		pSeparator.add(this.textSeparator, BorderLayout.CENTER);
		panel.add(pSeparator);
		this.textSeparator.setText(";");

		JPanel pOutpout = new JPanel(new BorderLayout());
		panel.add(new JLabel(Messages.getString("CSVToDataDialog.5")), BorderLayout.WEST); //$NON-NLS-1$
		pOutpout.add(this.textData, BorderLayout.CENTER);
		panel.add(pOutpout);

		this.textFile.setPreferredSize(new Dimension(200, 30));
		JPanel panelButton = new JPanel();
		JButton buttonOk = new JButton(Messages.getString("CSVToDataDialog.7")); //$NON-NLS-1$
		panelButton.add(buttonOk);

		buttonOk.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				buttonOk_actionPerformed();
			}
		});

		mainPanel.add(panel, BorderLayout.NORTH);
		mainPanel.add(panelButton, BorderLayout.SOUTH);

		this.setContentPane(mainPanel);
		this.setTitle(Messages.getString("CSVToDataDialog.8")); //$NON-NLS-1$
		this.setSize(300, 180);
		this.setResizable(true);
		this.setMaximizable(true);
		this.setIconifiable(true);
		this.setClosable(true);
		this.setVisible(true);
		this.requestFocus();
	}
	
	private void updateOutputName() {
		if (this.textData.getText() != null && this.textData.getText() != "") { //$NON-NLS-1$
			// we don't update if there is already a value
			return;
		}
		if (this.textFile.getText() != null && this.textFile.getText() != "") { //$NON-NLS-1$
			this.textData.setText(ImageHelper.extractFileName(
					ImageHelper.stripExtension(this.textData.getText())));
			
		}
	}

	protected void buttonOk_actionPerformed() {
		try {
			List<DataObject> result = new ArrayList<DataObject>();
			Data data = null;
			
			CSVUtils reader = null;
	        try {
	        	File file = new File(this.textFile.getText());
				reader = new CSVUtils(file, this.textSeparator.getText().charAt(0));
				List<String> line;
				while((line = reader.readNext()) != null) {
					extractObjectFromLine(result, line);
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				result = null;
			}
			
			if(result != null) {
				data = new SimpleData(result, null, null);
				DistanceModel model = DistanceModel.generateDefaultModel(data.getOneDataObject(), 
						new GlobalWeights(data.getOneDataObject()));
				DistanceParameter[][] parameters = DistanceModel.generateDefaultDistanceParameters(1,model, data);
				data.setDistanceModel(model, parameters);
				
				if (this.textData.getText().length() == 0) {
					this.textData.setText(ImageHelper.extractFileName(
							ImageHelper.stripExtension(this.textData.getText())));
				}
				data.setDataName(this.textData.getText());
				MainFrame.getInstance().createDataSession(data);
				
				JOptionPane.showMessageDialog(this,
						Messages.getString("CSVToDataDialog.151") + this.textData.getText() //$NON-NLS-1$
							+ " " + Messages.getString("CSVToDataDialog.152"),	//$NON-NLS-1$
						Messages.getString("CSVToDataDialog.16"),  //$NON-NLS-1$
						JOptionPane.INFORMATION_MESSAGE);
			} else {
				JOptionPane.showMessageDialog(this,
						Messages.getString("CSVToDataDialog.41"),  //$NON-NLS-1$
						Messages.getString("CSVToDataDialog.4"), //$NON-NLS-1$
						JOptionPane.WARNING_MESSAGE);
			}

			this.dispose();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this,
					Messages.getString("CSVToDataDialog.41"),  //$NON-NLS-1$
					Messages.getString("CSVToDataDialog.4"), //$NON-NLS-1$ 
					JOptionPane.WARNING_MESSAGE);
			e.printStackTrace();
		}
	}

	/**
	 * Construct a DataObject from a set of Strings and add it to a List
	 * @param list
	 * 			the list to add the DataObject to
	 * @param line
	 * 			the line to interpret
	 */
	private void extractObjectFromLine(List<DataObject> list, List<String> line) {
		DataObject obj = new DataObject(1);	
		double[][] seq = new double[line.size()][];
		for (int i = 0 ; i < line.size() ; i++) {
			seq[i] = new double[1];
			seq[i][0] = Double.parseDouble(line.get(i));
				
		}
		obj.setAttribute(0, new AttributeMultiDimSequence(seq));
		
		list.add(obj);
	}

	@Override
	public void update(String s) {
		
	}

}