View Javadoc

1   /*
2    * Copyright (c) 2005 The University of Reading
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    * 1. Redistributions of source code must retain the above copyright
9    *    notice, this list of conditions and the following disclaimer.
10   * 2. Redistributions in binary form must reproduce the above copyright
11   *    notice, this list of conditions and the following disclaimer in the
12   *    documentation and/or other materials provided with the distribution.
13   * 3. Neither the name of the University of Reading, nor the names of the
14   *    authors or contributors may be used to endorse or promote products
15   *    derived from this software without specific prior written permission.
16   * 
17   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20   * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27   */
28  
29  package uk.ac.rdg.resc.jstyx.interloper;
30  
31  import javax.swing.JPopupMenu;
32  import javax.swing.JMenuItem;
33  import java.awt.event.ActionListener;
34  import java.awt.event.ActionEvent;
35  import java.awt.Component;
36  
37  /***
38   * Pop-up menu that is shown when a user right-clicks on the table in 
39   * the StyxMon.
40   *
41   * @author Jon Blower
42   * $Revision: 50 $
43   * $Date: 2005-02-28 13:46:46 +0000 (Mon, 28 Feb 2005) $
44   * $Log$
45   * Revision 1.4  2005/02/28 13:46:46  jonblower
46   * Combined the two ActionListeners into a single one
47   *
48   * Revision 1.3  2005/02/28 12:53:47  jonblower
49   * Improved message filtering
50   *
51   * Revision 1.2  2005/02/24 11:23:40  jonblower
52   * Handles filtering by filename correctly
53   *
54   * Revision 1.1  2005/02/24 09:06:26  jonblower
55   * Initial import
56   *
57   */
58  public class StyxMonPopupMenu extends JPopupMenu
59  {
60      
61      private StyxMonTableModel theModel;
62      private JMenuItem menuShowAll;
63      private static final String prefix = "Filter by filename ";
64      
65      /*** Creates a new instance of StyxMonPopUpMenu */
66      public StyxMonPopupMenu(StyxMonTableModel model)
67      {
68          this.theModel = model;
69          menuShowAll = new JMenuItem("Show all messages");
70          menuShowAll.addActionListener(al);
71      }
72      
73      /***
74       * Shows a popup menu that gives the user the option to filter by a
75       * given filename
76       */
77      public void showContext(String filename, Component invoker, int x, int y)
78      {
79          this.removeAll();
80          if (this.theModel.isFiltered())
81          {
82              this.add(menuShowAll);
83          }
84          else
85          {
86              // Create a menu item for each level in the file hierarchy
87              JMenuItem root = new JMenuItem(prefix + filename);
88              root.addActionListener(al);
89              this.add(root);
90          }
91          super.show(invoker, x, y);
92      }
93      
94      /***
95       * Action listener for JMenuItems
96       */
97      ActionListener al = new ActionListener()
98      {
99          public void actionPerformed(ActionEvent e)
100         {
101             JMenuItem menuItem = (JMenuItem)e.getSource();
102             if (menuItem == menuShowAll)
103             {
104                 theModel.showAllData();
105             }
106             else
107             {
108                 String filename = menuItem.getText().substring(prefix.length());
109                 theModel.filterByFilename(filename);
110             }
111         }
112     };
113     
114 }