View Javadoc
1   //
2   // $Id: DebugDialog.java 298 2007-07-12 10:17:32 +0000 (jeu., 12 juil. 2007)
3   // etienne_sf $
4   //
5   // jupload - A file upload applet.
6   // Copyright 2007 The JUpload Team
7   //
8   // Created: 2006-07-10
9   // Creator: etienne_sf
10  // Last modified: $Date: 2008-04-16 09:58:02 +0200 (mer., 16 avr. 2008) $
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.BorderLayout;
25  import java.awt.Dimension;
26  import java.awt.Frame;
27  import java.awt.Toolkit;
28  import java.awt.event.ActionEvent;
29  import java.awt.event.ActionListener;
30  import java.io.File;
31  import java.io.FileOutputStream;
32  import java.io.IOException;
33  
34  import javax.swing.JButton;
35  import javax.swing.JDialog;
36  import javax.swing.JEditorPane;
37  import javax.swing.JScrollPane;
38  import javax.swing.ScrollPaneConstants;
39  import javax.swing.text.Document;
40  import javax.swing.text.html.HTMLEditorKit;
41  
42  import wjhk.jupload2.exception.JUploadIOException;
43  import wjhk.jupload2.policies.UploadPolicy;
44  
45  /**
46   * A maximized modal dialog box, that display the selected picture.
47   * 
48   * @author etienne_sf
49   */
50  public class DebugDialog extends JDialog implements ActionListener {
51  
52      /** A generated serialVersionUID, to avoid warning during compilation */
53      private static final long serialVersionUID = 7802205907550854333L;
54  
55      /**
56       * The close button, which closes this dialog window.
57       */
58      JButton buttonClose;
59  
60      /**
61       * The temporary file, that will contain the HTML response body.
62       */
63      File lastReponseBodyFile = null;
64  
65      /**
66       * The current upload policy.
67       */
68      UploadPolicy uploadPolicy = null;
69  
70      /**
71       * Creates a new instance.
72       * 
73       * @param owner The parent frame.
74       * @param text The text to display. It can be HTML.
75       * @param uploadPolicy The upload policy which applies.
76       * @throws JUploadIOException
77       */
78      public DebugDialog(Frame owner, String text, UploadPolicy uploadPolicy)
79              throws JUploadIOException {
80          this.uploadPolicy = uploadPolicy;
81  
82          // Creation of the buttonClose button.
83          this.buttonClose = new JButton(uploadPolicy
84                  .getLocalizedString("buttonClose"));
85          this.buttonClose.setMaximumSize(new Dimension(100, 100));
86          this.buttonClose.addActionListener(this);
87  
88          // Creation of the text (HTML) area
89          JEditorPane editorPane = new JEditorPane();
90          JScrollPane editorScrollPane = new JScrollPane(editorPane);
91          editorScrollPane
92                  .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
93          editorScrollPane.setPreferredSize(new Dimension(250, 145));
94          editorScrollPane.setMinimumSize(new Dimension(10, 10));
95  
96          setText(editorPane, text);
97  
98          getContentPane().add(this.buttonClose, BorderLayout.SOUTH);
99          getContentPane().add(editorScrollPane);
100 
101         try {
102             pack();
103         } catch (IllegalArgumentException e) {
104             // This can happen, while parsing HTML.
105             uploadPolicy
106                     .displayWarn("IllegalArgumentException while packing the DebugWindow (bad HTML ?)");
107             uploadPolicy.displayErr(e);
108         }
109         // Correction given by
110         // setSize(getMaximumSize()); generate very high number under MAC OSX ->
111         // Applet Crash
112         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
113         setBounds(0, 0, screenSize.width, screenSize.height);
114 
115         // The dialog is modal: the next line will return when the DialogPicture
116         // is hidden (to be closed, in our case)
117         setTitle("JUpload DebugDialog: last response body");
118         setVisible(true);
119     }
120 
121     /**
122      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
123      */
124     public void actionPerformed(ActionEvent event) {
125         if (event.getActionCommand() == this.buttonClose.getActionCommand()) {
126             this.uploadPolicy.displayDebug(
127                     "[DebugDialog] Before this.dispose()", 50);
128             this.dispose();
129         }
130     }
131 
132     /**
133      * Set the text to display. If it's full HTML, beginning with a <!DOC tag,
134      * this first tag and empty lines at the beginning are removed.
135      * 
136      * @param editorPane The target JEditorPane
137      * @param text The text to save.
138      * @throws JUploadIOException
139      */
140     public void setText(JEditorPane editorPane, String text)
141             throws JUploadIOException {
142         this.uploadPolicy.getContext().registerUnload(this, "deleteLog");
143         try {
144             // First: creation of a temporary file. This is necessary, as html
145             // output is not correctly displayed in the JEditorPane, when using
146             // the setText method. We need an URL to call the setPage one.
147             this.lastReponseBodyFile = File.createTempFile("jupload_",
148                     "_LRB.html");
149             // Let's put our output within this temp file.
150             FileOutputStream fos = new FileOutputStream(
151                     this.lastReponseBodyFile);
152             fos.write(text.getBytes());
153             fos.close();
154             // We can now call setPage(URL).
155             java.net.URL lastResponseBodyLocalPage = this.lastReponseBodyFile
156                     .toURI().toURL();
157             editorPane.setEditable(false);
158             editorPane.setPage(lastResponseBodyLocalPage);
159             HTMLEditorKit ek = (HTMLEditorKit) editorPane.getEditorKit();
160             Document doc = ek.createDefaultDocument();
161             doc.putProperty("Base", "http://localhost/coppermine/");
162         } catch (IOException e) {
163             throw new JUploadIOException(e);
164         }
165     }
166 
167     /**
168      * Delete the current log. (called upon applet termination)
169      */
170     public void deleteLog() {
171         try {
172             if (null != this.lastReponseBodyFile) {
173                 if (!this.lastReponseBodyFile.delete()) {
174                     this.uploadPolicy
175                             .displayWarn("Unable to delete this.lastReponseBodyFile ("
176                                     + this.lastReponseBodyFile.getName() + ")");
177                 }
178                 this.lastReponseBodyFile = null;
179             }
180         } catch (Exception e) {
181             // nothing to do
182         }
183     }
184 
185     /**
186      * dispose all internal resources. Mainly: the temporary file.
187      */
188     @Override
189     public void dispose() {
190         super.dispose();
191         deleteLog();
192     }
193 }