X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Fjsite%2Fgui%2FSwingInterface.java;h=d00ebe1a665e45bb98012927a2c4ce8ef4cc4770;hb=966d4ac902c693077f10ef4b1b7bc562559ca49e;hp=1210a4960834f9d02e4efd54796084c3f1da7bfd;hpb=2d12592a298c0f2d1b64635d51ad1c5453fc4532;p=jSite2.git diff --git a/src/net/pterodactylus/jsite/gui/SwingInterface.java b/src/net/pterodactylus/jsite/gui/SwingInterface.java index 1210a49..d00ebe1 100644 --- a/src/net/pterodactylus/jsite/gui/SwingInterface.java +++ b/src/net/pterodactylus/jsite/gui/SwingInterface.java @@ -20,18 +20,27 @@ package net.pterodactylus.jsite.gui; import java.awt.event.ActionEvent; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Locale; +import java.util.Properties; + +import javax.swing.JOptionPane; import net.pterodactylus.jsite.core.Core; import net.pterodactylus.jsite.core.CoreListener; import net.pterodactylus.jsite.core.Node; +import net.pterodactylus.jsite.core.Project; import net.pterodactylus.jsite.i18n.I18n; import net.pterodactylus.jsite.i18n.gui.I18nAction; +import net.pterodactylus.util.io.Closer; /** - * TODO + * The Swing user interface. * * @author David ‘Bombe’ Roden <bombe@freenetproject.org> * @version $Id$ @@ -41,9 +50,21 @@ public class SwingInterface implements CoreListener { /** The application core. */ private final Core core; + /** The configuration directory. */ + private final String configDirectory; + /** The main window. */ private MainWindow mainWindow; + /** The “configure” action. */ + private I18nAction configureAction; + + /** The “import config” action. */ + private I18nAction importConfigAction; + + /** The “quit” action. */ + private I18nAction quitAction; + /** The “manage nodes” action. */ private I18nAction manageNodesAction; @@ -59,18 +80,52 @@ public class SwingInterface implements CoreListener { /** All lanugage menu items. */ private List languageActions = new ArrayList(); + /** The “about” action. */ + private I18nAction helpAboutAction; + + /** The “add project” action. */ + private I18nAction addProjectAction; + + /** The “clone project” action. */ + private I18nAction cloneProjectAction; + + /** The “delete project” action. */ + private I18nAction deleteProjectAction; + + /** The “about” dialog. */ + private AboutDialog aboutDialog; + + /** The configuration dialog. */ + private ConfigurationDialog configurationDialog; + /** The list of all defined nodes. */ private List nodeList; + // + // CONFIGURATION + // + + /** Whether to beautify the GUI. */ + private boolean beautify; + /** * Creates a new swing interface. * * @param core * The core to operate on + * @param configDirectory + * The directory the configuration is stored in */ - public SwingInterface(Core core) { + public SwingInterface(Core core, String configDirectory) { this.core = core; - I18n.setLocale(Locale.ENGLISH); /* TODO - load config */ + this.configDirectory = configDirectory; + I18n.setLocale(Locale.ENGLISH); + loadConfig(); + if (beautify) { + System.setProperty("swing.aatext", "true"); + System.setProperty("swing.plaf.metal.controlFont", "Tahoma"); + System.setProperty("swing.plaf.metal.userFont", "Tahoma"); + } initActions(); initDialogs(); } @@ -98,6 +153,33 @@ public class SwingInterface implements CoreListener { } /** + * Returns the “configure” action. + * + * @return The “configure” action + */ + I18nAction getConfigureAction() { + return configureAction; + } + + /** + * Returns the “import config” action. + * + * @return The “import config” action + */ + I18nAction getImportConfigAction() { + return importConfigAction; + } + + /** + * Returns the “quit” action. + * + * @return The “quit” action + */ + I18nAction getQuitAction() { + return quitAction; + } + + /** * Returns the “manage nodes” action. * * @return The “manage nodes” action @@ -133,6 +215,42 @@ public class SwingInterface implements CoreListener { return languageActions; } + /** + * Returns the “about” action. + * + * @return The “about” action + */ + I18nAction getHelpAboutAction() { + return helpAboutAction; + } + + /** + * Returns the “add project” action. + * + * @return The “add project” action + */ + I18nAction getAddProjectAction() { + return addProjectAction; + } + + /** + * Returns the “clone project” action. + * + * @return The “clone project” action + */ + I18nAction getCloneProjectAction() { + return cloneProjectAction; + } + + /** + * Returns the “delete project” action. + * + * @return The “delete project” action + */ + I18nAction getDeleteProjectAction() { + return deleteProjectAction; + } + // // ACTIONS // @@ -153,23 +271,109 @@ public class SwingInterface implements CoreListener { // /** + * Loads the configuration of the interface. + */ + private void loadConfig() { + /* initialize default stuff. */ + beautify = false; + /* now read config. */ + File configFile = new File(configDirectory, "swing-interface.properties"); + if (!configFile.exists() || !configFile.canRead() || !configFile.isFile()) { + System.err.println("could not find “" + configFile.getAbsolutePath() + "”!"); + return; + } + Properties configProperties = new Properties(); + FileInputStream configInputStream = null; + try { + configInputStream = new FileInputStream(configFile); + configProperties.load(configInputStream); + } catch (IOException ioe1) { + System.err.println("could not load config, " + ioe1.getMessage()); + } finally { + Closer.close(configInputStream); + } + if (configProperties.containsKey("beautify")) { + beautify = Boolean.valueOf(configProperties.getProperty("beautify")); + } + } + + /** + * Saves the configuration. + */ + private void saveConfig() { + File configDirectory = new File(this.configDirectory); + if (!configDirectory.exists()) { + if (!configDirectory.mkdirs()) { + System.err.println("could not create “" + this.configDirectory + "”!"); + return; + } + } + if (!configDirectory.exists() || !configDirectory.isDirectory() || !configDirectory.canWrite()) { + System.err.println("can not access “" + this.configDirectory + "”!"); + return; + } + File configFile = new File(configDirectory, "swing-interface.properties"); + Properties configProperties = new Properties(); + configProperties.setProperty("beautify", String.valueOf(beautify)); + FileOutputStream configOutputStream = null; + try { + configOutputStream = new FileOutputStream(configFile); + configProperties.store(configOutputStream, "configuration of swing interface"); + } catch (IOException ioe1) { + System.err.println("could not save config, " + ioe1.getMessage()); + } finally { + Closer.close(configOutputStream); + } + } + + /** * Initializes all actions. */ private void initActions() { + configureAction = new I18nAction("mainWindow.menu.jSite.configure") { + + /** + * {@inheritDoc} + */ + @SuppressWarnings("synthetic-access") + public void actionPerformed(ActionEvent actionEvent) { + configure(); + } + }; + importConfigAction = new I18nAction("mainWindow.menu.jSite.importConfig") { + + /** + * {@inheritDoc} + */ + @SuppressWarnings("synthetic-access") + public void actionPerformed(ActionEvent actionEvent) { + importConfig(); + } + }; + quitAction = new I18nAction("mainWindow.menu.jSite.quit") { + + /** + * {@inheritDoc} + */ + @SuppressWarnings("synthetic-access") + public void actionPerformed(ActionEvent actionEvent) { + quit(); + } + }; manageNodesAction = new I18nAction("mainWindow.menu.node.item.manageNodes") { /** * {@inheritDoc} */ @SuppressWarnings("synthetic-access") - public void actionPerformed(ActionEvent e) { + public void actionPerformed(ActionEvent actionEvent) { manageNodes(); } }; nodeConnectAction = new I18nAction("mainWindow.menu.node.item.connect", false) { @SuppressWarnings("synthetic-access") - public void actionPerformed(ActionEvent e) { + public void actionPerformed(ActionEvent actionEvent) { nodeConnect(); } @@ -199,7 +403,46 @@ public class SwingInterface implements CoreListener { } languageActions.add(languageAction); } + helpAboutAction = new I18nAction("mainWindow.menu.help.item.about") { + + /** + * {@inheritDoc} + */ + @SuppressWarnings("synthetic-access") + public void actionPerformed(ActionEvent actionEvent) { + helpAbout(); + } + }; + addProjectAction = new I18nAction("mainWindow.button.addProject") { + + /** + * {@inheritDoc} + */ + @SuppressWarnings("synthetic-access") + public void actionPerformed(ActionEvent actionEvent) { + addProject(); + } + }; + cloneProjectAction = new I18nAction("mainWindow.button.cloneProject") { + /** + * {@inheritDoc} + */ + @SuppressWarnings("synthetic-access") + public void actionPerformed(ActionEvent actionEvent) { + cloneProject(); + } + }; + deleteProjectAction = new I18nAction("mainWindow.button.deleteProject") { + + /** + * {@inheritDoc} + */ + @SuppressWarnings("synthetic-access") + public void actionPerformed(ActionEvent actionEvent) { + deleteProject(); + } + }; } /** @@ -207,6 +450,8 @@ public class SwingInterface implements CoreListener { */ private void initDialogs() { manageNodesDialog = new ManageNodesDialog(this); + aboutDialog = new AboutDialog(this); + configurationDialog = new ConfigurationDialog(this); } // @@ -214,6 +459,32 @@ public class SwingInterface implements CoreListener { // /** + * Shows the configuration dialog. + */ + private void configure() { + configurationDialog.setBeautify(beautify); + configurationDialog.setVisible(true); + if (!configurationDialog.wasCancelled()) { + beautify = configurationDialog.getBeautify(); + saveConfig(); + } + } + + /** + * Imports old jSite configuration. + */ + private void importConfig() { + } + + /** + * Quits jSite. + */ + private void quit() { + saveConfig(); + System.exit(0); + } + + /** * Pops up the “manage nodes” dialog. */ private void manageNodes() { @@ -250,6 +521,34 @@ public class SwingInterface implements CoreListener { I18n.setLocale(newLocale); } + /** + * Shows the “about” dialog. + */ + private void helpAbout() { + aboutDialog.setVisible(true); + } + + /** + * Adds a project. + */ + private void addProject() { + Project project = new Project(); + project.setName("New Project"); + project.setDescription(""); + } + + /** + * Clones a project. + */ + private void cloneProject() { + } + + /** + * Deletes a project. + */ + private void deleteProject() { + } + // // INTERFACE CoreListener // @@ -257,6 +556,13 @@ public class SwingInterface implements CoreListener { /** * {@inheritDoc} */ + public void loadingProjectsFailed(String directory) { + JOptionPane.showMessageDialog(mainWindow, I18n.get("mainWindow.error.projectLoadingFailed.message", directory), I18n.get("mainWindow.error.projectLoadingFailed.title"), JOptionPane.ERROR_MESSAGE); + } + + /** + * {@inheritDoc} + */ public void coreLoaded() { this.nodeList = core.getNodes(); manageNodesDialog.setNodeList(nodeList);