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;
24 import java.io.IOException;
25 import java.text.MessageFormat;
26 import java.util.HashMap;
27 import java.util.Locale;
30 import java.util.Map.Entry;
32 import javax.swing.AbstractAction;
33 import javax.swing.Action;
34 import javax.swing.ButtonGroup;
35 import javax.swing.Icon;
36 import javax.swing.JList;
37 import javax.swing.JMenu;
38 import javax.swing.JMenuBar;
39 import javax.swing.JOptionPane;
40 import javax.swing.JPanel;
41 import javax.swing.JRadioButtonMenuItem;
42 import javax.swing.event.ListSelectionEvent;
43 import javax.swing.event.ListSelectionListener;
45 import de.todesbaum.jsite.application.FileOption;
46 import de.todesbaum.jsite.application.Freenet7Interface;
47 import de.todesbaum.jsite.application.Node;
48 import de.todesbaum.jsite.application.Project;
49 import de.todesbaum.jsite.gui.NodeManagerListener;
50 import de.todesbaum.jsite.gui.NodeManagerPage;
51 import de.todesbaum.jsite.gui.ProjectFilesPage;
52 import de.todesbaum.jsite.gui.ProjectInsertPage;
53 import de.todesbaum.jsite.gui.ProjectPage;
54 import de.todesbaum.jsite.i18n.I18n;
55 import de.todesbaum.util.image.IconLoader;
56 import de.todesbaum.util.swing.TWizard;
57 import de.todesbaum.util.swing.TWizardPage;
58 import de.todesbaum.util.swing.WizardListener;
61 * @author <a href="mailto:droden@gmail.com">David Roden </a>
64 public class Main implements ActionListener, ListSelectionListener, WizardListener, NodeManagerListener {
66 private static boolean debug = false;
67 private Configuration configuration;
68 private Freenet7Interface freenetInterface = new Freenet7Interface();
69 protected Icon jSiteIcon;
71 private static enum PageType {
72 PAGE_NODE_MANAGER, PAGE_PROJECTS, PAGE_PROJECT_FILES, PAGE_INSERT_PROJECT
75 private static final Locale[] SUPPORTED_LOCALES = new Locale[] { Locale.ENGLISH, Locale.GERMAN, Locale.FRENCH };
76 private Map<Locale, Action> languageActions = new HashMap<Locale, Action>();
77 private Action manageNodeAction;
78 private Action aboutAction;
79 protected TWizard wizard;
80 protected JMenu nodeMenu;
81 private Node selectedNode;
82 private final Map<PageType, TWizardPage> pages = new HashMap<PageType, TWizardPage>();
88 private Main(String configFilename) {
89 if (configFilename != null) {
90 configuration = new Configuration(configFilename);
92 configuration = new Configuration();
94 Locale.setDefault(configuration.getLocale());
95 I18n.setLocale(configuration.getLocale());
96 if (!configuration.createLockFile()) {
97 JOptionPane.showMessageDialog(null, I18n.getMessage("jsite.main.already-running"), null, JOptionPane.ERROR_MESSAGE);
100 wizard = new TWizard();
102 wizard.setJMenuBar(createMenuBar());
103 wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
104 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
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);
114 wizard.setPreviousName((String) manageNodeAction.getValue(Action.NAME));
117 private void createActions() {
118 for (final Locale locale: SUPPORTED_LOCALES) {
119 languageActions.put(locale, new AbstractAction(I18n.getMessage("jsite.menu.language." + locale.getLanguage())) {
121 public void actionPerformed(ActionEvent actionEvent) {
122 switchLanguage(locale);
126 manageNodeAction = new AbstractAction(I18n.getMessage("jsite.menu.nodes.manage-nodes")) {
127 public void actionPerformed(ActionEvent actionEvent) {
128 showPage(PageType.PAGE_NODE_MANAGER);
131 aboutAction = new AbstractAction(I18n.getMessage("jsite.menu.help.about")) {
133 public void actionPerformed(ActionEvent e) {
134 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.about.message"), Version.getVersion()), null, JOptionPane.INFORMATION_MESSAGE, jSiteIcon);
139 private JMenuBar createMenuBar() {
140 JMenuBar menuBar = new JMenuBar();
141 JMenu languageMenu = new JMenu(I18n.getMessage("jsite.menu.languages"));
142 menuBar.add(languageMenu);
143 ButtonGroup languageButtonGroup = new ButtonGroup();
144 for (Locale locale: SUPPORTED_LOCALES) {
145 Action languageAction = languageActions.get(locale);
146 JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(languageActions.get(locale));
147 if (locale.equals(Locale.getDefault())) {
148 menuItem.setSelected(true);
150 languageAction.putValue("menuItem", menuItem);
151 languageButtonGroup.add(menuItem);
152 languageMenu.add(menuItem);
154 nodeMenu = new JMenu(I18n.getMessage("jsite.menu.nodes"));
155 menuBar.add(nodeMenu);
156 selectedNode = configuration.getSelectedNode();
157 nodesUpdated(configuration.getNodes());
159 /* evil hack to right-align the help menu */
160 JPanel panel = new JPanel();
161 panel.setOpaque(false);
164 JMenu helpMenu = new JMenu(I18n.getMessage("jsite.menu.help"));
165 menuBar.add(helpMenu);
166 helpMenu.add(aboutAction);
170 private void initPages() {
171 NodeManagerPage nodeManagerPage = new NodeManagerPage();
172 nodeManagerPage.setName("page.node-manager");
173 nodeManagerPage.addNodeManagerListener(this);
174 nodeManagerPage.setNodes(configuration.getNodes());
175 pages.put(PageType.PAGE_NODE_MANAGER, nodeManagerPage);
177 ProjectPage projectPage = new ProjectPage();
178 projectPage.setName("page.project");
179 projectPage.setProjects(configuration.getProjects());
180 projectPage.setFreenetInterface(freenetInterface);
181 projectPage.addListSelectionListener(this);
182 pages.put(PageType.PAGE_PROJECTS, projectPage);
184 ProjectFilesPage projectFilesPage = new ProjectFilesPage();
185 projectFilesPage.setName("page.project.files");
186 pages.put(PageType.PAGE_PROJECT_FILES, projectFilesPage);
188 ProjectInsertPage projectInsertPage = new ProjectInsertPage();
189 projectInsertPage.setDebug(debug);
190 projectInsertPage.setName("page.project.insert");
191 projectInsertPage.setFreenetInterface(freenetInterface);
192 pages.put(PageType.PAGE_INSERT_PROJECT, projectInsertPage);
195 protected void showPage(PageType pageType) {
196 wizard.setPreviousEnabled(pageType.ordinal() > 0);
197 wizard.setNextEnabled(pageType.ordinal() < (pages.size() - 1));
198 wizard.setPage(pages.get(pageType));
199 wizard.setTitle(pages.get(pageType).getHeading() + " - jSite");
202 private boolean saveConfiguration() {
203 NodeManagerPage nodeManagerPage = (NodeManagerPage) pages.get(PageType.PAGE_NODE_MANAGER);
204 configuration.setNodes(nodeManagerPage.getNodes());
205 if (selectedNode != null) {
206 configuration.setSelectedNode(selectedNode);
209 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
210 configuration.setProjects(projectPage.getProjects());
212 return configuration.save();
215 private Locale findSupportedLocale(Locale forLocale) {
216 for (Locale locale: SUPPORTED_LOCALES) {
217 if (locale.equals(forLocale)) {
221 for (Locale locale: SUPPORTED_LOCALES) {
222 if (locale.getCountry().equals(forLocale.getCountry()) && locale.getLanguage().equals(forLocale.getLanguage())) {
226 for (Locale locale: SUPPORTED_LOCALES) {
227 if (locale.getLanguage().equals(forLocale.getLanguage())) {
231 return SUPPORTED_LOCALES[0];
238 protected void switchLanguage(Locale locale) {
239 Locale supportedLocale = findSupportedLocale(locale);
240 Action languageAction = languageActions.get(supportedLocale);
241 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) languageAction.getValue("menuItem");
242 menuItem.setSelected(true);
243 /* show the restart message in the other language! */
244 Locale currentLocale = I18n.getLocale();
245 I18n.setLocale(supportedLocale);
246 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.menu.language.change.restart-message"), null, JOptionPane.INFORMATION_MESSAGE);
247 I18n.setLocale(currentLocale);
248 configuration.setLocale(supportedLocale);
252 // INTERFACE ListSelectionListener
258 public void valueChanged(ListSelectionEvent e) {
259 JList list = (JList) e.getSource();
260 int selectedRow = list.getSelectedIndex();
261 wizard.setNextEnabled(selectedRow > -1);
265 // INTERFACE WizardListener
271 public void wizardNextPressed(TWizard wizard) {
272 String pageName = wizard.getPage().getName();
273 if ("page.node-manager".equals(pageName)) {
274 showPage(PageType.PAGE_PROJECTS);
275 wizard.setPreviousName((String) manageNodeAction.getValue(Action.NAME));
276 } else if ("page.project".equals(pageName)) {
277 ProjectPage projectPage = (ProjectPage) wizard.getPage();
278 Project project = projectPage.getSelectedProject();
279 if ((project.getLocalPath() == null) || (project.getLocalPath().trim().length() == 0)) {
280 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-local-path"), null, JOptionPane.ERROR_MESSAGE);
283 if ((project.getPath() == null) || (project.getPath().trim().length() == 0)) {
284 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-path"), null, JOptionPane.ERROR_MESSAGE);
287 ((ProjectFilesPage) pages.get(PageType.PAGE_PROJECT_FILES)).setProject(project);
288 ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).setProject(project);
289 showPage(PageType.PAGE_PROJECT_FILES);
290 wizard.setNextName(I18n.getMessage("jsite.project-files.insert-now"));
291 wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
292 } else if ("page.project.files".equals(pageName)) {
293 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
294 Project project = projectPage.getSelectedProject();
295 if (selectedNode == null) {
296 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
299 if (project.getIndexFile() == null) {
300 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.empty-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
304 if (!project.getFileOption(project.getIndexFile()).getContainer().equals("")) {
305 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.container-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
309 if (!project.getFileOption(project.getIndexFile()).getMimeType().equals("text/html")) {
310 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.index-not-html"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
314 Map<String, FileOption> fileOptions = project.getFileOptions();
315 Set<Entry<String, FileOption>> fileOptionEntries = fileOptions.entrySet();
316 for (Entry<String, FileOption> fileOptionEntry: fileOptionEntries) {
317 FileOption fileOption = fileOptionEntry.getValue();
318 if (!fileOption.isInsert() && ((fileOption.getCustomKey().length() == 0) || "CHK@".equals(fileOption.getCustomKey()))) {
319 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.project-files.no-custom-key"), fileOptionEntry.getKey()), null, JOptionPane.ERROR_MESSAGE);
323 boolean nodeRunning = false;
325 nodeRunning = freenetInterface.isNodePresent();
326 } catch (IOException e) {
329 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-running"), null, JOptionPane.ERROR_MESSAGE);
332 configuration.save();
333 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
334 showPage(PageType.PAGE_INSERT_PROJECT);
335 nodeMenu.setEnabled(false);
336 } else if ("page.project.insert".equals(pageName)) {
337 showPage(PageType.PAGE_PROJECTS);
338 nodeMenu.setEnabled(true);
345 public void wizardPreviousPressed(TWizard wizard) {
346 String pageName = wizard.getPage().getName();
347 if ("page.project".equals(pageName)) {
348 showPage(PageType.PAGE_NODE_MANAGER);
349 wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
350 } else if ("page.project.files".equals(pageName)) {
351 showPage(PageType.PAGE_PROJECTS);
352 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
353 wizard.setPreviousName((String) manageNodeAction.getValue(Action.NAME));
354 } else if ("page.project.insert".equals(pageName)) {
355 showPage(PageType.PAGE_PROJECT_FILES);
362 public void wizardQuitPressed(TWizard wizard) {
363 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.question"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
364 if (saveConfiguration()) {
367 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.config-not-saved"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
374 // INTERFACE NodeManagerListener
380 public void nodesUpdated(Node[] nodes) {
381 nodeMenu.removeAll();
382 ButtonGroup nodeButtonGroup = new ButtonGroup();
383 Node newSelectedNode = null;
384 for (Node node: nodes) {
385 JRadioButtonMenuItem nodeMenuItem = new JRadioButtonMenuItem(node.getName());
386 nodeMenuItem.putClientProperty("Node", node);
387 nodeMenuItem.addActionListener(this);
388 nodeButtonGroup.add(nodeMenuItem);
389 if (node.equals(selectedNode)) {
390 newSelectedNode = node;
391 nodeMenuItem.setSelected(true);
393 nodeMenu.add(nodeMenuItem);
395 nodeMenu.addSeparator();
396 nodeMenu.add(manageNodeAction);
397 selectedNode = newSelectedNode;
398 freenetInterface.setNode(selectedNode);
404 public void actionPerformed(ActionEvent e) {
405 Object source = e.getSource();
406 if (source instanceof JRadioButtonMenuItem) {
407 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) source;
408 Node node = (Node) menuItem.getClientProperty("Node");
410 freenetInterface.setNode(selectedNode);
417 public static void main(String[] args) {
418 System.setProperty("swing.plaf.metal.userFont", "Tahoma");
419 System.setProperty("swing.plaf.metal.controlFont", "Tahoma");
420 System.setProperty("swing.aatext", "true");
421 String configFilename = null;
422 boolean nextIsConfigFilename = false;
423 for (String argument: args) {
424 if (nextIsConfigFilename) {
425 configFilename = argument;
426 nextIsConfigFilename = false;
428 if ("--help".equals(argument)) {
431 } else if ("--debug".equals(argument)) {
433 } else if ("--config-file".equals(argument)) {
434 nextIsConfigFilename = true;
437 if (nextIsConfigFilename) {
438 System.out.println("--config-file needs parameter!");
441 new Main(configFilename);
444 private static void printHelp() {
445 System.out.println("--help\tshows this cruft");
446 System.out.println("--debug\tenables some debug output");
447 System.out.println("--config-file <file>\tuse specified configuration file");