version 0.4.8
[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.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;
36
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;
47
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;
55
56 /**
57  * @author David Roden <droden@gmail.com>
58  * @version $Id$
59  */
60 public class ProjectInsertPage extends TWizardPage implements InsertListener, ClipboardOwner {
61
62         protected TWizard wizard;
63         protected ProjectInserter projectInserter;
64
65         protected Action copyURIAction;
66         protected JTextField requestURITextField;
67         protected JLabel startTimeLabel;
68         protected JProgressBar progressBar;
69         protected long startTime;
70
71         public ProjectInsertPage() {
72                 super();
73                 createActions();
74                 pageInit();
75                 setHeading(I18n.getMessage("jsite.insert.heading"));
76                 setDescription(I18n.getMessage("jsite.insert.description"));
77                 projectInserter = new ProjectInserter();
78                 projectInserter.addInsertListener(this);
79         }
80         
81         private void createActions() {
82                 copyURIAction = new AbstractAction(I18n.getMessage("jsite.project.action.copy-uri")) {
83                         public void actionPerformed(ActionEvent actionEvent) {
84                                 actionCopyURI();
85                         }
86                 };
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);
90         }
91
92         private void pageInit() {
93                 setLayout(new BorderLayout(12, 12));
94                 add(createProjectInsertPanel(), BorderLayout.CENTER);
95         }
96
97         private JComponent createProjectInsertPanel() {
98                 JComponent projectInsertPanel = new JPanel(new GridBagLayout());
99
100                 requestURITextField = new JTextField();
101                 requestURITextField.setEditable(false);
102
103                 startTimeLabel = new JLabel();
104
105                 progressBar = new JProgressBar(0, 1);
106                 progressBar.setStringPainted(true);
107                 progressBar.setValue(0);
108
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));
117
118                 return projectInsertPanel;
119         }
120
121         /**
122          * {@inheritDoc}
123          */
124         @Override
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();
133         }
134
135         /**
136          * @param debug
137          *            The debug to set.
138          */
139         public void setDebug(boolean debug) {
140                 projectInserter.setDebug(debug);
141         }
142
143         /**
144          * @param project
145          *            The project to set.
146          */
147         public void setProject(final Project project) {
148                 projectInserter.setProject(project);
149                 SwingUtilities.invokeLater(new Runnable() {
150
151                         public void run() {
152                                 requestURITextField.setText(project.getFinalRequestURI(0));
153                         }
154                 });
155         }
156         
157         public void setFreenetInterface(Freenet7Interface freenetInterface) {
158                 projectInserter.setFreenetInterface(freenetInterface);
159         }
160
161         //
162         // INTERFACE InsertListener
163         //
164
165         /**
166          * {@inheritDoc}
167          */
168         public void projectInsertStarted(final Project project) {
169                 startTime = System.currentTimeMillis();
170                 SwingUtilities.invokeLater(new Runnable() {
171
172                         public void run() {
173                                 startTimeLabel.setText(DateFormat.getDateTimeInstance().format(new Date(startTime)));
174                         }
175                 });
176         }
177         
178         /**
179          * {@inheritDoc}
180          */
181         public void projectURIGenerated(Project project, final String uri) {
182                 SwingUtilities.invokeLater(new Runnable() {
183                         public void run() {
184                                 copyURIAction.setEnabled(true);
185                                 requestURITextField.setText(uri);
186                         }
187                 });
188         }
189
190         /**
191          * {@inheritDoc}
192          */
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() {
195
196                         public void run() {
197                                 progressBar.setMaximum(total);
198                                 progressBar.setValue(succeeded + failed + fatal);
199                         }
200                 });
201         }
202
203         /**
204          * {@inheritDoc}
205          */
206         public void projectInsertFinished(Project project, boolean success, Throwable cause) {
207                 if (success) {
208                         JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.insert.inserted"), null, JOptionPane.INFORMATION_MESSAGE);
209                 } else {
210                         if (cause == null) {
211                                 JOptionPane.showMessageDialog(this, I18n.getMessage("jsite.insert.insert-failed"), null, JOptionPane.ERROR_MESSAGE);
212                         } else {
213                                 JOptionPane.showMessageDialog(this, MessageFormat.format(I18n.getMessage("jsite.insert.insert-failed-with-cause"), cause.getMessage()), null, JOptionPane.ERROR_MESSAGE);
214                         }
215                 }
216                 SwingUtilities.invokeLater(new Runnable() {
217
218                         public void run() {
219                                 progressBar.setValue(progressBar.getMaximum());
220                                 wizard.setNextEnabled(true);
221                                 wizard.setQuitEnabled(true);
222                         }
223                 });
224         }
225         
226         //
227         // ACTIONS
228         //
229         
230         protected void actionCopyURI() {
231                 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
232                 clipboard.setContents(new StringSelection(requestURITextField.getText()), this);
233         }
234
235         //
236         // INTERFACE ClipboardOwner
237         //
238         
239         /**
240          * {@inheritDoc}
241          */
242         public void lostOwnership(Clipboard clipboard, Transferable contents) {
243         }
244
245 }