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 configuration. */
73 private Configuration configuration;
75 /** The freenet interface. */
76 private Freenet7Interface freenetInterface = new Freenet7Interface();
78 /** The jSite icon. */
79 private Icon jSiteIcon;
82 * Enumeration for all possible pages.
84 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
86 private static enum PageType {
88 /** The node manager page. */
91 /** The project page. */
94 /** The project files page. */
97 /** The project insert page. */
102 /** The supported locales. */
103 private static final Locale[] SUPPORTED_LOCALES = new Locale[] { Locale.ENGLISH, Locale.GERMAN, Locale.FRENCH, Locale.ITALIAN, new Locale("pl") };
105 /** The actions that switch the language. */
106 private Map<Locale, Action> languageActions = new HashMap<Locale, Action>();
108 /** The “manage nodes” action. */
109 private Action manageNodeAction;
111 /** The “about jSite” action. */
112 private Action aboutAction;
115 private TWizard wizard;
117 /** The node menu. */
118 private JMenu nodeMenu;
120 /** The currently selected node. */
121 private Node selectedNode;
123 /** Mapping from page type to page. */
124 private final Map<PageType, TWizardPage> pages = new HashMap<PageType, TWizardPage>();
127 * Creates a new core with the default configuration file.
134 * Creates a new core with the given configuration from the given file.
136 * @param configFilename
137 * The name of the configuration file
139 private Main(String configFilename) {
140 if (configFilename != null) {
141 configuration = new Configuration(configFilename);
143 configuration = new Configuration();
145 Locale.setDefault(configuration.getLocale());
146 I18n.setLocale(configuration.getLocale());
147 if (!configuration.createLockFile()) {
148 JOptionPane.showMessageDialog(null, I18n.getMessage("jsite.main.already-running"), null, JOptionPane.ERROR_MESSAGE);
151 wizard = new TWizard();
153 wizard.setJMenuBar(createMenuBar());
154 wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
155 wizard.setPreviousEnabled(false);
156 wizard.setNextEnabled(true);
157 wizard.addWizardListener(this);
158 jSiteIcon = IconLoader.loadIcon("/jsite-icon.png");
159 wizard.setIcon(jSiteIcon);
162 showPage(PageType.PAGE_PROJECTS);
166 * Creates all actions.
168 private void createActions() {
169 for (final Locale locale : SUPPORTED_LOCALES) {
170 languageActions.put(locale, new AbstractAction(I18n.getMessage("jsite.menu.language." + locale.getLanguage()), IconLoader.loadIcon("/flag-" + locale.getLanguage() + ".png")) {
172 @SuppressWarnings("synthetic-access")
173 public void actionPerformed(ActionEvent actionEvent) {
174 switchLanguage(locale);
178 manageNodeAction = new AbstractAction(I18n.getMessage("jsite.menu.nodes.manage-nodes")) {
180 @SuppressWarnings("synthetic-access")
181 public void actionPerformed(ActionEvent actionEvent) {
182 showPage(PageType.PAGE_NODE_MANAGER);
183 wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
184 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
187 aboutAction = new AbstractAction(I18n.getMessage("jsite.menu.help.about")) {
189 @SuppressWarnings("synthetic-access")
190 public void actionPerformed(ActionEvent e) {
191 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.about.message"), Version.getVersion()), null, JOptionPane.INFORMATION_MESSAGE, jSiteIcon);
195 I18nContainer.getInstance().registerRunnable(new Runnable() {
197 @SuppressWarnings("synthetic-access")
199 manageNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.nodes.manage-nodes"));
200 aboutAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.help.about"));
206 * Creates the menu bar.
208 * @return The menu bar
210 private JMenuBar createMenuBar() {
211 JMenuBar menuBar = new JMenuBar();
212 final JMenu languageMenu = new JMenu(I18n.getMessage("jsite.menu.languages"));
213 menuBar.add(languageMenu);
214 ButtonGroup languageButtonGroup = new ButtonGroup();
215 for (Locale locale : SUPPORTED_LOCALES) {
216 Action languageAction = languageActions.get(locale);
217 JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(languageActions.get(locale));
218 if (locale.equals(Locale.getDefault())) {
219 menuItem.setSelected(true);
221 languageAction.putValue("menuItem", menuItem);
222 languageButtonGroup.add(menuItem);
223 languageMenu.add(menuItem);
225 nodeMenu = new JMenu(I18n.getMessage("jsite.menu.nodes"));
226 menuBar.add(nodeMenu);
227 selectedNode = configuration.getSelectedNode();
228 nodesUpdated(configuration.getNodes());
230 /* evil hack to right-align the help menu */
231 JPanel panel = new JPanel();
232 panel.setOpaque(false);
235 final JMenu helpMenu = new JMenu(I18n.getMessage("jsite.menu.help"));
236 menuBar.add(helpMenu);
237 helpMenu.add(aboutAction);
239 I18nContainer.getInstance().registerRunnable(new Runnable() {
241 @SuppressWarnings("synthetic-access")
243 languageMenu.setText(I18n.getMessage("jsite.menu.languages"));
244 nodeMenu.setText(I18n.getMessage("jsite.menu.nodes"));
245 helpMenu.setText(I18n.getMessage("jsite.menu.help"));
246 for (Map.Entry<Locale, Action> languageActionEntry : languageActions.entrySet()) {
247 languageActionEntry.getValue().putValue(Action.NAME, I18n.getMessage("jsite.menu.language." + languageActionEntry.getKey().getLanguage()));
256 * Initializes all pages.
258 private void initPages() {
259 NodeManagerPage nodeManagerPage = new NodeManagerPage(wizard);
260 nodeManagerPage.setName("page.node-manager");
261 nodeManagerPage.addNodeManagerListener(this);
262 nodeManagerPage.setNodes(configuration.getNodes());
263 pages.put(PageType.PAGE_NODE_MANAGER, nodeManagerPage);
265 ProjectPage projectPage = new ProjectPage(wizard);
266 projectPage.setName("page.project");
267 projectPage.setProjects(configuration.getProjects());
268 projectPage.setFreenetInterface(freenetInterface);
269 projectPage.addListSelectionListener(this);
270 pages.put(PageType.PAGE_PROJECTS, projectPage);
272 ProjectFilesPage projectFilesPage = new ProjectFilesPage(wizard);
273 projectFilesPage.setName("page.project.files");
274 pages.put(PageType.PAGE_PROJECT_FILES, projectFilesPage);
276 ProjectInsertPage projectInsertPage = new ProjectInsertPage(wizard);
277 projectInsertPage.setDebug(debug);
278 projectInsertPage.setName("page.project.insert");
279 projectInsertPage.setFreenetInterface(freenetInterface);
280 pages.put(PageType.PAGE_INSERT_PROJECT, projectInsertPage);
284 * Shows the page with the given type.
287 * The page type to show
289 private void showPage(PageType pageType) {
290 wizard.setPreviousEnabled(pageType.ordinal() > 0);
291 wizard.setNextEnabled(pageType.ordinal() < (pages.size() - 1));
292 wizard.setPage(pages.get(pageType));
293 wizard.setTitle(pages.get(pageType).getHeading() + " - jSite");
297 * Saves the configuration.
299 * @return <code>true</code> if the configuration could be saved,
300 * <code>false</code> otherwise
302 private boolean saveConfiguration() {
303 NodeManagerPage nodeManagerPage = (NodeManagerPage) pages.get(PageType.PAGE_NODE_MANAGER);
304 configuration.setNodes(nodeManagerPage.getNodes());
305 if (selectedNode != null) {
306 configuration.setSelectedNode(selectedNode);
309 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
310 configuration.setProjects(projectPage.getProjects());
312 return configuration.save();
316 * Finds a supported locale for the given locale.
319 * The locale to find a supported locale for
320 * @return The supported locale that was found, or the default locale if no
321 * supported locale could be found
323 private Locale findSupportedLocale(Locale forLocale) {
324 for (Locale locale : SUPPORTED_LOCALES) {
325 if (locale.equals(forLocale)) {
329 for (Locale locale : SUPPORTED_LOCALES) {
330 if (locale.getCountry().equals(forLocale.getCountry()) && locale.getLanguage().equals(forLocale.getLanguage())) {
334 for (Locale locale : SUPPORTED_LOCALES) {
335 if (locale.getLanguage().equals(forLocale.getLanguage())) {
339 return SUPPORTED_LOCALES[0];
347 * Switches the language of the interface to the given locale.
350 * The locale to switch to
352 private void switchLanguage(Locale locale) {
353 Locale supportedLocale = findSupportedLocale(locale);
354 Action languageAction = languageActions.get(supportedLocale);
355 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) languageAction.getValue("menuItem");
356 menuItem.setSelected(true);
357 I18n.setLocale(supportedLocale);
358 for (Runnable i18nRunnable : I18nContainer.getInstance()) {
361 } catch (Throwable t) {
362 /* we probably shouldn't swallow this. */
365 wizard.setPage(wizard.getPage());
366 configuration.setLocale(supportedLocale);
370 // INTERFACE ListSelectionListener
376 public void valueChanged(ListSelectionEvent e) {
377 JList list = (JList) e.getSource();
378 int selectedRow = list.getSelectedIndex();
379 wizard.setNextEnabled(selectedRow > -1);
383 // INTERFACE WizardListener
389 public void wizardNextPressed(TWizard wizard) {
390 String pageName = wizard.getPage().getName();
391 if ("page.node-manager".equals(pageName)) {
392 showPage(PageType.PAGE_PROJECTS);
393 } else if ("page.project".equals(pageName)) {
394 ProjectPage projectPage = (ProjectPage) wizard.getPage();
395 Project project = projectPage.getSelectedProject();
396 if ((project.getLocalPath() == null) || (project.getLocalPath().trim().length() == 0)) {
397 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-local-path"), null, JOptionPane.ERROR_MESSAGE);
400 if ((project.getPath() == null) || (project.getPath().trim().length() == 0)) {
401 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-path"), null, JOptionPane.ERROR_MESSAGE);
404 ((ProjectFilesPage) pages.get(PageType.PAGE_PROJECT_FILES)).setProject(project);
405 ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).setProject(project);
406 showPage(PageType.PAGE_PROJECT_FILES);
407 } else if ("page.project.files".equals(pageName)) {
408 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
409 Project project = projectPage.getSelectedProject();
410 if (selectedNode == null) {
411 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
414 if (project.getIndexFile() == null) {
415 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.empty-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
419 File indexFile = new File(project.getLocalPath(), project.getIndexFile());
420 if (!indexFile.exists()) {
421 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.index-missing"), null, JOptionPane.ERROR_MESSAGE);
425 String indexFile = project.getIndexFile();
426 boolean hasIndexFile = (indexFile != null);
427 if (hasIndexFile && !project.getFileOption(indexFile).getContainer().equals("")) {
428 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.container-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
432 if (hasIndexFile && !project.getFileOption(indexFile).getMimeType().equals("text/html")) {
433 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.index-not-html"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
437 Map<String, FileOption> fileOptions = project.getFileOptions();
438 Set<Entry<String, FileOption>> fileOptionEntries = fileOptions.entrySet();
439 for (Entry<String, FileOption> fileOptionEntry : fileOptionEntries) {
440 FileOption fileOption = fileOptionEntry.getValue();
441 if (!fileOption.isInsert() && ((fileOption.getCustomKey().length() == 0) || "CHK@".equals(fileOption.getCustomKey()))) {
442 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.project-files.no-custom-key"), fileOptionEntry.getKey()), null, JOptionPane.ERROR_MESSAGE);
446 boolean nodeRunning = false;
448 nodeRunning = freenetInterface.isNodePresent();
449 } catch (IOException e) {
453 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-running"), null, JOptionPane.ERROR_MESSAGE);
456 configuration.save();
457 showPage(PageType.PAGE_INSERT_PROJECT);
458 ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).startInsert();
459 nodeMenu.setEnabled(false);
460 } else if ("page.project.insert".equals(pageName)) {
461 showPage(PageType.PAGE_PROJECTS);
462 nodeMenu.setEnabled(true);
469 public void wizardPreviousPressed(TWizard wizard) {
470 String pageName = wizard.getPage().getName();
471 if ("page.project".equals(pageName)) {
472 showPage(PageType.PAGE_NODE_MANAGER);
473 } else if ("page.project.files".equals(pageName)) {
474 showPage(PageType.PAGE_PROJECTS);
475 } else if ("page.project.insert".equals(pageName)) {
476 showPage(PageType.PAGE_PROJECT_FILES);
483 public void wizardQuitPressed(TWizard wizard) {
484 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.question"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
485 if (saveConfiguration()) {
488 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.config-not-saved"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
495 // INTERFACE NodeManagerListener
501 public void nodesUpdated(Node[] nodes) {
502 nodeMenu.removeAll();
503 ButtonGroup nodeButtonGroup = new ButtonGroup();
504 Node newSelectedNode = null;
505 for (Node node : nodes) {
506 JRadioButtonMenuItem nodeMenuItem = new JRadioButtonMenuItem(node.getName());
507 nodeMenuItem.putClientProperty("Node", node);
508 nodeMenuItem.addActionListener(this);
509 nodeButtonGroup.add(nodeMenuItem);
510 if (node.equals(selectedNode)) {
511 newSelectedNode = node;
512 nodeMenuItem.setSelected(true);
514 nodeMenu.add(nodeMenuItem);
516 nodeMenu.addSeparator();
517 nodeMenu.add(manageNodeAction);
518 selectedNode = newSelectedNode;
519 freenetInterface.setNode(selectedNode);
525 public void actionPerformed(ActionEvent e) {
526 Object source = e.getSource();
527 if (source instanceof JRadioButtonMenuItem) {
528 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) source;
529 Node node = (Node) menuItem.getClientProperty("Node");
531 freenetInterface.setNode(selectedNode);
540 * Main method that is called by the VM.
543 * The command-line arguments
545 public static void main(String[] args) {
546 String configFilename = null;
547 boolean nextIsConfigFilename = false;
548 for (String argument : args) {
549 if (nextIsConfigFilename) {
550 configFilename = argument;
551 nextIsConfigFilename = false;
553 if ("--help".equals(argument)) {
556 } else if ("--debug".equals(argument)) {
558 } else if ("--config-file".equals(argument)) {
559 nextIsConfigFilename = true;
562 if (nextIsConfigFilename) {
563 System.out.println("--config-file needs parameter!");
566 new Main(configFilename);
570 * Prints a small syntax help.
572 private static void printHelp() {
573 System.out.println("--help\tshows this cruft");
574 System.out.println("--debug\tenables some debug output");
575 System.out.println("--config-file <file>\tuse specified configuration file");