/**
 * This program and the accompanying materials
 * are made available under the terms of the License
 * which accompanies this distribution in the file LICENSE.txt
 */
package uk.ac.bolton.archimate.editor.ui.factory;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.RGB;

import uk.ac.bolton.archimate.editor.preferences.IPreferenceConstants;
import uk.ac.bolton.archimate.editor.preferences.Preferences;
import uk.ac.bolton.archimate.editor.ui.ColorFactory;
import uk.ac.bolton.archimate.editor.ui.IArchimateImages;



/**
 * Abstract Element UI Provider
 * 
 * @author Phillip Beauvoir
 */
public abstract class AbstractElementUIProvider implements IElementUIProvider {
    
    private static ImageRegistry fImageRegistry = new ImageRegistry();
    
    @Override
    public Image getImage(EObject instance) {
        return getImage();
    }
    
    @Override
    public String getDefaultShortName() {
        return getDefaultName();
    }
    
    @Override
    public Color getDefaultColor() {
        return ColorConstants.white;
    }
    
    /**
     * Create a new ImageDescriptor substituting the user's preference for fill color
     */
    protected ImageDescriptor getImageDescriptorWithUserFillColor(String imageName) {
        // Not a preference
        if(!Preferences.STORE.getBoolean(IPreferenceConstants.SHOW_FILL_COLORS_IN_GUI)) {
            return IArchimateImages.ImageFactory.getImageDescriptor(imageName);
        }
        
        ImageDescriptor newImageDescriptor = fImageRegistry.getDescriptor(imageName);
        
        // Create new ImageDescriptor
        if(newImageDescriptor == null) {
            ImageDescriptor originalImageDescriptor = IArchimateImages.ImageFactory.getImageDescriptor(imageName);
            
            Color color = ColorFactory.getUserDefaultFillColor(providerFor());

            // No user default color
            if(color == null) {
                return originalImageDescriptor;
            }

            ImageData imageData = originalImageDescriptor.getImageData();

            for(int i = 0; i < imageData.width; i++) {
                for(int j = 0; j < imageData.height; j++) {
                    RGB rgb = imageData.palette.getRGB(imageData.getPixel(i, j));
                    if(rgb.red > 0) {
                        imageData.setPixel(i, j, ColorFactory.getPixelValue(color.getRGB()));
                    }
                }
            }

            newImageDescriptor = ImageDescriptor.createFromImageData(imageData);
            fImageRegistry.put(imageName, newImageDescriptor);
        }

        return newImageDescriptor;
    }

    /**
     * Create a new Image substituting the user's preference for fill color
     * 
     * We can't dispose of any previous Image as it will still be referenced elsewhere in the application
     */
    protected Image getImageWithUserFillColor(String imageName) {
        // Not a preference
        if(!Preferences.STORE.getBoolean(IPreferenceConstants.SHOW_FILL_COLORS_IN_GUI)) {
            return IArchimateImages.ImageFactory.getImage(imageName);
        }
        
        Image image = fImageRegistry.get(imageName);
        
        if(image == null) {
            // Create ImageDescriptor and try again
            getImageDescriptorWithUserFillColor(imageName);
            image = fImageRegistry.get(imageName);
        }

        return image;
    }
}

@@ -3002,38 +3002,35 @@
     /**%0D%0A     * 
-Create
+Get
  a new Image sub
@@ -3067,32 +3067,100 @@
 e for fill color
+.%0D%0A     * If there is no user fill color or preference not set, then
 %0D%0A     * %0D%0A     
@@ -3664,16 +3664,22 @@
  Create 
+local 
 ImageDes
@@ -3806,32 +3806,268 @@
 et(imageName);%0D%0A
+            %0D%0A            // If image is still null then we didn't make a new one and so need the default image%0D%0A            if(image == null) %7B%0D%0A                return IArchimateImages.ImageFactory.getImage(imageName);%0D%0A            %7D%0D%0A
         %7D%0D%0A%0D%0A   

Check for null value