package de.todesbaum.jsite.main;
+import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
+import javax.swing.JDialog;
+import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
+import javax.swing.JProgressBar;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import de.todesbaum.util.swing.WizardListener;
/**
+ * The main class that ties together everything.
+ *
* @author David ‘Bombe’ Roden <bombe@freenetproject.org>
*/
public class Main implements ActionListener, ListSelectionListener, WizardListener, NodeManagerListener {
+ /** Whether the debug mode is activated. */
private static boolean debug = false;
+
+ /** The URL for update checks. */
+ private static final String UPDATE_KEY = "USK@e3myoFyp5avg6WYN16ImHri6J7Nj8980Fm~aQe4EX1U,QvbWT0ImE0TwLODTl7EoJx2NBnwDxTbLTE6zkB-eGPs,AQACAAE/jSite/0/currentVersion.txt";
+
+ /** The configuration. */
private Configuration configuration;
+
+ /** The freenet interface. */
private Freenet7Interface freenetInterface = new Freenet7Interface();
+
+ /** The jSite icon. */
private Icon jSiteIcon;
+ /**
+ * Enumeration for all possible pages.
+ *
+ * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
+ */
private static enum PageType {
- PAGE_NODE_MANAGER, PAGE_PROJECTS, PAGE_PROJECT_FILES, PAGE_INSERT_PROJECT
+
+ /** The node manager page. */
+ PAGE_NODE_MANAGER,
+
+ /** The project page. */
+ PAGE_PROJECTS,
+
+ /** The project files page. */
+ PAGE_PROJECT_FILES,
+
+ /** The project insert page. */
+ PAGE_INSERT_PROJECT
+
}
+ /** The supported locales. */
private static final Locale[] SUPPORTED_LOCALES = new Locale[] { Locale.ENGLISH, Locale.GERMAN, Locale.FRENCH, Locale.ITALIAN, new Locale("pl") };
+
+ /** The actions that switch the language. */
private Map<Locale, Action> languageActions = new HashMap<Locale, Action>();
+
+ /** The “manage nodes” action. */
private Action manageNodeAction;
+
+ /** The “check for updates” action. */
+ private Action checkForUpdatesAction;
+
+ /** The “about jSite” action. */
private Action aboutAction;
+
+ /** The wizard. */
private TWizard wizard;
+
+ /** The node menu. */
private JMenu nodeMenu;
+
+ /** The currently selected node. */
private Node selectedNode;
+
+ /** Mapping from page type to page. */
private final Map<PageType, TWizardPage> pages = new HashMap<PageType, TWizardPage>();
+ /**
+ * Creates a new core with the default configuration file.
+ */
private Main() {
this(null);
}
+ /**
+ * Creates a new core with the given configuration from the given file.
+ *
+ * @param configFilename
+ * The name of the configuration file
+ */
private Main(String configFilename) {
if (configFilename != null) {
configuration = new Configuration(configFilename);
Locale.setDefault(configuration.getLocale());
I18n.setLocale(configuration.getLocale());
if (!configuration.createLockFile()) {
- JOptionPane.showMessageDialog(null, I18n.getMessage("jsite.main.already-running"), null, JOptionPane.ERROR_MESSAGE);
- return;
+ int option = JOptionPane.showOptionDialog(null, I18n.getMessage("jsite.main.already-running"), "", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new Object[] { I18n.getMessage("jsite.main.already-running.override"), I18n.getMessage("jsite.wizard.quit") }, I18n.getMessage("jsite.wizard.quit"));
+ if (option != 0) {
+ return;
+ }
+ configuration.removeLockfileOnExit();
}
wizard = new TWizard();
createActions();
showPage(PageType.PAGE_PROJECTS);
}
+ /**
+ * Creates all actions.
+ */
private void createActions() {
for (final Locale locale : SUPPORTED_LOCALES) {
languageActions.put(locale, new AbstractAction(I18n.getMessage("jsite.menu.language." + locale.getLanguage()), IconLoader.loadIcon("/flag-" + locale.getLanguage() + ".png")) {
wizard.setNextName(I18n.getMessage("jsite.wizard.next"));
}
};
+ checkForUpdatesAction = new AbstractAction(I18n.getMessage("jsite.menu.help.check-for-updates")) {
+
+ /**
+ * {@inheritDoc}
+ */
+ @SuppressWarnings("synthetic-access")
+ public void actionPerformed(ActionEvent actionEvent) {
+ checkForUpdates();
+ }
+ };
aboutAction = new AbstractAction(I18n.getMessage("jsite.menu.help.about")) {
@SuppressWarnings("synthetic-access")
});
}
+ /**
+ * Creates the menu bar.
+ *
+ * @return The menu bar
+ */
private JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
final JMenu languageMenu = new JMenu(I18n.getMessage("jsite.menu.languages"));
final JMenu helpMenu = new JMenu(I18n.getMessage("jsite.menu.help"));
menuBar.add(helpMenu);
+ helpMenu.add(checkForUpdatesAction);
helpMenu.add(aboutAction);
I18nContainer.getInstance().registerRunnable(new Runnable() {
return menuBar;
}
+ /**
+ * Initializes all pages.
+ */
private void initPages() {
NodeManagerPage nodeManagerPage = new NodeManagerPage(wizard);
nodeManagerPage.setName("page.node-manager");
pages.put(PageType.PAGE_INSERT_PROJECT, projectInsertPage);
}
+ /**
+ * Shows the page with the given type.
+ *
+ * @param pageType
+ * The page type to show
+ */
private void showPage(PageType pageType) {
wizard.setPreviousEnabled(pageType.ordinal() > 0);
wizard.setNextEnabled(pageType.ordinal() < (pages.size() - 1));
wizard.setTitle(pages.get(pageType).getHeading() + " - jSite");
}
+ /**
+ * Saves the configuration.
+ *
+ * @return <code>true</code> if the configuration could be saved,
+ * <code>false</code> otherwise
+ */
private boolean saveConfiguration() {
NodeManagerPage nodeManagerPage = (NodeManagerPage) pages.get(PageType.PAGE_NODE_MANAGER);
configuration.setNodes(nodeManagerPage.getNodes());
return configuration.save();
}
+ /**
+ * Finds a supported locale for the given locale.
+ *
+ * @param forLocale
+ * The locale to find a supported locale for
+ * @return The supported locale that was found, or the default locale if no
+ * supported locale could be found
+ */
private Locale findSupportedLocale(Locale forLocale) {
for (Locale locale : SUPPORTED_LOCALES) {
if (locale.equals(forLocale)) {
// ACTIONS
//
+ /**
+ * Switches the language of the interface to the given locale.
+ *
+ * @param locale
+ * The locale to switch to
+ */
private void switchLanguage(Locale locale) {
Locale supportedLocale = findSupportedLocale(locale);
Action languageAction = languageActions.get(supportedLocale);
configuration.setLocale(supportedLocale);
}
+ /**
+ * Checks for updates of jSite.
+ */
+ private void checkForUpdates() {
+ System.out.println("checkForUpdates()");
+ /* construct a small panel for the dialog. */
+ JPanel waitingDialogPanel = new JPanel(new BorderLayout(12, 12));
+ waitingDialogPanel.add(new JLabel(I18n.getMessage("")), BorderLayout.PAGE_START);
+ JProgressBar progressBar = new JProgressBar();
+ progressBar.setIndeterminate(true);
+ waitingDialogPanel.add(progressBar, BorderLayout.PAGE_END);
+ JOptionPane waitingDialog = new JOptionPane(waitingDialogPanel, JOptionPane.INFORMATION_MESSAGE, 0, null, new Object[] { "Cancel" });
+ JDialog dialog = new JDialog(wizard, true);
+ dialog.getContentPane().add(waitingDialog, BorderLayout.CENTER);
+ dialog.pack();
+ dialog.setVisible(true);
+ }
+
//
// INTERFACE ListSelectionListener
//
try {
nodeRunning = freenetInterface.isNodePresent();
} catch (IOException e) {
+ /* ignore. */
}
if (!nodeRunning) {
JOptionPane.showMessageDialog(wizard, I18n.getMessage("jsite.project-files.no-node-running"), null, JOptionPane.ERROR_MESSAGE);
//
// MAIN METHOD
//
+
+ /**
+ * Main method that is called by the VM.
+ *
+ * @param args
+ * The command-line arguments
+ */
public static void main(String[] args) {
String configFilename = null;
boolean nextIsConfigFilename = false;
new Main(configFilename);
}
+ /**
+ * Prints a small syntax help.
+ */
private static void printHelp() {
System.out.println("--help\tshows this cruft");
System.out.println("--debug\tenables some debug output");