Package net.xoetrope.swing

Source Code of net.xoetrope.swing.XMenuBar

package net.xoetrope.swing;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import net.xoetrope.xui.XAppender;
import net.xoetrope.xui.XProject;
import net.xoetrope.xui.XProjectManager;
import net.xoetrope.xui.XStartupObject;

/**
* A wrapper for menus
* <p>Copyright (c) Xoetrope Ltd., 1998-2004<br>
* @version 1.0
*/
public class XMenuBar extends JMenuBar implements XAppender
{
  protected XProject currentProject = XProjectManager.getCurrentProject();
 
  /**
   * Create a new menu bar
   */
  public XMenuBar()
  {
    super();
  }
 
  /**
   * Set an Action for the menu bar - does nothing
   * @param action the action object
   */
  public void setAction( Object action )
  {
  }
 
  /**
   * Do any final setup of the menu needed. If there is an existing menu bar in
   * the application the menus of this menu bar are attached to the existing
   * menu bar, unless the menu bar name matches that of the exitsing menu bar,
   * in which case the old menu bar is replaced by this one. The menu bar is
   * normally named by the name of the page within which it was declared.
   */
  public void setup()
  {
    XStartupObject so = currentProject.getStartupObject();
    XMenuBar menuBar = (XMenuBar)so.getApplicationMenuBar();
    if (( menuBar == null ) || menuBar.getName().equals( getName())) {
      so.setApplicationMenuBar( this );
      currentProject.setObject( "MenuBar", this );
    }
    else {
      int numMenus = this.getMenuCount();
      for ( int i = 0; i < numMenus; i++ ) {
        XMenu menu = (XMenu)getMenu( i );
        menuBar.append( menu, menu.getName());
        // Decrement the counters as an item has been removed from the menu
        numMenus--;
        i--;
      }
    }
  }

  /**
   * Appends the object o to this item
   * @param o the appended item
   * @param name the name of the menui bar
   */
  public void append( Object o, String name )
  {
    ((JMenu)o).setName( name );
    add( (JMenu)o );
  }
 
  /**
   * Appends the object o to this item
   * @param o the appended item
   * @param name the name of the menui bar
   */
  public void append( Object o, String name, String owner )
  {
    ((JMenu)o).setName( name );
    add( (JMenu)o );
  }
 
  /**
   * Implemented from the XAppender interface. No separator needed at this level.
   */
  public void addSeparator(){}

  /**
   * Get a child object by name
   * @param name the name of the menu bar
   * @return the child
   */
  public Object getObject( String name )
  {
    int menuCount = getMenuCount();
    for ( int i = 0; i < menuCount; i++ ) {
      JMenu menu = getMenu( i );
      Object obj = getObject( menu, name );
      if ( obj != null )
        return obj;
    }
    return null;
  }
 
  /**
   * Get a child object by name
   * @param meu the menu or submenu to be searched
   * @param name the name of the menu bar
   * @return the child
   */
  public Object getObject( Object menu, String name )
  {
    JMenu jmenu = (JMenu)menu;
    int itemCount = jmenu.getItemCount();
    for ( int j = 0; j < itemCount; j++ ) {
      Object item = jmenu.getItem( j );
      if ( item instanceof JMenu ) {
        Object so = getObject( item, name );
        if ( so != null )
          return so;
      }
      else if ( item instanceof JMenuItem ) {
        String itemName = ( ( JMenuItem )item ).getName();
        if ( ( itemName != null ) && ( itemName.compareTo( name ) == 0 ) )
          return item;
      }
    }
   
    return null;
  }
}
TOP

Related Classes of net.xoetrope.swing.XMenuBar

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.