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.gridservice.client;
30  
31  import javax.swing.JPopupMenu;
32  import javax.swing.JMenuItem;
33  import javax.swing.JOptionPane;
34  
35  import java.awt.event.ActionListener;
36  import java.awt.event.ActionEvent;
37  
38  import java.awt.Component;
39  
40  import uk.ac.rdg.resc.jstyx.StyxException;
41  
42  /***
43   * A popup menu which appears when the user right-clicks on a node in the tree
44   * in the SGSExplorer window.  Only one instance of this class is ever created.
45   * Access this instance through the static <code>showContext()</code> method.
46   *
47   * @author Jon Blower
48   * $Revision: 530 $
49   * $Date: 2005-12-07 17:49:05 +0000 (Wed, 07 Dec 2005) $
50   * $Log$
51   * Revision 1.8  2005/12/07 17:49:05  jonblower
52   * Added getInstanceClient() method to CStyxFileNode
53   *
54   * Revision 1.7  2005/12/07 08:54:06  jonblower
55   * Changes due to SGSInstanceGUI.getGUI() throwing StyxException
56   *
57   * Revision 1.6  2005/07/08 12:19:58  jonblower
58   * Continuing to implement automatic updates of service instances
59   *
60   * Revision 1.5  2005/07/06 17:53:44  jonblower
61   * Implementing automatic update of SGS instances in SGS Explorer
62   *
63   * Revision 1.4  2005/05/27 17:05:07  jonblower
64   * Changes to incorporate GeneralCachingStreamReader
65   *
66   * Revision 1.3  2005/05/18 17:13:51  jonblower
67   * Created SGSInstanceGUI
68   *
69   * Revision 1.2  2005/05/18 08:03:24  jonblower
70   * Implemented creation of new service instances
71   *
72   * Revision 1.1  2005/05/17 18:21:36  jonblower
73   * Added initial pop-up menu support
74   *
75   */
76  class StyxExplorerPopupMenu extends JPopupMenu implements ActionListener
77  {
78      
79      private static final StyxExplorerPopupMenu popupMenu = new StyxExplorerPopupMenu();
80      
81      private JMenuItem newInstance; // Click this to create a new instance
82      private JMenuItem refresh; // Click this to refresh the given node
83      private JMenuItem showGUI; // Click this to show the GUI for an instance
84      private JMenuItem destroyInstance; // Click this to destroy the selected instance
85      private CStyxFileNode activeNode; // The node to which the menu refers
86      
87      /*** Creates a new instance of StyxExplorerPopupMenu */
88      private StyxExplorerPopupMenu()
89      {
90          this.newInstance = new JMenuItem("New instance");
91          this.refresh = new JMenuItem("Refresh");
92          this.showGUI = new JMenuItem("Show GUI");
93          this.destroyInstance = new JMenuItem("Destroy");
94          this.add(this.newInstance);
95          //this.add(this.refresh);
96          this.add(this.showGUI);
97          //this.add(this.destroyInstance);
98          
99          // Add this ActionListener to each component so that actionPerfomed will
100         // be called when any JMenuItem is clicked
101         for (int i = 0; i < this.getComponentCount(); i++)
102         {
103             Component comp = this.getComponent(i);
104             if (comp instanceof JMenuItem)
105             {
106                 ((JMenuItem)comp).addActionListener(this);
107             }
108         }
109     }
110     
111     /***
112      * Sets up the menu (i.e. shows/hides the appropriate menu items) for the
113      * given node
114      */
115     private void setupMenu(CStyxFileNode activeNode)
116     {
117         this.activeNode = activeNode;
118         int nodeType = this.activeNode.getType();
119         // The "New instance" menu item is only visible when clicking on a 
120         // service node
121         this.newInstance.setVisible(nodeType == CStyxFileNode.SERVICE);
122         // The "Show GUI" and "destroy" menu items are only visible when clicking
123         // on an instancenode
124         this.showGUI.setVisible(nodeType == CStyxFileNode.INSTANCE);
125         this.destroyInstance.setVisible(nodeType == CStyxFileNode.INSTANCE);
126         // Don't use Refresh at the moment - UI should keep itself updated
127         //this.refresh.setVisible(nodeType != CStyxFileNode.INSTANCE &&
128         //                        nodeType != CStyxFileNode.SERVICE);
129     }
130     
131     /***
132      * Called when a button on the popup menu is clicked
133      */
134     public void actionPerformed(ActionEvent e)
135     {
136         Object source = e.getSource();
137         if (source == this.newInstance)
138         {
139             this.activeNode.createNewInstance();
140         }
141         else if (source == this.showGUI)
142         {
143             try
144             {
145                 SGSInstanceClient client = this.activeNode.getInstanceClient();
146                 SGSInstanceGUI gui = SGSInstanceGUI.getGUI(client);
147                 gui.setVisible(true);
148             }
149             catch(StyxException se)
150             {
151                 // Called if there is an error creating the SGS instance client
152                 // object (unlikely)
153                 JOptionPane.showMessageDialog(null, "Error creating SGS instance" +
154                     " client: " + se.getMessage());
155             }
156         }
157         else if (source == this.refresh)
158         {
159             // Get the children of this node - the "true" parameter means that we are 
160             // forcing a refresh, even if we already have children for this node
161             this.activeNode.findChildren();
162         }
163         else if (source == this.destroyInstance)
164         {
165             
166         }
167     }
168     
169     /***
170      * Show the popup menu for the given CStyxFileNode
171      */
172     public static void showContext(CStyxFileNode node, Component invoker, int x, int y)
173     {
174         popupMenu.setupMenu(node);
175         popupMenu.show(invoker, x, y);
176     }
177     
178 }