/*
 * Created on 2005-04-05
 *
 * Part of the Symbian development plug-in for Eclipse
 * Copyright (C) Mikołaj Zalewski, 2005
 */
package pl.zalewski.mikolaj.symbdev.core.internal.mmpmodel;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;

import pl.zalewski.mikolaj.symbdev.core.mmpmodel.IAIFDeclaration;
import pl.zalewski.mikolaj.symbdev.core.mmpmodel.MMPFileReference;
import pl.zalewski.mikolaj.symbdev.utils.PathUtils;

/**
 * An implementation of the AIF declaration model which stores
 * all the information in memory (no file synchronization is done.
 * Subclasses does it).
 * 
 * @author mikolaj
 */
public class AIFDeclaration extends BitmapContainer implements IAIFDeclaration {

    public AIFDeclaration() {
        super();
    }

    public String getTargetFilename() {
        return fTargetFilename;
    }

    public void setTargetFilename(String name) throws CoreException {
        fTargetFilename=name;
        fDirty=true;
        fireChange();
    }

    public MMPFileReference getSourceFile() {
        return fSourceFile;
    }

    public void setSourceFile(IPath file) {
        throw new IllegalStateException();
    }
    
    public void setSourceFile(MMPFileReference file) {
        fSourceFile=file;
        fDirty=true;
        fireChange();
    }

    public boolean copyFrom(AIFDeclaration m) {
        boolean mod=false;
        if (copyFrom((BitmapContainer)m))
            mod=true;
        if (!MMPModel.isEqual(fTargetFilename, m.fTargetFilename))
        {
            fTargetFilename=m.fTargetFilename;
            mod=true;
        }
        if (!MMPModel.isEqual(fSourceFile, m.fSourceFile))
        {
            fSourceFile=m.fSourceFile;
            mod=true;
        }
        if (mod)
            fireChange();
        return mod;
    }
    
    public void setCurrentDir(IPath path) {
        fCurrentDir=path;
    }
    
    /**
     * If the current dir is set, add the bitmap. If not throws the
     * IllegalStateException
     * 
     * @see SetCurrentDir(IPath)
     */
    public void addBitmap(IPath file, String depth) throws CoreException {
        if (fCurrentDir==null)
            throw new IllegalStateException();
        IPath localPath=fCurrentDir.append(fSourceFile.fSourcePathString);
        String relative=PathUtils.makeRelativePath(file, localPath);
        addBitmap(new MMPFileReference(relative, fSourceFile.fSourcePathString),
                depth);
    }
    
    String fTargetFilename;
    MMPFileReference fSourceFile;
    IPath fCurrentDir;
}
