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