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