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