Package net.xoetrope.swing

Source Code of net.xoetrope.swing.XList

package net.xoetrope.swing;

import javax.swing.JList;
import net.xoetrope.xui.XListHolder;
import javax.swing.DefaultListModel;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.TransferHandler;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import net.xoetrope.swing.dnd.XTransferHandlerFactory;
import net.xoetrope.xui.XProjectManager;
import net.xoetrope.xui.events.XHandlerInvoker;
import net.xoetrope.xui.events.XListenerHelper;

/**
* <p>A wrapper for the Swing List class</p>
* <p>Copyright (c) Xoetrope Ltd., 1998-2006. See license.txt for more details</p>
* @version 1.0
*/
public class XList extends JList implements XListHolder, XListenerHelper, ListSelectionListener
{
  protected DefaultListModel listModel;
  protected XHandlerInvoker invoker;

  /**
   * Create a new DefaultListModel and set it as the model for this component
   */
  public XList()
  {
    listModel = new DefaultListModel();
    setModel( listModel );
   
    ListSelectionModel sellistModel = getSelectionModel();
    sellistModel.addListSelectionListener( this );
  }

  /**
   * Set the list to its default selection state
   */
  public void setDefaultSelection()
  {
    select( 0 );
  }
 
  /**
   * @return the selected object in the list
   */
  public Object getSelectedObject()
  {
    return getSelectedValue();
  }

  /**
   * @return the selected objects in the list
   */
  public Object[] getSelectedObjects()
  {
    return getSelectedValues();
  }

  /**
   * Get the selected object
   * @param o the selected object
   */
  public void setSelectedObject( Object o )
  {
    select( o );
  }

  /**
   * Set the selected objects
   * @param values the selected object
   */
  public void setSelectedObjects( Object[] values )
  {
    if ( values == null )
      return;
   
    if ( getSelectionMode() != ListSelectionModel.MULTIPLE_INTERVAL_SELECTION )
      select( values[ 0 ] );
    else {
      int[] indices = new int[ values.length ];
      int idx = 0;
      ListModel lm = getModel();
      int numElements = lm.getSize();
      for ( int i = 0; i < numElements; i++ ) {
        Object obji = lm.getElementAt( i );
        for ( int j = 0; j < values.length; j++ ) {
          if ( obji.equals( values[ j ] )) {
            indices[ idx++ ] = i;
            break;
          }
        }
      }
     
      if ( idx > 0 ) {
        int[] idxs = new int[ idx ];
        System.arraycopy( indices, 0, idxs, 0, idx );
        setSelectedIndices( idxs );
      }
      else
        clearSelection();
   
  }

  /**
   * set the selected object
   * @param o the value to be selected
   */
  public void select( Object o )
  {
    super.setSelectedValue( o, true );
  }

  /**
   * select the element at the specified index
   * @param i
   */
  public void select( int i )
  {
    super.setSelectedIndex( i );
  }
 
  /**
   * Add an item to the listmodel
   * @param s the item to be added
   */
  public void addItem( String s )
  {
    listModel.add( listModel.getSize(), s );
  }

  /**
   * @return the current size of the listmodel
   */
  public int getItemCount()
  {
    return listModel.getSize();
  }

  /**
   * Remove all the elements from the listmodel
   */
  public void removeAll()
  {
    listModel.removeAllElements();
  }
 
  public void addHandler( Object page, String type, String methodName ) throws NoSuchMethodException
  {
    invoker = new XHandlerInvoker( page, this, methodName );
  }
 
  public void valueChanged( ListSelectionEvent evt )
  {
    if ( invoker != null )
      invoker.invoke();
  }
 
  /**
   * Setup drag and drop support
   * @param state true to enable drag and drop support
   */
  public void setDragEnabled( boolean state )
  {   
    if ( state ) {
      XTransferHandlerFactory thf = XTransferHandlerFactory.getInstance( XProjectManager.getCurrentProject());
      if ( thf != null ) {
        TransferHandler th = thf.getTransferHandler( this, null );
        if ( th != null ) {
          super.setDragEnabled( true );
          setTransferHandler( th );
          return;
        }
      }
    }

    super.setDragEnabled( false );
  }
}
TOP

Related Classes of net.xoetrope.swing.XList

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.