/**
* This file is part of HIDB2.
*
* HIDB2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HIDB2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser Public License for more details.
*
* You should have received a copy of the GNU Lesser Public License
* along with HIDB2. If not, see <http://www.gnu.org/licenses/>.
*/
package hidb2.gui.editor;
import hidb2.Activator;
import hidb2.gui.Application;
import hidb2.gui.preferences.PrefConst;
import hidb2.gui.util.DlgImageViewer;
import hidb2.kern.AttrFile;
import hidb2.kern.AttrImage;
import hidb2.kern.AttrType;
import hidb2.kern.Attribut;
import hidb2.kern.AttributedDescription;
import hidb2.kern.Card;
import hidb2.kern.DeltaMMFile;
import hidb2.kern.FileStatus;
import hidb2.kern.Folder;
import hidb2.kern.FolderDescription;
import hidb2.kern.HIDBConst;
import hidb2.kern.StatusCycle;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.forms.widgets.FormToolkit;
/**
* HIDB2 form toolkit adds some convinient methods for object
* creations.
*/
public class HIDB2Toolkit extends FormToolkit implements HIDBConst
{
final protected static Logger log = Logger.getLogger("hidb2.gui.editor");
public HIDB2Toolkit(Display display)
{
super(display);
}
public Button createGridButton(Composite containerBtn, String label)
{
Button btn = createButton(containerBtn, label, SWT.PUSH);
GridData gd1 = new GridData(SWT.FILL, SWT.CENTER, true, false);
btn.setLayoutData(gd1);
return btn;
}
public Button createGridButton(Composite containerBtn, String label, SelectionListener callback)
{
Button btn = createButton(containerBtn, label, SWT.PUSH);
GridData gd1 = new GridData(SWT.FILL, SWT.CENTER, true, false);
btn.setLayoutData(gd1);
if (callback != null)
{
btn.addSelectionListener(callback);
}
return btn;
}
/**
* Open a dialog browser to browse the list of cards of a folder.
* @param parent
* @param f
*/
public static void BrowseCard(Shell parent, Folder f)
{
AttributedDescription descr = ((FolderDescription) f.getDescription()).getCardDescription();
Attribut a1 = descr.find(AttrType.T_Image);
if (a1 != null)
{
int attrIdx = descr.getAttributList().indexOf(a1);
List<Card> lstCard = f.getLstCard();
List<File> lstFic = new ArrayList<File>(lstCard.size());
DlgImageViewer div = new DlgImageViewer(parent);
IPreferenceStore pref = Activator.getDefault().getPreferenceStore();
div.setPref(pref.getInt(PrefConst.P_IVR_MIN_ZOOM), pref.getInt(PrefConst.P_IVR_MAX_ZOOM), pref
.getInt(PrefConst.P_IVR_DEFAULT_WIDTH), pref.getInt(PrefConst.P_IVR_DEFAULT_HEIGHT));
for (Card crd : lstCard)
{
AttrImage imgAttr = (AttrImage) crd.getValue(attrIdx);
if (imgAttr != null)
{
File imgFile = new File(imgAttr.getAbsolutePath());
if (imgFile.exists())
{
lstFic.add(imgFile);
}
else
{
log.warning("File [" + imgAttr.getAbsolutePath() + "] not found");
}
}
}
div.open(lstFic);
}
}
/**
* The if the Image attributs are pointed to exisiting files.
*
* @param parent
* @param f Folder to be checked.
*/
public static void checkFiles(Shell parent, Folder f)
{
// Check folder's data
List<AttrFile> lstFol = f.checkFile();
// Check cards data
for (Card cd : f.getLstCard())
{
List<AttrFile> lstCd = cd.checkFile();
lstFol.addAll(lstCd);
}
}
private static DirectoryDialog _dd = null;
public static int massImport(Shell parent, Folder f)
{
int res = C_OK;
// Open the mapping dialog (does not exist for the moment)
Attribut a1 = ((FolderDescription) f.getDescription()).getCardDescription().find(AttrType.T_Image);
if (a1 != null)
{
// Keep previous dialog -> Keep memory of last directory
if (_dd == null)
{
_dd = new DirectoryDialog(parent);
}
String fndir = _dd.open();
if (fndir != null)
{
File fdir = new File(fndir);
File[] tabMMFile = fdir.listFiles();
CardImportMonitor op = new CardImportMonitor(f, a1, tabMMFile);
try
{
new ProgressMonitorDialog(parent).run(true, false, op);
}
catch (InvocationTargetException e1)
{
e1.printStackTrace();
}
catch (InterruptedException e1)
{
e1.printStackTrace();
}
res = op.res;
}
}
else
{
MessageDialog.openError(parent, "Usage Error", "No multimedia attribut in card description");
res = C_FAIL;
}
return res;
}
/**
* The if the Image attributs are pointed to exisiting files.
*
* @param parent
* @param f Folder to be checked.
*/
public static int checkCardDir(final Shell parent, final Folder f)
{
int res = C_OK;
final Attribut a1 = ((FolderDescription) f.getDescription()).getCardDescription().find(AttrType.T_Image);
if (a1 != null)
{
final int attrIdx = ((FolderDescription) f.getDescription()).getCardDescription().getAttributList().indexOf(a1);
// Keep previous dialog -> Keep memory of last directory
if (_dd == null)
{
_dd = new DirectoryDialog(parent);
}
final String fndir = _dd.open();
final List<DeltaMMFile> lstDelta = new LinkedList<DeltaMMFile>();
if (fndir != null)
{
try
{
new ProgressMonitorDialog(parent).run(true, false, new IRunnableWithProgress()
{
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException
{
File fdir = new File(fndir);
File[] tabMMFile = fdir.listFiles();
monitor.beginTask("Checking", tabMMFile.length);
List<File> lstMMFile = new ArrayList<File>(tabMMFile.length);
for (File fic : tabMMFile)
{
if (Attribut.isImageExtension(fic))
{
lstMMFile.add(fic);
}
}
// Check card images files status
monitor.subTask("Card checking");
for (Card cd : f.getLstCard())
{
AttrFile ai = (AttrFile) cd.getValue(attrIdx);
if (ai != null)
{
String absCardFileName = ai.getAbsolutePath();
File foundMMFile = null;
for (Iterator<File> itfic = lstMMFile.iterator(); itfic.hasNext() && (foundMMFile == null);)
{
File floc = itfic.next();
if (absCardFileName.equals(floc.getAbsolutePath()))
{
foundMMFile = floc;
itfic.remove();
}
}
if (foundMMFile == null)
{
// Suggest Card Deletion
DeltaMMFile dlt = new DeltaMMFile(cd, attrIdx, FileStatus.MISSING);
lstDelta.add(dlt);
}
else
{
// Check Last modification Date and size
Date dateBase = ai.getLastModificationDate();
// Get the actual file last modification date
long lmd = foundMMFile.lastModified();
if (dateBase.getTime() < lmd)
{
// Suggest Card Update
DeltaMMFile dlt = new DeltaMMFile(cd, attrIdx, FileStatus.UPDATED);
dlt.setFoundFic(foundMMFile);
lstDelta.add(dlt);
}
else
{
// Nothing to do with the card, it's up to date
}
}
} // Else the card has a non valued AttrFile
}
monitor.subTask("New File Importation");
// If the list of read files is not empty, it means that some MM files are not recorded
// in the database ==> Suggest to create them
for (File fic : lstMMFile)
{
DeltaMMFile dlt = new DeltaMMFile(fic);
dlt.setFoundFic(fic);
AttrImage imgAttr = (AttrImage) a1.create(Application.getDataStore(), fic);
if (imgAttr != null)
{
dlt.setAttrFile(imgAttr);
}
lstDelta.add(dlt);
monitor.worked(1);
}
monitor.done();
}
});
}
catch (InvocationTargetException e1)
{
e1.printStackTrace();
}
catch (InterruptedException e1)
{
e1.printStackTrace();
}
DlgDeltaFile dlg = new DlgDeltaFile(parent);
res = dlg.open(lstDelta);
if (res == C_OK)
{
for (DeltaMMFile dmmfic : lstDelta)
{
Card fcd = dmmfic.getFoundCard();
switch (dmmfic.getAction())
{
case CREATE:
Card nc = Application.getDataStore().createCard(f);
nc.setValue(attrIdx, dmmfic.getAttrFile());
break;
case DELETE:
Application.getDataStore().remove(f, fcd);
break;
case UPDATE:
AttrFile atf = (AttrFile) fcd.getValue(attrIdx);
atf.setLastModificationDate(new Date(dmmfic.getFoundFic().lastModified()));
if (atf.getStatus() == StatusCycle.UNCHANGED)
{
atf.setStatus(StatusCycle.MODIFIED);
}
if (fcd.getStatus() == StatusCycle.UNCHANGED)
{
fcd.setStatus(StatusCycle.MODIFIED);
}
break;
}
}
}
}
}
else
{
MessageDialog.openError(parent, "Usage Error", "No multimedia attribut in card description");
res = C_FAIL;
}
return res;
}
}