Coverage Report - wjhk.jupload2.gui.DebugDialog
 
Classes in this File Line Coverage Branch Coverage Complexity
DebugDialog
0 %
0/60
0 %
0/6
2,4
 
 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  0
     File lastReponseBodyFile = null;
 64  
 
 65  
     /**
 66  
      * The current upload policy.
 67  
      */
 68  0
     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  0
             throws JUploadIOException {
 80  0
         this.uploadPolicy = uploadPolicy;
 81  
 
 82  
         // Creation of the buttonClose button.
 83  0
         this.buttonClose = new JButton(uploadPolicy
 84  0
                 .getLocalizedString("buttonClose"));
 85  0
         this.buttonClose.setMaximumSize(new Dimension(100, 100));
 86  0
         this.buttonClose.addActionListener(this);
 87  
 
 88  
         // Creation of the text (HTML) area
 89  0
         JEditorPane editorPane = new JEditorPane();
 90  0
         JScrollPane editorScrollPane = new JScrollPane(editorPane);
 91  0
         editorScrollPane
 92  0
                 .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
 93  0
         editorScrollPane.setPreferredSize(new Dimension(250, 145));
 94  0
         editorScrollPane.setMinimumSize(new Dimension(10, 10));
 95  
 
 96  0
         setText(editorPane, text);
 97  
 
 98  0
         getContentPane().add(this.buttonClose, BorderLayout.SOUTH);
 99  0
         getContentPane().add(editorScrollPane);
 100  
 
 101  
         try {
 102  0
             pack();
 103  0
         } catch (IllegalArgumentException e) {
 104  
             // This can happen, while parsing HTML.
 105  0
             uploadPolicy
 106  0
                     .displayWarn("IllegalArgumentException while packing the DebugWindow (bad HTML ?)");
 107  0
             uploadPolicy.displayErr(e);
 108  0
         }
 109  
         // Correction given by
 110  
         // setSize(getMaximumSize()); generate very high number under MAC OSX ->
 111  
         // Applet Crash
 112  0
         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
 113  0
         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  0
         setTitle("JUpload DebugDialog: last response body");
 118  0
         setVisible(true);
 119  0
     }
 120  
 
 121  
     /**
 122  
      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
 123  
      */
 124  
     public void actionPerformed(ActionEvent event) {
 125  0
         if (event.getActionCommand() == this.buttonClose.getActionCommand()) {
 126  0
             this.uploadPolicy.displayDebug(
 127  
                     "[DebugDialog] Before this.dispose()", 50);
 128  0
             this.dispose();
 129  
         }
 130  0
     }
 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  0
         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  0
             this.lastReponseBodyFile = File.createTempFile("jupload_",
 148  
                     "_LRB.html");
 149  
             // Let's put our output within this temp file.
 150  0
             FileOutputStream fos = new FileOutputStream(
 151  
                     this.lastReponseBodyFile);
 152  0
             fos.write(text.getBytes());
 153  0
             fos.close();
 154  
             // We can now call setPage(URL).
 155  0
             java.net.URL lastResponseBodyLocalPage = this.lastReponseBodyFile
 156  0
                     .toURI().toURL();
 157  0
             editorPane.setEditable(false);
 158  0
             editorPane.setPage(lastResponseBodyLocalPage);
 159  0
             HTMLEditorKit ek = (HTMLEditorKit) editorPane.getEditorKit();
 160  0
             Document doc = ek.createDefaultDocument();
 161  0
             doc.putProperty("Base", "http://localhost/coppermine/");
 162  0
         } catch (IOException e) {
 163  0
             throw new JUploadIOException(e);
 164  0
         }
 165  0
     }
 166  
 
 167  
     /**
 168  
      * Delete the current log. (called upon applet termination)
 169  
      */
 170  
     public void deleteLog() {
 171  
         try {
 172  0
             if (null != this.lastReponseBodyFile) {
 173  0
                 if (!this.lastReponseBodyFile.delete()) {
 174  0
                     this.uploadPolicy
 175  0
                             .displayWarn("Unable to delete this.lastReponseBodyFile ("
 176  0
                                     + this.lastReponseBodyFile.getName() + ")");
 177  
                 }
 178  0
                 this.lastReponseBodyFile = null;
 179  
             }
 180  0
         } catch (Exception e) {
 181  
             // nothing to do
 182  0
         }
 183  0
     }
 184  
 
 185  
     /**
 186  
      * dispose all internal resources. Mainly: the temporary file.
 187  
      */
 188  
     @Override
 189  
     public void dispose() {
 190  0
         super.dispose();
 191  0
         deleteLog();
 192  0
     }
 193  
 }