001 package net.sourceforge.mp3dings.utils;
002
003 /* Mp3dings - manage mp3 meta-information
004 * Copyright (C) 2003 Moritz Ringler
005 * $Id: InsertChecksumTask.java,v 1.9 2007/03/29 16:03:51 ringler Exp $
006 *
007 * This program is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU General Public License
009 * as published by the Free Software Foundation; either version 2
010 * of the License, or (at your option) any later version.
011 *
012 * This program is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015 * GNU General Public License for more details.
016 *
017 * You should have received a copy of the GNU General Public License
018 * along with this program; if not, write to the Free Software
019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020 */
021 import java.awt.Component;
022 import java.awt.Frame;
023 import java.io.File;
024 import java.util.logging.Logger;
025 import de.mospace.lang.ClassLoaderProvider;
026 import net.sourceforge.mp3dings.Globals;
027 import net.sourceforge.mp3dings.Mp3dingsTableModel;
028
029 public class InsertChecksumTask extends TimeIntensiveTask{
030 private static final Logger logger = Logger.getLogger(InsertChecksumTask.class.getPackage().getName());
031 private final Mp3dingsTableModel t;
032 private final int targetCol;
033 private final int[] selectedRows;
034 private final Checksum cs;
035 private String status = null;
036 private final Component trigger;
037 private final ClassLoaderProvider clp;
038
039 private Checksum getChecksum(){
040 ChecksumFactory cf = null;
041 try{
042 cf = (ChecksumFactory) Bean.load(Globals.getPrefNode(), "checksum");
043 } catch (Exception ex){
044 logger.warning("Error loading checksum factory.");
045 logger.warning(ex.toString());;
046 }
047
048 if(cf == null){
049 //System.err.println("Creating new ChecksumFactory.");
050 cf = new ChecksumFactory(clp);
051 String savepath = Globals.getPrefNode().get("checksum",null);
052 if(savepath == null){
053 savepath = Globals.getDir("BEANS");
054 } else {
055 savepath = (new File(savepath)).getAbsoluteFile().getParent();
056 }
057 cf.save(Globals.getPrefNode(), "checksum", savepath);
058 } else {
059 cf.setClassLoaderProvider(clp);
060 }
061 if(!cf.isJacksumAvailable()){
062 if((new ChecksumFactoryCustomizer(cf)).installJacksum(owner, clp)){
063 status = Globals.getString("JS_INST_SUCCESS");
064 return getChecksum();
065 } else {
066 status = Globals.getString("JS_INST_FAIL");
067 }
068 return null;
069 }
070 return cf.getChecksum();
071 }
072
073 /**
074 * @param owner parent window
075 **/
076 public InsertChecksumTask(Frame owner,
077 Component trigger,
078 Mp3dingsTableModel model,
079 int[] rows,
080 int targetCol,
081 ClassLoaderProvider clp){
082 super(owner, Globals.getString("CHECKSUM_MONITOR_TITLE"), "");
083 t = model;
084 this.targetCol = targetCol;
085 this.trigger = trigger;
086 this.clp = clp;
087 selectedRows = (int[]) rows.clone();
088 cs = getChecksum();
089 status = Globals.getString("CHECKSUM_READY");
090 }
091
092 public void run(){
093 if(cs == null){
094 return;
095 }
096 status = Globals.getString("CHECKSUM_BUSY");
097 getMonitor().setProgress(1);
098 int i = 0, row = -1;
099 int len = selectedRows.length;
100 File fi;
101 byte[] buff = new byte[ChecksumFactory.BUFFERSIZE];
102 for(i=0; i<selectedRows.length; i++){
103 if (getMonitor().isCanceled()) {
104 break;
105 }
106 row = selectedRows[i];
107 fi = t.getFile(row);
108 getMonitor().setNote(fi.getName());
109 cs.readFile(fi, buff);
110 t.set(cs.getFormattedValue(), row, targetCol);
111 /* calculate progress in integer arithmetic */
112 getMonitor().setProgress(1 + i * 99 / len);
113 }
114 status = Globals.getMessage("CHECKSUM_SUMMARY", new Object[]{
115 new Integer(i),
116 new Integer(len)});
117 }
118
119 public void finished(){
120 t.fireTableDataChanged();
121 }
122
123 public String getStatus(){
124 return status;
125 }
126 }