2 * jSite - a tool for uploading websites into Freenet
3 * Copyright (C) 2006 David Roden
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.
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.
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.
20 package de.todesbaum.jsite.gui;
22 import java.awt.BorderLayout;
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 import java.util.logging.Level;
38 import java.util.logging.Logger;
40 import javax.swing.AbstractAction;
41 import javax.swing.Action;
42 import javax.swing.JButton;
43 import javax.swing.JComponent;
44 import javax.swing.JLabel;
45 import javax.swing.JOptionPane;
46 import javax.swing.JPanel;
47 import javax.swing.JProgressBar;
48 import javax.swing.JTextField;
49 import javax.swing.SwingUtilities;
51 import de.todesbaum.jsite.application.AbortedException;
52 import de.todesbaum.jsite.application.Freenet7Interface;
53 import de.todesbaum.jsite.application.InsertListener;
54 import de.todesbaum.jsite.application.Project;
55 import de.todesbaum.jsite.application.ProjectInserter;
56 import de.todesbaum.jsite.i18n.I18n;
57 import de.todesbaum.jsite.i18n.I18nContainer;
58 import de.todesbaum.util.io.StreamCopier.ProgressListener;
59 import de.todesbaum.util.swing.TWizard;
60 import de.todesbaum.util.swing.TWizardPage;
63 * Wizard page that shows the progress of an insert.
65 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
67 public class ProjectInsertPage extends TWizardPage implements InsertListener, ClipboardOwner {
70 private static final Logger logger = Logger.getLogger(ProjectInsertPage.class.getName());
72 /** The project inserter. */
73 private ProjectInserter projectInserter;
75 /** The “copy URI” action. */
76 private Action copyURIAction;
78 /** The “request URI” textfield. */
79 private JTextField requestURITextField;
81 /** The “start time” label. */
82 private JLabel startTimeLabel;
84 /** The progress bar. */
85 private JProgressBar progressBar;
87 /** The start time of the insert. */
88 private long startTime = 0;
90 /** The number of inserted blocks. */
91 private volatile int insertedBlocks;
93 /** Whether the “copy URI to clipboard” button was used. */
94 private boolean uriCopied;
96 /** Whether the insert is currently running. */
97 private volatile boolean running = false;
100 * Creates a new progress insert wizard page.
103 * The wizard this page belongs to
105 public ProjectInsertPage(final TWizard wizard) {
109 setHeading(I18n.getMessage("jsite.insert.heading"));
110 setDescription(I18n.getMessage("jsite.insert.description"));
111 I18nContainer.getInstance().registerRunnable(new Runnable() {
114 setHeading(I18n.getMessage("jsite.insert.heading"));
115 setDescription(I18n.getMessage("jsite.insert.description"));
118 projectInserter = new ProjectInserter();
119 projectInserter.addInsertListener(this);
123 * Creates all used actions.
125 private void createActions() {
126 copyURIAction = new AbstractAction(I18n.getMessage("jsite.project.action.copy-uri")) {
128 @SuppressWarnings("synthetic-access")
129 public void actionPerformed(ActionEvent actionEvent) {
133 copyURIAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.copy-uri.tooltip"));
134 copyURIAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_U);
135 copyURIAction.setEnabled(false);
137 I18nContainer.getInstance().registerRunnable(new Runnable() {
139 @SuppressWarnings("synthetic-access")
141 copyURIAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.copy-uri"));
142 copyURIAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.copy-uri.tooltip"));
148 * Initializes the page.
150 private void pageInit() {
151 setLayout(new BorderLayout(12, 12));
152 add(createProjectInsertPanel(), BorderLayout.CENTER);
156 * Creates the main panel.
158 * @return The main panel
160 private JComponent createProjectInsertPanel() {
161 JComponent projectInsertPanel = new JPanel(new GridBagLayout());
163 requestURITextField = new JTextField();
164 requestURITextField.setEditable(false);
166 startTimeLabel = new JLabel();
168 progressBar = new JProgressBar(0, 1);
169 progressBar.setStringPainted(true);
170 progressBar.setValue(0);
172 final JLabel projectInformationLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.insert.project-information") + "</b></html>");
173 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));
174 final JLabel requestURILabel = new JLabel(I18n.getMessage("jsite.insert.request-uri") + ":");
175 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));
176 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));
177 final JLabel startTimeLeftLabel = new JLabel(I18n.getMessage("jsite.insert.start-time") + ":");
178 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));
179 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));
180 final JLabel progressLabel = new JLabel(I18n.getMessage("jsite.insert.progress") + ":");
181 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));
182 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));
183 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));
185 I18nContainer.getInstance().registerRunnable(new Runnable() {
187 @SuppressWarnings("synthetic-access")
189 projectInformationLabel.setText("<html><b>" + I18n.getMessage("jsite.insert.project-information") + "</b></html>");
190 requestURILabel.setText(I18n.getMessage("jsite.insert.request-uri") + ":");
191 startTimeLeftLabel.setText(I18n.getMessage("jsite.insert.start-time") + ":");
192 if (startTime != 0) {
193 startTimeLabel.setText(DateFormat.getDateTimeInstance().format(new Date(startTime)));
195 startTimeLabel.setText("");
197 progressLabel.setText(I18n.getMessage("jsite.insert.progress") + ":");
201 return projectInsertPanel;
208 public void pageAdded(TWizard wizard) {
209 this.wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
210 this.wizard.setPreviousEnabled(false);
211 this.wizard.setNextName(I18n.getMessage("jsite.general.cancel"));
212 this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
218 public void startInsert() {
220 copyURIAction.setEnabled(false);
221 progressBar.setValue(0);
222 progressBar.setString(I18n.getMessage("jsite.insert.starting"));
223 progressBar.setFont(progressBar.getFont().deriveFont(Font.PLAIN));
224 projectInserter.start(new ProgressListener() {
226 public void onProgress(final long copied, final long length) {
227 SwingUtilities.invokeLater(new Runnable() {
232 @SuppressWarnings("synthetic-access")
235 while (((copied / divisor) > Integer.MAX_VALUE) || ((length / divisor) > Integer.MAX_VALUE)) {
238 progressBar.setMaximum((int) (length / divisor));
239 progressBar.setValue((int) (copied / divisor));
240 progressBar.setString("Uploaded: " + copied + " / " + length);
248 * Stops the currently running insert.
250 public void stopInsert() {
252 wizard.setNextEnabled(false);
253 projectInserter.stop();
258 * Returns whether the insert is currently running.
260 * @return {@code true} if the insert is currently running, {@code false}
263 public boolean isRunning() {
268 * Sets the project to insert.
271 * The project to insert
273 public void setProject(final Project project) {
274 projectInserter.setProject(project);
275 SwingUtilities.invokeLater(new Runnable() {
277 @SuppressWarnings("synthetic-access")
279 requestURITextField.setText(project.getFinalRequestURI(1));
285 * Sets the freenet interface to use.
287 * @param freenetInterface
288 * The freenet interface to use
290 public void setFreenetInterface(Freenet7Interface freenetInterface) {
291 projectInserter.setFreenetInterface(freenetInterface);
295 * Sets the project inserter’s temp directory.
297 * @see ProjectInserter#setTempDirectory(String)
298 * @param tempDirectory
299 * The temp directory to use, or {@code null} to use the system
302 public void setTempDirectory(String tempDirectory) {
303 projectInserter.setTempDirectory(tempDirectory);
307 * Returns whether the “copy URI to clipboard” button was used.
309 * @return {@code true} if an URI was copied to clipboard, {@code false}
312 public boolean wasUriCopied() {
317 // INTERFACE InsertListener
323 public void projectInsertStarted(final Project project) {
325 SwingUtilities.invokeLater(new Runnable() {
327 @SuppressWarnings("synthetic-access")
329 startTimeLabel.setText(DateFormat.getDateTimeInstance().format(new Date()));
337 public void projectUploadFinished(Project project) {
338 startTime = System.currentTimeMillis();
339 SwingUtilities.invokeLater(new Runnable() {
341 @SuppressWarnings("synthetic-access")
343 progressBar.setString(I18n.getMessage("jsite.insert.starting"));
344 progressBar.setValue(0);
352 public void projectURIGenerated(Project project, final String uri) {
353 SwingUtilities.invokeLater(new Runnable() {
355 @SuppressWarnings("synthetic-access")
357 copyURIAction.setEnabled(true);
358 requestURITextField.setText(uri);
361 logger.log(Level.FINEST, "Insert generated URI: " + uri);
362 int slash = uri.indexOf('/');
363 slash = uri.indexOf('/', slash + 1);
364 int secondSlash = uri.indexOf('/', slash + 1);
365 if (secondSlash == -1) {
366 secondSlash = uri.length();
368 String editionNumber = uri.substring(slash + 1, secondSlash);
369 logger.log(Level.FINEST, "Extracted edition number: " + editionNumber);
372 edition = Integer.valueOf(editionNumber);
373 } catch (NumberFormatException nfe1) {
376 logger.log(Level.FINEST, "Insert edition: " + edition + ", Project edition: " + project.getEdition());
377 if ((edition != -1) && (edition == project.getEdition())) {
378 JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.insert.reinserted-edition"), I18n.getMessage("jsite.insert.reinserted-edition.title"), JOptionPane.INFORMATION_MESSAGE);
385 public void projectInsertProgress(Project project, final int succeeded, final int failed, final int fatal, final int total, final boolean finalized) {
386 insertedBlocks = succeeded;
387 SwingUtilities.invokeLater(new Runnable() {
389 @SuppressWarnings("synthetic-access")
391 progressBar.setMaximum(total);
392 progressBar.setValue(succeeded + failed + fatal);
393 int progress = (succeeded + failed + fatal) * 100 / total;
394 StringBuilder progressString = new StringBuilder();
395 progressString.append(progress).append("% (");
396 progressString.append(succeeded + failed + fatal).append('/').append(total);
397 progressString.append(") (");
398 progressString.append(getTransferRate());
399 progressString.append(' ').append(I18n.getMessage("jsite.insert.k-per-s")).append(')');
400 progressBar.setString(progressString.toString());
402 progressBar.setFont(progressBar.getFont().deriveFont(Font.BOLD));
411 public void projectInsertFinished(Project project, boolean success, Throwable cause) {
414 String copyURILabel = I18n.getMessage("jsite.insert.okay-copy-uri");
415 int selectedValue = JOptionPane.showOptionDialog(this, I18n.getMessage("jsite.insert.inserted"), I18n.getMessage("jsite.insert.done.title"), 0, JOptionPane.INFORMATION_MESSAGE, null, new Object[] { I18n.getMessage("jsite.general.ok"), copyURILabel }, copyURILabel);
416 if (selectedValue == 1) {
421 JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.insert.insert-failed"), I18n.getMessage("jsite.insert.insert-failed.title"), JOptionPane.ERROR_MESSAGE);
423 if (cause instanceof AbortedException) {
424 JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.insert.insert-aborted"), I18n.getMessage("jsite.insert.insert-aborted.title"), JOptionPane.INFORMATION_MESSAGE);
426 JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.insert.insert-failed-with-cause"), cause.getMessage()), I18n.getMessage("jsite.insert.insert-failed.title"), JOptionPane.ERROR_MESSAGE);
430 SwingUtilities.invokeLater(new Runnable() {
432 @SuppressWarnings("synthetic-access")
434 progressBar.setValue(progressBar.getMaximum());
435 progressBar.setString(I18n.getMessage("jsite.insert.done") + " (" + getTransferRate() + " " + I18n.getMessage("jsite.insert.k-per-s") + ")");
436 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
437 wizard.setNextEnabled(true);
438 wizard.setQuitEnabled(true);
448 * Copies the request URI of the project to the clipboard.
450 private void actionCopyURI() {
452 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
453 clipboard.setContents(new StringSelection(requestURITextField.getText()), this);
457 * Formats the given number so that it always has the the given number of
461 * The number to format
463 * The number of fractional digits
464 * @return The formatted number
466 private String formatNumber(double number, int digits) {
467 int multiplier = (int) Math.pow(10, digits);
468 String formattedNumber = String.valueOf((int) (number * multiplier) / (double) multiplier);
469 if (formattedNumber.indexOf('.') == -1) {
470 formattedNumber += '.';
471 for (int digit = 0; digit < digits; digit++) {
472 formattedNumber += "0";
475 return formattedNumber;
479 * Returns the formatted transfer rate at this point.
481 * @return The formatted transfer rate
483 private String getTransferRate() {
484 return formatNumber(insertedBlocks * 32.0 / ((System.currentTimeMillis() - startTime) / 1000), 1);
488 // INTERFACE ClipboardOwner
494 public void lostOwnership(Clipboard clipboard, Transferable contents) {