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.Freenet7Interface;
52 import de.todesbaum.jsite.application.InsertListener;
53 import de.todesbaum.jsite.application.Project;
54 import de.todesbaum.jsite.application.ProjectInserter;
55 import de.todesbaum.jsite.i18n.I18n;
56 import de.todesbaum.jsite.i18n.I18nContainer;
57 import de.todesbaum.util.swing.TWizard;
58 import de.todesbaum.util.swing.TWizardPage;
61 * Wizard page that shows the progress of an insert.
63 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
65 public class ProjectInsertPage extends TWizardPage implements InsertListener, ClipboardOwner {
68 private static final Logger logger = Logger.getLogger(ProjectInsertPage.class.getName());
70 /** The project inserter. */
71 private ProjectInserter projectInserter;
73 /** The “copy URI” action. */
74 private Action copyURIAction;
76 /** The “request URI” textfield. */
77 private JTextField requestURITextField;
79 /** The “start time” label. */
80 private JLabel startTimeLabel;
82 /** The progress bar. */
83 private JProgressBar progressBar;
85 /** The start time of the insert. */
86 private long startTime = 0;
88 /** The number of inserted blocks. */
89 private volatile int insertedBlocks;
91 /** Whether the “copy URI to clipboard” button was used. */
92 private boolean uriCopied;
95 * Creates a new progress insert wizard page.
98 * The wizard this page belongs to
100 public ProjectInsertPage(final TWizard wizard) {
104 setHeading(I18n.getMessage("jsite.insert.heading"));
105 setDescription(I18n.getMessage("jsite.insert.description"));
106 I18nContainer.getInstance().registerRunnable(new Runnable() {
109 setHeading(I18n.getMessage("jsite.insert.heading"));
110 setDescription(I18n.getMessage("jsite.insert.description"));
113 projectInserter = new ProjectInserter();
114 projectInserter.addInsertListener(this);
118 * Creates all used actions.
120 private void createActions() {
121 copyURIAction = new AbstractAction(I18n.getMessage("jsite.project.action.copy-uri")) {
123 @SuppressWarnings("synthetic-access")
124 public void actionPerformed(ActionEvent actionEvent) {
128 copyURIAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.copy-uri.tooltip"));
129 copyURIAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_U);
130 copyURIAction.setEnabled(false);
132 I18nContainer.getInstance().registerRunnable(new Runnable() {
134 @SuppressWarnings("synthetic-access")
136 copyURIAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.copy-uri"));
137 copyURIAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.copy-uri.tooltip"));
143 * Initializes the page.
145 private void pageInit() {
146 setLayout(new BorderLayout(12, 12));
147 add(createProjectInsertPanel(), BorderLayout.CENTER);
151 * Creates the main panel.
153 * @return The main panel
155 private JComponent createProjectInsertPanel() {
156 JComponent projectInsertPanel = new JPanel(new GridBagLayout());
158 requestURITextField = new JTextField();
159 requestURITextField.setEditable(false);
161 startTimeLabel = new JLabel();
163 progressBar = new JProgressBar(0, 1);
164 progressBar.setStringPainted(true);
165 progressBar.setValue(0);
167 final JLabel projectInformationLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.insert.project-information") + "</b></html>");
168 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));
169 final JLabel requestURILabel = new JLabel(I18n.getMessage("jsite.insert.request-uri") + ":");
170 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));
171 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));
172 final JLabel startTimeLeftLabel = new JLabel(I18n.getMessage("jsite.insert.start-time") + ":");
173 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));
174 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));
175 final JLabel progressLabel = new JLabel(I18n.getMessage("jsite.insert.progress") + ":");
176 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));
177 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));
178 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));
180 I18nContainer.getInstance().registerRunnable(new Runnable() {
182 @SuppressWarnings("synthetic-access")
184 projectInformationLabel.setText("<html><b>" + I18n.getMessage("jsite.insert.project-information") + "</b></html>");
185 requestURILabel.setText(I18n.getMessage("jsite.insert.request-uri") + ":");
186 startTimeLeftLabel.setText(I18n.getMessage("jsite.insert.start-time") + ":");
187 if (startTime != 0) {
188 startTimeLabel.setText(DateFormat.getDateTimeInstance().format(new Date(startTime)));
190 startTimeLabel.setText("");
192 progressLabel.setText(I18n.getMessage("jsite.insert.progress") + ":");
196 return projectInsertPanel;
203 public void pageAdded(TWizard wizard) {
204 this.wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
205 this.wizard.setPreviousEnabled(false);
206 this.wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
207 this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
213 public void startInsert() {
214 wizard.setNextEnabled(false);
215 copyURIAction.setEnabled(false);
216 progressBar.setValue(0);
217 progressBar.setString(I18n.getMessage("jsite.insert.starting"));
218 progressBar.setFont(progressBar.getFont().deriveFont(Font.PLAIN));
219 projectInserter.start();
223 * Sets the project to insert.
226 * The project to insert
228 public void setProject(final Project project) {
229 projectInserter.setProject(project);
230 SwingUtilities.invokeLater(new Runnable() {
232 @SuppressWarnings("synthetic-access")
234 requestURITextField.setText(project.getFinalRequestURI(1));
240 * Sets the freenet interface to use.
242 * @param freenetInterface
243 * The freenet interface to use
245 public void setFreenetInterface(Freenet7Interface freenetInterface) {
246 projectInserter.setFreenetInterface(freenetInterface);
250 * Sets the project inserter’s temp directory.
252 * @see ProjectInserter#setTempDirectory(String)
253 * @param tempDirectory
254 * The temp directory to use, or {@code null} to use the system
257 public void setTempDirectory(String tempDirectory) {
258 projectInserter.setTempDirectory(tempDirectory);
262 * Returns whether the “copy URI to clipboard” button was used.
264 * @return {@code true} if an URI was copied to clipboard, {@code false}
267 public boolean wasUriCopied() {
272 // INTERFACE InsertListener
278 public void projectInsertStarted(final Project project) {
280 SwingUtilities.invokeLater(new Runnable() {
282 @SuppressWarnings("synthetic-access")
284 startTimeLabel.setText(DateFormat.getDateTimeInstance().format(new Date()));
292 public void projectUploadFinished(Project project) {
293 startTime = System.currentTimeMillis();
299 public void projectURIGenerated(Project project, final String uri) {
300 SwingUtilities.invokeLater(new Runnable() {
302 @SuppressWarnings("synthetic-access")
304 copyURIAction.setEnabled(true);
305 requestURITextField.setText(uri);
308 logger.log(Level.FINEST, "Insert generated URI: " + uri);
309 int slash = uri.indexOf('/');
310 slash = uri.indexOf('/', slash + 1);
311 int secondSlash = uri.indexOf('/', slash + 1);
312 if (secondSlash == -1) {
313 secondSlash = uri.length();
315 String editionNumber = uri.substring(slash + 1, secondSlash);
316 logger.log(Level.FINEST, "Extracted edition number: " + editionNumber);
319 edition = Integer.valueOf(editionNumber);
320 } catch (NumberFormatException nfe1) {
323 logger.log(Level.FINEST, "Insert edition: " + edition + ", Project edition: " + project.getEdition());
324 if ((edition != -1) && (edition == project.getEdition())) {
325 JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.insert.reinserted-edition"), I18n.getMessage("jsite.insert.reinserted-edition.title"), JOptionPane.INFORMATION_MESSAGE);
332 public void projectInsertProgress(Project project, final int succeeded, final int failed, final int fatal, final int total, final boolean finalized) {
333 insertedBlocks = succeeded;
334 SwingUtilities.invokeLater(new Runnable() {
336 @SuppressWarnings("synthetic-access")
338 progressBar.setMaximum(total);
339 progressBar.setValue(succeeded + failed + fatal);
340 int progress = (succeeded + failed + fatal) * 100 / total;
341 StringBuilder progressString = new StringBuilder();
342 progressString.append(progress).append("% (");
343 progressString.append(succeeded + failed + fatal).append('/').append(total);
344 progressString.append(") (");
345 progressString.append(getTransferRate());
346 progressString.append(' ').append(I18n.getMessage("jsite.insert.k-per-s")).append(')');
347 progressBar.setString(progressString.toString());
349 progressBar.setFont(progressBar.getFont().deriveFont(Font.BOLD));
358 public void projectInsertFinished(Project project, boolean success, Throwable cause) {
360 String copyURILabel = I18n.getMessage("jsite.insert.okay-copy-uri");
361 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.okay"), copyURILabel }, copyURILabel);
362 if (selectedValue == 1) {
367 JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.insert.insert-failed"), null, JOptionPane.ERROR_MESSAGE);
369 JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.insert.insert-failed-with-cause"), cause.getMessage()), null, JOptionPane.ERROR_MESSAGE);
372 SwingUtilities.invokeLater(new Runnable() {
374 @SuppressWarnings("synthetic-access")
376 progressBar.setValue(progressBar.getMaximum());
377 progressBar.setString(I18n.getMessage("jsite.insert.done") + " (" + getTransferRate() + " " + I18n.getMessage("jsite.insert.k-per-s") + ")");
378 wizard.setNextEnabled(true);
379 wizard.setQuitEnabled(true);
389 * Copies the request URI of the project to the clipboard.
391 private void actionCopyURI() {
393 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
394 clipboard.setContents(new StringSelection(requestURITextField.getText()), this);
398 * Formats the given number so that it always has the the given number of
402 * The number to format
404 * The number of fractional digits
405 * @return The formatted number
407 private String formatNumber(double number, int digits) {
408 int multiplier = (int) Math.pow(10, digits);
409 String formattedNumber = String.valueOf((int) (number * multiplier) / (double) multiplier);
410 if (formattedNumber.indexOf('.') == -1) {
411 formattedNumber += '.';
412 for (int digit = 0; digit < digits; digit++) {
413 formattedNumber += "0";
416 return formattedNumber;
420 * Returns the formatted transfer rate at this point.
422 * @return The formatted transfer rate
424 private String getTransferRate() {
425 return formatNumber(insertedBlocks * 32.0 / ((System.currentTimeMillis() - startTime) / 1000), 1);
429 // INTERFACE ClipboardOwner
435 public void lostOwnership(Clipboard clipboard, Transferable contents) {