/**
 * 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 com.archimatetool.editor.diagram.figures.elements;

import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.graphics.Pattern;

import com.archimatetool.editor.diagram.figures.AbstractTextControlContainerFigure;
import com.archimatetool.editor.diagram.figures.FigureUtils;
import com.archimatetool.editor.diagram.figures.RectangleFigureDelegate;
import com.archimatetool.editor.preferences.IPreferenceConstants;
import com.archimatetool.editor.preferences.Preferences;
import com.archimatetool.model.ITextPosition;




/**
 * Product Figure
 * 
 * @author Phillip Beauvoir
 */
public class ProductFigure extends AbstractTextControlContainerFigure {
    
    protected static final int TOP_MARGIN = 12;

    public ProductFigure() {
        super(TEXT_FLOW_CONTROL);
        
        // Use a Rectangle Figure Delegate to Draw
        RectangleFigureDelegate figureDelegate = new RectangleFigureDelegate(this) {
            @Override
            public void drawFigure(Graphics graphics) {
                graphics.pushState();
                
                Rectangle bounds = getBounds();
                
                if(!isEnabled()) {
                    setDisabledState(graphics);
                }
                
                Pattern gradient = null;
                if(Preferences.STORE.getBoolean(IPreferenceConstants.SHOW_GRADIENT)) {
                    gradient = FigureUtils.createGradient(graphics, bounds, getFillColor());
                    graphics.setBackgroundPattern(gradient);
                }

                // Main Fill
                graphics.fillRectangle(bounds);
                
                if(gradient != null) {
                    gradient.dispose();
                }
                
                // Outline
                graphics.setForegroundColor(getLineColor());

                graphics.drawLine(bounds.x, bounds.y + TOP_MARGIN, bounds.getCenter().x, bounds.y + TOP_MARGIN);
                graphics.drawLine(bounds.getCenter().x, bounds.y + TOP_MARGIN, bounds.getCenter().x, bounds.y);
                        
                bounds.width--;
                bounds.height--;
                graphics.drawRectangle(bounds);
                
                graphics.popState();
            }

            @Override
            public Rectangle calculateTextControlBounds() {
                Rectangle bounds = getBounds();
                
                int textPosition = ((ITextPosition)getDiagramModelObject()).getTextPosition();
                
                if(textPosition == ITextPosition.TEXT_POSITION_TOP) {
                    bounds.y += TOP_MARGIN - 4;
                }
                
                return bounds;
            }
        };
        
        setFigureDelegate(figureDelegate);
    }
}

@@ -1484,32 +1484,137 @@
               %0D%0A
+                // Fill%0D%0A                graphics.setBackgroundColor(getFillColor());%0D%0A                %0D%0A
                 
@@ -1908,38 +1908,8 @@
 %0D%0A%0D%0A
-                // Main Fill%0D%0A
     

Fix Product Figure not setting background fill colour