Package net.xoetrope.swt

Source Code of net.xoetrope.swt.XToolItem

package net.xoetrope.swt;

import java.io.IOException;
import java.io.InputStream;
import net.xoetrope.xui.XAttributedComponent;
import net.xoetrope.xui.XModelHolder;
import net.xoetrope.xui.XProjectManager;
import net.xoetrope.xui.data.XModel;
import net.xoetrope.xui.events.XActionable;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.ToolItem;

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

  protected Menu menu;

  protected XModel model;

  protected final XToolBar tb;

  /**
   * Constructs a new toolitem for a toolbar
   *
   * @param parent
   *          parent toolbar
   * @param style
   *          the style
   */
  public XToolItem( XToolBar parent, int style )
  {
    super( parent, style );
    tb = parent;
    if ( style == SWT.DROP_DOWN )
      menu = new Menu( getDisplay().getActiveShell(), SWT.POP_UP );
  }

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

  /**
   * Set an Action for the toolitem - does nothing
   *
   * @param action
   *          the action object
   */
  public void setAction( Object action )
  {
  }

  /**
   * 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();
   
    int rc = 0;
    if ( attribNameLwr.equals( "content" ) )
      setText( attribValueStr );
    else if ( attribNameLwr.equals( "tooltip" ) )
      setToolTipText( attribValueStr );
    else if ( attribNameLwr.equals( "image" ) ) {
      try {
        InputStream url = XProjectManager.getCurrentProject().getUrl( attribValueStr ).openStream();
        Image im = new Image( getDisplay(), url );
        if ( im != null )
          setImage( im );
      }
      catch ( IOException e ) {
        e.printStackTrace();
      }
    }
    else
      rc = -1;

    ( (XToolBar)getParent() ).setDisplayAttributes();
    return rc;
  }

  /**
   * Set the XModel which we will be generating the toolitem dropdown from
   *
   * @param xmodel
   *          the XModel of data
   */
  public void setModel( XModel xmodel )
  {
    model = xmodel;
    if ( model != null ) {
      model.get();
    }
    update();
  }

  /**
   * Get the model
   */
  public XModel getModel()
  {
    return model;
  }

  /**
   * Update the toolitem dropdown
   */
  public void update()
  {
    menu = new Menu( getDisplay().getActiveShell(), SWT.POP_UP );
    int nbItems = model.getNumChildren();
    for ( int i = 0; i < nbItems; i++ ) {
      String text = getText( model, i );
      MenuItem mi = new MenuItem( menu, SWT.PUSH );
      mi.setText( text );
    }
    addListener( SWT.Selection, new Listener()
    {
      public void handleEvent( Event event )
      {
        if ( event.detail == SWT.ARROW ) {
          Rectangle rect = getBounds();
          Point pt = new Point( rect.x, rect.y + rect.height );
          pt = tb.toDisplay( pt );
          menu.setLocation( pt.x, pt.y );
          menu.setVisible( true );
        }
      }
    } );
  }

  public Menu getMenu()
  {
    return menu;
  }

  /**
   * Get the appropriate text inside the model
   *
   * @param xmodel
   *          parent model
   * @param i
   *          index of the children
   * @return the text
   */
  public String getText( XModel xmodel, int i )
  {
    XModel xm = xmodel.get( i );
    String value = xm.getAttribValueAsString( xm.getAttribute( "value" ) );
    if ( value != null )
      return value;
    return xm.getAttribValueAsString( xm.getAttribute( "id" ) );
  }
}
TOP

Related Classes of net.xoetrope.swt.XToolItem

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.