com.dubture.symfony.ui.viewsupport.ImagesOnFileSystemRegistry.java Source code

Java tutorial

Introduction

Here is the source code for com.dubture.symfony.ui.viewsupport.ImagesOnFileSystemRegistry.java

Source

/*******************************************************************************
 * This file is part of the Symfony eclipse plugin.
 * 
 * (c) Robert Gruendler <r.gruendler@gmail.com>
 * 
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 ******************************************************************************/
package com.dubture.symfony.ui.viewsupport;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

import org.eclipse.dltk.ui.ScriptElementImageProvider;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;

import com.dubture.symfony.ui.SymfonyUiPlugin;

/**
 * Image registry that keeps its images on the local file system.
 * 
 * @since 3.4
 */
@SuppressWarnings("rawtypes")
public class ImagesOnFileSystemRegistry {

    private static final String IMAGE_DIR = "sf-images"; //$NON-NLS-1$

    private HashMap fURLMap;
    private final File fTempDir;
    private int fImageCount;

    public ImagesOnFileSystemRegistry() {
        fURLMap = new HashMap();
        fTempDir = getTempDir();
        new ScriptElementImageProvider();
        fImageCount = 0;
    }

    private File getTempDir() {
        try {
            File imageDir = SymfonyUiPlugin.getDefault().getStateLocation().append(IMAGE_DIR).toFile();
            if (imageDir.exists()) {
                // has not been deleted on previous shutdown
                delete(imageDir);
            }
            if (!imageDir.exists()) {
                imageDir.mkdir();
            }
            if (!imageDir.isDirectory()) {
                //            SymfonyUiPlugin
                //                  .logErrorMessage("Failed to create image directory " + imageDir.toString()); //$NON-NLS-1$
                return null;
            }
            return imageDir;
        } catch (IllegalStateException e) {
            // no state location
            return null;
        }
    }

    private void delete(File file) {
        if (file.isDirectory()) {
            File[] listFiles = file.listFiles();
            for (int i = 0; i < listFiles.length; i++) {
                delete(listFiles[i]);
            }
        }
        file.delete();
    }

    @SuppressWarnings("unchecked")
    public URL getImageURL(ImageDescriptor descriptor) {
        if (fTempDir == null)
            return null;

        URL url = (URL) fURLMap.get(descriptor);
        if (url != null)
            return url;

        File imageFile = getNewFile();
        ImageData imageData = descriptor.getImageData();
        if (imageData == null) {
            return null;
        }

        ImageLoader loader = new ImageLoader();
        loader.data = new ImageData[] { imageData };
        loader.save(imageFile.getAbsolutePath(), SWT.IMAGE_PNG);

        try {
            url = imageFile.toURI().toURL();
            fURLMap.put(descriptor, url);
            return url;
        } catch (MalformedURLException e) {
            //         SymfonyUiPlugin.log(e);
        }
        return null;
    }

    private File getNewFile() {
        File file;
        do {
            file = new File(fTempDir, String.valueOf(getImageCount()) + ".png"); //$NON-NLS-1$
        } while (file.exists());
        return file;
    }

    private synchronized int getImageCount() {
        return fImageCount++;
    }

    public void dispose() {
        if (fTempDir != null) {
            delete(fTempDir);
        }
        fURLMap = null;
    }
}