2 * jSite - ProjectInsertPage.java - Copyright © 2006–2012 David Roden
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 package de.todesbaum.jsite.gui;
21 import java.awt.BorderLayout;
23 import java.awt.GridBagConstraints;
24 import java.awt.GridBagLayout;
25 import java.awt.Insets;
26 import java.awt.Toolkit;
27 import java.awt.datatransfer.Clipboard;
28 import java.awt.datatransfer.ClipboardOwner;
29 import java.awt.datatransfer.StringSelection;
30 import java.awt.datatransfer.Transferable;
31 import java.awt.event.ActionEvent;
32 import java.awt.event.KeyEvent;
33 import java.text.DateFormat;
34 import java.text.MessageFormat;
35 import java.util.Date;
36 import java.util.logging.Level;
37 import java.util.logging.Logger;
39 import javax.swing.AbstractAction;
40 import javax.swing.Action;
41 import javax.swing.JButton;
42 import javax.swing.JComponent;
43 import javax.swing.JLabel;
44 import javax.swing.JOptionPane;
45 import javax.swing.JPanel;
46 import javax.swing.JProgressBar;
47 import javax.swing.JTextField;
48 import javax.swing.SwingUtilities;
50 import de.todesbaum.jsite.application.AbortedException;
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.freenet.fcp2.PriorityClass;
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 * Sets whether to use the “early encode“ flag for the insert.
319 * @param useEarlyEncode
320 * {@code true} to set the “early encode” flag for the insert,
321 * {@code false} otherwise
323 public void setUseEarlyEncode(boolean useEarlyEncode) {
324 projectInserter.setUseEarlyEncode(useEarlyEncode);
328 * Sets the insert priority.
331 * The insert priority
333 public void setPriority(PriorityClass priority) {
334 projectInserter.setPriority(priority);
338 // INTERFACE InsertListener
344 public void projectInsertStarted(final Project project) {
346 SwingUtilities.invokeLater(new Runnable() {
348 @SuppressWarnings("synthetic-access")
350 startTimeLabel.setText(DateFormat.getDateTimeInstance().format(new Date()));
358 public void projectUploadFinished(Project project) {
359 startTime = System.currentTimeMillis();
360 SwingUtilities.invokeLater(new Runnable() {
362 @SuppressWarnings("synthetic-access")
364 progressBar.setString(I18n.getMessage("jsite.insert.starting"));
365 progressBar.setValue(0);
373 public void projectURIGenerated(Project project, final String uri) {
374 SwingUtilities.invokeLater(new Runnable() {
376 @SuppressWarnings("synthetic-access")
378 copyURIAction.setEnabled(true);
379 requestURITextField.setText(uri);
382 logger.log(Level.FINEST, "Insert generated URI: " + uri);
383 int slash = uri.indexOf('/');
384 slash = uri.indexOf('/', slash + 1);
385 int secondSlash = uri.indexOf('/', slash + 1);
386 if (secondSlash == -1) {
387 secondSlash = uri.length();
389 String editionNumber = uri.substring(slash + 1, secondSlash);
390 logger.log(Level.FINEST, "Extracted edition number: " + editionNumber);
393 edition = Integer.valueOf(editionNumber);
394 } catch (NumberFormatException nfe1) {
397 logger.log(Level.FINEST, "Insert edition: " + edition + ", Project edition: " + project.getEdition());
398 if ((edition != -1) && (edition == project.getEdition())) {
399 JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.insert.reinserted-edition"), I18n.getMessage("jsite.insert.reinserted-edition.title"), JOptionPane.INFORMATION_MESSAGE);
406 public void projectInsertProgress(Project project, final int succeeded, final int failed, final int fatal, final int total, final boolean finalized) {
407 insertedBlocks = succeeded;
408 SwingUtilities.invokeLater(new Runnable() {
410 @SuppressWarnings("synthetic-access")
415 progressBar.setMaximum(total);
416 progressBar.setValue(succeeded + failed + fatal);
417 int progress = (succeeded + failed + fatal) * 100 / total;
418 StringBuilder progressString = new StringBuilder();
419 progressString.append(progress).append("% (");
420 progressString.append(succeeded + failed + fatal).append('/').append(total);
421 progressString.append(") (");
422 progressString.append(getTransferRate());
423 progressString.append(' ').append(I18n.getMessage("jsite.insert.k-per-s")).append(')');
424 progressBar.setString(progressString.toString());
426 progressBar.setFont(progressBar.getFont().deriveFont(Font.BOLD));
435 public void projectInsertFinished(Project project, boolean success, Throwable cause) {
438 String copyURILabel = I18n.getMessage("jsite.insert.okay-copy-uri");
439 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);
440 if (selectedValue == 1) {
445 JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.insert.insert-failed"), I18n.getMessage("jsite.insert.insert-failed.title"), JOptionPane.ERROR_MESSAGE);
447 if (cause instanceof AbortedException) {
448 JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.insert.insert-aborted"), I18n.getMessage("jsite.insert.insert-aborted.title"), JOptionPane.INFORMATION_MESSAGE);
450 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);
454 SwingUtilities.invokeLater(new Runnable() {
456 @SuppressWarnings("synthetic-access")
458 progressBar.setValue(progressBar.getMaximum());
459 progressBar.setString(I18n.getMessage("jsite.insert.done") + " (" + getTransferRate() + " " + I18n.getMessage("jsite.insert.k-per-s") + ")");
460 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
461 wizard.setNextEnabled(true);
462 wizard.setQuitEnabled(true);
472 * Copies the request URI of the project to the clipboard.
474 private void actionCopyURI() {
476 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
477 clipboard.setContents(new StringSelection(requestURITextField.getText()), this);
481 * Formats the given number so that it always has the the given number of
485 * The number to format
487 * The number of fractional digits
488 * @return The formatted number
490 private String formatNumber(double number, int digits) {
491 int multiplier = (int) Math.pow(10, digits);
492 String formattedNumber = String.valueOf((int) (number * multiplier) / (double) multiplier);
493 if (formattedNumber.indexOf('.') == -1) {
494 formattedNumber += '.';
495 for (int digit = 0; digit < digits; digit++) {
496 formattedNumber += "0";
499 return formattedNumber;
503 * Returns the formatted transfer rate at this point.
505 * @return The formatted transfer rate
507 private String getTransferRate() {
508 return formatNumber(insertedBlocks * 32.0 / ((System.currentTimeMillis() - startTime) / 1000), 1);
512 // INTERFACE ClipboardOwner
518 public void lostOwnership(Clipboard clipboard, Transferable contents) {