1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
47
48
49
50 public class DebugDialog extends JDialog implements ActionListener {
51
52
53 private static final long serialVersionUID = 7802205907550854333L;
54
55
56
57
58 JButton buttonClose;
59
60
61
62
63 File lastReponseBodyFile = null;
64
65
66
67
68 UploadPolicy uploadPolicy = null;
69
70
71
72
73
74
75
76
77
78 public DebugDialog(Frame owner, String text, UploadPolicy uploadPolicy)
79 throws JUploadIOException {
80 this.uploadPolicy = uploadPolicy;
81
82
83 this.buttonClose = new JButton(uploadPolicy
84 .getLocalizedString("buttonClose"));
85 this.buttonClose.setMaximumSize(new Dimension(100, 100));
86 this.buttonClose.addActionListener(this);
87
88
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
105 uploadPolicy
106 .displayWarn("IllegalArgumentException while packing the DebugWindow (bad HTML ?)");
107 uploadPolicy.displayErr(e);
108 }
109
110
111
112 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
113 setBounds(0, 0, screenSize.width, screenSize.height);
114
115
116
117 setTitle("JUpload DebugDialog: last response body");
118 setVisible(true);
119 }
120
121
122
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
134
135
136
137
138
139
140 public void setText(JEditorPane editorPane, String text)
141 throws JUploadIOException {
142 this.uploadPolicy.getContext().registerUnload(this, "deleteLog");
143 try {
144
145
146
147 this.lastReponseBodyFile = File.createTempFile("jupload_",
148 "_LRB.html");
149
150 FileOutputStream fos = new FileOutputStream(
151 this.lastReponseBodyFile);
152 fos.write(text.getBytes());
153 fos.close();
154
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
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
182 }
183 }
184
185
186
187
188 @Override
189 public void dispose() {
190 super.dispose();
191 deleteLog();
192 }
193 }