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;
74 private static final Version VERSION = new Version(0, 6, 2);
76 /** The configuration. */
77 private Configuration configuration;
79 /** The freenet interface. */
80 private Freenet7Interface freenetInterface = new Freenet7Interface();
82 /** The update checker. */
83 private final UpdateChecker updateChecker;
85 /** The jSite icon. */
86 private Icon jSiteIcon;
89 * Enumeration for all possible pages.
91 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
93 private static enum PageType {
95 /** The node manager page. */
98 /** The project page. */
101 /** The project files page. */
104 /** The project insert page. */
109 /** The supported locales. */
110 private static final Locale[] SUPPORTED_LOCALES = new Locale[] { Locale.ENGLISH, Locale.GERMAN, Locale.FRENCH, Locale.ITALIAN, new Locale("pl") };
112 /** The actions that switch the language. */
113 private Map<Locale, Action> languageActions = new HashMap<Locale, Action>();
115 /** The “manage nodes” action. */
116 private Action manageNodeAction;
118 /** The “check for updates” action. */
119 private Action checkForUpdatesAction;
121 /** The “about jSite” action. */
122 private Action aboutAction;
125 private TWizard wizard;
127 /** The node menu. */
128 private JMenu nodeMenu;
130 /** The currently selected node. */
131 private Node selectedNode;
133 /** Mapping from page type to page. */
134 private final Map<PageType, TWizardPage> pages = new HashMap<PageType, TWizardPage>();
137 * Creates a new core with the default configuration file.
144 * Creates a new core with the given configuration from the given file.
146 * @param configFilename
147 * The name of the configuration file
149 private Main(String configFilename) {
150 if (configFilename != null) {
151 configuration = new Configuration(configFilename);
153 configuration = new Configuration();
155 Locale.setDefault(configuration.getLocale());
156 I18n.setLocale(configuration.getLocale());
157 if (!configuration.createLockFile()) {
158 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"));
160 throw new IllegalStateException("Lockfile override not active, refusing start.");
162 configuration.removeLockfileOnExit();
164 wizard = new TWizard();
166 wizard.setJMenuBar(createMenuBar());
167 wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
168 wizard.setPreviousEnabled(false);
169 wizard.setNextEnabled(true);
170 wizard.addWizardListener(this);
171 jSiteIcon = IconLoader.loadIcon("/jsite-icon.png");
172 wizard.setIcon(jSiteIcon);
174 updateChecker = new UpdateChecker(wizard, freenetInterface);
177 showPage(PageType.PAGE_PROJECTS);
181 * Creates all actions.
183 private void createActions() {
184 for (final Locale locale : SUPPORTED_LOCALES) {
185 languageActions.put(locale, new AbstractAction(I18n.getMessage("jsite.menu.language." + locale.getLanguage()), IconLoader.loadIcon("/flag-" + locale.getLanguage() + ".png")) {
187 @SuppressWarnings("synthetic-access")
188 public void actionPerformed(ActionEvent actionEvent) {
189 switchLanguage(locale);
193 manageNodeAction = new AbstractAction(I18n.getMessage("jsite.menu.nodes.manage-nodes")) {
195 @SuppressWarnings("synthetic-access")
196 public void actionPerformed(ActionEvent actionEvent) {
197 showPage(PageType.PAGE_NODE_MANAGER);
198 wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
199 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
202 checkForUpdatesAction = new AbstractAction(I18n.getMessage("jsite.menu.help.check-for-updates")) {
207 @SuppressWarnings("synthetic-access")
208 public void actionPerformed(ActionEvent actionEvent) {
209 updateChecker.checkForUpdates();
212 aboutAction = new AbstractAction(I18n.getMessage("jsite.menu.help.about")) {
214 @SuppressWarnings("synthetic-access")
215 public void actionPerformed(ActionEvent e) {
216 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.about.message"), getVersion().toString()), null, JOptionPane.INFORMATION_MESSAGE, jSiteIcon);
220 I18nContainer.getInstance().registerRunnable(new Runnable() {
222 @SuppressWarnings("synthetic-access")
224 manageNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.nodes.manage-nodes"));
225 aboutAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.help.about"));
231 * Creates the menu bar.
233 * @return The menu bar
235 private JMenuBar createMenuBar() {
236 JMenuBar menuBar = new JMenuBar();
237 final JMenu languageMenu = new JMenu(I18n.getMessage("jsite.menu.languages"));
238 menuBar.add(languageMenu);
239 ButtonGroup languageButtonGroup = new ButtonGroup();
240 for (Locale locale : SUPPORTED_LOCALES) {
241 Action languageAction = languageActions.get(locale);
242 JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(languageActions.get(locale));
243 if (locale.equals(Locale.getDefault())) {
244 menuItem.setSelected(true);
246 languageAction.putValue("menuItem", menuItem);
247 languageButtonGroup.add(menuItem);
248 languageMenu.add(menuItem);
250 nodeMenu = new JMenu(I18n.getMessage("jsite.menu.nodes"));
251 menuBar.add(nodeMenu);
252 selectedNode = configuration.getSelectedNode();
253 nodesUpdated(configuration.getNodes());
255 /* evil hack to right-align the help menu */
256 JPanel panel = new JPanel();
257 panel.setOpaque(false);
260 final JMenu helpMenu = new JMenu(I18n.getMessage("jsite.menu.help"));
261 menuBar.add(helpMenu);
262 helpMenu.add(checkForUpdatesAction);
263 helpMenu.add(aboutAction);
265 I18nContainer.getInstance().registerRunnable(new Runnable() {
267 @SuppressWarnings("synthetic-access")
269 languageMenu.setText(I18n.getMessage("jsite.menu.languages"));
270 nodeMenu.setText(I18n.getMessage("jsite.menu.nodes"));
271 helpMenu.setText(I18n.getMessage("jsite.menu.help"));
272 for (Map.Entry<Locale, Action> languageActionEntry : languageActions.entrySet()) {
273 languageActionEntry.getValue().putValue(Action.NAME, I18n.getMessage("jsite.menu.language." + languageActionEntry.getKey().getLanguage()));
282 * Initializes all pages.
284 private void initPages() {
285 NodeManagerPage nodeManagerPage = new NodeManagerPage(wizard);
286 nodeManagerPage.setName("page.node-manager");
287 nodeManagerPage.addNodeManagerListener(this);
288 nodeManagerPage.setNodes(configuration.getNodes());
289 pages.put(PageType.PAGE_NODE_MANAGER, nodeManagerPage);
291 ProjectPage projectPage = new ProjectPage(wizard);
292 projectPage.setName("page.project");
293 projectPage.setProjects(configuration.getProjects());
294 projectPage.setFreenetInterface(freenetInterface);
295 projectPage.addListSelectionListener(this);
296 pages.put(PageType.PAGE_PROJECTS, projectPage);
298 ProjectFilesPage projectFilesPage = new ProjectFilesPage(wizard);
299 projectFilesPage.setName("page.project.files");
300 pages.put(PageType.PAGE_PROJECT_FILES, projectFilesPage);
302 ProjectInsertPage projectInsertPage = new ProjectInsertPage(wizard);
303 projectInsertPage.setDebug(debug);
304 projectInsertPage.setName("page.project.insert");
305 projectInsertPage.setFreenetInterface(freenetInterface);
306 pages.put(PageType.PAGE_INSERT_PROJECT, projectInsertPage);
310 * Shows the page with the given type.
313 * The page type to show
315 private void showPage(PageType pageType) {
316 wizard.setPreviousEnabled(pageType.ordinal() > 0);
317 wizard.setNextEnabled(pageType.ordinal() < (pages.size() - 1));
318 wizard.setPage(pages.get(pageType));
319 wizard.setTitle(pages.get(pageType).getHeading() + " - jSite");
323 * Saves the configuration.
325 * @return <code>true</code> if the configuration could be saved,
326 * <code>false</code> otherwise
328 private boolean saveConfiguration() {
329 NodeManagerPage nodeManagerPage = (NodeManagerPage) pages.get(PageType.PAGE_NODE_MANAGER);
330 configuration.setNodes(nodeManagerPage.getNodes());
331 if (selectedNode != null) {
332 configuration.setSelectedNode(selectedNode);
335 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
336 configuration.setProjects(projectPage.getProjects());
338 return configuration.save();
342 * Finds a supported locale for the given locale.
345 * The locale to find a supported locale for
346 * @return The supported locale that was found, or the default locale if no
347 * supported locale could be found
349 private Locale findSupportedLocale(Locale forLocale) {
350 for (Locale locale : SUPPORTED_LOCALES) {
351 if (locale.equals(forLocale)) {
355 for (Locale locale : SUPPORTED_LOCALES) {
356 if (locale.getCountry().equals(forLocale.getCountry()) && locale.getLanguage().equals(forLocale.getLanguage())) {
360 for (Locale locale : SUPPORTED_LOCALES) {
361 if (locale.getLanguage().equals(forLocale.getLanguage())) {
365 return SUPPORTED_LOCALES[0];
369 * Returns the version.
371 * @return The version
373 public static final Version getVersion() {
382 * Switches the language of the interface to the given locale.
385 * The locale to switch to
387 private void switchLanguage(Locale locale) {
388 Locale supportedLocale = findSupportedLocale(locale);
389 Action languageAction = languageActions.get(supportedLocale);
390 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) languageAction.getValue("menuItem");
391 menuItem.setSelected(true);
392 I18n.setLocale(supportedLocale);
393 for (Runnable i18nRunnable : I18nContainer.getInstance()) {
396 } catch (Throwable t) {
397 /* we probably shouldn't swallow this. */
400 wizard.setPage(wizard.getPage());
401 configuration.setLocale(supportedLocale);
405 // INTERFACE ListSelectionListener
411 public void valueChanged(ListSelectionEvent e) {
412 JList list = (JList) e.getSource();
413 int selectedRow = list.getSelectedIndex();
414 wizard.setNextEnabled(selectedRow > -1);
418 // INTERFACE WizardListener
424 public void wizardNextPressed(TWizard wizard) {
425 String pageName = wizard.getPage().getName();
426 if ("page.node-manager".equals(pageName)) {
427 showPage(PageType.PAGE_PROJECTS);
428 } else if ("page.project".equals(pageName)) {
429 ProjectPage projectPage = (ProjectPage) wizard.getPage();
430 Project project = projectPage.getSelectedProject();
431 if ((project.getLocalPath() == null) || (project.getLocalPath().trim().length() == 0)) {
432 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-local-path"), null, JOptionPane.ERROR_MESSAGE);
435 if ((project.getPath() == null) || (project.getPath().trim().length() == 0)) {
436 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-path"), null, JOptionPane.ERROR_MESSAGE);
439 ((ProjectFilesPage) pages.get(PageType.PAGE_PROJECT_FILES)).setProject(project);
440 ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).setProject(project);
441 showPage(PageType.PAGE_PROJECT_FILES);
442 } else if ("page.project.files".equals(pageName)) {
443 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
444 Project project = projectPage.getSelectedProject();
445 if (selectedNode == null) {
446 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
449 if (project.getIndexFile() == null) {
450 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.empty-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
454 File indexFile = new File(project.getLocalPath(), project.getIndexFile());
455 if (!indexFile.exists()) {
456 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.index-missing"), null, JOptionPane.ERROR_MESSAGE);
460 String indexFile = project.getIndexFile();
461 boolean hasIndexFile = (indexFile != null);
462 if (hasIndexFile && !project.getFileOption(indexFile).getContainer().equals("")) {
463 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.container-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
467 if (hasIndexFile && !project.getFileOption(indexFile).getMimeType().equals("text/html")) {
468 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.index-not-html"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
472 Map<String, FileOption> fileOptions = project.getFileOptions();
473 Set<Entry<String, FileOption>> fileOptionEntries = fileOptions.entrySet();
474 for (Entry<String, FileOption> fileOptionEntry : fileOptionEntries) {
475 FileOption fileOption = fileOptionEntry.getValue();
476 if (!fileOption.isInsert() && ((fileOption.getCustomKey().length() == 0) || "CHK@".equals(fileOption.getCustomKey()))) {
477 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.project-files.no-custom-key"), fileOptionEntry.getKey()), null, JOptionPane.ERROR_MESSAGE);
481 boolean nodeRunning = false;
483 nodeRunning = freenetInterface.isNodePresent();
484 } catch (IOException e) {
488 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-running"), null, JOptionPane.ERROR_MESSAGE);
491 configuration.save();
492 showPage(PageType.PAGE_INSERT_PROJECT);
493 ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).startInsert();
494 nodeMenu.setEnabled(false);
495 } else if ("page.project.insert".equals(pageName)) {
496 showPage(PageType.PAGE_PROJECTS);
497 nodeMenu.setEnabled(true);
504 public void wizardPreviousPressed(TWizard wizard) {
505 String pageName = wizard.getPage().getName();
506 if ("page.project".equals(pageName)) {
507 showPage(PageType.PAGE_NODE_MANAGER);
508 } else if ("page.project.files".equals(pageName)) {
509 showPage(PageType.PAGE_PROJECTS);
510 } else if ("page.project.insert".equals(pageName)) {
511 showPage(PageType.PAGE_PROJECT_FILES);
518 public void wizardQuitPressed(TWizard wizard) {
519 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.question"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
520 if (saveConfiguration()) {
523 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.config-not-saved"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
530 // INTERFACE NodeManagerListener
536 public void nodesUpdated(Node[] nodes) {
537 nodeMenu.removeAll();
538 ButtonGroup nodeButtonGroup = new ButtonGroup();
539 Node newSelectedNode = null;
540 for (Node node : nodes) {
541 JRadioButtonMenuItem nodeMenuItem = new JRadioButtonMenuItem(node.getName());
542 nodeMenuItem.putClientProperty("Node", node);
543 nodeMenuItem.addActionListener(this);
544 nodeButtonGroup.add(nodeMenuItem);
545 if (node.equals(selectedNode)) {
546 newSelectedNode = node;
547 nodeMenuItem.setSelected(true);
549 nodeMenu.add(nodeMenuItem);
551 nodeMenu.addSeparator();
552 nodeMenu.add(manageNodeAction);
553 selectedNode = newSelectedNode;
554 freenetInterface.setNode(selectedNode);
560 public void actionPerformed(ActionEvent e) {
561 Object source = e.getSource();
562 if (source instanceof JRadioButtonMenuItem) {
563 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) source;
564 Node node = (Node) menuItem.getClientProperty("Node");
566 freenetInterface.setNode(selectedNode);
575 * Main method that is called by the VM.
578 * The command-line arguments
580 public static void main(String[] args) {
581 String configFilename = null;
582 boolean nextIsConfigFilename = false;
583 for (String argument : args) {
584 if (nextIsConfigFilename) {
585 configFilename = argument;
586 nextIsConfigFilename = false;
588 if ("--help".equals(argument)) {
591 } else if ("--debug".equals(argument)) {
593 } else if ("--config-file".equals(argument)) {
594 nextIsConfigFilename = true;
597 if (nextIsConfigFilename) {
598 System.out.println("--config-file needs parameter!");
601 new Main(configFilename);
605 * Prints a small syntax help.
607 private static void printHelp() {
608 System.out.println("--help\tshows this cruft");
609 System.out.println("--debug\tenables some debug output");
610 System.out.println("--config-file <file>\tuse specified configuration file");