View Javadoc
1   //
2   // $Id: JUploadPanelImpl.java 303 2007-07-21 07:42:51 +0000 (sam., 21 juil. 2007)
3   // etienne_sf $
4   //
5   // jupload - A file upload applet.
6   // Copyright 2007 The JUpload Team
7   //
8   // Created: ?
9   // Creator: etienne_sf
10  // Last modified: $Date: 2007-10-08 10:02:41 +0200 (lun., 08 oct. 2007) $
11  //
12  // This program is free software; you can redistribute it and/or modify it under
13  // the terms of the GNU General Public License as published by the Free Software
14  // Foundation; either version 2 of the License, or (at your option) any later
15  // version. This program is distributed in the hope that it will be useful, but
16  // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18  // details. You should have received a copy of the GNU General Public License
19  // along with this program; if not, write to the Free Software Foundation, Inc.,
20  // 675 Mass Ave, Cambridge, MA 02139, USA.
21  
22  package wjhk.jupload2.gui;
23  
24  import java.awt.event.ActionEvent;
25  import java.awt.event.ActionListener;
26  import java.awt.event.ItemEvent;
27  import java.awt.event.ItemListener;
28  
29  import javax.swing.JCheckBoxMenuItem;
30  import javax.swing.JComponent;
31  import javax.swing.JMenuItem;
32  import javax.swing.JPopupMenu;
33  import javax.swing.JSeparator;
34  import javax.swing.event.PopupMenuEvent;
35  import javax.swing.event.PopupMenuListener;
36  
37  import wjhk.jupload2.exception.JUploadIOException;
38  import wjhk.jupload2.gui.filepanel.FilePanel;
39  import wjhk.jupload2.gui.filepanel.FilePanel.FileListViewMode;
40  import wjhk.jupload2.policies.UploadPolicy;
41  
42  /**
43   * Global applet popup menu. It currently contains only the debug on/off menu entry.
44   */
45  
46  final class JUploadDebugPopupMenu extends JPopupMenu implements ActionListener, ItemListener, PopupMenuListener {
47  
48      /** A generated serialVersionUID, to avoid warning during compilation */
49      private static final long serialVersionUID = -5473337111643079720L;
50  
51      /**
52       * Identifies the menu item that will set debug mode on or off (on means: debugLevel=100)
53       */
54      JCheckBoxMenuItem cbmiDebugOnOff = null;
55  
56      JCheckBoxMenuItem cbmiLogWindowOnOff = null;
57  
58      JCheckBoxMenuItem cbmiTreeViewOnOff = null;
59  
60      JMenuItem jMenuItemViewLastResponseBody = null;
61  
62      JMenuItem jMenuItemClearLogWindowContent = null;
63  
64      JMenuItem jMenuItemCopyLogWindowContent = null;
65  
66      /**
67       * The current upload policy.
68       */
69      private UploadPolicy uploadPolicy;
70  
71      JUploadDebugPopupMenu(UploadPolicy uploadPolicy) {
72          this.uploadPolicy = uploadPolicy;
73  
74          this.addPopupMenuListener(this);
75  
76          // ////////////////////////////////////////////////////////////////////////
77          // Creation of the menu items
78          // ////////////////////////////////////////////////////////////////////////
79          // First: debug on or off
80          this.cbmiDebugOnOff = new JCheckBoxMenuItem("Debug enabled");
81          this.cbmiDebugOnOff.setState(this.uploadPolicy.getDebugLevel() == 100);
82          add(this.cbmiDebugOnOff);
83          this.cbmiDebugOnOff.addItemListener(this);
84          // Show or hide the log window
85          this.cbmiLogWindowOnOff = new JCheckBoxMenuItem("Show log window");
86          this.cbmiLogWindowOnOff.setState(this.uploadPolicy.getShowLogWindow().equals("true"));
87          add(this.cbmiLogWindowOnOff);
88          this.cbmiLogWindowOnOff.addItemListener(this);
89          // Show the flat or the tree view
90          this.cbmiTreeViewOnOff = new JCheckBoxMenuItem("Show the file list in Tree View mode");
91          this.cbmiTreeViewOnOff.setState(this.uploadPolicy.getFileListViewMode().equals(FileListViewMode.TREE_VIEW));
92          add(this.cbmiTreeViewOnOff);
93          this.cbmiTreeViewOnOff.addItemListener(this);
94  
95          add(new JSeparator());
96  
97          // Clear the last stringResponseBody
98          this.jMenuItemClearLogWindowContent = new JMenuItem("Clear the log window content");
99          add(this.jMenuItemClearLogWindowContent);
100         this.jMenuItemClearLogWindowContent.addActionListener(this);
101         // Copy the last stringResponseBody
102         this.jMenuItemCopyLogWindowContent = new JMenuItem("Copy the log window content");
103         add(this.jMenuItemCopyLogWindowContent);
104         this.jMenuItemCopyLogWindowContent.addActionListener(this);
105 
106         add(new JSeparator());
107 
108         // View the last stringResponseBody
109         this.jMenuItemViewLastResponseBody = new JMenuItem("View last response body");
110         add(this.jMenuItemViewLastResponseBody);
111         this.jMenuItemViewLastResponseBody.addActionListener(this);
112         // ////////////////////////////////////////////////////////////////////////
113     }
114 
115     /**
116      * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
117      */
118     public void itemStateChanged(ItemEvent e) {
119         if (this.cbmiDebugOnOff == e.getItem()) {
120             this.uploadPolicy.setDebugLevel((this.cbmiDebugOnOff.isSelected() ? 100 : 0));
121         } else if (this.cbmiLogWindowOnOff == e.getItem()) {
122             if (this.cbmiLogWindowOnOff.isSelected()) {
123                 this.uploadPolicy.setShowLogWindow(UploadPolicy.SHOWLOGWINDOW_TRUE);
124             } else {
125                 this.uploadPolicy.setShowLogWindow(UploadPolicy.SHOWLOGWINDOW_FALSE);
126             }
127         } else if (this.cbmiTreeViewOnOff == e.getItem()) {
128             if (this.cbmiTreeViewOnOff.isSelected()) {
129                 this.uploadPolicy.getContext().getUploadPanel().getFilePanel()
130                         .setFileListViewMode(FilePanel.FileListViewMode.TREE_VIEW);
131             } else {
132                 this.uploadPolicy.getContext().getUploadPanel().getFilePanel()
133                         .setFileListViewMode(FilePanel.FileListViewMode.FLAT);
134             }
135             ((JComponent) (this.uploadPolicy.getContext().getUploadPanel().getFilePanel())).validate();
136         }
137     }
138 
139     /**
140      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
141      */
142     public void actionPerformed(ActionEvent e) {
143         if (this.jMenuItemViewLastResponseBody == e.getSource()) {
144             try {
145                 new DebugDialog(null, this.uploadPolicy.getLastResponseBody(), this.uploadPolicy);
146             } catch (JUploadIOException e1) {
147                 this.uploadPolicy.displayErr(e1);
148             }
149         } else if (this.jMenuItemClearLogWindowContent == e.getSource()) {
150             this.uploadPolicy.getContext().getUploadPanel().clearLogWindow();
151         } else if (this.jMenuItemCopyLogWindowContent == e.getSource()) {
152             this.uploadPolicy.getContext().getUploadPanel().copyLogWindow();
153         }
154     }
155 
156     /** @see javax.swing.event.PopupMenuListener#popupMenuCanceled(javax.swing.event.PopupMenuEvent) */
157     public void popupMenuCanceled(PopupMenuEvent arg0) {
158         // Nothing to do.
159     }
160 
161     /** @see javax.swing.event.PopupMenuListener#popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent) */
162     public void popupMenuWillBecomeInvisible(PopupMenuEvent arg0) {
163         // Nothing to do.
164     }
165 
166     /**
167      * Set the "View last response body" menu enabled or disabled.
168      * 
169      * @see javax.swing.event.PopupMenuListener#popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent)
170      */
171     public void popupMenuWillBecomeVisible(PopupMenuEvent arg0) {
172         String s = this.uploadPolicy.getLastResponseBody();
173         this.jMenuItemViewLastResponseBody.setEnabled(s != null && !s.equals(""));
174     }
175 }