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 David ‘Bombe’ Roden <bombe@freenetproject.org>
65 public class Main implements ActionListener, ListSelectionListener, WizardListener, NodeManagerListener {
67 private static boolean debug = false;
68 private Configuration configuration;
69 private Freenet7Interface freenetInterface = new Freenet7Interface();
70 private Icon jSiteIcon;
72 private static enum PageType {
73 PAGE_NODE_MANAGER, PAGE_PROJECTS, PAGE_PROJECT_FILES, PAGE_INSERT_PROJECT
76 private static final Locale[] SUPPORTED_LOCALES = new Locale[] { Locale.ENGLISH, Locale.GERMAN, Locale.FRENCH, Locale.ITALIAN, new Locale("pl") };
77 private Map<Locale, Action> languageActions = new HashMap<Locale, Action>();
78 private Action manageNodeAction;
79 private Action aboutAction;
80 private TWizard wizard;
81 private JMenu nodeMenu;
82 private Node selectedNode;
83 private final Map<PageType, TWizardPage> pages = new HashMap<PageType, TWizardPage>();
89 private Main(String configFilename) {
90 if (configFilename != null) {
91 configuration = new Configuration(configFilename);
93 configuration = new Configuration();
95 Locale.setDefault(configuration.getLocale());
96 I18n.setLocale(configuration.getLocale());
97 if (!configuration.createLockFile()) {
98 JOptionPane.showMessageDialog(null, I18n.getMessage("jsite.main.already-running"), null, JOptionPane.ERROR_MESSAGE);
101 wizard = new TWizard();
103 wizard.setJMenuBar(createMenuBar());
104 wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
105 wizard.setPreviousEnabled(false);
106 wizard.setNextEnabled(true);
107 wizard.addWizardListener(this);
108 jSiteIcon = IconLoader.loadIcon("/jsite-icon.png");
109 wizard.setIcon(jSiteIcon);
112 showPage(PageType.PAGE_PROJECTS);
115 private void createActions() {
116 for (final Locale locale : SUPPORTED_LOCALES) {
117 languageActions.put(locale, new AbstractAction(I18n.getMessage("jsite.menu.language." + locale.getLanguage()), IconLoader.loadIcon("/flag-" + locale.getLanguage() + ".png")) {
119 @SuppressWarnings("synthetic-access")
120 public void actionPerformed(ActionEvent actionEvent) {
121 switchLanguage(locale);
125 manageNodeAction = new AbstractAction(I18n.getMessage("jsite.menu.nodes.manage-nodes")) {
127 @SuppressWarnings("synthetic-access")
128 public void actionPerformed(ActionEvent actionEvent) {
129 showPage(PageType.PAGE_NODE_MANAGER);
130 wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
131 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
134 aboutAction = new AbstractAction(I18n.getMessage("jsite.menu.help.about")) {
136 @SuppressWarnings("synthetic-access")
137 public void actionPerformed(ActionEvent e) {
138 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.about.message"), Version.getVersion()), null, JOptionPane.INFORMATION_MESSAGE, jSiteIcon);
142 I18nContainer.getInstance().registerRunnable(new Runnable() {
144 @SuppressWarnings("synthetic-access")
146 manageNodeAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.nodes.manage-nodes"));
147 aboutAction.putValue(Action.NAME, I18n.getMessage("jsite.menu.help.about"));
152 private JMenuBar createMenuBar() {
153 JMenuBar menuBar = new JMenuBar();
154 final JMenu languageMenu = new JMenu(I18n.getMessage("jsite.menu.languages"));
155 menuBar.add(languageMenu);
156 ButtonGroup languageButtonGroup = new ButtonGroup();
157 for (Locale locale : SUPPORTED_LOCALES) {
158 Action languageAction = languageActions.get(locale);
159 JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(languageActions.get(locale));
160 if (locale.equals(Locale.getDefault())) {
161 menuItem.setSelected(true);
163 languageAction.putValue("menuItem", menuItem);
164 languageButtonGroup.add(menuItem);
165 languageMenu.add(menuItem);
167 nodeMenu = new JMenu(I18n.getMessage("jsite.menu.nodes"));
168 menuBar.add(nodeMenu);
169 selectedNode = configuration.getSelectedNode();
170 nodesUpdated(configuration.getNodes());
172 /* evil hack to right-align the help menu */
173 JPanel panel = new JPanel();
174 panel.setOpaque(false);
177 final JMenu helpMenu = new JMenu(I18n.getMessage("jsite.menu.help"));
178 menuBar.add(helpMenu);
179 helpMenu.add(aboutAction);
181 I18nContainer.getInstance().registerRunnable(new Runnable() {
183 @SuppressWarnings("synthetic-access")
185 languageMenu.setText(I18n.getMessage("jsite.menu.languages"));
186 nodeMenu.setText(I18n.getMessage("jsite.menu.nodes"));
187 helpMenu.setText(I18n.getMessage("jsite.menu.help"));
188 for (Map.Entry<Locale, Action> languageActionEntry : languageActions.entrySet()) {
189 languageActionEntry.getValue().putValue(Action.NAME, I18n.getMessage("jsite.menu.language." + languageActionEntry.getKey().getLanguage()));
197 private void initPages() {
198 NodeManagerPage nodeManagerPage = new NodeManagerPage(wizard);
199 nodeManagerPage.setName("page.node-manager");
200 nodeManagerPage.addNodeManagerListener(this);
201 nodeManagerPage.setNodes(configuration.getNodes());
202 pages.put(PageType.PAGE_NODE_MANAGER, nodeManagerPage);
204 ProjectPage projectPage = new ProjectPage(wizard);
205 projectPage.setName("page.project");
206 projectPage.setProjects(configuration.getProjects());
207 projectPage.setFreenetInterface(freenetInterface);
208 projectPage.addListSelectionListener(this);
209 pages.put(PageType.PAGE_PROJECTS, projectPage);
211 ProjectFilesPage projectFilesPage = new ProjectFilesPage(wizard);
212 projectFilesPage.setName("page.project.files");
213 pages.put(PageType.PAGE_PROJECT_FILES, projectFilesPage);
215 ProjectInsertPage projectInsertPage = new ProjectInsertPage(wizard);
216 projectInsertPage.setDebug(debug);
217 projectInsertPage.setName("page.project.insert");
218 projectInsertPage.setFreenetInterface(freenetInterface);
219 pages.put(PageType.PAGE_INSERT_PROJECT, projectInsertPage);
222 private void showPage(PageType pageType) {
223 wizard.setPreviousEnabled(pageType.ordinal() > 0);
224 wizard.setNextEnabled(pageType.ordinal() < (pages.size() - 1));
225 wizard.setPage(pages.get(pageType));
226 wizard.setTitle(pages.get(pageType).getHeading() + " - jSite");
229 private boolean saveConfiguration() {
230 NodeManagerPage nodeManagerPage = (NodeManagerPage) pages.get(PageType.PAGE_NODE_MANAGER);
231 configuration.setNodes(nodeManagerPage.getNodes());
232 if (selectedNode != null) {
233 configuration.setSelectedNode(selectedNode);
236 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
237 configuration.setProjects(projectPage.getProjects());
239 return configuration.save();
242 private Locale findSupportedLocale(Locale forLocale) {
243 for (Locale locale : SUPPORTED_LOCALES) {
244 if (locale.equals(forLocale)) {
248 for (Locale locale : SUPPORTED_LOCALES) {
249 if (locale.getCountry().equals(forLocale.getCountry()) && locale.getLanguage().equals(forLocale.getLanguage())) {
253 for (Locale locale : SUPPORTED_LOCALES) {
254 if (locale.getLanguage().equals(forLocale.getLanguage())) {
258 return SUPPORTED_LOCALES[0];
265 private void switchLanguage(Locale locale) {
266 Locale supportedLocale = findSupportedLocale(locale);
267 Action languageAction = languageActions.get(supportedLocale);
268 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) languageAction.getValue("menuItem");
269 menuItem.setSelected(true);
270 I18n.setLocale(supportedLocale);
271 for (Runnable i18nRunnable : I18nContainer.getInstance()) {
274 } catch (Throwable t) {
275 /* we probably shouldn't swallow this. */
278 wizard.setPage(wizard.getPage());
279 configuration.setLocale(supportedLocale);
283 // INTERFACE ListSelectionListener
289 public void valueChanged(ListSelectionEvent e) {
290 JList list = (JList) e.getSource();
291 int selectedRow = list.getSelectedIndex();
292 wizard.setNextEnabled(selectedRow > -1);
296 // INTERFACE WizardListener
302 public void wizardNextPressed(TWizard wizard) {
303 String pageName = wizard.getPage().getName();
304 if ("page.node-manager".equals(pageName)) {
305 showPage(PageType.PAGE_PROJECTS);
306 } else if ("page.project".equals(pageName)) {
307 ProjectPage projectPage = (ProjectPage) wizard.getPage();
308 Project project = projectPage.getSelectedProject();
309 if ((project.getLocalPath() == null) || (project.getLocalPath().trim().length() == 0)) {
310 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-local-path"), null, JOptionPane.ERROR_MESSAGE);
313 if ((project.getPath() == null) || (project.getPath().trim().length() == 0)) {
314 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-path"), null, JOptionPane.ERROR_MESSAGE);
317 ((ProjectFilesPage) pages.get(PageType.PAGE_PROJECT_FILES)).setProject(project);
318 ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).setProject(project);
319 showPage(PageType.PAGE_PROJECT_FILES);
320 } else if ("page.project.files".equals(pageName)) {
321 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
322 Project project = projectPage.getSelectedProject();
323 if (selectedNode == null) {
324 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
327 if (project.getIndexFile() == null) {
328 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.empty-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
332 File indexFile = new File(project.getLocalPath(), project.getIndexFile());
333 if (!indexFile.exists()) {
334 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.index-missing"), null, JOptionPane.ERROR_MESSAGE);
338 String indexFile = project.getIndexFile();
339 boolean hasIndexFile = (indexFile != null);
340 if (hasIndexFile && !project.getFileOption(indexFile).getContainer().equals("")) {
341 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.container-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
345 if (hasIndexFile && !project.getFileOption(indexFile).getMimeType().equals("text/html")) {
346 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.index-not-html"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
350 Map<String, FileOption> fileOptions = project.getFileOptions();
351 Set<Entry<String, FileOption>> fileOptionEntries = fileOptions.entrySet();
352 for (Entry<String, FileOption> fileOptionEntry : fileOptionEntries) {
353 FileOption fileOption = fileOptionEntry.getValue();
354 if (!fileOption.isInsert() && ((fileOption.getCustomKey().length() == 0) || "CHK@".equals(fileOption.getCustomKey()))) {
355 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.project-files.no-custom-key"), fileOptionEntry.getKey()), null, JOptionPane.ERROR_MESSAGE);
359 boolean nodeRunning = false;
361 nodeRunning = freenetInterface.isNodePresent();
362 } catch (IOException e) {
365 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-running"), null, JOptionPane.ERROR_MESSAGE);
368 configuration.save();
369 showPage(PageType.PAGE_INSERT_PROJECT);
370 ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).startInsert();
371 nodeMenu.setEnabled(false);
372 } else if ("page.project.insert".equals(pageName)) {
373 showPage(PageType.PAGE_PROJECTS);
374 nodeMenu.setEnabled(true);
381 public void wizardPreviousPressed(TWizard wizard) {
382 String pageName = wizard.getPage().getName();
383 if ("page.project".equals(pageName)) {
384 showPage(PageType.PAGE_NODE_MANAGER);
385 } else if ("page.project.files".equals(pageName)) {
386 showPage(PageType.PAGE_PROJECTS);
387 } else if ("page.project.insert".equals(pageName)) {
388 showPage(PageType.PAGE_PROJECT_FILES);
395 public void wizardQuitPressed(TWizard wizard) {
396 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.question"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
397 if (saveConfiguration()) {
400 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.config-not-saved"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
407 // INTERFACE NodeManagerListener
413 public void nodesUpdated(Node[] nodes) {
414 nodeMenu.removeAll();
415 ButtonGroup nodeButtonGroup = new ButtonGroup();
416 Node newSelectedNode = null;
417 for (Node node : nodes) {
418 JRadioButtonMenuItem nodeMenuItem = new JRadioButtonMenuItem(node.getName());
419 nodeMenuItem.putClientProperty("Node", node);
420 nodeMenuItem.addActionListener(this);
421 nodeButtonGroup.add(nodeMenuItem);
422 if (node.equals(selectedNode)) {
423 newSelectedNode = node;
424 nodeMenuItem.setSelected(true);
426 nodeMenu.add(nodeMenuItem);
428 nodeMenu.addSeparator();
429 nodeMenu.add(manageNodeAction);
430 selectedNode = newSelectedNode;
431 freenetInterface.setNode(selectedNode);
437 public void actionPerformed(ActionEvent e) {
438 Object source = e.getSource();
439 if (source instanceof JRadioButtonMenuItem) {
440 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) source;
441 Node node = (Node) menuItem.getClientProperty("Node");
443 freenetInterface.setNode(selectedNode);
450 public static void main(String[] args) {
451 String configFilename = null;
452 boolean nextIsConfigFilename = false;
453 for (String argument : args) {
454 if (nextIsConfigFilename) {
455 configFilename = argument;
456 nextIsConfigFilename = false;
458 if ("--help".equals(argument)) {
461 } else if ("--debug".equals(argument)) {
463 } else if ("--config-file".equals(argument)) {
464 nextIsConfigFilename = true;
467 if (nextIsConfigFilename) {
468 System.out.println("--config-file needs parameter!");
471 new Main(configFilename);
474 private static void printHelp() {
475 System.out.println("--help\tshows this cruft");
476 System.out.println("--debug\tenables some debug output");
477 System.out.println("--config-file <file>\tuse specified configuration file");