version 0.4.9.1:
[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.Font;
24 import java.awt.GridBagConstraints;
25 import java.awt.GridBagLayout;
26 import java.awt.Insets;
27 import java.awt.Toolkit;
28 import java.awt.datatransfer.Clipboard;
29 import java.awt.datatransfer.ClipboardOwner;
30 import java.awt.datatransfer.StringSelection;
31 import java.awt.datatransfer.Transferable;
32 import java.awt.event.ActionEvent;
33 import java.awt.event.KeyEvent;
34 import java.text.DateFormat;
35 import java.text.MessageFormat;
36 import java.util.Date;
37
38 import javax.swing.AbstractAction;
39 import javax.swing.Action;
40 import javax.swing.JButton;
41 import javax.swing.JComponent;
42 import javax.swing.JLabel;
43 import javax.swing.JOptionPane;
44 import javax.swing.JPanel;
45 import javax.swing.JProgressBar;
46 import javax.swing.JTextField;
47 import javax.swing.SwingUtilities;
48
49 import de.todesbaum.jsite.application.Freenet7Interface;
50 import de.todesbaum.jsite.application.InsertListener;
51 import de.todesbaum.jsite.application.Project;
52 import de.todesbaum.jsite.application.ProjectInserter;
53 import de.todesbaum.jsite.i18n.I18n;
54 import de.todesbaum.util.swing.TWizard;
55 import de.todesbaum.util.swing.TWizardPage;
56
57 /**
58  * @author David Roden <droden@gmail.com>
59  * @version $Id$
60  */
61 public class ProjectInsertPage extends TWizardPage implements InsertListener, ClipboardOwner {
62
63         protected TWizard wizard;
64         protected ProjectInserter projectInserter;
65
66         protected Action copyURIAction;
67         protected JTextField requestURITextField;
68         protected JLabel startTimeLabel;
69         protected JProgressBar progressBar;
70         protected long startTime;
71
72         public ProjectInsertPage() {
73                 super();
74                 createActions();
75                 pageInit();
76                 setHeading(I18n.getMessage("jsite.insert.heading"));
77                 setDescription(I18n.getMessage("jsite.insert.description"));
78                 projectInserter = new ProjectInserter();
79                 projectInserter.addInsertListener(this);
80         }
81         
82         private void createActions() {
83                 copyURIAction = new AbstractAction(I18n.getMessage("jsite.project.action.copy-uri")) {
84                         public void actionPerformed(ActionEvent actionEvent) {
85                                 actionCopyURI();
86                         }
87                 };
88                 copyURIAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.copy-uri.tooltip"));
89                 copyURIAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_U);
90                 copyURIAction.setEnabled(false);
91         }
92
93         private void pageInit() {
94                 setLayout(new BorderLayout(12, 12));
95                 add(createProjectInsertPanel(), BorderLayout.CENTER);
96         }
97
98         private JComponent createProjectInsertPanel() {
99                 JComponent projectInsertPanel = new JPanel(new GridBagLayout());
100
101                 requestURITextField = new JTextField();
102                 requestURITextField.setEditable(false);
103
104                 startTimeLabel = new JLabel();
105
106                 progressBar = new JProgressBar(0, 1);
107                 progressBar.setStringPainted(true);
108                 progressBar.setValue(0);
109
110                 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));
111                 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));
112                 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));
113                 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));
114                 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));
115                 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));
116                 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));
117                 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
119                 return projectInsertPanel;
120         }
121
122         /**
123          * {@inheritDoc}
124          */
125         @Override
126         public void pageAdded(TWizard wizard) {
127                 this.wizard = wizard;
128                 wizard.setPreviousEnabled(false);
129                 wizard.setNextEnabled(false);
130                 wizard.setQuitEnabled(false);
131                 copyURIAction.setEnabled(false);
132                 progressBar.setValue(0);
133                 progressBar.setFont(progressBar.getFont().deriveFont(Font.PLAIN));
134                 projectInserter.start();
135         }
136
137         /**
138          * @param debug
139          *            The debug to set.
140          */
141         public void setDebug(boolean debug) {
142                 projectInserter.setDebug(debug);
143         }
144
145         /**
146          * @param project
147          *            The project to set.
148          */
149         public void setProject(final Project project) {
150                 projectInserter.setProject(project);
151                 SwingUtilities.invokeLater(new Runnable() {
152
153                         public void run() {
154                                 requestURITextField.setText(project.getFinalRequestURI(1));
155                         }
156                 });
157         }
158         
159         public void setFreenetInterface(Freenet7Interface freenetInterface) {
160                 projectInserter.setFreenetInterface(freenetInterface);
161         }
162
163         //
164         // INTERFACE InsertListener
165         //
166
167         /**
168          * {@inheritDoc}
169          */
170         public void projectInsertStarted(final Project project) {
171                 startTime = System.currentTimeMillis();
172                 SwingUtilities.invokeLater(new Runnable() {
173
174                         public void run() {
175                                 startTimeLabel.setText(DateFormat.getDateTimeInstance().format(new Date(startTime)));
176                         }
177                 });
178         }
179         
180         /**
181          * {@inheritDoc}
182          */
183         public void projectURIGenerated(Project project, final String uri) {
184                 SwingUtilities.invokeLater(new Runnable() {
185                         public void run() {
186                                 copyURIAction.setEnabled(true);
187                                 requestURITextField.setText(uri);
188                         }
189                 });
190         }
191
192         /**
193          * {@inheritDoc}
194          */
195         public void projectInsertProgress(Project project, final int succeeded, final int failed, final int fatal, final int total, final boolean finalized) {
196                 SwingUtilities.invokeLater(new Runnable() {
197
198                         public void run() {
199                                 progressBar.setMaximum(total);
200                                 progressBar.setValue(succeeded + failed + fatal);
201                                 int progress = (succeeded + failed + fatal) * 100 / total;
202                                 StringBuilder progressString = new StringBuilder();
203                                 progressString.append(progress).append("% (");
204                                 progressString.append(succeeded + failed + fatal).append("/").append(total);
205                                 progressString.append(")");
206                                 progressBar.setString(progressString.toString());
207                                 if (finalized) {
208                                         progressBar.setFont(progressBar.getFont().deriveFont(Font.BOLD));
209                                 }
210                         }
211                 });
212         }
213
214         /**
215          * {@inheritDoc}
216          */
217         public void projectInsertFinished(Project project, boolean success, Throwable cause) {
218                 if (success) {
219                         JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.insert.inserted"), null, JOptionPane.INFORMATION_MESSAGE);
220                 } else {
221                         if (cause == null) {
222                                 JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.insert.insert-failed"), null, JOptionPane.ERROR_MESSAGE);
223                         } else {
224                                 JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.insert.insert-failed-with-cause"), cause.getMessage()), null, JOptionPane.ERROR_MESSAGE);
225                         }
226                 }
227                 SwingUtilities.invokeLater(new Runnable() {
228
229                         public void run() {
230                                 progressBar.setValue(progressBar.getMaximum());
231                                 progressBar.setString("Done");
232                                 wizard.setNextEnabled(true);
233                                 wizard.setQuitEnabled(true);
234                         }
235                 });
236         }
237         
238         //
239         // ACTIONS
240         //
241         
242         protected void actionCopyURI() {
243                 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
244                 clipboard.setContents(new StringSelection(requestURITextField.getText()), this);
245         }
246
247         //
248         // INTERFACE ClipboardOwner
249         //
250         
251         /**
252          * {@inheritDoc}
253          */
254         public void lostOwnership(Clipboard clipboard, Transferable contents) {
255         }
256
257 }