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.client.browser;
30  
31  import org.apache.log4j.Logger;
32  
33  import javax.swing.JPopupMenu;
34  import javax.swing.JMenuItem;
35  import java.awt.event.ActionListener;
36  import java.awt.event.ActionEvent;
37  import java.awt.Component;
38  
39  import java.io.IOException;
40  
41  import uk.ac.rdg.resc.jstyx.client.CStyxFile;
42  import uk.ac.rdg.resc.jstyx.client.CStyxFileInputStream;
43  import uk.ac.rdg.resc.jstyx.StyxException;
44  import uk.ac.rdg.resc.jstyx.StyxUtils;
45  
46  /***
47   * Pop-up menu that is displayed when a user requests a pop-up on one of the 
48   * files in the hierarchy. Doesn't work very well yet!
49   *
50   * @author Jon Blower
51   * $Revision: 377 $
52   * $Date: 2005-08-31 18:03:19 +0100 (Wed, 31 Aug 2005) $
53   * $Log$
54   * Revision 1.6  2005/08/31 17:03:18  jonblower
55   * Renamed "StyxFile*putStream*" to "CStyxFile*putStream*" for consistency with CStyxFile class
56   *
57   * Revision 1.5  2005/03/11 13:58:54  jonblower
58   * Merged MINA-Test_20059309 into main line of development
59   *
60   * Revision 1.4.2.1  2005/03/11 08:29:52  jonblower
61   * Moved to log4j logging system (from apache commons logging)
62   *
63   * Revision 1.4  2005/02/28 11:43:38  jonblower
64   * Tidied up logging code
65   *
66   * Revision 1.3  2005/02/18 17:52:40  jonblower
67   * Added client.browser package
68   *
69   */
70  public class StyxBrowserPopupMenu extends JPopupMenu
71  {
72      
73      private static final Logger log = Logger.getLogger(StyxBrowserPopupMenu.class);
74      
75      private JMenuItem openItem;
76      private JMenuItem writeItem;
77      private JMenuItem refreshItem;
78      private JMenuItem propertiesItem;
79      
80      private FileNode node; // The file on which this menu will operate
81      
82      /*** Creates a new instance of StyxBrowserPopUpMenu */
83      public StyxBrowserPopupMenu()
84      {
85          openItem = new JMenuItem("Open");
86          writeItem = new JMenuItem("Write");
87          refreshItem = new JMenuItem("Refresh");
88          propertiesItem = new JMenuItem("Properties");
89          this.add(openItem);
90          this.add(writeItem);
91          this.add(refreshItem);
92          this.add(propertiesItem);
93          refreshItem.addActionListener(new ActionListener()
94          {
95              public void actionPerformed(ActionEvent e)
96              {
97                  if (node != null)
98                  {
99                      node.refresh();
100                 }
101             }
102         });
103         openItem.addActionListener(new OpenFileListener());
104         this.node = null;
105     }
106     
107     /***
108      * Shows the context menu for the particular CStyxFile that is passed in
109      */
110     public void showContext(FileNode node, Component invoker, int x, int y)
111     {
112         this.node = node;
113         CStyxFile file = node.getFile();
114         try
115         {
116             if (file.isDirectory())
117             {
118                 openItem.setVisible(false);
119                 writeItem.setVisible(false);
120             }
121             else
122             {
123                 openItem.setVisible(true);
124                 writeItem.setVisible(true);
125             }
126         }
127         catch (StyxException se)
128         {
129             // Log the error but don't do anything
130             se.printStackTrace();
131         }
132         super.show(invoker, x, y);
133     }
134     
135     class OpenFileListener implements ActionListener
136     {
137         public void actionPerformed(ActionEvent e)
138         {
139             // "file" should never be null but we check just in case
140             if (node != null)
141             {
142                 CStyxFile file = node.getFile();
143                 CStyxFileInputStream is = new CStyxFileInputStream(file);
144                 TextDisplayer display = new TextDisplayer(file.getName());
145                 display.setVisible(true);
146                 try
147                 {
148                     display.displayData(is);
149                 }
150                 catch(IOException ioe)
151                 {
152                     ioe.printStackTrace();
153                 }
154             }
155             else
156             {
157                 log.error("node is null");
158             }
159         }
160     }
161     
162 }