001 package net.sourceforge.mp3dings;
002
003 /*
004 * Mp3dings - manage mp3 meta-information
005 * Copyright (C) 2003 Moritz Ringler
006 * $Id: DataTableModel.java 125 2007-12-22 14:31:36Z ringler $
007 *
008 * This program is free software; you can redistribute it and/or
009 * modify it under the terms of the GNU General Public License
010 * as published by the Free Software Foundation; either version 2
011 * of the License, or (at your option) any later version.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, write to the Free Software
020 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021 */
022 import java.awt.BorderLayout;
023 import java.awt.Color;
024 import java.awt.Component;
025 import java.awt.Frame;
026 import java.io.File;
027 import java.io.FileOutputStream;
028 import java.io.IOException;
029 import java.util.ArrayList;
030 import java.util.Arrays;
031 import java.util.Collections;
032 import java.util.List;
033 import java.util.Properties;
034 import java.util.logging.Logger;
035 import java.util.prefs.PreferenceChangeEvent;
036 import java.util.prefs.PreferenceChangeListener;
037 import javax.swing.BorderFactory;
038 import javax.swing.DefaultListCellRenderer;
039 import javax.swing.JLabel;
040 import javax.swing.JList;
041 import javax.swing.SwingUtilities;
042 import de.mospace.swing.ListPicker;
043 import net.sourceforge.mp3dings.export.DatabaseExporter;
044 import net.sourceforge.mp3dings.export.DatabaseImporter;
045 import net.sourceforge.mp3dings.utils.DialogExceptionHandler;
046 import net.sourceforge.mp3dings.utils.FindFilesTask;
047 import net.sourceforge.mp3dings.utils.TimeIntensiveTask;
048
049 /**
050 * This class serves as the table model for the spreadsheet of the mp3dings
051 * "Data" tab. It provides functionality to
052 * <ul>
053 * <li> add mp3files to the workspace</li>
054 * <li> glean mp3 meta-information from file names and ID3 tags</li>
055 * <li> view, sort and edit this information globally</li>
056 * </ul>
057 *
058 *
059 *@author Moritz Ringler
060 *@version $Revision: 125 $ ($Date: 2007-12-22 15:31:36 +0100 (Sa, 22 Dez 2007) $)
061 */
062 public final class DataTableModel extends AbstractTableModel {
063 private static final Logger logger = Logger.getLogger(DataTableModel.class.getPackage().getName());
064 private List data;
065 private transient String jdbcImportWhereClause = null;
066 private transient String jdbcImportPassword = null;
067
068 /** Creates a new instance of this class. */
069 public DataTableModel() {
070 init();
071
072 /*
073 * data will have the following structure
074 * index_i Object[]{ File file_i, Object[] datatablerow_i}
075 */
076 data = new ArrayList();
077
078 /* Clear jdbcImportWhereClause and jdbcImportPassword when DatabaseImporter configuration has
079 * changed */
080 Globals.getPrefNode().addPreferenceChangeListener(
081 new PreferenceChangeListener(){
082 public void preferenceChange(PreferenceChangeEvent evt){
083 if(evt.getKey().equals(DatabaseImporter.PREF_IMPORT)){
084 //System.out.println("db config reset");
085 jdbcImportWhereClause = null;
086 jdbcImportPassword = null;
087 }
088 }
089 });
090 }
091
092 /**
093 * Gets the allowedFields attribute of the DataTableModel object
094 *
095 *@return The allowedFields value
096 */
097 public Field[] getAllowedFields() {
098 return new Field[]{
099 Field.FILENAME,
100 Field.ARTIST,
101 Field.TITLE,
102 Field.ALBUM,
103 Field.TRACK,
104 Field.YEAR,
105 Field.GENRE,
106 Field.COMMENT,
107 Field.ORIGINAL_ARTIST,
108 Field.LYRICIST,
109 Field.ORIGINAL_LYRICIST,
110 Field.LYRICS,
111 Field.CONDUCTOR,
112 Field.REMIXED_BY,
113 Field.SUBTITLE,
114 Field.CONTENT_GROUP,
115 Field.ORIGINAL_ALBUM,
116 Field.PART_OF_SET,
117 Field.SET_SUBTITLE,
118 Field.ISRC,
119 Field.FULLPATH,
120 Field.LENGTH,
121 Field.SIZE,
122 Field.COMPOSER,
123 Field.BPM,
124 Field.ORIGINAL_FILENAME,
125 Field.COPYRIGHT,
126 Field.ENCODER,
127 Field.MOOD,
128 Field.LANGUAGE,
129 Field.ORCHESTRA,
130 Field.PUBLISHER,
131 Field.KEY,
132 Field.COM_INFO_URL,
133 Field.LEGAL_URL,
134 Field.OFFICIAL_URL,
135 Field.ARTIST_URL,
136 Field.SOURCE_URL,
137 Field.WEB_RADIO_URL,
138 Field.PAY_URL,
139 Field.PUBLISHER_URL,
140 Field.ATTACHED_PICTURE,
141 Field.ENCODER_SETTINGS
142 };
143 }
144
145
146 /**
147 * Gets the persistentFields attribute of the DataTableModel object
148 *
149 *@return The persistentFields value
150 */
151 public Field[] getPersistentFields() {
152 return new Field[]{};
153 }
154
155
156 /**
157 * Returns whether values of the specified field can be edited in this
158 * table.
159 *
160 *@param field the field to (not) edit
161 *@return whether values of field can be edited
162 */
163 public boolean getEditable(Field field) {
164 if (field == Field.FILENAME ||
165 field == Field.FULLPATH ||
166 field == Field.LENGTH ||
167 field == Field.SIZE) {
168 return false;
169 } else {
170 return true;
171 }
172 }
173
174
175 /**
176 * Gets the defaultFields attribute of the DataTableModel object
177 *
178 *@return The defaultFields value
179 */
180 public Field[] getDefaultFields() {
181 return new Field[]{
182 Field.FILENAME,
183 Field.ARTIST,
184 Field.TITLE,
185 Field.ALBUM,
186 Field.TRACK,
187 Field.YEAR,
188 Field.GENRE,
189 Field.COMMENT,
190 Field.ATTACHED_PICTURE
191 };
192 }
193
194
195 /**
196 * Gets a field value from the table.
197 *
198 *@param entry Mp3 file identifier.
199 *@param field Field identifier.
200 *@return The fieldValue value
201 */
202 public Object getFieldValue(int entry, Field field) {
203 return (getColumn(field) == -1)
204 ? null
205 : getValueAt(getRows(entry), getColumn(field));
206 }
207
208
209 /*
210 * Functions for adding and removing data
211 */
212 /**
213 * Adds a single file to the workspace. Use addFile(File, Mp3FileReader)
214 * for adding multiple files.
215 *
216 *@param file mp3-file that will be added
217 */
218 private void addFile(File file) {
219 Mp3FileReader mp3FileReader = new Mp3FileReader(filters);
220 addFile(file, mp3FileReader);
221 }
222
223
224 /**
225 * Adds a single file to the workspace.
226 *
227 *@param file mp3-file that will be added
228 *@param mfr The feature to be added to the File attribute
229 */
230 private void addFile(File file, Mp3FileReader mfr) {
231 Object[] newRow = new Object[getColumnCount()];
232 Arrays.fill(newRow, null);
233 int col;
234 if ((col = getColumn(Field.FILENAME)) >= 0) {
235 newRow[col] = mfr.getField(file, Field.FILENAME);
236 }
237 if ((col = getColumn(Field.FULLPATH)) >= 0) {
238 newRow[col] = mfr.getField(file, Field.FULLPATH);
239 }
240 getData().add(new Object[]{file, newRow});
241 }
242
243
244 /**
245 * Guesses artist, track and title from each filename. Adds the information
246 * gathered in this way to the spreadsheet.
247 *
248 *@param owner The feature to be added to the FilenameInfo attribute
249 */
250 public void addFilenameInfo(Frame owner) {
251 if (!discardChanges(owner)) {
252 return;
253 }
254 int fileCount = 0;
255 Object[] dataRow;
256 Object[] tableRow;
257 Object buff = null;
258 ;
259 Mp3FileReader mp3FileReader = new Mp3FileReader(filters);
260 final int nEntries = getRowCount();
261 final int nc = getColumnCount();
262 for (int entry = 0; entry < nEntries; entry++) {
263 dataRow = (Object[]) getData().get(entry);
264 tableRow = (Object[]) dataRow[1];
265 String fn = (String) ((getColumn(Field.FILENAME) >= 1) ?
266 getFieldValue(entry, Field.FILENAME) :
267 ((File) dataRow[0]).getName());
268 for (int c = 0; c < nc; c++) {
269 buff = mp3FileReader.getFilenameField(fn, getField(c));
270 if (buff != null) {
271 tableRow[c] = buff;
272 }
273 }
274 fileCount++;
275 }
276 fireMessageSent(Globals.getMessage("DATA_GET_DATA_FROM_FILE_STATUS",
277 fileCount));
278 setSorted(false);
279 fireTableDataChanged();
280 //informs the listening Mp3dingsTable that something has changed
281 //and that it needs to doLyout()
282 //without incrementing attached change counters
283 fireDataChanged(0);
284 }
285
286
287 /**
288 * Adds several files to the workspace.
289 *
290 *@param nf array of mp3-files that will be added
291 */
292 public void addFiles(File[] nf) {
293 Mp3FileReader mp3FileReader = new Mp3FileReader(filters);
294 File[] oldFiles;
295 File[] newFiles;
296 int i;
297 int j;
298 int n = getData().size();
299 int m = nf.length;
300 int fileCount = 0;
301 if (n == 0) {
302 for (i = 0; i < m; i++) {
303 addFile(nf[i], mp3FileReader);
304 }
305 } else {
306 newFiles = new File[m];
307 System.arraycopy(nf, 0, newFiles, 0, m);
308 oldFiles = new File[n];
309 for (int row = 0; row < n; row++) {
310 oldFiles[row] = getFile(row);
311 }
312 Arrays.sort(oldFiles);
313 Arrays.sort(newFiles);
314 j = 0;
315 i = 0;
316 while (i < m && j < n) {
317 int k = oldFiles[j].compareTo(newFiles[i]);
318 if (k == 0) {
319 i++;
320 } else if (k > 0) {
321 addFile(newFiles[i], mp3FileReader);
322 i++;
323 } else {
324 j++;
325 }
326 }
327 for (; i < m; i++) {
328 addFile(newFiles[i], mp3FileReader);
329 }
330 }
331 addRowsToSorter(getData().size() - n, getData().size());
332 setSorted(false);
333 fireTableRowsInserted(n, getData().size() - 1);
334 fireDataChanged(0);
335 //tells the Mp3dingsTable to doLayout()
336 fireMessageSent(Globals.getMessage("DATA_ADD_FILES_STATUS",
337 getData().size() - n));
338 //Added x files
339 }
340
341
342 /**
343 * Removes all files from the workspace.
344 *
345 *@param owner Description of the Parameter
346 */
347 public void clearFiles(Frame owner) {
348 if (!discardChanges(owner)) {
349 return;
350 }
351 int n = getData().size();
352 if (n == 0) {
353 return;
354 }
355 getData().clear();
356 fireTableRowsDeleted(0, n - 1);
357 fireMessageSent(
358 Globals.getMessage("DATA_CLEAR_FILES_STATUS", n));
359 //"Removed all x files."
360 setSorted(false);
361 remAllRowsFromSorter();
362 }
363
364
365 /**
366 * Removes the files in rows <code>rows[]</code> from the workspace.
367 *
368 *@param rows Array contaning the rows to be removed
369 */
370 public void removeRows(int[] rows) {
371 int[] entries = getEntries(rows);
372 int m = entries.length;
373 /*
374 * must start highest index first
375 * if we don't want to mess up
376 * the indexes
377 */
378 Arrays.sort(entries);
379 for (int i = m - 1; i >= 0; i--) {
380 if (entries[i] < getData().size()) {
381 getData().remove(entries[i]);
382 /*
383 * tell the world which files have gone
384 */
385 fireTableRowsDeleted(getRows(entries[i]), getRows(entries[i]));
386 remRowFromSorter(entries[i]);
387 }
388 }
389 }
390
391
392 /**
393 * Gets the data attribute of the DataTableModel object
394 *
395 *@return The data value
396 */
397 List getData() {
398 return data;
399 }
400
401
402 /**
403 * Description of the Method
404 *
405 *@param old2new Description of the Parameter
406 *@param newlength Description of the Parameter
407 *@return Description of the Return Value
408 */
409 protected List reorganizeData(int[] old2new, int newlength) {
410 final int nEntries = getData().size();
411 List newData = new ArrayList(nEntries);
412 Object[] newRow;
413 Object[] oldRow;
414 File file;
415 for (int entry = 0; entry < nEntries; entry++) {
416 oldRow = (Object[]) getData().get(entry);
417 file = (File) oldRow[0];
418 oldRow = (Object[]) oldRow[1];
419 newRow = new Object[newlength];
420 Arrays.fill(newRow, null);
421 for (int c = 0, nc = old2new.length; c < nc; c++) {
422 if (old2new[c] != -1) {
423 newRow[old2new[c]] = oldRow[c];
424 }
425 }
426 newData.add(new Object[]{file, newRow});
427 }
428 return newData;
429 }
430
431
432 /**
433 * Sets the data attribute of the DataTableModel object
434 *
435 *@param v The new data value
436 */
437 protected void setData(List v) {
438 data = v;
439 }
440
441
442 /**
443 * Recursively grabs files from directories and adds them to the model.
444 *
445 *@param files Input list of mp3-files (f) and directories (d)
446 *@param owner The feature to be added to the Recursively attribute
447 */
448 public void addRecursively(File[] files, Frame owner) {
449 if (files == null || files.length == 0) {
450 return;
451 }
452 FindFilesTask addJob =
453 new FindFilesTask(owner, files,
454 new Mp3FileChooser.Mp3Filter()) {
455 public void finished() {
456 File[] f = getFiles();
457 if (f != null && f.length > 0) {
458 addFiles(f);
459 }
460 fireTableDataChanged();
461 //informs the listening Mp3dingsTable that something has changed
462 //and that it needs to doLayout()
463 //without incrementing attached change counters
464 fireDataChanged(0);
465 }
466 };
467 addJob.start();
468 }
469
470
471 /**
472 * Reads information from the ID3v1 or ID3v2 tags. Updates the spreadsheet
473 *
474 *@param ntag 1 for ID3v1, 2 for ID3v2. If <code>tag</code> is neither
475 * the method returns without doing anything.
476 *@param owner The feature to be added to the ID3v attribute
477 */
478 public void addID3v(int ntag, Frame owner) {
479 if (!discardChanges(owner)) {
480 return;
481 }
482 final int tag = ntag;
483 if (tag != 1 && tag != 2) {
484 return;
485 }
486
487 (
488 new TimeIntensiveTask(owner,
489 Globals.getMessage("GATHER_DATA_FROM_ID3vX_TAB", tag),
490 null){
491 int fileCount = 0;
492 int frameCount = 0;
493
494
495 public void run() {
496 getMonitor().setProgress(1);
497 //UpdateRunnable notify = new UpdateRunnable(0,0);
498 int nEntries = getData().size();
499 int tagtype = (tag == 1) ?
500 Mp3FileReader.ID3V1 :
501 Mp3FileReader.ID3V2;
502 File mp3;
503 Object[] dataRow;
504 Object[] tableRow;
505 Object buff = null;
506 Mp3FileReader mp3FileReader = new Mp3FileReader(filters, tagtype);
507 final int nc = getColumnCount();
508 for (int entry = 0; entry < nEntries && !getMonitor().isCanceled(); entry++) {
509 dataRow = (Object[]) getData().get(entry);
510 tableRow = (Object[]) dataRow[1];
511 mp3 = getFile(getRows(entry));
512 if (tag == 2 || tag == 1) {
513 for (int c = 0; c < nc; c++) {
514 buff = mp3FileReader.getID3Field(mp3, getField(c));
515 if (buff != null && !buff.equals("null")) {
516 tableRow[c] = buff;
517 frameCount++;
518 }
519 }
520 fileCount++;
521 }
522 getMonitor().setProgress(1 + 99 * entry / nEntries);
523 //notify.setLastRow(entry);
524 //SwingUtilities.invokeLater(notify); (too slow for many files!!!)
525 }
526 }
527
528
529 public void finished() {
530 fireMessageSent(
531 Globals.getMessage("DATA_GET_DATA_FROM_ID3VX_STATUS",
532 new Integer[]{
533 new Integer(tag),
534 new Integer(fileCount),
535 new Integer(frameCount)
536 }));
537 setSorted(false);
538 fireTableDataChanged();
539 //informs the listening Mp3dingsTable that something has changed
540 //and that it needs to doLyout()
541 //without incrementing attached change counters
542 fireDataChanged(0);
543 }
544
545 }).start();
546 }
547
548 public void queryDatabase(MainFrame owner){
549 if(getRowCount() > 0){
550 (new DatabaseReader(owner)).start();
551 }
552 }
553
554
555 private class DatabaseReader extends TimeIntensiveTask{
556 private int fileCount = 0;
557 private final MainFrame owner;
558 private final Mp3FileReader mp3FileReader = new Mp3FileReader(filters);
559 private final Class[] columnclasses;
560 private final Runnable fireTableDataChanged = new Runnable(){
561 public void run(){
562 fireTableDataChanged();
563 }
564 };
565
566 DatabaseReader(MainFrame owner){
567 super(owner,
568 Globals.getString("DB_IMPORT_PROGRESS"),
569 null);
570 this.owner = owner;
571 columnclasses = new Class[getColumnCount()];
572 for(int i=0, n = columnclasses.length; i<n; i++){
573 columnclasses[i] = getColumnClass(i);
574 }
575 }
576
577 /* saves a newly created mp3dings configuration to a default location */
578 private void saveConfig(Properties p){
579 FileOutputStream fos = null;
580 File f = new File(Globals.getDir("HOME"), "defaultjdbcconf.properties");
581 try{
582 fos = new FileOutputStream(f);
583 p.store(fos, "mp3dings<->jdbc configuration");
584 Globals.getPrefNode().put(DatabaseImporter.PREF_IMPORT, f.getAbsolutePath());
585 } catch (IOException ex){
586 logger.severe("Error saving mp3dings/jdbc configuration " + f);
587 logger.severe(ex.toString());
588 return;
589 } finally {
590 if(fos != null){
591 try{
592 fos.close();
593 } catch (IOException ex){
594 logger.severe("Error saving mp3dings/jdbc configuration " + f);
595 logger.severe(ex.toString());
596 }
597 }
598 }
599 }
600
601 public void run() {
602 getMonitor().setProgress(1);
603 DatabaseImporter imp = null;
604 try{
605
606 /* load or create new database connection configuration */
607 Properties config = DatabaseImporter.loadConfig();
608 boolean queryWhere = true;
609 if(config == null){
610
611 config = DatabaseExporter.loadConfig();
612 config = DatabaseImporter.editConfig(owner, config, getFields(), null);
613 queryWhere = false;
614 if (config == null){
615 return; //user cancel
616 } else {
617 saveConfig(config);
618 }
619 }
620
621 /* overwrite and edit where clause */
622 if(jdbcImportWhereClause != null){
623 config.setProperty(DatabaseImporter.P_WHERE, jdbcImportWhereClause);
624 }
625 if(queryWhere && DatabaseImporter.editWhere(owner, config) == null){
626 return; //user cancel
627 }
628 jdbcImportWhereClause = config.getProperty(DatabaseImporter.P_WHERE);
629
630 /* fill in password if we've cached it */
631 String pw = config.getProperty(DatabaseImporter.P_PASSWORD);
632 if ((jdbcImportPassword != null) &&
633 (pw == null || pw.length() == 0)){
634 config.setProperty(DatabaseImporter.P_PASSWORD, jdbcImportPassword);
635 }
636
637 /* create a DatabaseImporter for this table */
638 imp = new DatabaseImporter(config, owner.app, getFields());
639 jdbcImportPassword = config.getProperty(DatabaseImporter.P_PASSWORD);
640
641 /* declare loop variables */
642 List results = new ArrayList();
643 Object[] res = null;
644 ListPicker picker = null;
645 JLabel top = null;
646 final String SKIP = "SKIP";
647 final String SKIPALL = "SKIPALL";
648 final String BESTMATCH = "BESTMATCH";
649
650 boolean skipall = false;
651 boolean bestmatch = false;
652
653 for(int i=0, n = getRowCount(); i< n && !getMonitor().isCanceled(); i++){
654 int entry = getEntries(i);
655
656 /* query the database */
657 Object[] input = getData(entry);
658 imp.query(input);
659
660 /* load the query results into results;
661 the result array for the first row is re-used,
662 result arrays for further rows are newly allocated */
663 results.clear();
664 if(imp.hasResult()){
665 results.add(imp.nextResult(res));
666 }
667 while(imp.hasResult()){
668 results.add(imp.nextResult(null));
669 }
670
671 final int l = input.length;
672 final int rsize = results.size();
673
674 /* decide what do to depending on the number of results */
675 if(rsize == 0 || (rsize != 1 && skipall)){
676 /* we have no result, or several results which
677 the user wants to skip
678 --> skip this entry
679 */
680 continue;
681 }
682
683 if(rsize == 1){
684 /* we have a single result
685 --> use it */
686 res = (Object[]) results.get(0); //this is probably unnecessary but does not cost much
687 } else if( bestmatch ){
688 /* we have several results and the user
689 * has told us to alwas use the best match
690 * --> find the best result and use it
691 */
692 res = (Object[]) Collections.min(results, new Ranker(input));
693 } else {
694 /* we have several results and don't know which to use */
695
696 /* sort results according to how well they
697 * match the input */
698 Collections.sort(results, new Ranker(input));
699
700 /* display a modal dialog
701 * where the user can pick a match
702 * and decide what to do in the future
703 */
704 if(picker == null){
705 /* create the dialog */
706 picker = new ListPicker(owner, results);
707 top = new JLabel("<html>"+asString(input)+"</html>");
708 top.setOpaque(true);
709 top.setBackground(new Color(-1184257));
710 top.setBorder(BorderFactory.createEmptyBorder(5,0,5,0));
711 top.setFont(Globals.SANS_BOLD_12);
712 picker.getContentPane().add(top, BorderLayout.NORTH);
713 picker.setTitle(Globals.getString("DB_IMPORT_PICK_MATCH"));
714 picker.setCellRenderer(new DefaultListCellRenderer(){
715 public Component getListCellRendererComponent(JList llist, Object value, int index, boolean isSelected, boolean cellHasFocus){
716 return super.getListCellRendererComponent(llist, asString((Object[]) value), index, isSelected, cellHasFocus);
717 }
718 });
719 picker.addPickerButton(Globals.getString("SKIP"), SKIP);
720 picker.addPickerButton(Globals.getString("SKIP_ALL"), SKIPALL);
721 picker.addPickerButton(Globals.getString("BEST_MATCH"), BESTMATCH);
722 } else {
723 picker.setChoice(results);
724 top.setText("<html>"+asString(getData(entry))+"</html>");
725 }
726 /* update the table and display the dialog */
727 SwingUtilities.invokeLater(fireTableDataChanged);
728 picker.setVisible(true);
729
730 /* get the dialog result */
731 Object pickres = picker.getValue();
732 if(pickres == null){
733 /* user cancelled the dialog
734 * --> abort */
735 break;
736 } else if (pickres.equals(SKIP)){
737 /* skip this entry */
738 res = null;
739 getMonitor().setProgress(1 + 99 * i / n);
740 continue;
741 } else if (pickres.equals(SKIPALL)){
742 /* skip all non-unique results */
743 res = null;
744 skipall = true;
745 getMonitor().setProgress(1 + 99 * i / n);
746 continue;
747 } else if (pickres.equals(BESTMATCH)){
748 /* always use the best match */
749 bestmatch = true;
750 res = (Object[]) results.get(0);
751 } else {
752 /* use the user-selected result */
753 res = (Object[]) pickres;
754 }
755 }
756
757 /* update the current entry */
758 updateEntry(entry, res);
759 getMonitor().setProgress(1 + 99 * i / n);
760 fileCount++;
761 }
762 getMonitor().setProgress(100);
763 } catch (RuntimeException rexc){
764 logger.severe("Error importing data from SQL database");
765 logger.severe(rexc.toString());
766 } catch (Exception ex){
767 jdbcImportPassword = null;
768 (new DialogExceptionHandler(Globals.getString("DB_IMPORT_ERROR"), owner)).handleException(ex);
769 } finally {
770 if (imp != null && imp.isAlive()){
771 try{
772 imp.close();
773 } catch (Exception ex){
774 logger.warning("Error closing database connection." + imp);
775 logger.warning(ex.toString());
776 }
777 }
778 }
779 }
780
781 public void finished() {
782 fireMessageSent(Globals.getMessage("DB_IMPORT_STATUS", fileCount));
783 setSorted(false);
784 fireTableDataChanged();
785 //informs the listening Mp3dingsTable that something has changed
786 //and that it needs to doLyout()
787 //without incrementing attached change counters
788 fireDataChanged(0);
789 }
790
791 private void updateEntry(int entry, Object[] res){
792 Object[] input = getData(entry);
793 for(int c=0, n = res.length; c < n; c++){
794 Object v = res[c];
795 if(v != null && !v.equals("")){
796 input[c] = mp3FileReader.postProcessField(fields[c], makeValue(v, columnclasses[c]));
797 }
798 }
799 }
800 }//End of DatabaseReader class
801
802 private static String asString(Object[] obj){
803 StringBuffer sb = new StringBuffer();
804 for(int i=0, n=obj.length; i<n; i++){
805 if((obj[i] == null)){
806 sb.append(" ");
807 } else {
808 String str = obj[i].toString().replaceAll("\\s+", " ");
809 if(str.length() > 80){
810 str = str.substring(0, 81);
811 }
812 sb.append(str);
813 }
814 sb.append('\u2023');
815 }
816 return sb.substring(0, sb.length() -1);
817 }
818
819
820 /**
821 * Gets the file associated with the specified row.
822 *
823 *@param row A spreadsheet row
824 *@return The file associated with that row
825 */
826 public File getFile(int row) {
827 int entry = getEntries(row);
828 Object[] dataRow = (Object[]) getData().get(entry);
829 return ((File) dataRow[0]);
830 }
831
832
833 /**
834 * Associates the specified row with file f.
835 *
836 *@param f The new file value
837 *@param row The new file value
838 */
839 public void setFile(File f, int row) {
840 Mp3FileReader mp3FileReader = new Mp3FileReader(filters);
841 ((Object[]) getData().get(getEntries(row)))[0] = f;
842 int c = getColumn(Field.FILENAME);
843 if (c >= 0) {
844 setValueAt(mp3FileReader.getField(f, Field.FILENAME), row, c);
845 }
846 if ((c = getColumn(Field.FULLPATH)) >= 0) {
847 setValueAt(mp3FileReader.getField(f, Field.FULLPATH), row, c);
848 }
849 }
850
851
852 /**
853 * Associates the specified row with file f.
854 *
855 *@param f Description of the Parameter
856 *@param row Description of the Parameter
857 */
858 public void silentlySetFile(File f, int row) {
859 Mp3FileReader mp3FileReader = new Mp3FileReader(filters);
860 ((Object[]) getData().get(getEntries(row)))[0] = f;
861 int c = getColumn(Field.FILENAME);
862 if (c >= 0) {
863 set(mp3FileReader.getField(f, Field.FILENAME), row, c);
864 }
865 if ((c = getColumn(Field.FULLPATH)) >= 0) {
866 set(mp3FileReader.getField(f, Field.FULLPATH), row, c);
867 }
868 }
869
870
871 /*
872 * methods defined in Mp3dingsSortableTabelModel
873 */
874 /**
875 * Gets the data attribute of the DataTableModel object
876 *
877 *@param entry Description of the Parameter
878 *@return The data value
879 */
880 Object[] getData(int entry) {
881 return ((Object[]) ((Object[]) getData().get(entry))[1]);
882 }
883
884
885 /**
886 * Gets the prefKey attribute of the DataTableModel object
887 *
888 *@return The prefKey value
889 */
890 public String getPrefKey() {
891 return "data";
892 }
893 }