Repository navigation
Expand file tree
/
Copy pathLibrary.java
More file actions
287 lines (269 loc) · 9.05 KB
/
Copy pathLibrary.java
File metadata and controls
287 lines (269 loc) · 9.05 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import javax.swing.JFileChooser;
/**
* Clase Biblioteca encargada de contener y administrar la biblioteca de musica, tambien esta encargada de crear la biblioteca explorando el arbol de directorios
*
* @author Rodolfo
*
*/
public class Library
{
/**
* Almacena la biblioteca de canciones
*/
private Track[] library;
/**
* Guarda la posicion que ocupara la proxima pista a agregar a la biblioteca
*/
private int nextTrack;
/**
* Si la biblioteca ha sido previamente creada en un archivo, se carga
* Si la biblioteca no ha sido previamente creada se debe construir a partir de una ruta indicada por el usuario
*/
public Library()
{
library = new Track[100];
nextTrack = 0;
File saveroot = new File("biblioteca");
File savefile = new File("biblioteca\\library.txt");
if(!saveroot.exists()){
try {
saveroot.mkdir();
} catch (Exception e) {
e.printStackTrace();
}
}
if(!savefile.exists()){
try {
savefile.createNewFile();
buildLibrary();
} catch (Exception e) {
e.printStackTrace();
}
}else if(savefile.exists()) {
loadLibrary();
}
// TODO
}
/**
* Carga la biblioteca a partir de un archivo de texto previamente creado
*/
public void loadLibrary()
{
String localroot = "biblioteca\\library.txt";
String SaveLib = ReadFile(localroot); //Modificar ruta dejando \\playthis\\filename.txt
File file = new File(localroot);
String[] song = SaveLib.split("\n");
String cache = "";
int i = 0;
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
while(true) {
cache=br.readLine();
if(cache!= null) {
addToLibrary(song[i]);
i++;
}
else {
break;
}
}
br.close();
fr.close();
}catch (Exception e) {
e.printStackTrace();
}
// TODO
}
public void buildLibrary()
{
// La raiz se obtiene de un explorador de archivos usando la clase correspondiente de la biblioteca swing
// Documentacion del JFileChooser: https://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html
JFileChooser chooser = new JFileChooser();
// Debemos garantizar que el explorador muestre unicamente directorios
// https://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#setFileSelectionMode(int)
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
// El showOpenDialog recibe como parametro un componente de javax.swing, en este caso no nos interesa por que vamos a usar JavaFX por eso ponemos null
// El showOpenDialog retorna un valor distinto segun el modo en que se cerro el dialogo
// https://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html#showOpenDialog(java.awt.Component)
if(chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
{
// Si el FileChooser se cerro correctamente, necesitamos el directorio que fue seleccionado para empezar a explorar
File root = chooser.getSelectedFile();
// Se explora el directorio seleccionado por el usuario de forma recursiva buscando todos los archivos con extension .mp3
exploreLibrary(root);
}
}
/**
* Explora de forma recursiva el directorio seleccionado por el usuario
* Agrega a la biblioteca unicamente los archivos de tipo .mp3
*
* @param root
*/
private void exploreLibrary(File root)
{
File[] subFiles = root.listFiles();
for(int i = 0; i < subFiles.length; ++i)
{
if(subFiles[i].isDirectory())
{
exploreLibrary(subFiles[i]);
}
else if(subFiles[i].getName().endsWith(".mp3"))
{
addToLibrary(subFiles[i]);
}
}
}
/**
* Agrega una nueva cancion a la biblioteca a partir del archivo que se encontro
* Si la biblioteca esta llena, se agregan 100 espacios nuevos a la biblioteca
* @param trackFile
*/
private void addToLibrary(String trackString) {
String cache = trackString;
if(nextTrack == library.length)
{
// Cada vez que tenemos que agrandar la biblioteca, agregamos 100 espacios mas
Track[] newLibrary = new Track[library.length + 100];
for(int i = 0; i < library.length; ++i)
{
// Movemos una por una las canciones que estan almacenadas en la biblioteca actual, hacia la nueva biblioteca
// Se estan pasando las canciones por referencia, eso significa que no se estan copiando (no se esta haciendo memoria nueva para cada cancion independiente)
newLibrary[i] = library[i];
}
// Reemplazamos la biblioteca actual por la nueva biblioteca
library = newLibrary;
}
String[] atributtes = cache.split("&&");
int temp_trackNo = Integer.parseInt(atributtes[0]);
String temp_artist = atributtes[1];
String temp_album = atributtes[2];
String temp_tittle = atributtes[3];
String temp_path = atributtes[4];
Track temp_Track = new Track(temp_tittle, temp_album, temp_artist, temp_trackNo, temp_path);
library[nextTrack++] = temp_Track;
}
private void addToLibrary(File trackFile)
{
// Si la biblioteca esta llena, tenemos que agrandarla
if(nextTrack == library.length)
{
// Cada vez que tenemos que agrandar la biblioteca, agregamos 100 espacios mas
Track[] newLibrary = new Track[library.length + 100];
for(int i = 0; i < library.length; ++i)
{
// Movemos una por una las canciones que estan almacenadas en la biblioteca actual, hacia la nueva biblioteca
// Se estan pasando las canciones por referencia, eso significa que no se estan copiando (no se esta haciendo memoria nueva para cada cancion independiente)
newLibrary[i] = library[i];
}
// Reemplazamos la biblioteca actual por la nueva biblioteca
library = newLibrary;
}
// Se almacena la nueva cancion en la posicion correspondiente y posteriormente se incrementa el indice de la cancion siguiente
// Notese el uso del ++ y el uso del constructor de canciones que ya hace todo solo
library[nextTrack++] = new Track(trackFile);
Track Tcache = new Track(trackFile);
String save = Tcache.getTrackNo()+"&&"+Tcache.getArtist()+"&&"+Tcache.getAlbum()+"&&"+Tcache.getTitle()+"&&"+Tcache.getPath();
WriteFile(save);
}
/**
* Imprime el largo de la biblioteca y todos los titulos de sus canciones
*/
// Metodos de crear y leer archivo txt
public void WriteFile(String message) {
File file = new File("biblioteca\\library.txt");//Modificar ruta dejando \\playthis\\filename.txt
try {
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file,true);
PrintWriter bw = new PrintWriter(fw);
bw.println(message);
bw.close();
}catch (Exception e) {
e.printStackTrace();
}
}
public File Filechoose() {
File cache = null;
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
if(chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
{
cache = chooser.getSelectedFile();
}
return cache;
}
public static String ReadFile(String fileroot) {
File file = new File("biblioteca\\library.txt");
//File file = new File(fileroot);
String text = "";
String cache = "";
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
while(true) {
cache=br.readLine();
if(cache!= null) {
text = text+cache+"\n";
}
else {
break;
}
}
br.close();
fr.close();
}catch (Exception e) {
e.printStackTrace();
}
return text;
}
public void PrintFile(File file) {
String ruta = file.getAbsolutePath();
File archivo = null;
FileReader fr = null;
BufferedReader br = null;
try {
// Apertura del fichero y creacion de BufferedReader para poder
// hacer una lectura comoda (disponer del metodo readLine()).
archivo = new File (ruta);
fr = new FileReader (archivo);
br = new BufferedReader(fr);
// Lectura del fichero
String linea;
while((linea=br.readLine())!=null)
System.out.println(linea);
}
catch(Exception e){
e.printStackTrace();
}finally{
// En el finally cerramos el fichero, para asegurarnos
// que se cierra tanto si todo va bien como si salta
// una excepcion.
try{
if( null != fr ){
fr.close();
}
}catch (Exception e2){
e2.printStackTrace();
}
}
}
// Fin de metodos de crear y leer archivos
public void printLibrary()
{
System.out.println("Library size: " + library.length);
System.out.println("Track count: " + nextTrack);
System.out.println("Wasted slots: " + (library.length - nextTrack));
for(int i = 0; i < nextTrack; ++i)
{
System.out.println("Track No " + (i+1) + " : " + library[i].getTitle());
}
}
}