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>
62 * @version $Id: Main.java 456 2006-04-03 17:54:44Z bombe $
64 public class Main implements ActionListener, ListSelectionListener, WizardListener, NodeManagerListener {
66 private static boolean debug = false;
67 private Configuration configuration = new 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 };
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>();
85 Locale.setDefault(configuration.getLocale());
86 I18n.setLocale(configuration.getLocale());
87 if (!configuration.createLockFile()) {
88 JOptionPane.showMessageDialog(null, I18n.getMessage("jsite.main.already-running"), null, JOptionPane.ERROR_MESSAGE);
91 wizard = new TWizard();
93 wizard.setJMenuBar(createMenuBar());
94 wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
95 wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
96 wizard.setQuitName(I18n.getMessage("jsite.wizard.quit"));
97 wizard.setPreviousEnabled(false);
98 wizard.setNextEnabled(true);
99 wizard.addWizardListener(this);
100 jSiteIcon = IconLoader.loadIcon("/jsite-icon.png");
101 wizard.setIcon(jSiteIcon);
104 showPage(PageType.PAGE_PROJECTS);
105 wizard.setPreviousName((String) manageNodeAction.getValue(Action.NAME));
108 private void createActions() {
109 for (final Locale locale: SUPPORTED_LOCALES) {
110 languageActions.put(locale, new AbstractAction(I18n.getMessage("jsite.menu.language." + locale.getLanguage())) {
112 public void actionPerformed(ActionEvent actionEvent) {
113 switchLanguage(locale);
117 manageNodeAction = new AbstractAction(I18n.getMessage("jsite.menu.nodes.manage-nodes")) {
118 public void actionPerformed(ActionEvent actionEvent) {
119 showPage(PageType.PAGE_NODE_MANAGER);
122 aboutAction = new AbstractAction(I18n.getMessage("jsite.menu.help.about")) {
124 public void actionPerformed(ActionEvent e) {
125 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.about.message"), Version.getVersion()), null, JOptionPane.INFORMATION_MESSAGE, jSiteIcon);
130 private JMenuBar createMenuBar() {
131 JMenuBar menuBar = new JMenuBar();
132 JMenu languageMenu = new JMenu(I18n.getMessage("jsite.menu.languages"));
133 menuBar.add(languageMenu);
134 ButtonGroup languageButtonGroup = new ButtonGroup();
135 for (Locale locale: SUPPORTED_LOCALES) {
136 Action languageAction = languageActions.get(locale);
137 JRadioButtonMenuItem menuItem = new JRadioButtonMenuItem(languageActions.get(locale));
138 if (locale.equals(Locale.getDefault())) {
139 menuItem.setSelected(true);
141 languageAction.putValue("menuItem", menuItem);
142 languageButtonGroup.add(menuItem);
143 languageMenu.add(menuItem);
145 nodeMenu = new JMenu(I18n.getMessage("jsite.menu.nodes"));
146 menuBar.add(nodeMenu);
147 selectedNode = configuration.getSelectedNode();
148 nodesUpdated(configuration.getNodes());
150 /* evil hack to right-align the help menu */
151 JPanel panel = new JPanel();
152 panel.setOpaque(false);
155 JMenu helpMenu = new JMenu(I18n.getMessage("jsite.menu.help"));
156 menuBar.add(helpMenu);
157 helpMenu.add(aboutAction);
161 private void initPages() {
162 NodeManagerPage nodeManagerPage = new NodeManagerPage();
163 nodeManagerPage.setName("page.node-manager");
164 nodeManagerPage.addNodeManagerListener(this);
165 nodeManagerPage.setNodes(configuration.getNodes());
166 pages.put(PageType.PAGE_NODE_MANAGER, nodeManagerPage);
168 ProjectPage projectPage = new ProjectPage();
169 projectPage.setName("page.project");
170 projectPage.setProjects(configuration.getProjects());
171 projectPage.setFreenetInterface(freenetInterface);
172 projectPage.addListSelectionListener(this);
173 pages.put(PageType.PAGE_PROJECTS, projectPage);
175 ProjectFilesPage projectFilesPage = new ProjectFilesPage();
176 projectFilesPage.setName("page.project.files");
177 pages.put(PageType.PAGE_PROJECT_FILES, projectFilesPage);
179 ProjectInsertPage projectInsertPage = new ProjectInsertPage();
180 projectInsertPage.setDebug(debug);
181 projectInsertPage.setName("page.project.insert");
182 projectInsertPage.setFreenetInterface(freenetInterface);
183 pages.put(PageType.PAGE_INSERT_PROJECT, projectInsertPage);
186 protected void showPage(PageType pageType) {
187 wizard.setPreviousEnabled(pageType.ordinal() > 0);
188 wizard.setNextEnabled(pageType.ordinal() < (pages.size() - 1));
189 wizard.setPage(pages.get(pageType));
190 wizard.setTitle(pages.get(pageType).getHeading() + " - jSite");
193 private boolean saveConfiguration() {
194 NodeManagerPage nodeManagerPage = (NodeManagerPage) pages.get(PageType.PAGE_NODE_MANAGER);
195 configuration.setNodes(nodeManagerPage.getNodes());
196 if (selectedNode != null) {
197 configuration.setSelectedNode(selectedNode);
200 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
201 configuration.setProjects(projectPage.getProjects());
203 return configuration.save();
206 private Locale findSupportedLocale(Locale forLocale) {
207 for (Locale locale: SUPPORTED_LOCALES) {
208 if (locale.equals(forLocale)) {
212 for (Locale locale: SUPPORTED_LOCALES) {
213 if (locale.getCountry().equals(forLocale.getCountry()) && locale.getLanguage().equals(forLocale.getLanguage())) {
217 for (Locale locale: SUPPORTED_LOCALES) {
218 if (locale.getLanguage().equals(forLocale.getLanguage())) {
222 return SUPPORTED_LOCALES[0];
229 protected void switchLanguage(Locale locale) {
230 Locale supportedLocale = findSupportedLocale(locale);
231 Action languageAction = languageActions.get(supportedLocale);
232 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) languageAction.getValue("menuItem");
233 menuItem.setSelected(true);
234 /* show the restart message in the other language! */
235 Locale currentLocale = I18n.getLocale();
236 I18n.setLocale(supportedLocale);
237 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.menu.language.change.restart-message"), null, JOptionPane.INFORMATION_MESSAGE);
238 I18n.setLocale(currentLocale);
239 configuration.setLocale(supportedLocale);
243 // INTERFACE ListSelectionListener
249 public void valueChanged(ListSelectionEvent e) {
250 JList list = (JList) e.getSource();
251 int selectedRow = list.getSelectedIndex();
252 wizard.setNextEnabled(selectedRow > -1);
256 // INTERFACE WizardListener
262 public void wizardNextPressed(TWizard wizard) {
263 String pageName = wizard.getPage().getName();
264 if ("page.node-manager".equals(pageName)) {
265 showPage(PageType.PAGE_PROJECTS);
266 wizard.setPreviousName((String) manageNodeAction.getValue(Action.NAME));
267 } else if ("page.project".equals(pageName)) {
268 ProjectPage projectPage = (ProjectPage) wizard.getPage();
269 Project project = projectPage.getSelectedProject();
270 if ((project.getLocalPath() == null) || (project.getLocalPath().trim().length() == 0)) {
271 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-local-path"), null, JOptionPane.ERROR_MESSAGE);
274 if ((project.getPath() == null) || (project.getPath().trim().length() == 0)) {
275 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project.warning.no-path"), null, JOptionPane.ERROR_MESSAGE);
278 ((ProjectFilesPage) pages.get(PageType.PAGE_PROJECT_FILES)).setProject(project);
279 ((ProjectInsertPage) pages.get(PageType.PAGE_INSERT_PROJECT)).setProject(project);
280 showPage(PageType.PAGE_PROJECT_FILES);
281 wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
282 } else if ("page.project.files".equals(pageName)) {
283 ProjectPage projectPage = (ProjectPage) pages.get(PageType.PAGE_PROJECTS);
284 Project project = projectPage.getSelectedProject();
285 if (selectedNode == null) {
286 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-selected"), null, JOptionPane.ERROR_MESSAGE);
289 if (project.getIndexFile() == null) {
290 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.empty-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
294 if (!project.getFileOption(project.getIndexFile()).getContainer().equals("")) {
295 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.container-index"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
299 if (!project.getFileOption(project.getIndexFile()).getMimeType().equals("text/html")) {
300 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.project-files.index-not-html"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.OK_OPTION) {
304 Map<String, FileOption> fileOptions = project.getFileOptions();
305 Set<Entry<String, FileOption>> fileOptionEntries = fileOptions.entrySet();
306 for (Entry<String, FileOption> fileOptionEntry: fileOptionEntries) {
307 FileOption fileOption = fileOptionEntry.getValue();
308 if (!fileOption.isInsert() && ((fileOption.getCustomKey().length() == 0) || "CHK@".equals(fileOption.getCustomKey()))) {
309 JOptionPane.showMessageDialog(wizard, MessageFormat.format(I18n.getMessage("jsite.project-files.no-custom-key"), fileOptionEntry.getKey()), null, JOptionPane.ERROR_MESSAGE);
313 boolean nodeRunning = false;
315 nodeRunning = freenetInterface.isNodePresent();
316 } catch (IOException e) {
319 JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-running"), null, JOptionPane.ERROR_MESSAGE);
322 showPage(PageType.PAGE_INSERT_PROJECT);
323 nodeMenu.setEnabled(false);
324 } else if ("page.project.insert".equals(pageName)) {
325 showPage(PageType.PAGE_PROJECTS);
326 nodeMenu.setEnabled(true);
333 public void wizardPreviousPressed(TWizard wizard) {
334 String pageName = wizard.getPage().getName();
335 if ("page.project".equals(pageName)) {
336 showPage(PageType.PAGE_NODE_MANAGER);
337 wizard.setPreviousName(I18n.getMessage("jsite.wizard.previous"));
338 } else if ("page.project.files".equals(pageName)) {
339 showPage(PageType.PAGE_PROJECTS);
340 wizard.setPreviousName((String) manageNodeAction.getValue(Action.NAME));
341 } else if ("page.project.insert".equals(pageName)) {
342 showPage(PageType.PAGE_PROJECT_FILES);
349 public void wizardQuitPressed(TWizard wizard) {
350 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.question"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
351 if (saveConfiguration()) {
354 if (JOptionPane.showConfirmDialog(wizard, I18n.getMessage("jsite.quit.config-not-saved"), null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
361 // INTERFACE NodeManagerListener
367 public void nodesUpdated(Node[] nodes) {
368 nodeMenu.removeAll();
369 ButtonGroup nodeButtonGroup = new ButtonGroup();
370 Node newSelectedNode = null;
371 for (Node node: nodes) {
372 JRadioButtonMenuItem nodeMenuItem = new JRadioButtonMenuItem(node.getName());
373 nodeMenuItem.putClientProperty("Node", node);
374 nodeMenuItem.addActionListener(this);
375 nodeButtonGroup.add(nodeMenuItem);
376 if (node.equals(selectedNode)) {
377 newSelectedNode = node;
378 nodeMenuItem.setSelected(true);
380 nodeMenu.add(nodeMenuItem);
382 nodeMenu.addSeparator();
383 nodeMenu.add(manageNodeAction);
384 selectedNode = newSelectedNode;
385 freenetInterface.setNode(selectedNode);
391 public void actionPerformed(ActionEvent e) {
392 Object source = e.getSource();
393 if (source instanceof JRadioButtonMenuItem) {
394 JRadioButtonMenuItem menuItem = (JRadioButtonMenuItem) source;
395 Node node = (Node) menuItem.getClientProperty("Node");
397 freenetInterface.setNode(selectedNode);
405 public static void main(String[] args) {
406 System.setProperty("swing.plaf.metal.userFont", "Tahoma");
407 System.setProperty("swing.plaf.metal.controlFont", "Tahoma");
408 System.setProperty("swing.aatext", "true");
409 debug = (args.length > 0) && (args[0].equals("--debug"));