Blame view

VRGNYMusicLights/Sources/ApplicationJava/ImportExport.java 2.7 KB
d175ba55   pfrison   VRGNYMusicLight a...
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
  import java.io.BufferedWriter;
  import java.io.File;
  import java.io.FileWriter;
  import java.io.IOException;
  import java.nio.charset.Charset;
  import java.nio.file.Files;
  import java.nio.file.Paths;
  
  import javax.swing.JFileChooser;
  import javax.swing.JOptionPane;
  import javax.swing.filechooser.FileNameExtensionFilter;
  
  public class ImportExport {
  	public static void exportWithDialog(MusicList list) {
  		JFileChooser chooser = new JFileChooser();
  		FileNameExtensionFilter filter = new FileNameExtensionFilter("Array data", "adata");
  		chooser.setFileFilter(filter);
  		chooser.setApproveButtonText("Exporter");
  		int returnVal = chooser.showOpenDialog(null);
  		if(returnVal == JFileChooser.APPROVE_OPTION)
  			export(list, chooser.getSelectedFile().getPath());
  	}
  	private static void export(MusicList list, String path) {
  		String str = "";//list.getLeftDeltaArray() + "\n" + list.getRightDeltaArray() + "\n" + list.getLeftDelaiArray() + "\n" + list.getRightDelaiArray();
  		
  		if(!path.endsWith(".adata"))
  			path += ".adata";
  		BufferedWriter bufferedWriter = null;
  		try {
  			File file = new File(path);
  			if (!file.exists())
  				file.createNewFile();
  			bufferedWriter = new BufferedWriter(new FileWriter(file));
  			bufferedWriter.write(str);
  			bufferedWriter.close();
  		} catch (IOException e) {
  			e.printStackTrace();
  		}
  	}
  	
  	public static MusicList importsWithDialog() {
  		JFileChooser chooser = new JFileChooser();
  		FileNameExtensionFilter filter = new FileNameExtensionFilter("Array data", "adata");
  		chooser.setFileFilter(filter);
  		chooser.setMultiSelectionEnabled(false);
  		chooser.setApproveButtonText("Importer");
  		int returnVal = chooser.showOpenDialog(null);
  		if(returnVal == JFileChooser.APPROVE_OPTION && chooser.getSelectedFile().exists() && chooser.getSelectedFile().isFile())
  			return imports(chooser.getSelectedFile().getPath());
  		if(returnVal == JFileChooser.APPROVE_OPTION)
  			JOptionPane.showMessageDialog(null, "You have to choose an exisiting file", "Import error", JOptionPane.ERROR_MESSAGE);
  		return null;
  	}
  	private static MusicList imports(String path) {
  		try {
  			byte[] encoded = Files.readAllBytes(Paths.get(path));
  			String str = new String(encoded, Charset.defaultCharset());
  			
  			//long[][] longList = Util.getArraysFromImport(str);
  			/*if(longList == null) {
  				JOptionPane.showMessageDialog(null, "The choosen file is invalid or currupted", "Import error", JOptionPane.ERROR_MESSAGE);
  				return null;
  			}
  			int[][] intList = new int[2][longList[0].length];
  			for(int i=0; i<2; i++)
  				for(int j=0; j<longList[0].length; j++)
  					intList[i][j] = (int) longList[i][j];
  			
  			return new MusicList(intList[0], intList[1], longList[2], longList[3]);*/
  		} catch (IOException ignored) {}
  		return null;
  	}
  }