2 * jSite - a tool for uploading websites into Freenet
3 * Copyright (C) 2006 David Roden
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.
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.
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.
20 package de.todesbaum.jsite.main;
22 import java.awt.event.ActionEvent;
23 import java.awt.event.ActionListener;
25 import java.io.IOException;
26 import java.text.MessageFormat;
27 import java.util.Date;
28 import java.util.HashMap;
29 import java.util.Locale;
32 import java.util.Map.Entry;
33 import java.util.logging.ConsoleHandler;
34 import java.util.logging.Handler;
35 import java.util.logging.Level;
36 import java.util.logging.Logger;
38 import javax.swing.AbstractAction;
39 import javax.swing.Action;
40 import javax.swing.ButtonGroup;
41 import javax.swing.Icon;
42 import javax.swing.JList;
43 import javax.swing.JMenu;
44 import javax.swing.JMenuBar;
45 import javax.swing.JOptionPane;
46 import javax.swing.JPanel;
47 import javax.swing.JRadioButtonMenuItem;
48 import javax.swing.event.ListSelectionEvent;
49 import javax.swing.event.ListSelectionListener;
51 import de.todesbaum.jsite.application.FileOption;
52 import de.todesbaum.jsite.application.Freenet7Interface;
53 import de.todesbaum.jsite.application.Node;
54 import de.todesbaum.jsite.application.Project;
55 import de.todesbaum.jsite.application.UpdateChecker;
56 import de.todesbaum.jsite.application.UpdateListener;
57 import de.todesbaum.jsite.gui.NodeManagerListener;
58 import de.todesbaum.jsite.gui.NodeManagerPage;
59 import de.todesbaum.jsite.gui.ProjectFilesPage;
60 import de.todesbaum.jsite.gui.ProjectInsertPage;
61 import de.todesbaum.jsite.gui.ProjectPage;
62 import de.todesbaum.jsite.i18n.I18n;
63 import de.todesbaum.jsite.i18n.I18nContainer;
64 import de.todesbaum.util.image.IconLoader;
65 import de.todesbaum.util.swing.TWizard;
66 import de.todesbaum.util.swing.TWizardPage;
67 import de.todesbaum.util.swing.WizardListener;
70 * The main class that ties together everything.
72 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
74 public class Main implements ActionListener, ListSelectionListener, WizardListener, NodeManagerListener, UpdateListener {
77 private static final Version VERSION = new Version(0, 7, 1);
79 /** The configuration. */
80 private Configuration configuration;
82 /** The freenet interface. */
83 private Freenet7Interface freenetInterface = new Freenet7Interface();
85 /** The update checker. */
86 private final UpdateChecker updateChecker;
88 /** The jSite icon. */
89 private Icon jSiteIcon;
92 * Enumeration for all possible pages.
94 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
96 private static enum PageType {
98 /** The node manager page. */
101 /** The project page. */
104 /** The project files page. */
107 /** The project insert page. */
112 /** The supported locales. */
113 private static final Locale[] SUPPORTED_LOCALES = new Locale[] { Locale.ENGLISH, Locale.GERMAN, Locale.FRENCH, Locale.ITALIAN, new Locale("pl") };
115 /** The actions that switch the language. */
116 private Map<Locale, Action> languageActions = new HashMap<Locale, Action>();
118 /** The “manage nodes” action. */
119 private Action manageNodeAction;
121 /** The “check for updates” action. */
122 private Action checkForUpdatesAction;
124 /** The “about jSite” action. */
125 private Action aboutAction;
128 private TWizard wizard;
130 /** The node menu. */
131 private JMenu nodeMenu;
133 /** The currently selected node. */
134 private Node selectedNode;
136 /** Mapping from page type to page. */
137 private final Map<PageType, TWizardPage> pages = new HashMap<PageType, TWizardPage>();
140 * Creates a new core with the default configuration file.
147 * Creates a new core with the given configuration from the given file.
149 * @param configFilename
150 * The name of the configuration file
152 private Main(String configFilename) {
153 if (configFilename != null) {
154 configuration = new Configuration(configFilename);
156 configuration = new Configuration();
158 Locale.setDefault(configuration.getLocale());
159 I18n.setLocale(configuration.getLocale());
160 if (!configuration.createLockFile()) {
161 int option = JOptionPane.showOptionDialog(null, I18n.getMessage("jsite.main.already-running"), "", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new Object[] { I18n.getMessage("jsite.main.already-running.override"), I18n.getMessage("jsite.wizard.quit") }, I18n.getMessage("jsite.wizard.quit"));
163 throw new IllegalStateException("Lockfile override not active, refusing start.");
165 configuration.removeLockfileOnExit();
167 wizard = new TWizard();
169 wizard.setJMenuBar(createMenuBar());
170 wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
171 wizard.setPreviousEnabled(false);
172 wizard.setNextEnabled(true);
173 wizard.addWizardListener(this);
174 jSiteIcon = IconLoader.loadIcon("/jsite-icon.png");
175 wizard.setIcon(jSiteIcon);
177 updateChecker = new UpdateChecker(freenetInterface);
178 updateChecker.addUpdateListener(this);
179 updateChecker.start();
182 showPage(PageType.PAGE_PROJECTS);
186 * Creates all actions.
188 private void createActions() {
189 for (final Locale locale : SUPPORTED_LOCALES) {
190 languageActions.put(locale, new AbstractAction(I18n.getMessage("jsite.menu.language." + locale.getLanguage()), IconLoader.loadIcon("/flag-" + locale.getLanguage() + ".png")) {
192 @SuppressWarnings("synthetic-access")
193 public void actionPerformed(ActionEvent actionEvent) {
194 switchLanguage(locale);
198 manageNodeAction = new AbstractAction(I18n.getMessage("jsite.menu.nodes.manage-nodes")) {
200 @SuppressWarnings("synthetic-access")
201 public void actionPerformed(ActionEvent actionEvent) {
202 showPage(PageType.PAGE_NODE_MANAGER);
203 wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
204 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
207 checkForUpdatesAction = new AbstractAction(I18n.getMessage("jsite.menu.help.check-for-updates")) {
212 @SuppressWarnings("synthetic-access")
213 public void actionPerformed(ActionEvent actionEvent) {
217 aboutAction = new AbstractAction(I18n.getMessage("jsite.menu.help.about")) {
219 @SuppressWarnings("synthetic-access")
220 public void actionPerformed(ActionEvent e) {
221 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.about.message"), getVersion().toString()), null, JOptionPane.INFORMATION_MESSAGE, jSiteIcon);
225 I18nContainer.getInstance().registerRunnable(new Runnable() {
227 @SuppressWarnings("synthetic-access")
229 manageNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.nodes.manage-nodes"));
230 aboutAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.help.about"));
236 * Creates the menu bar.
238 * @return The menu bar
240 private JMenuBar createMenuBar() {
241 JMenuBar menuBar = new JMenuBar();
242 final JMenu languageMenu = new JMenu(I18n.getMessage("jsite.menu.languages"));
243 menuBar.add(languageMenu);
244 ButtonGroup languageButtonGroup = new ButtonGroup();
245 for (Locale locale : SUPPORTED_LOCALES) {
246 Action languageAction = languageActions.get(locale);
247 JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(languageActions.get(locale));
248 if (locale.equals(Locale.getDefault())) {
249 menuItem.setSelected(true);
251 languageAction.putValue("menuItem", menuItem);
252 languageButtonGroup.add(menuItem);
253 languageMenu.add(menuItem);
255 nodeMenu = new JMenu(I18n.getMessage("jsite.menu.nodes"));
256 menuBar.add(nodeMenu);
257 selectedNode = configuration.getSelectedNode();
258 nodesUpdated(configuration.getNodes());
260 /* evil hack to right-align the help menu */
261 JPanel panel = new JPanel();
262 panel.setOpaque(false);
265 final JMenu helpMenu = new JMenu(I18n.getMessage("jsite.menu.help"));
266 menuBar.add(helpMenu);
267 helpMenu.add(checkForUpdatesAction);
268 helpMenu.add(aboutAction);
270 I18nContainer.getInstance().registerRunnable(new Runnable() {
272 @SuppressWarnings("synthetic-access")
274 languageMenu.setText(I18n.getMessage("jsite.menu.languages"));
275 nodeMenu.setText(I18n.getMessage("jsite.menu.nodes"));
276 helpMenu.setText(I18n.getMessage("jsite.menu.help"));
277 for (Map.Entry<Locale, Action> languageActionEntry : languageActions.entrySet()) {
278 languageActionEntry.getValue().putValue(Action.NAME, I18n.getMessage("jsite.menu.language." + languageActionEntry.getKey().getLanguage()));
287 * Initializes all pages.
289 private void initPages() {
290 NodeManagerPage nodeManagerPage = new NodeManagerPage(wizard);
291 nodeManagerPage.setName("page.node-manager");
292 nodeManagerPage.addNodeManagerListener(this);
293 nodeManagerPage.setNodes(configuration.getNodes());
294 pages.put(PageType.PAGE_NODE_MANAGER, nodeManagerPage);
296 ProjectPage projectPage = new ProjectPage(wizard);
297 projectPage.setName("page.project");
298 projectPage.setProjects(configuration.getProjects());
299 projectPage.setFreenetInterface(freenetInterface);
300 projectPage.addListSelectionListener(this);
301 pages.put(PageType.PAGE_PROJECTS, projectPage);
303 ProjectFilesPage projectFilesPage = new ProjectFilesPage(wizard);
304 projectFilesPage.setName("page.project.files");
305 pages.put(PageType.PAGE_PROJECT_FILES, projectFilesPage);
307 ProjectInsertPage projectInsertPage = new ProjectInsertPage(wizard);
308 projectInsertPage.setName("page.project.insert");
309 projectInsertPage.setFreenetInterface(freenetInterface);
310 pages.put(PageType.PAGE_INSERT_PROJECT, projectInsertPage);
314 * Shows the page with the given type.
317 * The page type to show
319 private void showPage(PageType pageType) {
320 wizard.setPreviousEnabled(pageType.ordinal() > 0);
321 wizard.setNextEnabled(pageType.ordinal() < (pages.size() - 1));
322 wizard.setPage(pages.get(pageType));
323 wizard.setTitle(pages.get(pageType).getHeading() + " - jSite");
327 * Saves the configuration.
329 * @return <code>true</code> if the configuration could be saved,
330 * <code>false</code> otherwise
332 private boolean saveConfiguration() {
333 NodeManagerPage nodeManagerPage = (NodeManagerPage) pages.get(PageType.PAGE_NODE_MANAGER);
334 configuration.setNodes(nodeManagerPage.getNodes());
335 if (selectedNode != null) {
336 configuration.setSelectedNode(selectedNode);
339 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
340 configuration.setProjects(projectPage.getProjects());
342 return configuration.save();
346 * Finds a supported locale for the given locale.
349 * The locale to find a supported locale for
350 * @return The supported locale that was found, or the default locale if no
351 * supported locale could be found
353 private Locale findSupportedLocale(Locale forLocale) {
354 for (Locale locale : SUPPORTED_LOCALES) {
355 if (locale.equals(forLocale)) {
359 for (Locale locale : SUPPORTED_LOCALES) {
360 if (locale.getCountry().equals(forLocale.getCountry()) && locale.getLanguage().equals(forLocale.getLanguage())) {
364 for (Locale locale : SUPPORTED_LOCALES) {
365 if (locale.getLanguage().equals(forLocale.getLanguage())) {
369 return SUPPORTED_LOCALES[0];
373 * Returns the version.
375 * @return The version
377 public static final Version getVersion() {
386 * Switches the language of the interface to the given locale.
389 * The locale to switch to
391 private void switchLanguage(Locale locale) {
392 Locale supportedLocale = findSupportedLocale(locale);
393 Action languageAction = languageActions.get(supportedLocale);
394 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) languageAction.getValue("menuItem");
395 menuItem.setSelected(true);
396 I18n.setLocale(supportedLocale);
397 for (Runnable i18nRunnable : I18nContainer.getInstance()) {
400 } catch (Throwable t) {
401 /* we probably shouldn't swallow this. */
404 wizard.setPage(wizard.getPage());
405 configuration.setLocale(supportedLocale);
409 * Shows a dialog box that shows the last version that was found by the
410 * {@link UpdateChecker}.
412 private void showLatestUpdate() {
413 Version latestVersion = updateChecker.getLatestVersion();
414 int versionDifference = latestVersion.compareTo(VERSION);
415 if (versionDifference > 0) {
416 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.update-checker.latest-version.newer.message"), VERSION, latestVersion), I18n.getMessage("jsite.update-checker.latest-version.title"), JOptionPane.INFORMATION_MESSAGE);
417 } else if (versionDifference < 0) {
418 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.update-checker.latest-version.older.message"), VERSION, latestVersion), I18n.getMessage("jsite.update-checker.latest-version.title"), JOptionPane.INFORMATION_MESSAGE);
420 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.update-checker.latest-version.okay.message"), VERSION, latestVersion), I18n.getMessage("jsite.update-checker.latest-version.title"), JOptionPane.INFORMATION_MESSAGE);
425 // INTERFACE ListSelectionListener
431 public void valueChanged(ListSelectionEvent e) {
432 JList list = (JList) e.getSource();
433 int selectedRow = list.getSelectedIndex();
434 wizard.setNextEnabled(selectedRow > -1);
438 // INTERFACE WizardListener
444 public void wizardNextPressed(TWizard wizard) {
445 String pageName = wizard.getPage().getName();
446 if ("page.node-manager".equals(pageName)) {
447 showPage(PageType.PAGE_PROJECTS);
448 } else if ("page.project".equals(pageName)) {
449 ProjectPage projectPage = (ProjectPage) wizard.getPage();
450 Project project = projectPage.getSelectedProject();
451 if ((project.getLocalPath() == null) || (project.getLocalPath().trim().length() == 0)) {
452 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-local-path"), null, JOptionPane.ERROR_MESSAGE);
455 if ((project.getPath() == null) || (project.getPath().trim().length() == 0)) {
456 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-path"), null, JOptionPane.ERROR_MESSAGE);
459 ((ProjectFilesPage) pages.get(PageType.PAGE_PROJECT_FILES)).setProject(project);
460 ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).setProject(project);
461 showPage(PageType.PAGE_PROJECT_FILES);
462 } else if ("page.project.files".equals(pageName)) {
463 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
464 Project project = projectPage.getSelectedProject();
465 if (selectedNode == null) {
466 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
469 if ((project.getIndexFile() == null) || (project.getIndexFile().length() == 0)) {
470 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.empty-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
474 File indexFile = new File(project.getLocalPath(), project.getIndexFile());
475 if (!indexFile.exists()) {
476 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.index-missing"), null, JOptionPane.ERROR_MESSAGE);
480 String indexFile = project.getIndexFile();
481 boolean hasIndexFile = (indexFile != null);
482 if (hasIndexFile && !project.getFileOption(indexFile).getContainer().equals("")) {
483 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.container-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
487 if (hasIndexFile && !project.getFileOption(indexFile).getMimeType().equals("text/html")) {
488 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.index-not-html"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
492 Map<String, FileOption> fileOptions = project.getFileOptions();
493 Set<Entry<String, FileOption>> fileOptionEntries = fileOptions.entrySet();
494 for (Entry<String, FileOption> fileOptionEntry : fileOptionEntries) {
495 FileOption fileOption = fileOptionEntry.getValue();
496 if (!fileOption.isInsert() && ((fileOption.getCustomKey().length() == 0) || "CHK@".equals(fileOption.getCustomKey()))) {
497 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.project-files.no-custom-key"), fileOptionEntry.getKey()), null, JOptionPane.ERROR_MESSAGE);
501 boolean nodeRunning = false;
503 nodeRunning = freenetInterface.isNodePresent();
504 } catch (IOException e) {
508 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-running"), null, JOptionPane.ERROR_MESSAGE);
511 configuration.save();
512 showPage(PageType.PAGE_INSERT_PROJECT);
513 ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).startInsert();
514 nodeMenu.setEnabled(false);
515 } else if ("page.project.insert".equals(pageName)) {
516 showPage(PageType.PAGE_PROJECTS);
517 nodeMenu.setEnabled(true);
524 public void wizardPreviousPressed(TWizard wizard) {
525 String pageName = wizard.getPage().getName();
526 if ("page.project".equals(pageName)) {
527 showPage(PageType.PAGE_NODE_MANAGER);
528 } else if ("page.project.files".equals(pageName)) {
529 showPage(PageType.PAGE_PROJECTS);
530 } else if ("page.project.insert".equals(pageName)) {
531 showPage(PageType.PAGE_PROJECT_FILES);
538 public void wizardQuitPressed(TWizard wizard) {
539 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.question"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
540 if (saveConfiguration()) {
543 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.config-not-saved"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
550 // INTERFACE NodeManagerListener
556 public void nodesUpdated(Node[] nodes) {
557 nodeMenu.removeAll();
558 ButtonGroup nodeButtonGroup = new ButtonGroup();
559 Node newSelectedNode = null;
560 for (Node node : nodes) {
561 JRadioButtonMenuItem nodeMenuItem = new JRadioButtonMenuItem(node.getName());
562 nodeMenuItem.putClientProperty("Node", node);
563 nodeMenuItem.addActionListener(this);
564 nodeButtonGroup.add(nodeMenuItem);
565 if (node.equals(selectedNode)) {
566 newSelectedNode = node;
567 nodeMenuItem.setSelected(true);
569 nodeMenu.add(nodeMenuItem);
571 nodeMenu.addSeparator();
572 nodeMenu.add(manageNodeAction);
573 selectedNode = newSelectedNode;
574 freenetInterface.setNode(selectedNode);
580 public void actionPerformed(ActionEvent e) {
581 Object source = e.getSource();
582 if (source instanceof JRadioButtonMenuItem) {
583 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) source;
584 Node node = (Node) menuItem.getClientProperty("Node");
586 freenetInterface.setNode(selectedNode);
591 // INTERFACE UpdateListener
597 public void foundUpdateData(Version foundVersion, long versionTimestamp) {
598 if (foundVersion.compareTo(VERSION) > 0) {
599 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.update-checker.found-version.message"), foundVersion.toString(), new Date(versionTimestamp)), I18n.getMessage("jsite.update-checker.found-version.title"), JOptionPane.INFORMATION_MESSAGE);
608 * Main method that is called by the VM.
611 * The command-line arguments
613 public static void main(String[] args) {
614 /* initialize logger. */
615 Logger logger = Logger.getLogger("de.todesbaum");
616 Handler handler = new ConsoleHandler();
617 logger.addHandler(handler);
618 String configFilename = null;
619 boolean nextIsConfigFilename = false;
620 for (String argument : args) {
621 if (nextIsConfigFilename) {
622 configFilename = argument;
623 nextIsConfigFilename = false;
625 if ("--help".equals(argument)) {
628 } else if ("--debug".equals(argument)) {
629 logger.setLevel(Level.ALL);
630 handler.setLevel(Level.ALL);
631 } else if ("--config-file".equals(argument)) {
632 nextIsConfigFilename = true;
635 if (nextIsConfigFilename) {
636 System.out.println("--config-file needs parameter!");
639 new Main(configFilename);
643 * Prints a small syntax help.
645 private static void printHelp() {
646 System.out.println("--help\tshows this cruft");
647 System.out.println("--debug\tenables some debug output");
648 System.out.println("--config-file <file>\tuse specified configuration file");