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.BorderLayout;
23 import java.awt.event.ActionEvent;
24 import java.awt.event.ActionListener;
26 import java.io.IOException;
27 import java.text.MessageFormat;
28 import java.util.HashMap;
29 import java.util.Locale;
32 import java.util.Map.Entry;
34 import javax.swing.AbstractAction;
35 import javax.swing.Action;
36 import javax.swing.ButtonGroup;
37 import javax.swing.Icon;
38 import javax.swing.JDialog;
39 import javax.swing.JLabel;
40 import javax.swing.JList;
41 import javax.swing.JMenu;
42 import javax.swing.JMenuBar;
43 import javax.swing.JOptionPane;
44 import javax.swing.JPanel;
45 import javax.swing.JProgressBar;
46 import javax.swing.JRadioButtonMenuItem;
47 import javax.swing.event.ListSelectionEvent;
48 import javax.swing.event.ListSelectionListener;
50 import de.todesbaum.jsite.application.FileOption;
51 import de.todesbaum.jsite.application.Freenet7Interface;
52 import de.todesbaum.jsite.application.Node;
53 import de.todesbaum.jsite.application.Project;
54 import de.todesbaum.jsite.gui.NodeManagerListener;
55 import de.todesbaum.jsite.gui.NodeManagerPage;
56 import de.todesbaum.jsite.gui.ProjectFilesPage;
57 import de.todesbaum.jsite.gui.ProjectInsertPage;
58 import de.todesbaum.jsite.gui.ProjectPage;
59 import de.todesbaum.jsite.i18n.I18n;
60 import de.todesbaum.jsite.i18n.I18nContainer;
61 import de.todesbaum.util.image.IconLoader;
62 import de.todesbaum.util.swing.TWizard;
63 import de.todesbaum.util.swing.TWizardPage;
64 import de.todesbaum.util.swing.WizardListener;
67 * The main class that ties together everything.
69 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
71 public class Main implements ActionListener, ListSelectionListener, WizardListener, NodeManagerListener {
73 /** Whether the debug mode is activated. */
74 private static boolean debug = false;
76 /** The URL for update checks. */
77 private static final String UPDATE_KEY = "USK@e3myoFyp5avg6WYN16ImHri6J7Nj8980Fm~aQe4EX1U,QvbWT0ImE0TwLODTl7EoJx2NBnwDxTbLTE6zkB-eGPs,AQACAAE/jSite/0/currentVersion.txt";
79 /** The configuration. */
80 private Configuration configuration;
82 /** The freenet interface. */
83 private Freenet7Interface freenetInterface = new Freenet7Interface();
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"));
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);
175 showPage(PageType.PAGE_PROJECTS);
179 * Creates all actions.
181 private void createActions() {
182 for (final Locale locale : SUPPORTED_LOCALES) {
183 languageActions.put(locale, new AbstractAction(I18n.getMessage("jsite.menu.language." + locale.getLanguage()), IconLoader.loadIcon("/flag-" + locale.getLanguage() + ".png")) {
185 @SuppressWarnings("synthetic-access")
186 public void actionPerformed(ActionEvent actionEvent) {
187 switchLanguage(locale);
191 manageNodeAction = new AbstractAction(I18n.getMessage("jsite.menu.nodes.manage-nodes")) {
193 @SuppressWarnings("synthetic-access")
194 public void actionPerformed(ActionEvent actionEvent) {
195 showPage(PageType.PAGE_NODE_MANAGER);
196 wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
197 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
200 checkForUpdatesAction = new AbstractAction(I18n.getMessage("jsite.menu.help.check-for-updates")) {
205 @SuppressWarnings("synthetic-access")
206 public void actionPerformed(ActionEvent actionEvent) {
210 aboutAction = new AbstractAction(I18n.getMessage("jsite.menu.help.about")) {
212 @SuppressWarnings("synthetic-access")
213 public void actionPerformed(ActionEvent e) {
214 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.about.message"), Version.getVersion()), null, JOptionPane.INFORMATION_MESSAGE, jSiteIcon);
218 I18nContainer.getInstance().registerRunnable(new Runnable() {
220 @SuppressWarnings("synthetic-access")
222 manageNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.nodes.manage-nodes"));
223 aboutAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.help.about"));
229 * Creates the menu bar.
231 * @return The menu bar
233 private JMenuBar createMenuBar() {
234 JMenuBar menuBar = new JMenuBar();
235 final JMenu languageMenu = new JMenu(I18n.getMessage("jsite.menu.languages"));
236 menuBar.add(languageMenu);
237 ButtonGroup languageButtonGroup = new ButtonGroup();
238 for (Locale locale : SUPPORTED_LOCALES) {
239 Action languageAction = languageActions.get(locale);
240 JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(languageActions.get(locale));
241 if (locale.equals(Locale.getDefault())) {
242 menuItem.setSelected(true);
244 languageAction.putValue("menuItem", menuItem);
245 languageButtonGroup.add(menuItem);
246 languageMenu.add(menuItem);
248 nodeMenu = new JMenu(I18n.getMessage("jsite.menu.nodes"));
249 menuBar.add(nodeMenu);
250 selectedNode = configuration.getSelectedNode();
251 nodesUpdated(configuration.getNodes());
253 /* evil hack to right-align the help menu */
254 JPanel panel = new JPanel();
255 panel.setOpaque(false);
258 final JMenu helpMenu = new JMenu(I18n.getMessage("jsite.menu.help"));
259 menuBar.add(helpMenu);
260 helpMenu.add(checkForUpdatesAction);
261 helpMenu.add(aboutAction);
263 I18nContainer.getInstance().registerRunnable(new Runnable() {
265 @SuppressWarnings("synthetic-access")
267 languageMenu.setText(I18n.getMessage("jsite.menu.languages"));
268 nodeMenu.setText(I18n.getMessage("jsite.menu.nodes"));
269 helpMenu.setText(I18n.getMessage("jsite.menu.help"));
270 for (Map.Entry<Locale, Action> languageActionEntry : languageActions.entrySet()) {
271 languageActionEntry.getValue().putValue(Action.NAME, I18n.getMessage("jsite.menu.language." + languageActionEntry.getKey().getLanguage()));
280 * Initializes all pages.
282 private void initPages() {
283 NodeManagerPage nodeManagerPage = new NodeManagerPage(wizard);
284 nodeManagerPage.setName("page.node-manager");
285 nodeManagerPage.addNodeManagerListener(this);
286 nodeManagerPage.setNodes(configuration.getNodes());
287 pages.put(PageType.PAGE_NODE_MANAGER, nodeManagerPage);
289 ProjectPage projectPage = new ProjectPage(wizard);
290 projectPage.setName("page.project");
291 projectPage.setProjects(configuration.getProjects());
292 projectPage.setFreenetInterface(freenetInterface);
293 projectPage.addListSelectionListener(this);
294 pages.put(PageType.PAGE_PROJECTS, projectPage);
296 ProjectFilesPage projectFilesPage = new ProjectFilesPage(wizard);
297 projectFilesPage.setName("page.project.files");
298 pages.put(PageType.PAGE_PROJECT_FILES, projectFilesPage);
300 ProjectInsertPage projectInsertPage = new ProjectInsertPage(wizard);
301 projectInsertPage.setDebug(debug);
302 projectInsertPage.setName("page.project.insert");
303 projectInsertPage.setFreenetInterface(freenetInterface);
304 pages.put(PageType.PAGE_INSERT_PROJECT, projectInsertPage);
308 * Shows the page with the given type.
311 * The page type to show
313 private void showPage(PageType pageType) {
314 wizard.setPreviousEnabled(pageType.ordinal() > 0);
315 wizard.setNextEnabled(pageType.ordinal() < (pages.size() - 1));
316 wizard.setPage(pages.get(pageType));
317 wizard.setTitle(pages.get(pageType).getHeading() + " - jSite");
321 * Saves the configuration.
323 * @return <code>true</code> if the configuration could be saved,
324 * <code>false</code> otherwise
326 private boolean saveConfiguration() {
327 NodeManagerPage nodeManagerPage = (NodeManagerPage) pages.get(PageType.PAGE_NODE_MANAGER);
328 configuration.setNodes(nodeManagerPage.getNodes());
329 if (selectedNode != null) {
330 configuration.setSelectedNode(selectedNode);
333 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
334 configuration.setProjects(projectPage.getProjects());
336 return configuration.save();
340 * Finds a supported locale for the given locale.
343 * The locale to find a supported locale for
344 * @return The supported locale that was found, or the default locale if no
345 * supported locale could be found
347 private Locale findSupportedLocale(Locale forLocale) {
348 for (Locale locale : SUPPORTED_LOCALES) {
349 if (locale.equals(forLocale)) {
353 for (Locale locale : SUPPORTED_LOCALES) {
354 if (locale.getCountry().equals(forLocale.getCountry()) && locale.getLanguage().equals(forLocale.getLanguage())) {
358 for (Locale locale : SUPPORTED_LOCALES) {
359 if (locale.getLanguage().equals(forLocale.getLanguage())) {
363 return SUPPORTED_LOCALES[0];
371 * Switches the language of the interface to the given locale.
374 * The locale to switch to
376 private void switchLanguage(Locale locale) {
377 Locale supportedLocale = findSupportedLocale(locale);
378 Action languageAction = languageActions.get(supportedLocale);
379 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) languageAction.getValue("menuItem");
380 menuItem.setSelected(true);
381 I18n.setLocale(supportedLocale);
382 for (Runnable i18nRunnable : I18nContainer.getInstance()) {
385 } catch (Throwable t) {
386 /* we probably shouldn't swallow this. */
389 wizard.setPage(wizard.getPage());
390 configuration.setLocale(supportedLocale);
394 * Checks for updates of jSite.
396 private void checkForUpdates() {
397 System.out.println("checkForUpdates()");
398 /* construct a small panel for the dialog. */
399 JPanel waitingDialogPanel = new JPanel(new BorderLayout(12, 12));
400 waitingDialogPanel.add(new JLabel(I18n.getMessage("")), BorderLayout.PAGE_START);
401 JProgressBar progressBar = new JProgressBar();
402 progressBar.setIndeterminate(true);
403 waitingDialogPanel.add(progressBar, BorderLayout.PAGE_END);
404 JOptionPane waitingDialog = new JOptionPane(waitingDialogPanel, JOptionPane.INFORMATION_MESSAGE, 0, null, new Object[] { "Cancel" });
405 JDialog dialog = new JDialog(wizard, true);
406 dialog.getContentPane().add(waitingDialog, BorderLayout.CENTER);
408 dialog.setVisible(true);
412 // INTERFACE ListSelectionListener
418 public void valueChanged(ListSelectionEvent e) {
419 JList list = (JList) e.getSource();
420 int selectedRow = list.getSelectedIndex();
421 wizard.setNextEnabled(selectedRow > -1);
425 // INTERFACE WizardListener
431 public void wizardNextPressed(TWizard wizard) {
432 String pageName = wizard.getPage().getName();
433 if ("page.node-manager".equals(pageName)) {
434 showPage(PageType.PAGE_PROJECTS);
435 } else if ("page.project".equals(pageName)) {
436 ProjectPage projectPage = (ProjectPage) wizard.getPage();
437 Project project = projectPage.getSelectedProject();
438 if ((project.getLocalPath() == null) || (project.getLocalPath().trim().length() == 0)) {
439 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-local-path"), null, JOptionPane.ERROR_MESSAGE);
442 if ((project.getPath() == null) || (project.getPath().trim().length() == 0)) {
443 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-path"), null, JOptionPane.ERROR_MESSAGE);
446 ((ProjectFilesPage) pages.get(PageType.PAGE_PROJECT_FILES)).setProject(project);
447 ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).setProject(project);
448 showPage(PageType.PAGE_PROJECT_FILES);
449 } else if ("page.project.files".equals(pageName)) {
450 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
451 Project project = projectPage.getSelectedProject();
452 if (selectedNode == null) {
453 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
456 if (project.getIndexFile() == null) {
457 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.empty-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
461 File indexFile = new File(project.getLocalPath(), project.getIndexFile());
462 if (!indexFile.exists()) {
463 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.index-missing"), null, JOptionPane.ERROR_MESSAGE);
467 String indexFile = project.getIndexFile();
468 boolean hasIndexFile = (indexFile != null);
469 if (hasIndexFile && !project.getFileOption(indexFile).getContainer().equals("")) {
470 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.container-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
474 if (hasIndexFile && !project.getFileOption(indexFile).getMimeType().equals("text/html")) {
475 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.index-not-html"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
479 Map<String, FileOption> fileOptions = project.getFileOptions();
480 Set<Entry<String, FileOption>> fileOptionEntries = fileOptions.entrySet();
481 for (Entry<String, FileOption> fileOptionEntry : fileOptionEntries) {
482 FileOption fileOption = fileOptionEntry.getValue();
483 if (!fileOption.isInsert() && ((fileOption.getCustomKey().length() == 0) || "CHK@".equals(fileOption.getCustomKey()))) {
484 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.project-files.no-custom-key"), fileOptionEntry.getKey()), null, JOptionPane.ERROR_MESSAGE);
488 boolean nodeRunning = false;
490 nodeRunning = freenetInterface.isNodePresent();
491 } catch (IOException e) {
495 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-running"), null, JOptionPane.ERROR_MESSAGE);
498 configuration.save();
499 showPage(PageType.PAGE_INSERT_PROJECT);
500 ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).startInsert();
501 nodeMenu.setEnabled(false);
502 } else if ("page.project.insert".equals(pageName)) {
503 showPage(PageType.PAGE_PROJECTS);
504 nodeMenu.setEnabled(true);
511 public void wizardPreviousPressed(TWizard wizard) {
512 String pageName = wizard.getPage().getName();
513 if ("page.project".equals(pageName)) {
514 showPage(PageType.PAGE_NODE_MANAGER);
515 } else if ("page.project.files".equals(pageName)) {
516 showPage(PageType.PAGE_PROJECTS);
517 } else if ("page.project.insert".equals(pageName)) {
518 showPage(PageType.PAGE_PROJECT_FILES);
525 public void wizardQuitPressed(TWizard wizard) {
526 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.question"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
527 if (saveConfiguration()) {
530 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.config-not-saved"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
537 // INTERFACE NodeManagerListener
543 public void nodesUpdated(Node[] nodes) {
544 nodeMenu.removeAll();
545 ButtonGroup nodeButtonGroup = new ButtonGroup();
546 Node newSelectedNode = null;
547 for (Node node : nodes) {
548 JRadioButtonMenuItem nodeMenuItem = new JRadioButtonMenuItem(node.getName());
549 nodeMenuItem.putClientProperty("Node", node);
550 nodeMenuItem.addActionListener(this);
551 nodeButtonGroup.add(nodeMenuItem);
552 if (node.equals(selectedNode)) {
553 newSelectedNode = node;
554 nodeMenuItem.setSelected(true);
556 nodeMenu.add(nodeMenuItem);
558 nodeMenu.addSeparator();
559 nodeMenu.add(manageNodeAction);
560 selectedNode = newSelectedNode;
561 freenetInterface.setNode(selectedNode);
567 public void actionPerformed(ActionEvent e) {
568 Object source = e.getSource();
569 if (source instanceof JRadioButtonMenuItem) {
570 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) source;
571 Node node = (Node) menuItem.getClientProperty("Node");
573 freenetInterface.setNode(selectedNode);
582 * Main method that is called by the VM.
585 * The command-line arguments
587 public static void main(String[] args) {
588 String configFilename = null;
589 boolean nextIsConfigFilename = false;
590 for (String argument : args) {
591 if (nextIsConfigFilename) {
592 configFilename = argument;
593 nextIsConfigFilename = false;
595 if ("--help".equals(argument)) {
598 } else if ("--debug".equals(argument)) {
600 } else if ("--config-file".equals(argument)) {
601 nextIsConfigFilename = true;
604 if (nextIsConfigFilename) {
605 System.out.println("--config-file needs parameter!");
608 new Main(configFilename);
612 * Prints a small syntax help.
614 private static void printHelp() {
615 System.out.println("--help\tshows this cruft");
616 System.out.println("--debug\tenables some debug output");
617 System.out.println("--config-file <file>\tuse specified configuration file");