Package net.xoetrope.swt

Source Code of net.xoetrope.swt.XSplitPane

package net.xoetrope.swt;

import net.xoetrope.xui.XAttributedComponent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Sash;

/**
* <p>
* Draws a splitpane
* </p>
* <p>
* Copyright (c) Xoetrope Ltd., 1998-2003
* </p>
* License: see license.txt $Revision: 2.18 $
*/
public class XSplitPane extends XPanel implements XAttributedComponent
{

  private boolean vertical;

  private int location;

  private int size;

  /**
   * Create a splitpane
   *
   * @param parent
   *          parent object
   */
  public XSplitPane( Object parent )
  {
    super( parent );
  }

  /**
   * Suppress the subclassing exception
   */
  protected void checkSubclass()
  {
  }

  /**
   * Set one or more attributes of the component.
   *
   * @param attribName
   *          the name of the attribute
   * @param attribValue
   *          the value of the attribute
   * @return 0 for success, non zero for failure or to require some further
   *         action
   */
  public int setAttribute( String attribName, Object attribValue )
  {
    String attribNameLwr = attribName.toLowerCase();
    String attribValueStr = (String)attribValue;
    String attribValueLwr = null;
    if ( attribValue != null )
      attribValueLwr = attribValueStr.toLowerCase();
    if ( attribNameLwr.equals( "tooltip" ) )
      setToolTipText( attribValueStr );
    else if ( attribNameLwr.equals( "orientation" ) ) {
      vertical = attribValueStr.compareToIgnoreCase( "vert" ) == 0;
      setLayout( new FormLayout() );
    }
    else if ( attribNameLwr.equals( "location" ) )
      location = Integer.parseInt( attribValueStr );
    else if ( attribNameLwr.equals( "size" ) )
      size = Integer.parseInt( attribValueStr );
    else if ( attribNameLwr.equals( "visible" ) )
      setVisible( attribValueLwr.equals( "true" ) || attribValueLwr.equals( "1" ) );
    else
      return -1;

    return 0;
  }

  /**
   * Set the location attribute
   *
   * @param location
   *          the value of location
   */
  public void setLocation( int location )
  {
    this.location = location;
  }

  /**
   * Set the size attribute
   *
   * @param size
   *          the value of size
   */
  public void setSize( int size )
  {
    this.size = size;
  }

  /**
   * Return the size attribute
   */
  public int getSizeSplit()
  {
    return size;
  }

  /**
   * Do the layout
   */
  public void layout()
  {
    if ( getBounds().width > 0 ) {
      if ( vertical )
        layoutVertical();
      else
        layoutHorizontal();
      super.layout();
    }
  }

  /**
   * Do the layout of the panel
   */
  public void superLayout()
  {
    super.layout();
  }

  /**
   * Do the layout for a vertical splitpane
   */
  public void layoutVertical()
  {
    Control[] c = getChildren();
    if ( c.length > 1 ) {
      final Sash sash = new Sash( this, SWT.VERTICAL );
      final int width = getBounds().width;
      FormData data = new FormData();
      data.top = new FormAttachment( 0, 0 ); // Attach to top
      data.bottom = new FormAttachment( 100, 0 ); // Attach to bottom
      data.left = new FormAttachment( location * 100 / width, 0 ); // Attach
                                                                    // halfway
                                                                    // across
      data.width = size;
      sash.setLayoutData( data );
      sash.addSelectionListener( new SelectionAdapter()
      {
        public void widgetSelected( SelectionEvent event )
        {
          // We reattach to the left edge, and we use the x value of the event
          // to determine the offset from the left
          ( (FormData)sash.getLayoutData() ).left = new FormAttachment( event.x * 100 / width, 0 );
          // Until the parent window does a layout, the sash will not be redrawn
          // in its new location.
          superLayout();
        }
      } );
      data = new FormData();
      data.top = new FormAttachment( 0, 0 );
      data.bottom = new FormAttachment( 100, 0 );
      data.left = new FormAttachment( 0, 0 );
      data.right = new FormAttachment( sash, 0 );
      c[ 0 ].setLayoutData( data );
      data = new FormData();
      data.top = new FormAttachment( 0, 0 );
      data.bottom = new FormAttachment( 100, 0 );
      data.left = new FormAttachment( sash, 0 );
      data.right = new FormAttachment( 100, 0 );
      c[ 1 ].setLayoutData( data );
    }
  }

  /**
   * Do the layout for a horizontal splitpane
   */
  public void layoutHorizontal()
  {
    Control[] c = getChildren();
    if ( c.length > 1 ) {
      final Sash sash = new Sash( this, SWT.HORIZONTAL );
      final int height = getBounds().height;
      FormData data = new FormData();
      data.left = new FormAttachment( 0, 0 ); // Attach halfway across
      data.right = new FormAttachment( 100, 0 ); // Attach to bottom
      data.top = new FormAttachment( location * 100 / height, 0 ); // Attach to
                                                                    // top
      data.width = size;
      sash.setLayoutData( data );
      sash.addSelectionListener( new SelectionAdapter()
      {
        public void widgetSelected( SelectionEvent event )
        {
          // We reattach to the left edge, and we use the x value of the event
          // to determine the offset from the left
          ( (FormData)sash.getLayoutData() ).top = new FormAttachment( event.y * 100 / height, 0 );
          // Until the parent window does a layout, the sash will not be redrawn
          // in its new location.
          superLayout();
        }
      } );
      data = new FormData();
      data.top = new FormAttachment( 0, 0 );
      data.bottom = new FormAttachment( sash, 0 );
      data.left = new FormAttachment( 0, 0 );
      data.right = new FormAttachment( 100, 0 );
      c[ 0 ].setLayoutData( data );
      data = new FormData();
      data.top = new FormAttachment( sash, 0 );
      data.bottom = new FormAttachment( 100, 0 );
      data.left = new FormAttachment( 0, 0 );
      data.right = new FormAttachment( 100, 0 );
      c[ 1 ].setLayoutData( data );
    }
  }
}
TOP

Related Classes of net.xoetrope.swt.XSplitPane

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.