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.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;
63 * The main class that ties together everything.
65 * @author David āBombeā Roden <bombe@freenetproject.org>
67 public class Main implements ActionListener, ListSelectionListener, WizardListener, NodeManagerListener {
69 /** Whether the debug mode is activated. */
70 private static boolean debug = false;
72 /** The URL for update checks. */
73 private static final String UPDATE_KEY = "USK@e3myoFyp5avg6WYN16ImHri6J7Nj8980Fm~aQe4EX1U,QvbWT0ImE0TwLODTl7EoJx2NBnwDxTbLTE6zkB-eGPs,AQACAAE/jSite/0/currentVersion.txt";
75 /** The configuration. */
76 private Configuration configuration;
78 /** The freenet interface. */
79 private Freenet7Interface freenetInterface = new Freenet7Interface();
81 /** The jSite icon. */
82 private Icon jSiteIcon;
85 * Enumeration for all possible pages.
87 * @author David āBombeā Roden <bombe@freenetproject.org>
89 private static enum PageType {
91 /** The node manager page. */
94 /** The project page. */
97 /** The project files page. */
100 /** The project insert page. */
105 /** The supported locales. */
106 private static final Locale[] SUPPORTED_LOCALES = new Locale[] { Locale.ENGLISH, Locale.GERMAN, Locale.FRENCH, Locale.ITALIAN, new Locale("pl") };
108 /** The actions that switch the language. */
109 private Map<Locale, Action> languageActions = new HashMap<Locale, Action>();
111 /** The āmanage nodesā action. */
112 private Action manageNodeAction;
114 /** The ācheck for updatesā action. */
115 private Action checkForUpdatesAction;
117 /** The āabout jSiteā action. */
118 private Action aboutAction;
121 private TWizard wizard;
123 /** The node menu. */
124 private JMenu nodeMenu;
126 /** The currently selected node. */
127 private Node selectedNode;
129 /** Mapping from page type to page. */
130 private final Map<PageType, TWizardPage> pages = new HashMap<PageType, TWizardPage>();
133 * Creates a new core with the default configuration file.
140 * Creates a new core with the given configuration from the given file.
142 * @param configFilename
143 * The name of the configuration file
145 private Main(String configFilename) {
146 if (configFilename != null) {
147 configuration = new Configuration(configFilename);
149 configuration = new Configuration();
151 Locale.setDefault(configuration.getLocale());
152 I18n.setLocale(configuration.getLocale());
153 if (!configuration.createLockFile()) {
154 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"));
158 configuration.removeLockfileOnExit();
160 wizard = new TWizard();
162 wizard.setJMenuBar(createMenuBar());
163 wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
164 wizard.setPreviousEnabled(false);
165 wizard.setNextEnabled(true);
166 wizard.addWizardListener(this);
167 jSiteIcon = IconLoader.loadIcon("/jsite-icon.png");
168 wizard.setIcon(jSiteIcon);
171 showPage(PageType.PAGE_PROJECTS);
175 * Creates all actions.
177 private void createActions() {
178 for (final Locale locale : SUPPORTED_LOCALES) {
179 languageActions.put(locale, new AbstractAction(I18n.getMessage("jsite.menu.language." + locale.getLanguage()), IconLoader.loadIcon("/flag-" + locale.getLanguage() + ".png")) {
181 @SuppressWarnings("synthetic-access")
182 public void actionPerformed(ActionEvent actionEvent) {
183 switchLanguage(locale);
187 manageNodeAction = new AbstractAction(I18n.getMessage("jsite.menu.nodes.manage-nodes")) {
189 @SuppressWarnings("synthetic-access")
190 public void actionPerformed(ActionEvent actionEvent) {
191 showPage(PageType.PAGE_NODE_MANAGER);
192 wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
193 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
196 checkForUpdatesAction = new AbstractAction(I18n.getMessage("jsite.menu.help.check-for-updates")) {
201 @SuppressWarnings("synthetic-access")
202 public void actionPerformed(ActionEvent actionEvent) {
206 aboutAction = new AbstractAction(I18n.getMessage("jsite.menu.help.about")) {
208 @SuppressWarnings("synthetic-access")
209 public void actionPerformed(ActionEvent e) {
210 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.about.message"), Version.getVersion()), null, JOptionPane.INFORMATION_MESSAGE, jSiteIcon);
214 I18nContainer.getInstance().registerRunnable(new Runnable() {
216 @SuppressWarnings("synthetic-access")
218 manageNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.nodes.manage-nodes"));
219 aboutAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.help.about"));
225 * Creates the menu bar.
227 * @return The menu bar
229 private JMenuBar createMenuBar() {
230 JMenuBar menuBar = new JMenuBar();
231 final JMenu languageMenu = new JMenu(I18n.getMessage("jsite.menu.languages"));
232 menuBar.add(languageMenu);
233 ButtonGroup languageButtonGroup = new ButtonGroup();
234 for (Locale locale : SUPPORTED_LOCALES) {
235 Action languageAction = languageActions.get(locale);
236 JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(languageActions.get(locale));
237 if (locale.equals(Locale.getDefault())) {
238 menuItem.setSelected(true);
240 languageAction.putValue("menuItem", menuItem);
241 languageButtonGroup.add(menuItem);
242 languageMenu.add(menuItem);
244 nodeMenu = new JMenu(I18n.getMessage("jsite.menu.nodes"));
245 menuBar.add(nodeMenu);
246 selectedNode = configuration.getSelectedNode();
247 nodesUpdated(configuration.getNodes());
249 /* evil hack to right-align the help menu */
250 JPanel panel = new JPanel();
251 panel.setOpaque(false);
254 final JMenu helpMenu = new JMenu(I18n.getMessage("jsite.menu.help"));
255 menuBar.add(helpMenu);
256 helpMenu.add(aboutAction);
258 I18nContainer.getInstance().registerRunnable(new Runnable() {
260 @SuppressWarnings("synthetic-access")
262 languageMenu.setText(I18n.getMessage("jsite.menu.languages"));
263 nodeMenu.setText(I18n.getMessage("jsite.menu.nodes"));
264 helpMenu.setText(I18n.getMessage("jsite.menu.help"));
265 for (Map.Entry<Locale, Action> languageActionEntry : languageActions.entrySet()) {
266 languageActionEntry.getValue().putValue(Action.NAME, I18n.getMessage("jsite.menu.language." + languageActionEntry.getKey().getLanguage()));
275 * Initializes all pages.
277 private void initPages() {
278 NodeManagerPage nodeManagerPage = new NodeManagerPage(wizard);
279 nodeManagerPage.setName("page.node-manager");
280 nodeManagerPage.addNodeManagerListener(this);
281 nodeManagerPage.setNodes(configuration.getNodes());
282 pages.put(PageType.PAGE_NODE_MANAGER, nodeManagerPage);
284 ProjectPage projectPage = new ProjectPage(wizard);
285 projectPage.setName("page.project");
286 projectPage.setProjects(configuration.getProjects());
287 projectPage.setFreenetInterface(freenetInterface);
288 projectPage.addListSelectionListener(this);
289 pages.put(PageType.PAGE_PROJECTS, projectPage);
291 ProjectFilesPage projectFilesPage = new ProjectFilesPage(wizard);
292 projectFilesPage.setName("page.project.files");
293 pages.put(PageType.PAGE_PROJECT_FILES, projectFilesPage);
295 ProjectInsertPage projectInsertPage = new ProjectInsertPage(wizard);
296 projectInsertPage.setDebug(debug);
297 projectInsertPage.setName("page.project.insert");
298 projectInsertPage.setFreenetInterface(freenetInterface);
299 pages.put(PageType.PAGE_INSERT_PROJECT, projectInsertPage);
303 * Shows the page with the given type.
306 * The page type to show
308 private void showPage(PageType pageType) {
309 wizard.setPreviousEnabled(pageType.ordinal() > 0);
310 wizard.setNextEnabled(pageType.ordinal() < (pages.size() - 1));
311 wizard.setPage(pages.get(pageType));
312 wizard.setTitle(pages.get(pageType).getHeading() + " - jSite");
316 * Saves the configuration.
318 * @return <code>true</code> if the configuration could be saved,
319 * <code>false</code> otherwise
321 private boolean saveConfiguration() {
322 NodeManagerPage nodeManagerPage = (NodeManagerPage) pages.get(PageType.PAGE_NODE_MANAGER);
323 configuration.setNodes(nodeManagerPage.getNodes());
324 if (selectedNode != null) {
325 configuration.setSelectedNode(selectedNode);
328 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
329 configuration.setProjects(projectPage.getProjects());
331 return configuration.save();
335 * Finds a supported locale for the given locale.
338 * The locale to find a supported locale for
339 * @return The supported locale that was found, or the default locale if no
340 * supported locale could be found
342 private Locale findSupportedLocale(Locale forLocale) {
343 for (Locale locale : SUPPORTED_LOCALES) {
344 if (locale.equals(forLocale)) {
348 for (Locale locale : SUPPORTED_LOCALES) {
349 if (locale.getCountry().equals(forLocale.getCountry()) && locale.getLanguage().equals(forLocale.getLanguage())) {
353 for (Locale locale : SUPPORTED_LOCALES) {
354 if (locale.getLanguage().equals(forLocale.getLanguage())) {
358 return SUPPORTED_LOCALES[0];
366 * Switches the language of the interface to the given locale.
369 * The locale to switch to
371 private void switchLanguage(Locale locale) {
372 Locale supportedLocale = findSupportedLocale(locale);
373 Action languageAction = languageActions.get(supportedLocale);
374 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) languageAction.getValue("menuItem");
375 menuItem.setSelected(true);
376 I18n.setLocale(supportedLocale);
377 for (Runnable i18nRunnable : I18nContainer.getInstance()) {
380 } catch (Throwable t) {
381 /* we probably shouldn't swallow this. */
384 wizard.setPage(wizard.getPage());
385 configuration.setLocale(supportedLocale);
389 * Checks for updates of jSite.
391 private void checkForUpdates() {
396 // INTERFACE ListSelectionListener
402 public void valueChanged(ListSelectionEvent e) {
403 JList list = (JList) e.getSource();
404 int selectedRow = list.getSelectedIndex();
405 wizard.setNextEnabled(selectedRow > -1);
409 // INTERFACE WizardListener
415 public void wizardNextPressed(TWizard wizard) {
416 String pageName = wizard.getPage().getName();
417 if ("page.node-manager".equals(pageName)) {
418 showPage(PageType.PAGE_PROJECTS);
419 } else if ("page.project".equals(pageName)) {
420 ProjectPage projectPage = (ProjectPage) wizard.getPage();
421 Project project = projectPage.getSelectedProject();
422 if ((project.getLocalPath() == null) || (project.getLocalPath().trim().length() == 0)) {
423 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-local-path"), null, JOptionPane.ERROR_MESSAGE);
426 if ((project.getPath() == null) || (project.getPath().trim().length() == 0)) {
427 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-path"), null, JOptionPane.ERROR_MESSAGE);
430 ((ProjectFilesPage) pages.get(PageType.PAGE_PROJECT_FILES)).setProject(project);
431 ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).setProject(project);
432 showPage(PageType.PAGE_PROJECT_FILES);
433 } else if ("page.project.files".equals(pageName)) {
434 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
435 Project project = projectPage.getSelectedProject();
436 if (selectedNode == null) {
437 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
440 if (project.getIndexFile() == null) {
441 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.empty-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
445 File indexFile = new File(project.getLocalPath(), project.getIndexFile());
446 if (!indexFile.exists()) {
447 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.index-missing"), null, JOptionPane.ERROR_MESSAGE);
451 String indexFile = project.getIndexFile();
452 boolean hasIndexFile = (indexFile != null);
453 if (hasIndexFile && !project.getFileOption(indexFile).getContainer().equals("")) {
454 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.container-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
458 if (hasIndexFile && !project.getFileOption(indexFile).getMimeType().equals("text/html")) {
459 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.index-not-html"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
463 Map<String, FileOption> fileOptions = project.getFileOptions();
464 Set<Entry<String, FileOption>> fileOptionEntries = fileOptions.entrySet();
465 for (Entry<String, FileOption> fileOptionEntry : fileOptionEntries) {
466 FileOption fileOption = fileOptionEntry.getValue();
467 if (!fileOption.isInsert() && ((fileOption.getCustomKey().length() == 0) || "CHK@".equals(fileOption.getCustomKey()))) {
468 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.project-files.no-custom-key"), fileOptionEntry.getKey()), null, JOptionPane.ERROR_MESSAGE);
472 boolean nodeRunning = false;
474 nodeRunning = freenetInterface.isNodePresent();
475 } catch (IOException e) {
479 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-running"), null, JOptionPane.ERROR_MESSAGE);
482 configuration.save();
483 showPage(PageType.PAGE_INSERT_PROJECT);
484 ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).startInsert();
485 nodeMenu.setEnabled(false);
486 } else if ("page.project.insert".equals(pageName)) {
487 showPage(PageType.PAGE_PROJECTS);
488 nodeMenu.setEnabled(true);
495 public void wizardPreviousPressed(TWizard wizard) {
496 String pageName = wizard.getPage().getName();
497 if ("page.project".equals(pageName)) {
498 showPage(PageType.PAGE_NODE_MANAGER);
499 } else if ("page.project.files".equals(pageName)) {
500 showPage(PageType.PAGE_PROJECTS);
501 } else if ("page.project.insert".equals(pageName)) {
502 showPage(PageType.PAGE_PROJECT_FILES);
509 public void wizardQuitPressed(TWizard wizard) {
510 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.question"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
511 if (saveConfiguration()) {
514 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.config-not-saved"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
521 // INTERFACE NodeManagerListener
527 public void nodesUpdated(Node[] nodes) {
528 nodeMenu.removeAll();
529 ButtonGroup nodeButtonGroup = new ButtonGroup();
530 Node newSelectedNode = null;
531 for (Node node : nodes) {
532 JRadioButtonMenuItem nodeMenuItem = new JRadioButtonMenuItem(node.getName());
533 nodeMenuItem.putClientProperty("Node", node);
534 nodeMenuItem.addActionListener(this);
535 nodeButtonGroup.add(nodeMenuItem);
536 if (node.equals(selectedNode)) {
537 newSelectedNode = node;
538 nodeMenuItem.setSelected(true);
540 nodeMenu.add(nodeMenuItem);
542 nodeMenu.addSeparator();
543 nodeMenu.add(manageNodeAction);
544 selectedNode = newSelectedNode;
545 freenetInterface.setNode(selectedNode);
551 public void actionPerformed(ActionEvent e) {
552 Object source = e.getSource();
553 if (source instanceof JRadioButtonMenuItem) {
554 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) source;
555 Node node = (Node) menuItem.getClientProperty("Node");
557 freenetInterface.setNode(selectedNode);
566 * Main method that is called by the VM.
569 * The command-line arguments
571 public static void main(String[] args) {
572 String configFilename = null;
573 boolean nextIsConfigFilename = false;
574 for (String argument : args) {
575 if (nextIsConfigFilename) {
576 configFilename = argument;
577 nextIsConfigFilename = false;
579 if ("--help".equals(argument)) {
582 } else if ("--debug".equals(argument)) {
584 } else if ("--config-file".equals(argument)) {
585 nextIsConfigFilename = true;
588 if (nextIsConfigFilename) {
589 System.out.println("--config-file needs parameter!");
592 new Main(configFilename);
596 * Prints a small syntax help.
598 private static void printHelp() {
599 System.out.println("--help\tshows this cruft");
600 System.out.println("--debug\tenables some debug output");
601 System.out.println("--config-file <file>\tuse specified configuration file");