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;
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;
37 import javax.swing.AbstractAction;
38 import javax.swing.Action;
39 import javax.swing.JButton;
40 import javax.swing.JComponent;
41 import javax.swing.JLabel;
42 import javax.swing.JOptionPane;
43 import javax.swing.JPanel;
44 import javax.swing.JProgressBar;
45 import javax.swing.JTextField;
46 import javax.swing.SwingUtilities;
48 import de.todesbaum.jsite.application.Freenet7Interface;
49 import de.todesbaum.jsite.application.InsertListener;
50 import de.todesbaum.jsite.application.Project;
51 import de.todesbaum.jsite.application.ProjectInserter;
52 import de.todesbaum.jsite.i18n.I18n;
53 import de.todesbaum.util.swing.TWizard;
54 import de.todesbaum.util.swing.TWizardPage;
57 * @author David Roden <droden@gmail.com>
60 public class ProjectInsertPage extends TWizardPage implements InsertListener, ClipboardOwner {
62 protected TWizard wizard;
63 protected ProjectInserter projectInserter;
65 protected Action copyURIAction;
66 protected JTextField requestURITextField;
67 protected JLabel startTimeLabel;
68 protected JProgressBar progressBar;
69 protected long startTime;
71 public ProjectInsertPage() {
75 setHeading(I18n.getMessage("jsite.insert.heading"));
76 setDescription(I18n.getMessage("jsite.insert.description"));
77 projectInserter = new ProjectInserter();
78 projectInserter.addInsertListener(this);
81 private void createActions() {
82 copyURIAction = new AbstractAction(I18n.getMessage("jsite.project.action.copy-uri")) {
83 public void actionPerformed(ActionEvent actionEvent) {
87 copyURIAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.copy-uri.tooltip"));
88 copyURIAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_U);
89 copyURIAction.setEnabled(false);
92 private void pageInit() {
93 setLayout(new BorderLayout(12, 12));
94 add(createProjectInsertPanel(), BorderLayout.CENTER);
97 private JComponent createProjectInsertPanel() {
98 JComponent projectInsertPanel = new JPanel(new GridBagLayout());
100 requestURITextField = new JTextField();
101 requestURITextField.setEditable(false);
103 startTimeLabel = new JLabel();
105 progressBar = new JProgressBar(0, 1);
106 progressBar.setStringPainted(true);
107 progressBar.setValue(0);
109 projectInsertPanel.add(new JLabel("<html><b>" + I18n.getMessage("jsite.insert.project-information") + "</b></html>"), new GridBagConstraints(0, 0, 2, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
110 projectInsertPanel.add(new JLabel(I18n.getMessage("jsite.insert.request-uri") + ":"), new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 18, 0, 0), 0, 0));
111 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));
112 projectInsertPanel.add(new JLabel(I18n.getMessage("jsite.insert.start-time") + ":"), new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 18, 0, 0), 0, 0));
113 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));
114 projectInsertPanel.add(new JLabel(I18n.getMessage("jsite.insert.progress") + ":"), new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 18, 0, 0), 0, 0));
115 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));
116 projectInsertPanel.add(new JButton(copyURIAction), new GridBagConstraints(0, 4, 2, 1, 0.0, 0.0, GridBagConstraints.LINE_END, GridBagConstraints.NONE, new Insets(6, 18, 0, 0), 0, 0));
118 return projectInsertPanel;
125 public void pageAdded(TWizard wizard) {
126 this.wizard = wizard;
127 wizard.setPreviousEnabled(false);
128 wizard.setNextEnabled(false);
129 wizard.setQuitEnabled(false);
130 copyURIAction.setEnabled(false);
131 progressBar.setValue(0);
132 projectInserter.start();
139 public void setDebug(boolean debug) {
140 projectInserter.setDebug(debug);
145 * The project to set.
147 public void setProject(final Project project) {
148 projectInserter.setProject(project);
149 SwingUtilities.invokeLater(new Runnable() {
152 requestURITextField.setText(project.getFinalRequestURI(0));
157 public void setFreenetInterface(Freenet7Interface freenetInterface) {
158 projectInserter.setFreenetInterface(freenetInterface);
162 // INTERFACE InsertListener
168 public void projectInsertStarted(final Project project) {
169 startTime = System.currentTimeMillis();
170 SwingUtilities.invokeLater(new Runnable() {
173 startTimeLabel.setText(DateFormat.getDateTimeInstance().format(new Date(startTime)));
181 public void projectURIGenerated(Project project, final String uri) {
182 SwingUtilities.invokeLater(new Runnable() {
184 copyURIAction.setEnabled(true);
185 requestURITextField.setText(uri);
193 public void projectInsertProgress(Project project, final int succeeded, final int failed, final int fatal, final int total, final boolean finalized) {
194 SwingUtilities.invokeLater(new Runnable() {
197 progressBar.setMaximum(total);
198 progressBar.setValue(succeeded + failed + fatal);
206 public void projectInsertFinished(Project project, boolean success, Throwable cause) {
208 JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.insert.inserted"), null, JOptionPane.INFORMATION_MESSAGE);
211 JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.insert.insert-failed"), null, JOptionPane.ERROR_MESSAGE);
213 JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.insert.insert-failed-with-cause"), cause.getMessage()), null, JOptionPane.ERROR_MESSAGE);
216 SwingUtilities.invokeLater(new Runnable() {
219 progressBar.setValue(progressBar.getMaximum());
220 wizard.setNextEnabled(true);
221 wizard.setQuitEnabled(true);
230 protected void actionCopyURI() {
231 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
232 clipboard.setContents(new StringSelection(requestURITextField.getText()), this);
236 // INTERFACE ClipboardOwner
242 public void lostOwnership(Clipboard clipboard, Transferable contents) {