63eaf492d754b788e6f6c043698fda034133b80f
[jSite.git] / src / de / todesbaum / jsite / gui / ProjectInsertPage.java
1 /*
2  * jSite - a tool for uploading websites into Freenet
3  * Copyright (C) 2006 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package de.todesbaum.jsite.gui;
21
22 import java.awt.BorderLayout;
23 import java.awt.Font;
24 import java.awt.GridBagConstraints;
25 import java.awt.GridBagLayout;
26 import java.awt.Insets;
27 import java.awt.Toolkit;
28 import java.awt.datatransfer.Clipboard;
29 import java.awt.datatransfer.ClipboardOwner;
30 import java.awt.datatransfer.StringSelection;
31 import java.awt.datatransfer.Transferable;
32 import java.awt.event.ActionEvent;
33 import java.awt.event.KeyEvent;
34 import java.text.DateFormat;
35 import java.text.MessageFormat;
36 import java.util.Date;
37
38 import javax.swing.AbstractAction;
39 import javax.swing.Action;
40 import javax.swing.JButton;
41 import javax.swing.JComponent;
42 import javax.swing.JLabel;
43 import javax.swing.JOptionPane;
44 import javax.swing.JPanel;
45 import javax.swing.JProgressBar;
46 import javax.swing.JTextField;
47 import javax.swing.SwingUtilities;
48
49 import de.todesbaum.jsite.application.Freenet7Interface;
50 import de.todesbaum.jsite.application.InsertListener;
51 import de.todesbaum.jsite.application.Project;
52 import de.todesbaum.jsite.application.ProjectInserter;
53 import de.todesbaum.jsite.i18n.I18n;
54 import de.todesbaum.jsite.i18n.I18nContainer;
55 import de.todesbaum.util.swing.TWizard;
56 import de.todesbaum.util.swing.TWizardPage;
57
58 /**
59  * Wizard page that shows the progress of an insert.
60  *
61  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
62  */
63 public class ProjectInsertPage extends TWizardPage implements InsertListener, ClipboardOwner {
64
65         /** The project inserter. */
66         private ProjectInserter projectInserter;
67
68         /** The “copy URI” action. */
69         private Action copyURIAction;
70
71         /** The “request URI” textfield. */
72         private JTextField requestURITextField;
73
74         /** The “start time” label. */
75         private JLabel startTimeLabel;
76
77         /** The progress bar. */
78         private JProgressBar progressBar;
79
80         /** The start time of the insert. */
81         private long startTime = 0;
82
83         /**
84          * Creates a new progress insert wizard page.
85          *
86          * @param wizard
87          *            The wizard this page belongs to
88          */
89         public ProjectInsertPage(final TWizard wizard) {
90                 super(wizard);
91                 createActions();
92                 pageInit();
93                 setHeading(I18n.getMessage("jsite.insert.heading"));
94                 setDescription(I18n.getMessage("jsite.insert.description"));
95                 I18nContainer.getInstance().registerRunnable(new Runnable() {
96
97                         public void run() {
98                                 setHeading(I18n.getMessage("jsite.insert.heading"));
99                                 setDescription(I18n.getMessage("jsite.insert.description"));
100                         }
101                 });
102                 projectInserter = new ProjectInserter();
103                 projectInserter.addInsertListener(this);
104         }
105
106         /**
107          * Creates all used actions.
108          */
109         private void createActions() {
110                 copyURIAction = new AbstractAction(I18n.getMessage("jsite.project.action.copy-uri")) {
111
112                         @SuppressWarnings("synthetic-access")
113                         public void actionPerformed(ActionEvent actionEvent) {
114                                 actionCopyURI();
115                         }
116                 };
117                 copyURIAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.copy-uri.tooltip"));
118                 copyURIAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_U);
119                 copyURIAction.setEnabled(false);
120
121                 I18nContainer.getInstance().registerRunnable(new Runnable() {
122
123                         @SuppressWarnings("synthetic-access")
124                         public void run() {
125                                 copyURIAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.copy-uri"));
126                                 copyURIAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.copy-uri.tooltip"));
127                         }
128                 });
129         }
130
131         /**
132          * Initializes the page.
133          */
134         private void pageInit() {
135                 setLayout(new BorderLayout(12, 12));
136                 add(createProjectInsertPanel(), BorderLayout.CENTER);
137         }
138
139         /**
140          * Creates the main panel.
141          *
142          * @return The main panel
143          */
144         private JComponent createProjectInsertPanel() {
145                 JComponent projectInsertPanel = new JPanel(new GridBagLayout());
146
147                 requestURITextField = new JTextField();
148                 requestURITextField.setEditable(false);
149
150                 startTimeLabel = new JLabel();
151
152                 progressBar = new JProgressBar(0, 1);
153                 progressBar.setStringPainted(true);
154                 progressBar.setValue(0);
155
156                 final JLabel projectInformationLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.insert.project-information") + "</b></html>");
157                 projectInsertPanel.add(projectInformationLabel, new GridBagConstraints(0, 0, 2, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
158                 final JLabel requestURILabel = new JLabel(I18n.getMessage("jsite.insert.request-uri") + ":");
159                 projectInsertPanel.add(requestURILabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 18, 0, 0), 0, 0));
160                 projectInsertPanel.add(requestURITextField, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
161                 final JLabel startTimeLeftLabel = new JLabel(I18n.getMessage("jsite.insert.start-time") + ":");
162                 projectInsertPanel.add(startTimeLeftLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 18, 0, 0), 0, 0));
163                 projectInsertPanel.add(startTimeLabel, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
164                 final JLabel progressLabel = new JLabel(I18n.getMessage("jsite.insert.progress") + ":");
165                 projectInsertPanel.add(progressLabel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 18, 0, 0), 0, 0));
166                 projectInsertPanel.add(progressBar, new GridBagConstraints(1, 3, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 6, 0, 0), 0, 0));
167                 projectInsertPanel.add(new JButton(copyURIAction), new GridBagConstraints(0, 4, 2, 1, 0.0, 0.0, GridBagConstraints.LINE_END, GridBagConstraints.NONE, new Insets(12, 18, 0, 0), 0, 0));
168
169                 I18nContainer.getInstance().registerRunnable(new Runnable() {
170
171                         @SuppressWarnings("synthetic-access")
172                         public void run() {
173                                 projectInformationLabel.setText("<html><b>" + I18n.getMessage("jsite.insert.project-information") + "</b></html>");
174                                 requestURILabel.setText(I18n.getMessage("jsite.insert.request-uri") + ":");
175                                 startTimeLeftLabel.setText(I18n.getMessage("jsite.insert.start-time") + ":");
176                                 if (startTime != 0) {
177                                         startTimeLabel.setText(DateFormat.getDateTimeInstance().format(new Date(startTime)));
178                                 } else {
179                                         startTimeLabel.setText("");
180                                 }
181                                 progressLabel.setText(I18n.getMessage("jsite.insert.progress") + ":");
182                         }
183                 });
184
185                 return projectInsertPanel;
186         }
187
188         /**
189          * {@inheritDoc}
190          */
191         @Override
192         public void pageAdded(TWizard wizard) {
193                 this.wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
194                 this.wizard.setPreviousEnabled(false);
195                 this.wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
196                 this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
197         }
198
199         /**
200          * Starts the insert.
201          */
202         public void startInsert() {
203                 wizard.setNextEnabled(false);
204                 copyURIAction.setEnabled(false);
205                 progressBar.setValue(0);
206                 progressBar.setString(I18n.getMessage("jsite.insert.starting"));
207                 progressBar.setFont(progressBar.getFont().deriveFont(Font.PLAIN));
208                 projectInserter.start();
209         }
210
211         /**
212          * Sets whether to activate the debug mode.
213          *
214          * @param debug
215          *            <code>true</code> to activate the debug mode,
216          *            <code>false</code> to deactivate.
217          */
218         public void setDebug(boolean debug) {
219                 projectInserter.setDebug(debug);
220         }
221
222         /**
223          * Sets the project to insert.
224          *
225          * @param project
226          *            The project to insert
227          */
228         public void setProject(final Project project) {
229                 projectInserter.setProject(project);
230                 SwingUtilities.invokeLater(new Runnable() {
231
232                         @SuppressWarnings("synthetic-access")
233                         public void run() {
234                                 requestURITextField.setText(project.getFinalRequestURI(1));
235                         }
236                 });
237         }
238
239         /**
240          * Sets the freenet interface to use.
241          *
242          * @param freenetInterface
243          *            The freenet interface to use
244          */
245         public void setFreenetInterface(Freenet7Interface freenetInterface) {
246                 projectInserter.setFreenetInterface(freenetInterface);
247         }
248
249         //
250         // INTERFACE InsertListener
251         //
252
253         /**
254          * {@inheritDoc}
255          */
256         public void projectInsertStarted(final Project project) {
257                 startTime = System.currentTimeMillis();
258                 SwingUtilities.invokeLater(new Runnable() {
259
260                         @SuppressWarnings("synthetic-access")
261                         public void run() {
262                                 startTimeLabel.setText(DateFormat.getDateTimeInstance().format(new Date(startTime)));
263                         }
264                 });
265         }
266
267         /**
268          * {@inheritDoc}
269          */
270         public void projectUploadFinished(Project project) {
271                 startTime = System.currentTimeMillis();
272         }
273
274         /**
275          * {@inheritDoc}
276          */
277         public void projectURIGenerated(Project project, final String uri) {
278                 SwingUtilities.invokeLater(new Runnable() {
279
280                         @SuppressWarnings("synthetic-access")
281                         public void run() {
282                                 copyURIAction.setEnabled(true);
283                                 requestURITextField.setText(uri);
284                         }
285                 });
286         }
287
288         /**
289          * {@inheritDoc}
290          */
291         public void projectInsertProgress(Project project, final int succeeded, final int failed, final int fatal, final int total, final boolean finalized) {
292                 SwingUtilities.invokeLater(new Runnable() {
293
294                         @SuppressWarnings("synthetic-access")
295                         public void run() {
296                                 progressBar.setMaximum(total);
297                                 progressBar.setValue(succeeded + failed + fatal);
298                                 int progress = (succeeded + failed + fatal) * 100 / total;
299                                 StringBuilder progressString = new StringBuilder();
300                                 progressString.append(progress).append("% (");
301                                 progressString.append(succeeded + failed + fatal).append('/').append(total);
302                                 progressString.append(") (");
303                                 progressString.append(formatNumber(succeeded * 32.0 / ((System.currentTimeMillis() - startTime) / 1000), 1));
304                                 progressString.append(' ').append(I18n.getMessage("jsite.insert.k-per-s")).append(')');
305                                 progressBar.setString(progressString.toString());
306                                 if (finalized) {
307                                         progressBar.setFont(progressBar.getFont().deriveFont(Font.BOLD));
308                                 }
309                         }
310                 });
311         }
312
313         /**
314          * {@inheritDoc}
315          */
316         public void projectInsertFinished(Project project, boolean success, Throwable cause) {
317                 if (success) {
318                         JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.insert.inserted"), null, JOptionPane.INFORMATION_MESSAGE);
319                 } else {
320                         if (cause == null) {
321                                 JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.insert.insert-failed"), null, JOptionPane.ERROR_MESSAGE);
322                         } else {
323                                 JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.insert.insert-failed-with-cause"), cause.getMessage()), null, JOptionPane.ERROR_MESSAGE);
324                         }
325                 }
326                 SwingUtilities.invokeLater(new Runnable() {
327
328                         @SuppressWarnings("synthetic-access")
329                         public void run() {
330                                 progressBar.setValue(progressBar.getMaximum());
331                                 progressBar.setString(I18n.getMessage("jsite.insert.done"));
332                                 wizard.setNextEnabled(true);
333                                 wizard.setQuitEnabled(true);
334                         }
335                 });
336         }
337
338         //
339         // ACTIONS
340         //
341
342         /**
343          * Copies the request URI of the project to the clipboard.
344          */
345         private void actionCopyURI() {
346                 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
347                 clipboard.setContents(new StringSelection(requestURITextField.getText()), this);
348         }
349
350         /**
351          * Formats the given number so that it always has the the given number of
352          * fractional digits.
353          *
354          * @param number
355          *            The number to format
356          * @param digits
357          *            The number of fractional digits
358          * @return The formatted number
359          */
360         private String formatNumber(double number, int digits) {
361                 int multiplier = (int) Math.pow(10, digits);
362                 String formattedNumber = String.valueOf((int) (number * multiplier) / (double) multiplier);
363                 if (formattedNumber.indexOf('.') == -1) {
364                         formattedNumber += '.';
365                         for (int digit = 0; digit < digits; digit++) {
366                                 formattedNumber += "0";
367                         }
368                 }
369                 return formattedNumber;
370         }
371
372         //
373         // INTERFACE ClipboardOwner
374         //
375
376         /**
377          * {@inheritDoc}
378          */
379         public void lostOwnership(Clipboard clipboard, Transferable contents) {
380                 /* ignore. */
381         }
382
383 }