package org.csstudio.opibuilder.widgets.figures;

import org.csstudio.opibuilder.widgets.Activator;
import org.csstudio.swt.widgets.figures.ITextFigure;
import org.csstudio.ui.util.CustomMediaFactory;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.graphics.Image;

/**
 * Figure for the menu button.
 *
 * @author Matthew Furseman
 *
 */
public class MenuButtonFigure extends Label implements ITextFigure{

    public static final int ICON_WIDTH = 15;

    private static final Image downArrow = CustomMediaFactory.getInstance().
            getImageFromPlugin(Activator.PLUGIN_ID, "icons/downArrow.png");

    private boolean showDownArrow = false;

    @Override
    public void setText(String s) {
        super.setText(s);
        layoutIcons();
    }

    @Override
    public void setBounds(Rectangle rect) {
        super.setBounds(rect);
        layoutIcons();
    }

    /**
     * Control the appearance of the down arrow indicator
     * on the menu button.
     *
     * @param showArrow if the arrow is to be shown.
     */
    public void showDownArrow(boolean showArrow) {
        this.showDownArrow = showArrow;
        if(showArrow) {
            setIcon(downArrow);
        } else {
            setIcon(null);
        }
        layoutIcons();
    }

    /**
     * Layout the contents of the widget so that, if an icon is displayed, it is
     * right aligned and the text remains centred.
     */
    private void layoutIcons() {
        if(showDownArrow) {
            setLabelAlignment(PositionConstants.RIGHT);
            setTextPlacement(PositionConstants.WEST);
        } else {
            setLabelAlignment(PositionConstants.CENTER);
        }
    }
}

@@ -704,52 +704,8 @@
 );%0A%0A
-    private boolean showDownArrow = false;%0A%0A
     
@@ -780,35 +780,36 @@
 s);%0A        
-layoutIcons
+updateLayout
 ();%0A    %7D%0A%0A 
@@ -900,35 +900,36 @@
 t);%0A        
-layoutIcons
+updateLayout
 ();%0A    %7D%0A%0A 
@@ -1143,48 +1143,8 @@
 ) %7B%0A
-        this.showDownArrow = showArrow;%0A
     
@@ -1207,73 +1207,241 @@
     
-%7D else %7B%0A            setIcon(null);%0A        %7D%0A        layoutIcons
+    setLabelAlignment(PositionConstants.RIGHT);%0A            setTextPlacement(PositionConstants.WEST);%0A        %7D else %7B%0A            setIcon(null);%0A            setLabelAlignment(PositionConstants.CENTER);%0A        %7D%0A        updateLayout
 ();%0A
@@ -1616,19 +1616,20 @@
 oid 
-layoutIcons
+updateLayout
 () %7B
@@ -1641,221 +1641,88 @@
     
-if(showDownArrow) %7B%0A            setLabelAlignment(PositionConstants.RIGHT);%0A            setTextPlacement(PositionConstants.WEST);%0A        %7D else %7B%0A            setLabelAlignment(PositionConstants.CENTER);%0A        %7D
+setIconTextGap((getBounds().width - this.getTextBounds().width - ICON_WIDTH)/2);
 %0A   

Refactor new figure class