View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 eviware.com
    3    *
    4    *  soapUI is free software; you can redistribute it and/or modify it under the
    5    *  terms of version 2.1 of the GNU Lesser General Public License as published by
    6    *  the Free Software Foundation.
    7    *
    8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
    9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    10   *  See the GNU Lesser General Public License for more details at gnu.org.
    11   */
    12
    13  package com.eviware.soapui.support.action.swing;
    14
    15  import java.awt.event.ActionEvent;
    16
    17  import javax.swing.AbstractAction;
    18  import javax.swing.Action;
    19  import javax.swing.JButton;
    20  import javax.swing.JMenu;
    21  import javax.swing.JPopupMenu;
    22
    23  import com.eviware.soapui.support.actions.MarkerAction;
    24  import com.eviware.soapui.support.components.JXToolBar;
    25  import com.jgoodies.forms.builder.ButtonBarBuilder;
    26
    27  /***
    28   * ActionList-related utilities
    29   *
    30   * @author Ole.Matzura
    31   */
    32
    33  public class ActionSupport
    34  {
    35  	public static JPopupMenu buildPopup( ActionList actions )
    36  	{
    37  		if( actions == null || actions.getActionCount() == 0 )
    38  			return null;
    39
    40  		JPopupMenu popup = new JPopupMenu( actions.getLabel() );
    41
    42  	   return ActionSupport.addActions(actions, popup);
    43  	}
    44
    45  	public static JMenu buildMenu( ActionList actions )
    46  	{
    47  		if( actions == null || actions.getActionCount() == 0 )
    48  			return null;
    49
    50  		JMenu menu = new JMenu( actions.getLabel() );
    51
    52  	   return ActionSupport.addActions(actions, menu);
    53  	}
    54
    55  	public static JPopupMenu addActions(ActionList actions, JPopupMenu popup)
    56  	{
    57  		if( actions == null || actions.getActionCount() == 0 )
    58  			return popup;
    59
    60  		for (int i = 0; i < actions.getActionCount(); i++)
    61  	   {
    62  	   	Action action = actions.getActionAt(i);
    63  	   	if( action instanceof MarkerAction )
    64  	   		continue;
    65
    66  	   	if( action == ActionSupport.SEPARATOR_ACTION )
    67  	   		popup.addSeparator();
    68  			else if( action instanceof ActionSupport.ActionListAction )
    69  			{
    70  				ActionList actionList = ((ActionListAction)action).getActionList();
    71  				if( actionList == null || actionList.getActionCount() == 0 )
    72  					System.err.println( "null/empty ActionList in action " + action.getValue( Action.NAME ));
    73  				else
    74  					popup.add( buildMenu( actionList ));
    75  			}
    76  	   	else
    77  	   		popup.add( action );
    78  	   }
    79
    80  	   return popup;
    81  	}
    82
    83  	public static JMenu addActions(ActionList actions, JMenu menu)
    84  	{
    85  		if( actions == null || menu == null )
    86  			return menu;
    87
    88  		for (int i = 0; i < actions.getActionCount(); i++)
    89  	   {
    90  	   	Action action = actions.getActionAt(i);
    91
    92  	   	if( action instanceof MarkerAction )
    93  	   		continue;
    94
    95  			if( action == ActionSupport.SEPARATOR_ACTION )
    96  			{
    97  	   		menu.addSeparator();
    98  			}
    99  			else if( action instanceof ActionSupport.ActionListAction )
    100 			{
    101 				JMenu subMenu = buildMenu( ((ActionListAction)action).getActionList() );
    102 				if( subMenu == null )
    103 					subMenu = new JMenu( ((ActionListAction)action).getActionList().getLabel() );
    104 				menu.add( subMenu);
    105 			}
    106 	   	else if( action != null )
    107 	   	{
    108 	   		menu.add( action );
    109 	   	}
    110 	   }
    111
    112 	   return menu;
    113 	}
    114
    115 	public final static Action SEPARATOR_ACTION = new AbstractAction()
    116 	{
    117 		public void actionPerformed(ActionEvent e)
    118 		{
    119 		}
    120 	};
    121
    122 	public static class ActionListAction extends AbstractAction
    123 	{
    124 		private final ActionList actionList;
    125
    126 		public ActionListAction( ActionList actionList )
    127 		{
    128 			this.actionList = actionList;
    129 		}
    130
    131 		public ActionList getActionList()
    132 		{
    133 			return actionList;
    134 		}
    135
    136 		public void actionPerformed(ActionEvent e)
    137 		{
    138 			Action defaultAction = actionList.getDefaultAction();
    139 			if( defaultAction != null )
    140 				defaultAction.actionPerformed(  e  );
    141 		}
    142 	};
    143
    144 	public static JPopupMenu insertActions(ActionList actions, JPopupMenu popup, int index)
    145 	{
    146 		for (int i = 0; i < actions.getActionCount(); i++)
    147 	   {
    148 	   	Action action = actions.getActionAt(i);
    149 	   	if( action instanceof MarkerAction )
    150 	   		continue;
    151
    152 			if( action == ActionSupport.SEPARATOR_ACTION )
    153 	   		popup.insert( new JPopupMenu.Separator(), index+i );
    154 	   	else if( action instanceof ActionSupport.ActionListAction )
    155 		   	popup.insert( buildMenu( ((ActionSupport.ActionListAction)action).getActionList() ), index+i );
    156 	   	else
    157 	   		popup.insert( action, index+i );
    158 	   }
    159
    160 	   return popup;
    161 	}
    162
    163 	public static void addActions( ActionList actionList, ButtonBarBuilder builder )
    164 	{
    165 		for( int c = 0; c < actionList.getActionCount(); c++ )
    166 		{
    167 			Action action = actionList.getActionAt( c );
    168 			if( action == SEPARATOR_ACTION )
    169 			{
    170 				builder.addUnrelatedGap();
    171 			}
    172 			else
    173 			{
    174 				if( c > 0 )
    175 					builder.addRelatedGap();
    176
    177 				builder.addFixed( new JButton( action ));
    178 			}
    179 		}
    180 	}
    181
    182 	public static void addActions( ActionList actionList, JXToolBar toolbar )
    183 	{
    184 		for( int c = 0; c < actionList.getActionCount(); c++ )
    185 		{
    186 			Action action = actionList.getActionAt( c );
    187 			if( action == SEPARATOR_ACTION )
    188 			{
    189 				toolbar.addUnrelatedGap();
    190 			}
    191 			else
    192 			{
    193 				if( c > 0 )
    194 					toolbar.addRelatedGap();
    195
    196 				toolbar.addFixed( new JButton( action ));
    197 			}
    198 		}
    199 	}
    200 }