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 * @author <a href="mailto:droden@gmail.com">David Roden </a>
66 public class Main implements ActionListener, ListSelectionListener, WizardListener, NodeManagerListener {
68 private static boolean debug = false;
69 private Configuration configuration;
70 private Freenet7Interface freenetInterface = new Freenet7Interface();
71 protected Icon jSiteIcon;
73 private static enum PageType {
74 PAGE_NODE_MANAGER, PAGE_PROJECTS, PAGE_PROJECT_FILES, PAGE_INSERT_PROJECT
77 private static final Locale[] SUPPORTED_LOCALES = new Locale[] { Locale.ENGLISH, Locale.GERMAN, Locale.FRENCH };
78 protected Map<Locale, Action> languageActions = new HashMap<Locale, Action>();
79 protected Action manageNodeAction;
80 protected Action aboutAction;
81 protected TWizard wizard;
82 protected JMenu nodeMenu;
83 private Node selectedNode;
84 private final Map<PageType, TWizardPage> pages = new HashMap<PageType, TWizardPage>();
90 private Main(String configFilename) {
91 if (configFilename != null) {
92 configuration = new Configuration(configFilename);
94 configuration = new Configuration();
96 Locale.setDefault(configuration.getLocale());
97 I18n.setLocale(configuration.getLocale());
98 if (!configuration.createLockFile()) {
99 JOptionPane.showMessageDialog(null, I18n.getMessage("jsite.main.already-running"), null, JOptionPane.ERROR_MESSAGE);
102 wizard = new TWizard();
104 wizard.setJMenuBar(createMenuBar());
105 wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
106 wizard.setPreviousEnabled(false);
107 wizard.setNextEnabled(true);
108 wizard.addWizardListener(this);
109 jSiteIcon = IconLoader.loadIcon("/jsite-icon.png");
110 wizard.setIcon(jSiteIcon);
113 showPage(PageType.PAGE_PROJECTS);
116 private void createActions() {
117 for (final Locale locale: SUPPORTED_LOCALES) {
118 languageActions.put(locale, new AbstractAction(I18n.getMessage("jsite.menu.language." + locale.getLanguage())) {
120 public void actionPerformed(ActionEvent actionEvent) {
121 switchLanguage(locale);
125 manageNodeAction = new AbstractAction(I18n.getMessage("jsite.menu.nodes.manage-nodes")) {
126 public void actionPerformed(ActionEvent actionEvent) {
127 showPage(PageType.PAGE_NODE_MANAGER);
128 wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
129 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
132 aboutAction = new AbstractAction(I18n.getMessage("jsite.menu.help.about")) {
134 public void actionPerformed(ActionEvent e) {
135 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.about.message"), Version.getVersion()), null, JOptionPane.INFORMATION_MESSAGE, jSiteIcon);
139 I18nContainer.getInstance().registerRunnable(new Runnable() {
141 manageNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.nodes.manage-nodes"));
142 aboutAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.help.about"));
147 private JMenuBar createMenuBar() {
148 JMenuBar menuBar = new JMenuBar();
149 final JMenu languageMenu = new JMenu(I18n.getMessage("jsite.menu.languages"));
150 menuBar.add(languageMenu);
151 ButtonGroup languageButtonGroup = new ButtonGroup();
152 for (Locale locale: SUPPORTED_LOCALES) {
153 Action languageAction = languageActions.get(locale);
154 JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(languageActions.get(locale));
155 if (locale.equals(Locale.getDefault())) {
156 menuItem.setSelected(true);
158 languageAction.putValue("menuItem", menuItem);
159 languageButtonGroup.add(menuItem);
160 languageMenu.add(menuItem);
162 nodeMenu = new JMenu(I18n.getMessage("jsite.menu.nodes"));
163 menuBar.add(nodeMenu);
164 selectedNode = configuration.getSelectedNode();
165 nodesUpdated(configuration.getNodes());
167 /* evil hack to right-align the help menu */
168 JPanel panel = new JPanel();
169 panel.setOpaque(false);
172 final JMenu helpMenu = new JMenu(I18n.getMessage("jsite.menu.help"));
173 menuBar.add(helpMenu);
174 helpMenu.add(aboutAction);
176 I18nContainer.getInstance().registerRunnable(new Runnable() {
178 languageMenu.setText(I18n.getMessage("jsite.menu.languages"));
179 nodeMenu.setText(I18n.getMessage("jsite.menu.nodes"));
180 helpMenu.setText(I18n.getMessage("jsite.menu.help"));
181 for (Map.Entry<Locale, Action> languageActionEntry: languageActions.entrySet()) {
182 languageActionEntry.getValue().putValue(Action.NAME, I18n.getMessage("jsite.menu.language." + languageActionEntry.getKey().getLanguage()));
190 private void initPages() {
191 NodeManagerPage nodeManagerPage = new NodeManagerPage(wizard);
192 nodeManagerPage.setName("page.node-manager");
193 nodeManagerPage.addNodeManagerListener(this);
194 nodeManagerPage.setNodes(configuration.getNodes());
195 pages.put(PageType.PAGE_NODE_MANAGER, nodeManagerPage);
197 ProjectPage projectPage = new ProjectPage(wizard);
198 projectPage.setName("page.project");
199 projectPage.setProjects(configuration.getProjects());
200 projectPage.setFreenetInterface(freenetInterface);
201 projectPage.addListSelectionListener(this);
202 pages.put(PageType.PAGE_PROJECTS, projectPage);
204 ProjectFilesPage projectFilesPage = new ProjectFilesPage(wizard);
205 projectFilesPage.setName("page.project.files");
206 pages.put(PageType.PAGE_PROJECT_FILES, projectFilesPage);
208 ProjectInsertPage projectInsertPage = new ProjectInsertPage(wizard);
209 projectInsertPage.setDebug(debug);
210 projectInsertPage.setName("page.project.insert");
211 projectInsertPage.setFreenetInterface(freenetInterface);
212 pages.put(PageType.PAGE_INSERT_PROJECT, projectInsertPage);
215 protected void showPage(PageType pageType) {
216 wizard.setPreviousEnabled(pageType.ordinal() > 0);
217 wizard.setNextEnabled(pageType.ordinal() < (pages.size() - 1));
218 wizard.setPage(pages.get(pageType));
219 wizard.setTitle(pages.get(pageType).getHeading() + " - jSite");
222 private boolean saveConfiguration() {
223 NodeManagerPage nodeManagerPage = (NodeManagerPage) pages.get(PageType.PAGE_NODE_MANAGER);
224 configuration.setNodes(nodeManagerPage.getNodes());
225 if (selectedNode != null) {
226 configuration.setSelectedNode(selectedNode);
229 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
230 configuration.setProjects(projectPage.getProjects());
232 return configuration.save();
235 private Locale findSupportedLocale(Locale forLocale) {
236 for (Locale locale: SUPPORTED_LOCALES) {
237 if (locale.equals(forLocale)) {
241 for (Locale locale: SUPPORTED_LOCALES) {
242 if (locale.getCountry().equals(forLocale.getCountry()) && locale.getLanguage().equals(forLocale.getLanguage())) {
246 for (Locale locale: SUPPORTED_LOCALES) {
247 if (locale.getLanguage().equals(forLocale.getLanguage())) {
251 return SUPPORTED_LOCALES[0];
258 protected void switchLanguage(Locale locale) {
259 Locale supportedLocale = findSupportedLocale(locale);
260 Action languageAction = languageActions.get(supportedLocale);
261 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) languageAction.getValue("menuItem");
262 menuItem.setSelected(true);
263 I18n.setLocale(supportedLocale);
264 for (Runnable i18nRunnable: I18nContainer.getInstance()) {
267 } catch (Throwable t) {
268 /* we probably shouldn't swallow this. */
271 wizard.setPage(wizard.getPage());
272 configuration.setLocale(supportedLocale);
276 // INTERFACE ListSelectionListener
282 public void valueChanged(ListSelectionEvent e) {
283 JList list = (JList) e.getSource();
284 int selectedRow = list.getSelectedIndex();
285 wizard.setNextEnabled(selectedRow > -1);
289 // INTERFACE WizardListener
295 public void wizardNextPressed(TWizard wizard) {
296 String pageName = wizard.getPage().getName();
297 if ("page.node-manager".equals(pageName)) {
298 showPage(PageType.PAGE_PROJECTS);
299 } else if ("page.project".equals(pageName)) {
300 ProjectPage projectPage = (ProjectPage) wizard.getPage();
301 Project project = projectPage.getSelectedProject();
302 if ((project.getLocalPath() == null) || (project.getLocalPath().trim().length() == 0)) {
303 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-local-path"), null, JOptionPane.ERROR_MESSAGE);
306 if ((project.getPath() == null) || (project.getPath().trim().length() == 0)) {
307 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-path"), null, JOptionPane.ERROR_MESSAGE);
310 ((ProjectFilesPage) pages.get(PageType.PAGE_PROJECT_FILES)).setProject(project);
311 ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).setProject(project);
312 showPage(PageType.PAGE_PROJECT_FILES);
313 } else if ("page.project.files".equals(pageName)) {
314 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
315 Project project = projectPage.getSelectedProject();
316 if (selectedNode == null) {
317 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
320 if (project.getIndexFile() == null) {
321 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.empty-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
325 File indexFile = new File(project.getLocalPath(), project.getIndexFile());
326 if (!indexFile.exists()) {
327 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.index-missing"), null, JOptionPane.ERROR_MESSAGE);
331 if (!project.getFileOption(project.getIndexFile()).getContainer().equals("")) {
332 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.container-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
336 if (!project.getFileOption(project.getIndexFile()).getMimeType().equals("text/html")) {
337 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.index-not-html"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
341 Map<String, FileOption> fileOptions = project.getFileOptions();
342 Set<Entry<String, FileOption>> fileOptionEntries = fileOptions.entrySet();
343 for (Entry<String, FileOption> fileOptionEntry: fileOptionEntries) {
344 FileOption fileOption = fileOptionEntry.getValue();
345 if (!fileOption.isInsert() && ((fileOption.getCustomKey().length() == 0) || "CHK@".equals(fileOption.getCustomKey()))) {
346 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.project-files.no-custom-key"), fileOptionEntry.getKey()), null, JOptionPane.ERROR_MESSAGE);
350 boolean nodeRunning = false;
352 nodeRunning = freenetInterface.isNodePresent();
353 } catch (IOException e) {
356 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-running"), null, JOptionPane.ERROR_MESSAGE);
359 configuration.save();
360 showPage(PageType.PAGE_INSERT_PROJECT);
361 ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).startInsert();
362 nodeMenu.setEnabled(false);
363 } else if ("page.project.insert".equals(pageName)) {
364 showPage(PageType.PAGE_PROJECTS);
365 nodeMenu.setEnabled(true);
372 public void wizardPreviousPressed(TWizard wizard) {
373 String pageName = wizard.getPage().getName();
374 if ("page.project".equals(pageName)) {
375 showPage(PageType.PAGE_NODE_MANAGER);
376 } else if ("page.project.files".equals(pageName)) {
377 showPage(PageType.PAGE_PROJECTS);
378 } else if ("page.project.insert".equals(pageName)) {
379 showPage(PageType.PAGE_PROJECT_FILES);
386 public void wizardQuitPressed(TWizard wizard) {
387 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.question"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
388 if (saveConfiguration()) {
391 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.config-not-saved"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
398 // INTERFACE NodeManagerListener
404 public void nodesUpdated(Node[] nodes) {
405 nodeMenu.removeAll();
406 ButtonGroup nodeButtonGroup = new ButtonGroup();
407 Node newSelectedNode = null;
408 for (Node node: nodes) {
409 JRadioButtonMenuItem nodeMenuItem = new JRadioButtonMenuItem(node.getName());
410 nodeMenuItem.putClientProperty("Node", node);
411 nodeMenuItem.addActionListener(this);
412 nodeButtonGroup.add(nodeMenuItem);
413 if (node.equals(selectedNode)) {
414 newSelectedNode = node;
415 nodeMenuItem.setSelected(true);
417 nodeMenu.add(nodeMenuItem);
419 nodeMenu.addSeparator();
420 nodeMenu.add(manageNodeAction);
421 selectedNode = newSelectedNode;
422 freenetInterface.setNode(selectedNode);
428 public void actionPerformed(ActionEvent e) {
429 Object source = e.getSource();
430 if (source instanceof JRadioButtonMenuItem) {
431 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) source;
432 Node node = (Node) menuItem.getClientProperty("Node");
434 freenetInterface.setNode(selectedNode);
441 public static void main(String[] args) {
442 String configFilename = null;
443 boolean nextIsConfigFilename = false;
444 for (String argument: args) {
445 if (nextIsConfigFilename) {
446 configFilename = argument;
447 nextIsConfigFilename = false;
449 if ("--help".equals(argument)) {
452 } else if ("--debug".equals(argument)) {
454 } else if ("--config-file".equals(argument)) {
455 nextIsConfigFilename = true;
458 if (nextIsConfigFilename) {
459 System.out.println("--config-file needs parameter!");
462 new Main(configFilename);
465 private static void printHelp() {
466 System.out.println("--help\tshows this cruft");
467 System.out.println("--debug\tenables some debug output");
468 System.out.println("--config-file <file>\tuse specified configuration file");