make action methods private and add annotations
[jSite.git] / src / de / todesbaum / jsite / main / Main.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.main;
21
22 import java.awt.event.ActionEvent;
23 import java.awt.event.ActionListener;
24 import java.io.File;
25 import java.io.IOException;
26 import java.text.MessageFormat;
27 import java.util.HashMap;
28 import java.util.Locale;
29 import java.util.Map;
30 import java.util.Set;
31 import java.util.Map.Entry;
32
33 import javax.swing.AbstractAction;
34 import javax.swing.Action;
35 import javax.swing.ButtonGroup;
36 import javax.swing.Icon;
37 import javax.swing.JList;
38 import javax.swing.JMenu;
39 import javax.swing.JMenuBar;
40 import javax.swing.JOptionPane;
41 import javax.swing.JPanel;
42 import javax.swing.JRadioButtonMenuItem;
43 import javax.swing.event.ListSelectionEvent;
44 import javax.swing.event.ListSelectionListener;
45
46 import de.todesbaum.jsite.application.FileOption;
47 import de.todesbaum.jsite.application.Freenet7Interface;
48 import de.todesbaum.jsite.application.Node;
49 import de.todesbaum.jsite.application.Project;
50 import de.todesbaum.jsite.gui.NodeManagerListener;
51 import de.todesbaum.jsite.gui.NodeManagerPage;
52 import de.todesbaum.jsite.gui.ProjectFilesPage;
53 import de.todesbaum.jsite.gui.ProjectInsertPage;
54 import de.todesbaum.jsite.gui.ProjectPage;
55 import de.todesbaum.jsite.i18n.I18n;
56 import de.todesbaum.jsite.i18n.I18nContainer;
57 import de.todesbaum.util.image.IconLoader;
58 import de.todesbaum.util.swing.TWizard;
59 import de.todesbaum.util.swing.TWizardPage;
60 import de.todesbaum.util.swing.WizardListener;
61
62 /**
63  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
64  */
65 public class Main implements ActionListener, ListSelectionListener, WizardListener, NodeManagerListener {
66
67         private static boolean debug = false;
68         private Configuration configuration;
69         private Freenet7Interface freenetInterface = new Freenet7Interface();
70         private Icon jSiteIcon;
71
72         private static enum PageType {
73                 PAGE_NODE_MANAGER, PAGE_PROJECTS, PAGE_PROJECT_FILES, PAGE_INSERT_PROJECT
74         }
75
76         private static final Locale[] SUPPORTED_LOCALES = new Locale[] { Locale.ENGLISH, Locale.GERMAN, Locale.FRENCH, Locale.ITALIAN, new Locale("pl") };
77         private Map<Locale, Action> languageActions = new HashMap<Locale, Action>();
78         private Action manageNodeAction;
79         private Action aboutAction;
80         private TWizard wizard;
81         private JMenu nodeMenu;
82         private Node selectedNode;
83         private final Map<PageType, TWizardPage> pages = new HashMap<PageType, TWizardPage>();
84
85         private Main() {
86                 this(null);
87         }
88
89         private Main(String configFilename) {
90                 if (configFilename != null) {
91                         configuration = new Configuration(configFilename);
92                 } else {
93                         configuration = new Configuration();
94                 }
95                 Locale.setDefault(configuration.getLocale());
96                 I18n.setLocale(configuration.getLocale());
97                 if (!configuration.createLockFile()) {
98                         JOptionPane.showMessageDialog(null, I18n.getMessage("jsite.main.already-running"), null, JOptionPane.ERROR_MESSAGE);
99                         return;
100                 }
101                 wizard = new TWizard();
102                 createActions();
103                 wizard.setJMenuBar(createMenuBar());
104                 wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
105                 wizard.setPreviousEnabled(false);
106                 wizard.setNextEnabled(true);
107                 wizard.addWizardListener(this);
108                 jSiteIcon = IconLoader.loadIcon("/jsite-icon.png");
109                 wizard.setIcon(jSiteIcon);
110
111                 initPages();
112                 showPage(PageType.PAGE_PROJECTS);
113         }
114
115         private void createActions() {
116                 for (final Locale locale : SUPPORTED_LOCALES) {
117                         languageActions.put(locale, new AbstractAction(I18n.getMessage("jsite.menu.language." + locale.getLanguage()), IconLoader.loadIcon("/flag-" + locale.getLanguage() + ".png")) {
118
119                                 @SuppressWarnings("synthetic-access")
120                                 public void actionPerformed(ActionEvent actionEvent) {
121                                         switchLanguage(locale);
122                                 }
123                         });
124                 }
125                 manageNodeAction = new AbstractAction(I18n.getMessage("jsite.menu.nodes.manage-nodes")) {
126
127                         @SuppressWarnings("synthetic-access")
128                         public void actionPerformed(ActionEvent actionEvent) {
129                                 showPage(PageType.PAGE_NODE_MANAGER);
130                                 wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
131                                 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
132                         }
133                 };
134                 aboutAction = new AbstractAction(I18n.getMessage("jsite.menu.help.about")) {
135
136                         @SuppressWarnings("synthetic-access")
137                         public void actionPerformed(ActionEvent e) {
138                                 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.about.message"), Version.getVersion()), null, JOptionPane.INFORMATION_MESSAGE, jSiteIcon);
139                         }
140                 };
141
142                 I18nContainer.getInstance().registerRunnable(new Runnable() {
143
144                         @SuppressWarnings("synthetic-access")
145                         public void run() {
146                                 manageNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.nodes.manage-nodes"));
147                                 aboutAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.help.about"));
148                         }
149                 });
150         }
151
152         private JMenuBar createMenuBar() {
153                 JMenuBar menuBar = new JMenuBar();
154                 final JMenu languageMenu = new JMenu(I18n.getMessage("jsite.menu.languages"));
155                 menuBar.add(languageMenu);
156                 ButtonGroup languageButtonGroup = new ButtonGroup();
157                 for (Locale locale : SUPPORTED_LOCALES) {
158                         Action languageAction = languageActions.get(locale);
159                         JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(languageActions.get(locale));
160                         if (locale.equals(Locale.getDefault())) {
161                                 menuItem.setSelected(true);
162                         }
163                         languageAction.putValue("menuItem", menuItem);
164                         languageButtonGroup.add(menuItem);
165                         languageMenu.add(menuItem);
166                 }
167                 nodeMenu = new JMenu(I18n.getMessage("jsite.menu.nodes"));
168                 menuBar.add(nodeMenu);
169                 selectedNode = configuration.getSelectedNode();
170                 nodesUpdated(configuration.getNodes());
171
172                 /* evil hack to right-align the help menu */
173                 JPanel panel = new JPanel();
174                 panel.setOpaque(false);
175                 menuBar.add(panel);
176
177                 final JMenu helpMenu = new JMenu(I18n.getMessage("jsite.menu.help"));
178                 menuBar.add(helpMenu);
179                 helpMenu.add(aboutAction);
180
181                 I18nContainer.getInstance().registerRunnable(new Runnable() {
182
183                         @SuppressWarnings("synthetic-access")
184                         public void run() {
185                                 languageMenu.setText(I18n.getMessage("jsite.menu.languages"));
186                                 nodeMenu.setText(I18n.getMessage("jsite.menu.nodes"));
187                                 helpMenu.setText(I18n.getMessage("jsite.menu.help"));
188                                 for (Map.Entry<Locale, Action> languageActionEntry : languageActions.entrySet()) {
189                                         languageActionEntry.getValue().putValue(Action.NAME, I18n.getMessage("jsite.menu.language." + languageActionEntry.getKey().getLanguage()));
190                                 }
191                         }
192                 });
193
194                 return menuBar;
195         }
196
197         private void initPages() {
198                 NodeManagerPage nodeManagerPage = new NodeManagerPage(wizard);
199                 nodeManagerPage.setName("page.node-manager");
200                 nodeManagerPage.addNodeManagerListener(this);
201                 nodeManagerPage.setNodes(configuration.getNodes());
202                 pages.put(PageType.PAGE_NODE_MANAGER, nodeManagerPage);
203
204                 ProjectPage projectPage = new ProjectPage(wizard);
205                 projectPage.setName("page.project");
206                 projectPage.setProjects(configuration.getProjects());
207                 projectPage.setFreenetInterface(freenetInterface);
208                 projectPage.addListSelectionListener(this);
209                 pages.put(PageType.PAGE_PROJECTS, projectPage);
210
211                 ProjectFilesPage projectFilesPage = new ProjectFilesPage(wizard);
212                 projectFilesPage.setName("page.project.files");
213                 pages.put(PageType.PAGE_PROJECT_FILES, projectFilesPage);
214
215                 ProjectInsertPage projectInsertPage = new ProjectInsertPage(wizard);
216                 projectInsertPage.setDebug(debug);
217                 projectInsertPage.setName("page.project.insert");
218                 projectInsertPage.setFreenetInterface(freenetInterface);
219                 pages.put(PageType.PAGE_INSERT_PROJECT, projectInsertPage);
220         }
221
222         private void showPage(PageType pageType) {
223                 wizard.setPreviousEnabled(pageType.ordinal() > 0);
224                 wizard.setNextEnabled(pageType.ordinal() < (pages.size() - 1));
225                 wizard.setPage(pages.get(pageType));
226                 wizard.setTitle(pages.get(pageType).getHeading() + " - jSite");
227         }
228
229         private boolean saveConfiguration() {
230                 NodeManagerPage nodeManagerPage = (NodeManagerPage) pages.get(PageType.PAGE_NODE_MANAGER);
231                 configuration.setNodes(nodeManagerPage.getNodes());
232                 if (selectedNode != null) {
233                         configuration.setSelectedNode(selectedNode);
234                 }
235
236                 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
237                 configuration.setProjects(projectPage.getProjects());
238
239                 return configuration.save();
240         }
241
242         private Locale findSupportedLocale(Locale forLocale) {
243                 for (Locale locale : SUPPORTED_LOCALES) {
244                         if (locale.equals(forLocale)) {
245                                 return locale;
246                         }
247                 }
248                 for (Locale locale : SUPPORTED_LOCALES) {
249                         if (locale.getCountry().equals(forLocale.getCountry()) && locale.getLanguage().equals(forLocale.getLanguage())) {
250                                 return locale;
251                         }
252                 }
253                 for (Locale locale : SUPPORTED_LOCALES) {
254                         if (locale.getLanguage().equals(forLocale.getLanguage())) {
255                                 return locale;
256                         }
257                 }
258                 return SUPPORTED_LOCALES[0];
259         }
260
261         //
262         // ACTIONS
263         //
264
265         private void switchLanguage(Locale locale) {
266                 Locale supportedLocale = findSupportedLocale(locale);
267                 Action languageAction = languageActions.get(supportedLocale);
268                 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) languageAction.getValue("menuItem");
269                 menuItem.setSelected(true);
270                 I18n.setLocale(supportedLocale);
271                 for (Runnable i18nRunnable : I18nContainer.getInstance()) {
272                         try {
273                                 i18nRunnable.run();
274                         } catch (Throwable t) {
275                                 /* we probably shouldn't swallow this. */
276                         }
277                 }
278                 wizard.setPage(wizard.getPage());
279                 configuration.setLocale(supportedLocale);
280         }
281
282         //
283         // INTERFACE ListSelectionListener
284         //
285
286         /**
287          * {@inheritDoc}
288          */
289         public void valueChanged(ListSelectionEvent e) {
290                 JList list = (JList) e.getSource();
291                 int selectedRow = list.getSelectedIndex();
292                 wizard.setNextEnabled(selectedRow > -1);
293         }
294
295         //
296         // INTERFACE WizardListener
297         //
298
299         /**
300          * {@inheritDoc}
301          */
302         public void wizardNextPressed(TWizard wizard) {
303                 String pageName = wizard.getPage().getName();
304                 if ("page.node-manager".equals(pageName)) {
305                         showPage(PageType.PAGE_PROJECTS);
306                 } else if ("page.project".equals(pageName)) {
307                         ProjectPage projectPage = (ProjectPage) wizard.getPage();
308                         Project project = projectPage.getSelectedProject();
309                         if ((project.getLocalPath() == null) || (project.getLocalPath().trim().length() == 0)) {
310                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-local-path"), null, JOptionPane.ERROR_MESSAGE);
311                                 return;
312                         }
313                         if ((project.getPath() == null) || (project.getPath().trim().length() == 0)) {
314                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-path"), null, JOptionPane.ERROR_MESSAGE);
315                                 return;
316                         }
317                         ((ProjectFilesPage) pages.get(PageType.PAGE_PROJECT_FILES)).setProject(project);
318                         ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).setProject(project);
319                         showPage(PageType.PAGE_PROJECT_FILES);
320                 } else if ("page.project.files".equals(pageName)) {
321                         ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
322                         Project project = projectPage.getSelectedProject();
323                         if (selectedNode == null) {
324                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
325                                 return;
326                         }
327                         if (project.getIndexFile() == null) {
328                                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.empty-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
329                                         return;
330                                 }
331                         } else {
332                                 File indexFile = new File(project.getLocalPath(), project.getIndexFile());
333                                 if (!indexFile.exists()) {
334                                         JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.index-missing"), null, JOptionPane.ERROR_MESSAGE);
335                                         return;
336                                 }
337                         }
338                         String indexFile = project.getIndexFile();
339                         boolean hasIndexFile = (indexFile != null);
340                         if (hasIndexFile && !project.getFileOption(indexFile).getContainer().equals("")) {
341                                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.container-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
342                                         return;
343                                 }
344                         }
345                         if (hasIndexFile && !project.getFileOption(indexFile).getMimeType().equals("text/html")) {
346                                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.index-not-html"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
347                                         return;
348                                 }
349                         }
350                         Map<String, FileOption> fileOptions = project.getFileOptions();
351                         Set<Entry<String, FileOption>> fileOptionEntries = fileOptions.entrySet();
352                         for (Entry<String, FileOption> fileOptionEntry : fileOptionEntries) {
353                                 FileOption fileOption = fileOptionEntry.getValue();
354                                 if (!fileOption.isInsert() && ((fileOption.getCustomKey().length() == 0) || "CHK@".equals(fileOption.getCustomKey()))) {
355                                         JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.project-files.no-custom-key"), fileOptionEntry.getKey()), null, JOptionPane.ERROR_MESSAGE);
356                                         return;
357                                 }
358                         }
359                         boolean nodeRunning = false;
360                         try {
361                                 nodeRunning = freenetInterface.isNodePresent();
362                         } catch (IOException e) {
363                         }
364                         if (!nodeRunning) {
365                                 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-running"), null, JOptionPane.ERROR_MESSAGE);
366                                 return;
367                         }
368                         configuration.save();
369                         showPage(PageType.PAGE_INSERT_PROJECT);
370                         ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).startInsert();
371                         nodeMenu.setEnabled(false);
372                 } else if ("page.project.insert".equals(pageName)) {
373                         showPage(PageType.PAGE_PROJECTS);
374                         nodeMenu.setEnabled(true);
375                 }
376         }
377
378         /**
379          * {@inheritDoc}
380          */
381         public void wizardPreviousPressed(TWizard wizard) {
382                 String pageName = wizard.getPage().getName();
383                 if ("page.project".equals(pageName)) {
384                         showPage(PageType.PAGE_NODE_MANAGER);
385                 } else if ("page.project.files".equals(pageName)) {
386                         showPage(PageType.PAGE_PROJECTS);
387                 } else if ("page.project.insert".equals(pageName)) {
388                         showPage(PageType.PAGE_PROJECT_FILES);
389                 }
390         }
391
392         /**
393          * {@inheritDoc}
394          */
395         public void wizardQuitPressed(TWizard wizard) {
396                 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.question"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
397                         if (saveConfiguration()) {
398                                 System.exit(0);
399                         }
400                         if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.config-not-saved"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
401                                 System.exit(0);
402                         }
403                 }
404         }
405
406         //
407         // INTERFACE NodeManagerListener
408         //
409
410         /**
411          * {@inheritDoc}
412          */
413         public void nodesUpdated(Node[] nodes) {
414                 nodeMenu.removeAll();
415                 ButtonGroup nodeButtonGroup = new ButtonGroup();
416                 Node newSelectedNode = null;
417                 for (Node node : nodes) {
418                         JRadioButtonMenuItem nodeMenuItem = new JRadioButtonMenuItem(node.getName());
419                         nodeMenuItem.putClientProperty("Node", node);
420                         nodeMenuItem.addActionListener(this);
421                         nodeButtonGroup.add(nodeMenuItem);
422                         if (node.equals(selectedNode)) {
423                                 newSelectedNode = node;
424                                 nodeMenuItem.setSelected(true);
425                         }
426                         nodeMenu.add(nodeMenuItem);
427                 }
428                 nodeMenu.addSeparator();
429                 nodeMenu.add(manageNodeAction);
430                 selectedNode = newSelectedNode;
431                 freenetInterface.setNode(selectedNode);
432         }
433
434         /**
435          * {@inheritDoc}
436          */
437         public void actionPerformed(ActionEvent e) {
438                 Object source = e.getSource();
439                 if (source instanceof JRadioButtonMenuItem) {
440                         JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) source;
441                         Node node = (Node) menuItem.getClientProperty("Node");
442                         selectedNode = node;
443                         freenetInterface.setNode(selectedNode);
444                 }
445         }
446
447         //
448         // MAIN METHOD
449         //
450         public static void main(String[] args) {
451                 String configFilename = null;
452                 boolean nextIsConfigFilename = false;
453                 for (String argument : args) {
454                         if (nextIsConfigFilename) {
455                                 configFilename = argument;
456                                 nextIsConfigFilename = false;
457                         }
458                         if ("--help".equals(argument)) {
459                                 printHelp();
460                                 return;
461                         } else if ("--debug".equals(argument)) {
462                                 debug = true;
463                         } else if ("--config-file".equals(argument)) {
464                                 nextIsConfigFilename = true;
465                         }
466                 }
467                 if (nextIsConfigFilename) {
468                         System.out.println("--config-file needs parameter!");
469                         return;
470                 }
471                 new Main(configFilename);
472         }
473
474         private static void printHelp() {
475                 System.out.println("--help\tshows this cruft");
476                 System.out.println("--debug\tenables some debug output");
477                 System.out.println("--config-file <file>\tuse specified configuration file");
478         }
479
480 }