version 0.5: show usk keys only, update usk on insert completion
[jSite.git] / src / de / todesbaum / jsite / gui / ProjectInsertPage.java
1 /*
2  * jSite - a tool for uploading websites into Freenet
3  * Copyright (C) 2006 David Roden
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 package de.todesbaum.jsite.gui;
21
22 import java.awt.BorderLayout;
23 import java.awt.GridBagConstraints;
24 import java.awt.GridBagLayout;
25 import java.awt.Insets;
26 import java.text.DateFormat;
27 import java.text.MessageFormat;
28 import java.util.Date;
29
30 import javax.swing.JComponent;
31 import javax.swing.JLabel;
32 import javax.swing.JOptionPane;
33 import javax.swing.JPanel;
34 import javax.swing.JProgressBar;
35 import javax.swing.JTextField;
36 import javax.swing.SwingUtilities;
37
38 import de.todesbaum.jsite.application.Freenet7Interface;
39 import de.todesbaum.jsite.application.InsertListener;
40 import de.todesbaum.jsite.application.Project;
41 import de.todesbaum.jsite.application.ProjectInserter;
42 import de.todesbaum.jsite.i18n.I18n;
43 import de.todesbaum.util.swing.TWizard;
44 import de.todesbaum.util.swing.TWizardPage;
45
46 /**
47  * @author David Roden <droden@gmail.com>
48  * @version $Id$
49  */
50 public class ProjectInsertPage extends TWizardPage implements InsertListener {
51
52         protected TWizard wizard;
53         protected ProjectInserter projectInserter;
54
55         protected JTextField requestURITextField;
56         protected JLabel startTimeLabel;
57         protected JProgressBar progressBar;
58         protected long startTime;
59
60         public ProjectInsertPage() {
61                 super();
62                 pageInit();
63                 setHeading(I18n.getMessage("jsite.insert.heading"));
64                 setDescription(I18n.getMessage("jsite.insert.description"));
65                 projectInserter = new ProjectInserter();
66                 projectInserter.addInsertListener(this);
67         }
68
69         private void pageInit() {
70                 setLayout(new BorderLayout(12, 12));
71                 add(createProjectInsertPanel(), BorderLayout.CENTER);
72         }
73
74         private JComponent createProjectInsertPanel() {
75                 JComponent projectInsertPanel = new JPanel(new GridBagLayout());
76
77                 requestURITextField = new JTextField();
78                 requestURITextField.setEditable(false);
79
80                 startTimeLabel = new JLabel();
81
82                 progressBar = new JProgressBar(0, 1);
83                 progressBar.setStringPainted(true);
84                 progressBar.setValue(0);
85
86                 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));
87                 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));
88                 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));
89                 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));
90                 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));
91                 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));
92                 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));
93
94                 return projectInsertPanel;
95         }
96
97         /**
98          * {@inheritDoc}
99          */
100         @Override
101         public void pageAdded(TWizard wizard) {
102                 this.wizard = wizard;
103                 wizard.setPreviousEnabled(false);
104                 wizard.setNextEnabled(false);
105                 wizard.setQuitEnabled(false);
106                 progressBar.setValue(0);
107                 projectInserter.start();
108         }
109
110         /**
111          * @param debug
112          *            The debug to set.
113          */
114         public void setDebug(boolean debug) {
115                 projectInserter.setDebug(debug);
116         }
117
118         /**
119          * @param project
120          *            The project to set.
121          */
122         public void setProject(final Project project) {
123                 projectInserter.setProject(project);
124                 SwingUtilities.invokeLater(new Runnable() {
125
126                         public void run() {
127                                 requestURITextField.setText(project.getFinalRequestURI(0));
128                         }
129                 });
130         }
131         
132         public void setFreenetInterface(Freenet7Interface freenetInterface) {
133                 projectInserter.setFreenetInterface(freenetInterface);
134         }
135
136         //
137         // INTERFACE InsertListener
138         //
139
140         /**
141          * {@inheritDoc}
142          */
143         public void projectInsertStarted(final Project project) {
144                 startTime = System.currentTimeMillis();
145                 SwingUtilities.invokeLater(new Runnable() {
146
147                         public void run() {
148                                 startTimeLabel.setText(DateFormat.getDateTimeInstance().format(new Date(startTime)));
149                         }
150                 });
151         }
152         
153         /**
154          * {@inheritDoc}
155          */
156         public void projectURIGenerated(Project project, final String uri) {
157                 SwingUtilities.invokeLater(new Runnable() {
158                         public void run() {
159                                 requestURITextField.setText(uri);
160                         }
161                 });
162         }
163
164         /**
165          * {@inheritDoc}
166          */
167         public void projectInsertProgress(Project project, final int succeeded, final int failed, final int fatal, final int total, final boolean finalized) {
168                 SwingUtilities.invokeLater(new Runnable() {
169
170                         public void run() {
171                                 progressBar.setMaximum(total);
172                                 progressBar.setValue(succeeded + failed + fatal);
173                         }
174                 });
175         }
176
177         /**
178          * {@inheritDoc}
179          */
180         public void projectInsertFinished(Project project, boolean success, Throwable cause) {
181                 if (success) {
182                         JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.insert.inserted"), null, JOptionPane.INFORMATION_MESSAGE);
183                 } else {
184                         if (cause == null) {
185                                 JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.insert.insert-failed"), null, JOptionPane.ERROR_MESSAGE);
186                         } else {
187                                 JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.insert.insert-failed-with-cause"), cause.getMessage()), null, JOptionPane.ERROR_MESSAGE);
188                         }
189                 }
190                 SwingUtilities.invokeLater(new Runnable() {
191
192                         public void run() {
193                                 wizard.setNextEnabled(true);
194                                 wizard.setQuitEnabled(true);
195                         }
196                 });
197         }
198
199 }