jSite: First commit : verion 4.0 (written by Bombe)
[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.EditionProject;
39 import de.todesbaum.jsite.application.Freenet7Interface;
40 import de.todesbaum.jsite.application.InsertListener;
41 import de.todesbaum.jsite.application.Project;
42 import de.todesbaum.jsite.application.ProjectInserter;
43 import de.todesbaum.jsite.i18n.I18n;
44 import de.todesbaum.util.swing.TWizard;
45 import de.todesbaum.util.swing.TWizardPage;
46
47 /**
48  * @author David Roden <droden@gmail.com>
49  * @version $Id: ProjectInsertPage.java 408 2006-03-29 09:31:10Z bombe $
50  */
51 public class ProjectInsertPage extends TWizardPage implements InsertListener {
52
53         protected TWizard wizard;
54         protected ProjectInserter projectInserter;
55
56         protected JTextField requestURITextField;
57         protected JLabel startTimeLabel;
58         protected JProgressBar progressBar;
59         protected long startTime;
60
61         public ProjectInsertPage() {
62                 super();
63                 pageInit();
64                 setHeading(I18n.getMessage("jsite.insert.heading"));
65                 setDescription(I18n.getMessage("jsite.insert.description"));
66                 projectInserter = new ProjectInserter();
67                 projectInserter.addInsertListener(this);
68         }
69
70         private void pageInit() {
71                 setLayout(new BorderLayout(12, 12));
72                 add(createProjectInsertPanel(), BorderLayout.CENTER);
73         }
74
75         private JComponent createProjectInsertPanel() {
76                 JComponent projectInsertPanel = new JPanel(new GridBagLayout());
77
78                 requestURITextField = new JTextField();
79                 requestURITextField.setEditable(false);
80                 requestURITextField.setBackground(getBackground());
81                 requestURITextField.setBorder(null);
82
83                 startTimeLabel = new JLabel();
84
85                 progressBar = new JProgressBar(0, 1);
86                 progressBar.setStringPainted(true);
87                 progressBar.setValue(0);
88
89                 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));
90                 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));
91                 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));
92                 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));
93                 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));
94                 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));
95                 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));
96
97                 return projectInsertPanel;
98         }
99
100         /**
101          * {@inheritDoc}
102          */
103         @Override
104         public void pageAdded(TWizard wizard) {
105                 this.wizard = wizard;
106                 wizard.setPreviousEnabled(false);
107                 wizard.setNextEnabled(false);
108                 wizard.setQuitEnabled(false);
109                 progressBar.setValue(0);
110                 projectInserter.start();
111         }
112
113         /**
114          * @param debug
115          *            The debug to set.
116          */
117         public void setDebug(boolean debug) {
118                 projectInserter.setDebug(debug);
119         }
120
121         /**
122          * @param project
123          *            The project to set.
124          */
125         public void setProject(final Project project) {
126                 projectInserter.setProject(project);
127                 SwingUtilities.invokeLater(new Runnable() {
128
129                         public void run() {
130                                 StringBuffer uriBuffer = new StringBuffer();
131                                 uriBuffer.append(project.getRequestURI());
132                                 uriBuffer.append(project.getPath());
133                                 if (project instanceof EditionProject) {
134                                         uriBuffer.append('-').append(((EditionProject) project).getEdition());
135                                 }
136                                 uriBuffer.append('/');
137                                 requestURITextField.setText(uriBuffer.toString());
138                         }
139                 });
140         }
141
142         public void setFreenetInterface(Freenet7Interface freenetInterface) {
143                 projectInserter.setFreenetInterface(freenetInterface);
144         }
145
146         //
147         // INTERFACE InsertListener
148         //
149
150         /**
151          * {@inheritDoc}
152          */
153         public void projectInsertStarted(final Project project) {
154                 startTime = System.currentTimeMillis();
155                 SwingUtilities.invokeLater(new Runnable() {
156
157                         public void run() {
158                                 startTimeLabel.setText(DateFormat.getDateTimeInstance().format(new Date(startTime)));
159                         }
160                 });
161         }
162
163         /**
164          * {@inheritDoc}
165          */
166         public void projectInsertProgress(Project project, final int succeeded, final int failed, final int fatal, final int total, final boolean finalized) {
167                 SwingUtilities.invokeLater(new Runnable() {
168
169                         public void run() {
170                                 progressBar.setMaximum(total);
171                                 progressBar.setValue(succeeded + failed + fatal);
172                         }
173                 });
174         }
175
176         /**
177          * {@inheritDoc}
178          */
179         public void projectInsertFinished(Project project, boolean success, Throwable cause) {
180                 if (success) {
181                         JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.insert.inserted"), null, JOptionPane.INFORMATION_MESSAGE);
182                 } else {
183                         if (cause == null) {
184                                 JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.insert.insert-failed"), null, JOptionPane.ERROR_MESSAGE);
185                         } else {
186                                 JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.insert.insert-failed-with-cause"), cause.getMessage()), null, JOptionPane.ERROR_MESSAGE);
187                         }
188                 }
189                 SwingUtilities.invokeLater(new Runnable() {
190
191                         public void run() {
192                                 wizard.setNextEnabled(true);
193                                 wizard.setQuitEnabled(true);
194                         }
195                 });
196         }
197
198 }