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.HashMap;
28 import java.util.Locale;
31 import java.util.Map.Entry;
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;
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.gui.UpdateChecker;
56 import de.todesbaum.jsite.i18n.I18n;
57 import de.todesbaum.jsite.i18n.I18nContainer;
58 import de.todesbaum.util.image.IconLoader;
59 import de.todesbaum.util.swing.TWizard;
60 import de.todesbaum.util.swing.TWizardPage;
61 import de.todesbaum.util.swing.WizardListener;
64 * The main class that ties together everything.
66 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
68 public class Main implements ActionListener, ListSelectionListener, WizardListener, NodeManagerListener {
70 /** Whether the debug mode is activated. */
71 private static boolean debug = false;
73 /** The configuration. */
74 private Configuration configuration;
76 /** The freenet interface. */
77 private Freenet7Interface freenetInterface = new Freenet7Interface();
79 /** The update checker. */
80 private final UpdateChecker updateChecker;
82 /** The jSite icon. */
83 private Icon jSiteIcon;
86 * Enumeration for all possible pages.
88 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
90 private static enum PageType {
92 /** The node manager page. */
95 /** The project page. */
98 /** The project files page. */
101 /** The project insert page. */
106 /** The supported locales. */
107 private static final Locale[] SUPPORTED_LOCALES = new Locale[] { Locale.ENGLISH, Locale.GERMAN, Locale.FRENCH, Locale.ITALIAN, new Locale("pl") };
109 /** The actions that switch the language. */
110 private Map<Locale, Action> languageActions = new HashMap<Locale, Action>();
112 /** The “manage nodes” action. */
113 private Action manageNodeAction;
115 /** The “check for updates” action. */
116 private Action checkForUpdatesAction;
118 /** The “about jSite” action. */
119 private Action aboutAction;
122 private TWizard wizard;
124 /** The node menu. */
125 private JMenu nodeMenu;
127 /** The currently selected node. */
128 private Node selectedNode;
130 /** Mapping from page type to page. */
131 private final Map<PageType, TWizardPage> pages = new HashMap<PageType, TWizardPage>();
134 * Creates a new core with the default configuration file.
141 * Creates a new core with the given configuration from the given file.
143 * @param configFilename
144 * The name of the configuration file
146 private Main(String configFilename) {
147 if (configFilename != null) {
148 configuration = new Configuration(configFilename);
150 configuration = new Configuration();
152 Locale.setDefault(configuration.getLocale());
153 I18n.setLocale(configuration.getLocale());
154 if (!configuration.createLockFile()) {
155 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"));
157 throw new IllegalStateException("Lockfile override not active, refusing start.");
159 configuration.removeLockfileOnExit();
161 wizard = new TWizard();
163 wizard.setJMenuBar(createMenuBar());
164 wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
165 wizard.setPreviousEnabled(false);
166 wizard.setNextEnabled(true);
167 wizard.addWizardListener(this);
168 jSiteIcon = IconLoader.loadIcon("/jsite-icon.png");
169 wizard.setIcon(jSiteIcon);
171 updateChecker = new UpdateChecker(wizard, freenetInterface);
174 showPage(PageType.PAGE_PROJECTS);
178 * Creates all actions.
180 private void createActions() {
181 for (final Locale locale : SUPPORTED_LOCALES) {
182 languageActions.put(locale, new AbstractAction(I18n.getMessage("jsite.menu.language." + locale.getLanguage()), IconLoader.loadIcon("/flag-" + locale.getLanguage() + ".png")) {
184 @SuppressWarnings("synthetic-access")
185 public void actionPerformed(ActionEvent actionEvent) {
186 switchLanguage(locale);
190 manageNodeAction = new AbstractAction(I18n.getMessage("jsite.menu.nodes.manage-nodes")) {
192 @SuppressWarnings("synthetic-access")
193 public void actionPerformed(ActionEvent actionEvent) {
194 showPage(PageType.PAGE_NODE_MANAGER);
195 wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
196 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
199 checkForUpdatesAction = new AbstractAction(I18n.getMessage("jsite.menu.help.check-for-updates")) {
204 @SuppressWarnings("synthetic-access")
205 public void actionPerformed(ActionEvent actionEvent) {
206 updateChecker.checkForUpdates();
209 aboutAction = new AbstractAction(I18n.getMessage("jsite.menu.help.about")) {
211 @SuppressWarnings("synthetic-access")
212 public void actionPerformed(ActionEvent e) {
213 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.about.message"), Version.getVersion()), null, JOptionPane.INFORMATION_MESSAGE, jSiteIcon);
217 I18nContainer.getInstance().registerRunnable(new Runnable() {
219 @SuppressWarnings("synthetic-access")
221 manageNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.nodes.manage-nodes"));
222 aboutAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.help.about"));
228 * Creates the menu bar.
230 * @return The menu bar
232 private JMenuBar createMenuBar() {
233 JMenuBar menuBar = new JMenuBar();
234 final JMenu languageMenu = new JMenu(I18n.getMessage("jsite.menu.languages"));
235 menuBar.add(languageMenu);
236 ButtonGroup languageButtonGroup = new ButtonGroup();
237 for (Locale locale : SUPPORTED_LOCALES) {
238 Action languageAction = languageActions.get(locale);
239 JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(languageActions.get(locale));
240 if (locale.equals(Locale.getDefault())) {
241 menuItem.setSelected(true);
243 languageAction.putValue("menuItem", menuItem);
244 languageButtonGroup.add(menuItem);
245 languageMenu.add(menuItem);
247 nodeMenu = new JMenu(I18n.getMessage("jsite.menu.nodes"));
248 menuBar.add(nodeMenu);
249 selectedNode = configuration.getSelectedNode();
250 nodesUpdated(configuration.getNodes());
252 /* evil hack to right-align the help menu */
253 JPanel panel = new JPanel();
254 panel.setOpaque(false);
257 final JMenu helpMenu = new JMenu(I18n.getMessage("jsite.menu.help"));
258 menuBar.add(helpMenu);
259 helpMenu.add(checkForUpdatesAction);
260 helpMenu.add(aboutAction);
262 I18nContainer.getInstance().registerRunnable(new Runnable() {
264 @SuppressWarnings("synthetic-access")
266 languageMenu.setText(I18n.getMessage("jsite.menu.languages"));
267 nodeMenu.setText(I18n.getMessage("jsite.menu.nodes"));
268 helpMenu.setText(I18n.getMessage("jsite.menu.help"));
269 for (Map.Entry<Locale, Action> languageActionEntry : languageActions.entrySet()) {
270 languageActionEntry.getValue().putValue(Action.NAME, I18n.getMessage("jsite.menu.language." + languageActionEntry.getKey().getLanguage()));
279 * Initializes all pages.
281 private void initPages() {
282 NodeManagerPage nodeManagerPage = new NodeManagerPage(wizard);
283 nodeManagerPage.setName("page.node-manager");
284 nodeManagerPage.addNodeManagerListener(this);
285 nodeManagerPage.setNodes(configuration.getNodes());
286 pages.put(PageType.PAGE_NODE_MANAGER, nodeManagerPage);
288 ProjectPage projectPage = new ProjectPage(wizard);
289 projectPage.setName("page.project");
290 projectPage.setProjects(configuration.getProjects());
291 projectPage.setFreenetInterface(freenetInterface);
292 projectPage.addListSelectionListener(this);
293 pages.put(PageType.PAGE_PROJECTS, projectPage);
295 ProjectFilesPage projectFilesPage = new ProjectFilesPage(wizard);
296 projectFilesPage.setName("page.project.files");
297 pages.put(PageType.PAGE_PROJECT_FILES, projectFilesPage);
299 ProjectInsertPage projectInsertPage = new ProjectInsertPage(wizard);
300 projectInsertPage.setDebug(debug);
301 projectInsertPage.setName("page.project.insert");
302 projectInsertPage.setFreenetInterface(freenetInterface);
303 pages.put(PageType.PAGE_INSERT_PROJECT, projectInsertPage);
307 * Shows the page with the given type.
310 * The page type to show
312 private void showPage(PageType pageType) {
313 wizard.setPreviousEnabled(pageType.ordinal() > 0);
314 wizard.setNextEnabled(pageType.ordinal() < (pages.size() - 1));
315 wizard.setPage(pages.get(pageType));
316 wizard.setTitle(pages.get(pageType).getHeading() + " - jSite");
320 * Saves the configuration.
322 * @return <code>true</code> if the configuration could be saved,
323 * <code>false</code> otherwise
325 private boolean saveConfiguration() {
326 NodeManagerPage nodeManagerPage = (NodeManagerPage) pages.get(PageType.PAGE_NODE_MANAGER);
327 configuration.setNodes(nodeManagerPage.getNodes());
328 if (selectedNode != null) {
329 configuration.setSelectedNode(selectedNode);
332 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
333 configuration.setProjects(projectPage.getProjects());
335 return configuration.save();
339 * Finds a supported locale for the given locale.
342 * The locale to find a supported locale for
343 * @return The supported locale that was found, or the default locale if no
344 * supported locale could be found
346 private Locale findSupportedLocale(Locale forLocale) {
347 for (Locale locale : SUPPORTED_LOCALES) {
348 if (locale.equals(forLocale)) {
352 for (Locale locale : SUPPORTED_LOCALES) {
353 if (locale.getCountry().equals(forLocale.getCountry()) && locale.getLanguage().equals(forLocale.getLanguage())) {
357 for (Locale locale : SUPPORTED_LOCALES) {
358 if (locale.getLanguage().equals(forLocale.getLanguage())) {
362 return SUPPORTED_LOCALES[0];
370 * Switches the language of the interface to the given locale.
373 * The locale to switch to
375 private void switchLanguage(Locale locale) {
376 Locale supportedLocale = findSupportedLocale(locale);
377 Action languageAction = languageActions.get(supportedLocale);
378 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) languageAction.getValue("menuItem");
379 menuItem.setSelected(true);
380 I18n.setLocale(supportedLocale);
381 for (Runnable i18nRunnable : I18nContainer.getInstance()) {
384 } catch (Throwable t) {
385 /* we probably shouldn't swallow this. */
388 wizard.setPage(wizard.getPage());
389 configuration.setLocale(supportedLocale);
393 // INTERFACE ListSelectionListener
399 public void valueChanged(ListSelectionEvent e) {
400 JList list = (JList) e.getSource();
401 int selectedRow = list.getSelectedIndex();
402 wizard.setNextEnabled(selectedRow > -1);
406 // INTERFACE WizardListener
412 public void wizardNextPressed(TWizard wizard) {
413 String pageName = wizard.getPage().getName();
414 if ("page.node-manager".equals(pageName)) {
415 showPage(PageType.PAGE_PROJECTS);
416 } else if ("page.project".equals(pageName)) {
417 ProjectPage projectPage = (ProjectPage) wizard.getPage();
418 Project project = projectPage.getSelectedProject();
419 if ((project.getLocalPath() == null) || (project.getLocalPath().trim().length() == 0)) {
420 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-local-path"), null, JOptionPane.ERROR_MESSAGE);
423 if ((project.getPath() == null) || (project.getPath().trim().length() == 0)) {
424 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-path"), null, JOptionPane.ERROR_MESSAGE);
427 ((ProjectFilesPage) pages.get(PageType.PAGE_PROJECT_FILES)).setProject(project);
428 ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).setProject(project);
429 showPage(PageType.PAGE_PROJECT_FILES);
430 } else if ("page.project.files".equals(pageName)) {
431 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
432 Project project = projectPage.getSelectedProject();
433 if (selectedNode == null) {
434 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
437 if (project.getIndexFile() == null) {
438 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.empty-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
442 File indexFile = new File(project.getLocalPath(), project.getIndexFile());
443 if (!indexFile.exists()) {
444 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.index-missing"), null, JOptionPane.ERROR_MESSAGE);
448 String indexFile = project.getIndexFile();
449 boolean hasIndexFile = (indexFile != null);
450 if (hasIndexFile && !project.getFileOption(indexFile).getContainer().equals("")) {
451 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.container-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
455 if (hasIndexFile && !project.getFileOption(indexFile).getMimeType().equals("text/html")) {
456 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.index-not-html"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
460 Map<String, FileOption> fileOptions = project.getFileOptions();
461 Set<Entry<String, FileOption>> fileOptionEntries = fileOptions.entrySet();
462 for (Entry<String, FileOption> fileOptionEntry : fileOptionEntries) {
463 FileOption fileOption = fileOptionEntry.getValue();
464 if (!fileOption.isInsert() && ((fileOption.getCustomKey().length() == 0) || "CHK@".equals(fileOption.getCustomKey()))) {
465 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.project-files.no-custom-key"), fileOptionEntry.getKey()), null, JOptionPane.ERROR_MESSAGE);
469 boolean nodeRunning = false;
471 nodeRunning = freenetInterface.isNodePresent();
472 } catch (IOException e) {
476 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-running"), null, JOptionPane.ERROR_MESSAGE);
479 configuration.save();
480 showPage(PageType.PAGE_INSERT_PROJECT);
481 ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).startInsert();
482 nodeMenu.setEnabled(false);
483 } else if ("page.project.insert".equals(pageName)) {
484 showPage(PageType.PAGE_PROJECTS);
485 nodeMenu.setEnabled(true);
492 public void wizardPreviousPressed(TWizard wizard) {
493 String pageName = wizard.getPage().getName();
494 if ("page.project".equals(pageName)) {
495 showPage(PageType.PAGE_NODE_MANAGER);
496 } else if ("page.project.files".equals(pageName)) {
497 showPage(PageType.PAGE_PROJECTS);
498 } else if ("page.project.insert".equals(pageName)) {
499 showPage(PageType.PAGE_PROJECT_FILES);
506 public void wizardQuitPressed(TWizard wizard) {
507 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.question"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
508 if (saveConfiguration()) {
511 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.config-not-saved"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
518 // INTERFACE NodeManagerListener
524 public void nodesUpdated(Node[] nodes) {
525 nodeMenu.removeAll();
526 ButtonGroup nodeButtonGroup = new ButtonGroup();
527 Node newSelectedNode = null;
528 for (Node node : nodes) {
529 JRadioButtonMenuItem nodeMenuItem = new JRadioButtonMenuItem(node.getName());
530 nodeMenuItem.putClientProperty("Node", node);
531 nodeMenuItem.addActionListener(this);
532 nodeButtonGroup.add(nodeMenuItem);
533 if (node.equals(selectedNode)) {
534 newSelectedNode = node;
535 nodeMenuItem.setSelected(true);
537 nodeMenu.add(nodeMenuItem);
539 nodeMenu.addSeparator();
540 nodeMenu.add(manageNodeAction);
541 selectedNode = newSelectedNode;
542 freenetInterface.setNode(selectedNode);
548 public void actionPerformed(ActionEvent e) {
549 Object source = e.getSource();
550 if (source instanceof JRadioButtonMenuItem) {
551 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) source;
552 Node node = (Node) menuItem.getClientProperty("Node");
554 freenetInterface.setNode(selectedNode);
563 * Main method that is called by the VM.
566 * The command-line arguments
568 public static void main(String[] args) {
569 String configFilename = null;
570 boolean nextIsConfigFilename = false;
571 for (String argument : args) {
572 if (nextIsConfigFilename) {
573 configFilename = argument;
574 nextIsConfigFilename = false;
576 if ("--help".equals(argument)) {
579 } else if ("--debug".equals(argument)) {
581 } else if ("--config-file".equals(argument)) {
582 nextIsConfigFilename = true;
585 if (nextIsConfigFilename) {
586 System.out.println("--config-file needs parameter!");
589 new Main(configFilename);
593 * Prints a small syntax help.
595 private static void printHelp() {
596 System.out.println("--help\tshows this cruft");
597 System.out.println("--debug\tenables some debug output");
598 System.out.println("--config-file <file>\tuse specified configuration file");