version 0.4.9.7
[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.jsite.i18n.I18nContainer;
55 import de.todesbaum.util.swing.TWizard;
56 import de.todesbaum.util.swing.TWizardPage;
57
58 /**
59  * @author David Roden <droden@gmail.com>
60  * @version $Id$
61  */
62 public class ProjectInsertPage extends TWizardPage implements InsertListener, ClipboardOwner {
63
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 = 0;
71
72         public ProjectInsertPage(final TWizard wizard) {
73                 super(wizard);
74                 createActions();
75                 pageInit();
76                 setHeading(I18n.getMessage("jsite.insert.heading"));
77                 setDescription(I18n.getMessage("jsite.insert.description"));
78                 I18nContainer.getInstance().registerRunnable(new Runnable() {
79
80                         public void run() {
81                                 setHeading(I18n.getMessage("jsite.insert.heading"));
82                                 setDescription(I18n.getMessage("jsite.insert.description"));
83                         }
84                 });
85                 projectInserter = new ProjectInserter();
86                 projectInserter.addInsertListener(this);
87         }
88
89         private void createActions() {
90                 copyURIAction = new AbstractAction(I18n.getMessage("jsite.project.action.copy-uri")) {
91                         public void actionPerformed(ActionEvent actionEvent) {
92                                 actionCopyURI();
93                         }
94                 };
95                 copyURIAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.copy-uri.tooltip"));
96                 copyURIAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_U);
97                 copyURIAction.setEnabled(false);
98
99                 I18nContainer.getInstance().registerRunnable(new Runnable() {
100
101                         public void run() {
102                                 copyURIAction.putValue(Action.NAME, I18n.getMessage("jsite.project.action.copy-uri"));
103                                 copyURIAction.putValue(Action.SHORT_DESCRIPTION, I18n.getMessage("jsite.project.action.copy-uri.tooltip"));
104                         }
105                 });
106         }
107
108         private void pageInit() {
109                 setLayout(new BorderLayout(12, 12));
110                 add(createProjectInsertPanel(), BorderLayout.CENTER);
111         }
112
113         private JComponent createProjectInsertPanel() {
114                 JComponent projectInsertPanel = new JPanel(new GridBagLayout());
115
116                 requestURITextField = new JTextField();
117                 requestURITextField.setEditable(false);
118
119                 startTimeLabel = new JLabel();
120
121                 progressBar = new JProgressBar(0, 1);
122                 progressBar.setStringPainted(true);
123                 progressBar.setValue(0);
124
125                 final JLabel projectInformationLabel = new JLabel("<html><b>" + I18n.getMessage("jsite.insert.project-information") + "</b></html>");
126                 projectInsertPanel.add(projectInformationLabel, new GridBagConstraints(0, 0, 2, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
127                 final JLabel requestURILabel = new JLabel(I18n.getMessage("jsite.insert.request-uri") + ":");
128                 projectInsertPanel.add(requestURILabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 18, 0, 0), 0, 0));
129                 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));
130                 final JLabel startTimeLeftLabel = new JLabel(I18n.getMessage("jsite.insert.start-time") + ":");
131                 projectInsertPanel.add(startTimeLeftLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 18, 0, 0), 0, 0));
132                 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));
133                 final JLabel progressLabel = new JLabel(I18n.getMessage("jsite.insert.progress") + ":");
134                 projectInsertPanel.add(progressLabel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, new Insets(6, 18, 0, 0), 0, 0));
135                 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));
136                 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));
137
138                 I18nContainer.getInstance().registerRunnable(new Runnable() {
139
140                         public void run() {
141                                 projectInformationLabel.setText("<html><b>" + I18n.getMessage("jsite.insert.project-information") + "</b></html>");
142                                 requestURILabel.setText(I18n.getMessage("jsite.insert.request-uri") + ":");
143                                 startTimeLeftLabel.setText(I18n.getMessage("jsite.insert.start-time") + ":");
144                                 if (startTime != 0) {
145                                         startTimeLabel.setText(DateFormat.getDateTimeInstance().format(new Date(startTime)));
146                                 } else {
147                                         startTimeLabel.setText("");
148                                 }
149                                 progressLabel.setText(I18n.getMessage("jsite.insert.progress") + ":");
150                         }
151                 });
152
153                 return projectInsertPanel;
154         }
155
156         /**
157          * {@inheritDoc}
158          */
159         @Override
160         public void pageAdded(TWizard wizard) {
161                 this.wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
162                 this.wizard.setPreviousEnabled(false);
163                 this.wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
164                 this.wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
165         }
166
167         public void startInsert() {
168                 wizard.setNextEnabled(false);
169                 copyURIAction.setEnabled(false);
170                 progressBar.setValue(0);
171                 progressBar.setString(I18n.getMessage("jsite.insert.starting"));
172                 progressBar.setFont(progressBar.getFont().deriveFont(Font.PLAIN));
173                 projectInserter.start();
174         }
175
176         /**
177          * @param debug
178          *            The debug to set.
179          */
180         public void setDebug(boolean debug) {
181                 projectInserter.setDebug(debug);
182         }
183
184         /**
185          * @param project
186          *            The project to set.
187          */
188         public void setProject(final Project project) {
189                 projectInserter.setProject(project);
190                 SwingUtilities.invokeLater(new Runnable() {
191
192                         public void run() {
193                                 requestURITextField.setText(project.getFinalRequestURI(1));
194                         }
195                 });
196         }
197
198         public void setFreenetInterface(Freenet7Interface freenetInterface) {
199                 projectInserter.setFreenetInterface(freenetInterface);
200         }
201
202         //
203         // INTERFACE InsertListener
204         //
205
206         /**
207          * {@inheritDoc}
208          */
209         public void projectInsertStarted(final Project project) {
210                 startTime = System.currentTimeMillis();
211                 SwingUtilities.invokeLater(new Runnable() {
212
213                         public void run() {
214                                 startTimeLabel.setText(DateFormat.getDateTimeInstance().format(new Date(startTime)));
215                         }
216                 });
217         }
218
219         /**
220          * {@inheritDoc}
221          */
222         public void projectURIGenerated(Project project, final String uri) {
223                 SwingUtilities.invokeLater(new Runnable() {
224                         public void run() {
225                                 copyURIAction.setEnabled(true);
226                                 requestURITextField.setText(uri);
227                         }
228                 });
229         }
230
231         /**
232          * {@inheritDoc}
233          */
234         public void projectInsertProgress(Project project, final int succeeded, final int failed, final int fatal, final int total, final boolean finalized) {
235                 SwingUtilities.invokeLater(new Runnable() {
236
237                         public void run() {
238                                 progressBar.setMaximum(total);
239                                 progressBar.setValue(succeeded + failed + fatal);
240                                 int progress = (succeeded + failed + fatal) * 100 / total;
241                                 StringBuilder progressString = new StringBuilder();
242                                 progressString.append(progress).append("% (");
243                                 progressString.append(succeeded + failed + fatal).append("/").append(total);
244                                 progressString.append(")");
245                                 progressBar.setString(progressString.toString());
246                                 if (finalized) {
247                                         progressBar.setFont(progressBar.getFont().deriveFont(Font.BOLD));
248                                 }
249                         }
250                 });
251         }
252
253         /**
254          * {@inheritDoc}
255          */
256         public void projectInsertFinished(Project project, boolean success, Throwable cause) {
257                 if (success) {
258                         JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.insert.inserted"), null, JOptionPane.INFORMATION_MESSAGE);
259                 } else {
260                         if (cause == null) {
261                                 JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.insert.insert-failed"), null, JOptionPane.ERROR_MESSAGE);
262                         } else {
263                                 JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.insert.insert-failed-with-cause"), cause.getMessage()), null, JOptionPane.ERROR_MESSAGE);
264                         }
265                 }
266                 SwingUtilities.invokeLater(new Runnable() {
267
268                         public void run() {
269                                 progressBar.setValue(progressBar.getMaximum());
270                                 progressBar.setString(I18n.getMessage("jsite.insert.done"));
271                                 wizard.setNextEnabled(true);
272                                 wizard.setQuitEnabled(true);
273                         }
274                 });
275         }
276
277         //
278         // ACTIONS
279         //
280
281         protected void actionCopyURI() {
282                 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
283                 clipboard.setContents(new StringSelection(requestURITextField.getText()), this);
284         }
285
286         //
287         // INTERFACE ClipboardOwner
288         //
289
290         /**
291          * {@inheritDoc}
292          */
293         public void lostOwnership(Clipboard clipboard, Transferable contents) {
294         }
295
296 }