Fix speed calculation.
[jSite.git] / src / de / todesbaum / jsite / gui / ProjectInsertPage.java
index f5990b4..315b89f 100644 (file)
@@ -56,19 +56,36 @@ import de.todesbaum.util.swing.TWizard;
 import de.todesbaum.util.swing.TWizardPage;
 
 /**
- * @author David Roden <droden@gmail.com>
- * @version $Id$
+ * Wizard page that shows the progress of an insert.
+ *
+ * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
  */
 public class ProjectInsertPage extends TWizardPage implements InsertListener, ClipboardOwner {
 
-       protected ProjectInserter projectInserter;
+       /** The project inserter. */
+       private ProjectInserter projectInserter;
+
+       /** The “copy URI” action. */
+       private Action copyURIAction;
+
+       /** The “request URI” textfield. */
+       private JTextField requestURITextField;
+
+       /** The “start time” label. */
+       private JLabel startTimeLabel;
 
-       protected Action copyURIAction;
-       protected JTextField requestURITextField;
-       protected JLabel startTimeLabel;
-       protected JProgressBar progressBar;
-       protected long startTime = 0;
+       /** The progress bar. */
+       private JProgressBar progressBar;
 
+       /** The start time of the insert. */
+       private long startTime = 0;
+
+       /**
+        * Creates a new progress insert wizard page.
+        *
+        * @param wizard
+        *            The wizard this page belongs to
+        */
        public ProjectInsertPage(final TWizard wizard) {
                super(wizard);
                createActions();
@@ -86,8 +103,13 @@ public class ProjectInsertPage extends TWizardPage implements InsertListener, Cl
                projectInserter.addInsertListener(this);
        }
 
+       /**
+        * Creates all used actions.
+        */
        private void createActions() {
                copyURIAction = new AbstractAction(I18n.getMessage("jsite.project.action.copy-uri")) {
+
+                       @SuppressWarnings("synthetic-access")
                        public void actionPerformed(ActionEvent actionEvent) {
                                actionCopyURI();
                        }
@@ -98,6 +120,7 @@ public class ProjectInsertPage extends TWizardPage implements InsertListener, Cl
 
                I18nContainer.getInstance().registerRunnable(new Runnable() {
 
+                       @SuppressWarnings("synthetic-access")
                        public void run() {
                                copyURIAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.copy-uri"));
                                copyURIAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.copy-uri.tooltip"));
@@ -105,11 +128,19 @@ public class ProjectInsertPage extends TWizardPage implements InsertListener, Cl
                });
        }
 
+       /**
+        * Initializes the page.
+        */
        private void pageInit() {
                setLayout(new BorderLayout(12, 12));
                add(createProjectInsertPanel(), BorderLayout.CENTER);
        }
 
+       /**
+        * Creates the main panel.
+        *
+        * @return The main panel
+        */
        private JComponent createProjectInsertPanel() {
                JComponent projectInsertPanel = new JPanel(new GridBagLayout());
 
@@ -137,6 +168,7 @@ public class ProjectInsertPage extends TWizardPage implements InsertListener, Cl
 
                I18nContainer.getInstance().registerRunnable(new Runnable() {
 
+                       @SuppressWarnings("synthetic-access")
                        public void run() {
                                projectInformationLabel.setText("<html><b>" + I18n.getMessage("jsite.insert.project-information") + "</b></html>");
                                requestURILabel.setText(I18n.getMessage("jsite.insert.request-uri") + ":");
@@ -164,6 +196,9 @@ public class ProjectInsertPage extends TWizardPage implements InsertListener, Cl
                this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
        }
 
+       /**
+        * Starts the insert.
+        */
        public void startInsert() {
                wizard.setNextEnabled(false);
                copyURIAction.setEnabled(false);
@@ -174,27 +209,39 @@ public class ProjectInsertPage extends TWizardPage implements InsertListener, Cl
        }
 
        /**
+        * Sets whether to activate the debug mode.
+        *
         * @param debug
-        *            The debug to set.
+        *            <code>true</code> to activate the debug mode,
+        *            <code>false</code> to deactivate.
         */
        public void setDebug(boolean debug) {
                projectInserter.setDebug(debug);
        }
 
        /**
+        * Sets the project to insert.
+        *
         * @param project
-        *            The project to set.
+        *            The project to insert
         */
        public void setProject(final Project project) {
                projectInserter.setProject(project);
                SwingUtilities.invokeLater(new Runnable() {
 
+                       @SuppressWarnings("synthetic-access")
                        public void run() {
                                requestURITextField.setText(project.getFinalRequestURI(1));
                        }
                });
        }
 
+       /**
+        * Sets the freenet interface to use.
+        *
+        * @param freenetInterface
+        *            The freenet interface to use
+        */
        public void setFreenetInterface(Freenet7Interface freenetInterface) {
                projectInserter.setFreenetInterface(freenetInterface);
        }
@@ -210,6 +257,7 @@ public class ProjectInsertPage extends TWizardPage implements InsertListener, Cl
                startTime = System.currentTimeMillis();
                SwingUtilities.invokeLater(new Runnable() {
 
+                       @SuppressWarnings("synthetic-access")
                        public void run() {
                                startTimeLabel.setText(DateFormat.getDateTimeInstance().format(new Date(startTime)));
                        }
@@ -221,6 +269,8 @@ public class ProjectInsertPage extends TWizardPage implements InsertListener, Cl
         */
        public void projectURIGenerated(Project project, final String uri) {
                SwingUtilities.invokeLater(new Runnable() {
+
+                       @SuppressWarnings("synthetic-access")
                        public void run() {
                                copyURIAction.setEnabled(true);
                                requestURITextField.setText(uri);
@@ -234,14 +284,17 @@ public class ProjectInsertPage extends TWizardPage implements InsertListener, Cl
        public void projectInsertProgress(Project project, final int succeeded, final int failed, final int fatal, final int total, final boolean finalized) {
                SwingUtilities.invokeLater(new Runnable() {
 
+                       @SuppressWarnings("synthetic-access")
                        public void run() {
                                progressBar.setMaximum(total);
                                progressBar.setValue(succeeded + failed + fatal);
                                int progress = (succeeded + failed + fatal) * 100 / total;
                                StringBuilder progressString = new StringBuilder();
                                progressString.append(progress).append("% (");
-                               progressString.append(succeeded + failed + fatal).append("/").append(total);
-                               progressString.append(")");
+                               progressString.append(succeeded + failed + fatal).append('/').append(total);
+                               progressString.append(") (");
+                               progressString.append(formatNumber(succeeded * 32.0 / ((System.currentTimeMillis() - startTime) / 1000), 1));
+                               progressString.append(' ').append(I18n.getMessage("jsite.insert.k-per-s")).append(')');
                                progressBar.setString(progressString.toString());
                                if (finalized) {
                                        progressBar.setFont(progressBar.getFont().deriveFont(Font.BOLD));
@@ -265,6 +318,7 @@ public class ProjectInsertPage extends TWizardPage implements InsertListener, Cl
                }
                SwingUtilities.invokeLater(new Runnable() {
 
+                       @SuppressWarnings("synthetic-access")
                        public void run() {
                                progressBar.setValue(progressBar.getMaximum());
                                progressBar.setString(I18n.getMessage("jsite.insert.done"));
@@ -278,11 +332,36 @@ public class ProjectInsertPage extends TWizardPage implements InsertListener, Cl
        // ACTIONS
        //
 
-       protected void actionCopyURI() {
+       /**
+        * Copies the request URI of the project to the clipboard.
+        */
+       private void actionCopyURI() {
                Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                clipboard.setContents(new StringSelection(requestURITextField.getText()), this);
        }
 
+       /**
+        * Formats the given number so that it always has the the given number of
+        * fractional digits.
+        *
+        * @param number
+        *            The number to format
+        * @param digits
+        *            The number of fractional digits
+        * @return The formatted number
+        */
+       private String formatNumber(double number, int digits) {
+               int multiplier = (int) Math.pow(10, digits);
+               String formattedNumber = String.valueOf((int) (number * multiplier) / (double) multiplier);
+               if (formattedNumber.indexOf('.') == -1) {
+                       formattedNumber += '.';
+                       for (int digit = 0; digit < digits; digit++) {
+                               formattedNumber += "0";
+                       }
+               }
+               return formattedNumber;
+       }
+
        //
        // INTERFACE ClipboardOwner
        //
@@ -291,6 +370,7 @@ public class ProjectInsertPage extends TWizardPage implements InsertListener, Cl
         * {@inheritDoc}
         */
        public void lostOwnership(Clipboard clipboard, Transferable contents) {
+               /* ignore. */
        }
 
 }