MultipleSelectionHelper.java :  » XML-UI » XUI » net » xoetrope » builder » editor » components » Java Open Source

Java Open Source » XML UI » XUI 
XUI » net » xoetrope » builder » editor » components » MultipleSelectionHelper.java
package net.xoetrope.builder.editor.components;

import java.util.Vector;
import java.awt.Component;
import net.xoetrope.builder.editor.XComponentSizer;
import net.xoetrope.xui.style.XStyle;
import net.xoetrope.xui.style.XStyleManager;
import java.awt.Font;
import java.awt.Color;
import net.xoetrope.xui.XProjectManager;

/**
 * A helper to help in the manipulation of multiple components
 * <p> Copyright (c) Xoetrope Ltd., 2002-2003</p>
 * <p> $Revision: 1.5 $</p>
 */
public class MultipleSelectionHelper extends PropertyHelper
{
  protected final static String propertyName[] = {"Type", "Name", "Left", "Top", "Width", "Height", "Style"};
  protected final static int numBasicProperties = 7;

  protected final static int LEFT_VALUE = 0;
  protected final static int TOP_VALUE = 1;
  protected final static int WIDTH_VALUE = 2;
  protected final static int HEIGHT_VALUE = 3;

  private Vector selectedComponents;

  /**
   * A helper for multiple component types
   */
  public MultipleSelectionHelper()
  {
  }

  /**
   * Get the class name for this component type
   * @return
   */
  public String getClassName()
  {
    return Vector.class.getName();
  }

  /**
   * Get the friendly type name for this component
   * @return "Multiple"
   */
  public String getComponentType()
  {
    return "Multiple";
  }

  public void setSelectedComponents( Vector comps )
  {
    selectedComponents = comps;
  }

  /**
   * Get the value of the property exposed by the component
   * @param comp the component instance
   * @param i the component property index
   * @return the value
   */
  public String getPropertyValue( Component comp, int i )
  {
    switch ( i ) {
      case 0: {
        return "Multiple";
      }
      case 1:
        return "Multiple";
      case 2:
        return new Integer( getValue( LEFT_VALUE ) ).toString();
      case 3:
        return new Integer( getValue( TOP_VALUE ) ).toString();
      case 4:
        return new Integer( getValue( WIDTH_VALUE ) ).toString();
      case 5:
        return new Integer( getValue( HEIGHT_VALUE ) ).toString();
      case 6:
        return "";
    }
    return null;
  }

  /**
   * Set the value of the property exposed by the component
   * @param comp the component instance
   * @param i the component property index
   * @return the value
   */
  public void setPropertyValue( Component comp, int i, String value )
  {
    switch ( i ) {
      case 0:
        break;
      case 1:
        comp.setName( value );
        break;
      case 2:
        comp.setLocation( new Integer( value ).intValue(), comp.getLocation().y );
        break;
      case 3:
        comp.setLocation( comp.getLocation().x, new Integer( value ).intValue() );
        break;
      case 4:
        comp.setSize( new Integer( value ).intValue(), comp.getSize().height );
        break;
      case 5:
        comp.setSize( comp.getSize().width, new Integer( value ).intValue() );
        break;
      case 6: {
        XStyle style = XProjectManager.getStyleManager().getStyle( value );
        int fontStyle = 0;
        if ( style.getStyleAsInt( XStyle.FONT_WEIGHT ) == 1 )
          fontStyle = Font.BOLD;
        if ( style.getStyleAsInt( XStyle.FONT_ITALIC ) == 1 )
          fontStyle = fontStyle | Font.ITALIC;

        Font font = new Font( style.getStyleAsString( XStyle.FONT_FACE ), fontStyle,
                              style.getStyleAsInt( XStyle.FONT_SIZE ) );
        Color fgColor = style.getStyleAsColor( XStyle.COLOR_FORE );
        Color bkColor = style.getStyleAsColor( XStyle.COLOR_BACK );

        setForeground( fgColor );
        setBackground( bkColor );
        setFont( font );
      }
      break;
    }
  }

  private int getValue( int type )
  {
    int value = 100000;
    if ( ( type == WIDTH_VALUE ) || ( type == HEIGHT_VALUE ) )
      value = -100000;

    if ( selectedComponents == null )
      return 0;

    int numComponents = selectedComponents.size();
    for ( int i = 0; i < numComponents; i++ ) {
      if ( type == LEFT_VALUE )
        value = Math.min( value, ( ( XComponentSizer )selectedComponents.elementAt( i ) ).getTarget().getLocation().x );
      else if ( type == TOP_VALUE )
        value = Math.min( value, ( ( XComponentSizer )selectedComponents.elementAt( i ) ).getTarget().getLocation().y );
      else if ( type == WIDTH_VALUE )
        value = Math.max( value, ( ( XComponentSizer )selectedComponents.elementAt( i ) ).getTarget().getSize().width );
      else if ( type == HEIGHT_VALUE )
        value = Math.max( value, ( ( XComponentSizer )selectedComponents.elementAt( i ) ).getTarget().getSize().height );
    }

    return value;
  }

  private void setValue( int type, int newValue )
  {
    int numComponents = selectedComponents.size();
    for ( int i = 0; i < numComponents; i++ ) {
      XComponentSizer sizer = ( ( XComponentSizer )selectedComponents.elementAt( i ) );
      if ( type == LEFT_VALUE )
        sizer.setLocation( newValue, sizer.getLocation().y );
      else if ( type == TOP_VALUE )
        sizer.setLocation( sizer.getLocation().x, newValue );
      else if ( type == WIDTH_VALUE )
        sizer.setSize( newValue, sizer.getSize().height );
      else if ( type == HEIGHT_VALUE )
        sizer.setSize( sizer.getSize().width, newValue );

      sizer.repositionTarget();
    }
  }

  private void setForeground( Color fgColor )
  {
    int numComponents = selectedComponents.size();
    for ( int i = 0; i < numComponents; i++ ) {
      XComponentSizer sizer = ( ( XComponentSizer )selectedComponents.elementAt( i ) );
      sizer.getTarget().setForeground( fgColor );
    }
  }

  private void setBackground( Color bkColor )
  {
    int numComponents = selectedComponents.size();
    for ( int i = 0; i < numComponents; i++ ) {
      XComponentSizer sizer = ( ( XComponentSizer )selectedComponents.elementAt( i ) );
      sizer.getTarget().setBackground( bkColor );
    }
  }

  private void setFont( Font font )
  {
    int numComponents = selectedComponents.size();
    for ( int i = 0; i < numComponents; i++ ) {
      XComponentSizer sizer = ( ( XComponentSizer )selectedComponents.elementAt( i ) );
      sizer.getTarget().setFont( font );
    }
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.