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.swing;
    14
    15  import java.awt.event.ActionEvent;
    16  import java.awt.event.MouseAdapter;
    17  import java.awt.event.MouseEvent;
    18
    19  import javax.swing.JList;
    20  import javax.swing.JPopupMenu;
    21
    22  import com.eviware.soapui.support.UISupport;
    23  import com.eviware.soapui.support.action.swing.ActionList;
    24  import com.eviware.soapui.support.action.swing.ActionSupport;
    25
    26  /***
    27   * Abstract MouseListener for JLists that displays a row-sensitive popup-menu
    28   *
    29   * @author ole.matzura
    30   */
    31
    32  public abstract class ListMouseListener extends MouseAdapter
    33  {
    34  	protected abstract ActionList getActionsForRow( JList list, int row );
    35
    36  	public void mouseClicked( MouseEvent e )
    37  	{
    38  		if (e.getClickCount() < 2)
    39  			return;
    40
    41  		JList list = ( JList ) e.getSource();
    42
    43  		int selectedIndex = list.getSelectedIndex();
    44  		if (selectedIndex == -1)
    45  			return;
    46
    47  		ActionList actions = getActionsForRow( list, selectedIndex );
    48
    49  		if(actions != null)
    50  			actions.performDefaultAction(new ActionEvent(this, 0, null));
    51  	}
    52
    53  	public void mousePressed( MouseEvent e )
    54  	{
    55  		if (e.isPopupTrigger())
    56  			showPopup(e);
    57  	}
    58
    59  	public void mouseReleased( MouseEvent e )
    60  	{
    61  		if (e.isPopupTrigger())
    62  			showPopup(e);
    63  	}
    64
    65  	public void showPopup( MouseEvent e )
    66  	{
    67  		JList list = ( JList ) e.getSource();
    68  		int row = list.locationToIndex(e.getPoint());
    69  		if (row == -1)
    70  			return;
    71
    72  		if (list.getSelectedIndex() != row)
    73  		{
    74  			list.setSelectedIndex(row);
    75  		}
    76
    77  		ActionList actions = getActionsForRow( list, row );
    78
    79  		if (actions == null || actions.getActionCount() == 0)
    80  			return;
    81
    82  		JPopupMenu popup = ActionSupport.buildPopup(actions);
    83  		UISupport.showPopup(popup, list, e.getPoint());
    84  	}
    85
    86  }