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;
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;
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;
59 * Wizard page that shows the progress of an insert.
61 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
63 public class ProjectInsertPage extends TWizardPage implements InsertListener, ClipboardOwner {
65 /** The project inserter. */
66 private ProjectInserter projectInserter;
68 /** The “copy URI” action. */
69 private Action copyURIAction;
71 /** The “request URI” textfield. */
72 private JTextField requestURITextField;
74 /** The “start time” label. */
75 private JLabel startTimeLabel;
77 /** The progress bar. */
78 private JProgressBar progressBar;
80 /** The start time of the insert. */
81 private long startTime = 0;
83 /** The number of inserted blocks. */
84 private volatile int insertedBlocks;
86 /** Whether the “copy URI to clipboard” button was used. */
87 private boolean uriCopied;
90 * Creates a new progress insert wizard page.
93 * The wizard this page belongs to
95 public ProjectInsertPage(final TWizard wizard) {
99 setHeading(I18n.getMessage("jsite.insert.heading"));
100 setDescription(I18n.getMessage("jsite.insert.description"));
101 I18nContainer.getInstance().registerRunnable(new Runnable() {
104 setHeading(I18n.getMessage("jsite.insert.heading"));
105 setDescription(I18n.getMessage("jsite.insert.description"));
108 projectInserter = new ProjectInserter();
109 projectInserter.addInsertListener(this);
113 * Creates all used actions.
115 private void createActions() {
116 copyURIAction = new AbstractAction(I18n.getMessage("jsite.project.action.copy-uri")) {
118 @SuppressWarnings("synthetic-access")
119 public void actionPerformed(ActionEvent actionEvent) {
123 copyURIAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.copy-uri.tooltip"));
124 copyURIAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_U);
125 copyURIAction.setEnabled(false);
127 I18nContainer.getInstance().registerRunnable(new Runnable() {
129 @SuppressWarnings("synthetic-access")
131 copyURIAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.copy-uri"));
132 copyURIAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.copy-uri.tooltip"));
138 * Initializes the page.
140 private void pageInit() {
141 setLayout(new BorderLayout(12, 12));
142 add(createProjectInsertPanel(), BorderLayout.CENTER);
146 * Creates the main panel.
148 * @return The main panel
150 private JComponent createProjectInsertPanel() {
151 JComponent projectInsertPanel = new JPanel(new GridBagLayout());
153 requestURITextField = new JTextField();
154 requestURITextField.setEditable(false);
156 startTimeLabel = new JLabel();
158 progressBar = new JProgressBar(0, 1);
159 progressBar.setStringPainted(true);
160 progressBar.setValue(0);
162 final JLabel projectInformationLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.insert.project-information") + "</b></html>");
163 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));
164 final JLabel requestURILabel = new JLabel(I18n.getMessage("jsite.insert.request-uri") + ":");
165 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));
166 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));
167 final JLabel startTimeLeftLabel = new JLabel(I18n.getMessage("jsite.insert.start-time") + ":");
168 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));
169 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));
170 final JLabel progressLabel = new JLabel(I18n.getMessage("jsite.insert.progress") + ":");
171 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));
172 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));
173 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));
175 I18nContainer.getInstance().registerRunnable(new Runnable() {
177 @SuppressWarnings("synthetic-access")
179 projectInformationLabel.setText("<html><b>" + I18n.getMessage("jsite.insert.project-information") + "</b></html>");
180 requestURILabel.setText(I18n.getMessage("jsite.insert.request-uri") + ":");
181 startTimeLeftLabel.setText(I18n.getMessage("jsite.insert.start-time") + ":");
182 if (startTime != 0) {
183 startTimeLabel.setText(DateFormat.getDateTimeInstance().format(new Date(startTime)));
185 startTimeLabel.setText("");
187 progressLabel.setText(I18n.getMessage("jsite.insert.progress") + ":");
191 return projectInsertPanel;
198 public void pageAdded(TWizard wizard) {
199 this.wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
200 this.wizard.setPreviousEnabled(false);
201 this.wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
202 this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
208 public void startInsert() {
209 wizard.setNextEnabled(false);
210 copyURIAction.setEnabled(false);
211 progressBar.setValue(0);
212 progressBar.setString(I18n.getMessage("jsite.insert.starting"));
213 progressBar.setFont(progressBar.getFont().deriveFont(Font.PLAIN));
214 projectInserter.start();
218 * Sets the project to insert.
221 * The project to insert
223 public void setProject(final Project project) {
224 projectInserter.setProject(project);
225 SwingUtilities.invokeLater(new Runnable() {
227 @SuppressWarnings("synthetic-access")
229 requestURITextField.setText(project.getFinalRequestURI(1));
235 * Sets the freenet interface to use.
237 * @param freenetInterface
238 * The freenet interface to use
240 public void setFreenetInterface(Freenet7Interface freenetInterface) {
241 projectInserter.setFreenetInterface(freenetInterface);
245 * Sets the project inserter’s temp directory.
247 * @see ProjectInserter#setTempDirectory(String)
248 * @param tempDirectory
249 * The temp directory to use, or {@code null} to use the system
252 public void setTempDirectory(String tempDirectory) {
253 projectInserter.setTempDirectory(tempDirectory);
257 * Returns whether the “copy URI to clipboard” button was used.
259 * @return {@code true} if an URI was copied to clipboard, {@code false}
262 public boolean wasUriCopied() {
267 // INTERFACE InsertListener
273 public void projectInsertStarted(final Project project) {
275 SwingUtilities.invokeLater(new Runnable() {
277 @SuppressWarnings("synthetic-access")
279 startTimeLabel.setText(DateFormat.getDateTimeInstance().format(new Date()));
287 public void projectUploadFinished(Project project) {
288 startTime = System.currentTimeMillis();
294 public void projectURIGenerated(Project project, final String uri) {
295 SwingUtilities.invokeLater(new Runnable() {
297 @SuppressWarnings("synthetic-access")
299 copyURIAction.setEnabled(true);
300 requestURITextField.setText(uri);
308 public void projectInsertProgress(Project project, final int succeeded, final int failed, final int fatal, final int total, final boolean finalized) {
309 insertedBlocks = succeeded;
310 SwingUtilities.invokeLater(new Runnable() {
312 @SuppressWarnings("synthetic-access")
314 progressBar.setMaximum(total);
315 progressBar.setValue(succeeded + failed + fatal);
316 int progress = (succeeded + failed + fatal) * 100 / total;
317 StringBuilder progressString = new StringBuilder();
318 progressString.append(progress).append("% (");
319 progressString.append(succeeded + failed + fatal).append('/').append(total);
320 progressString.append(") (");
321 progressString.append(getTransferRate());
322 progressString.append(' ').append(I18n.getMessage("jsite.insert.k-per-s")).append(')');
323 progressBar.setString(progressString.toString());
325 progressBar.setFont(progressBar.getFont().deriveFont(Font.BOLD));
334 public void projectInsertFinished(Project project, boolean success, Throwable cause) {
336 String copyURILabel = I18n.getMessage("jsite.insert.okay-copy-uri");
337 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.insert.okay"), copyURILabel }, copyURILabel);
338 if (selectedValue == 1) {
343 JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.insert.insert-failed"), null, JOptionPane.ERROR_MESSAGE);
345 JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.insert.insert-failed-with-cause"), cause.getMessage()), null, JOptionPane.ERROR_MESSAGE);
348 SwingUtilities.invokeLater(new Runnable() {
350 @SuppressWarnings("synthetic-access")
352 progressBar.setValue(progressBar.getMaximum());
353 progressBar.setString(I18n.getMessage("jsite.insert.done") + " (" + getTransferRate() + " " + I18n.getMessage("jsite.insert.k-per-s") + ")");
354 wizard.setNextEnabled(true);
355 wizard.setQuitEnabled(true);
365 * Copies the request URI of the project to the clipboard.
367 private void actionCopyURI() {
369 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
370 clipboard.setContents(new StringSelection(requestURITextField.getText()), this);
374 * Formats the given number so that it always has the the given number of
378 * The number to format
380 * The number of fractional digits
381 * @return The formatted number
383 private String formatNumber(double number, int digits) {
384 int multiplier = (int) Math.pow(10, digits);
385 String formattedNumber = String.valueOf((int) (number * multiplier) / (double) multiplier);
386 if (formattedNumber.indexOf('.') == -1) {
387 formattedNumber += '.';
388 for (int digit = 0; digit < digits; digit++) {
389 formattedNumber += "0";
392 return formattedNumber;
396 * Returns the formatted transfer rate at this point.
398 * @return The formatted transfer rate
400 private String getTransferRate() {
401 return formatNumber(insertedBlocks * 32.0 / ((System.currentTimeMillis() - startTime) / 1000), 1);
405 // INTERFACE ClipboardOwner
411 public void lostOwnership(Clipboard clipboard, Transferable contents) {