X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Fjsite%2Fgui%2FSwingInterface.java;h=2f6f96538b456b358ad2fb2b0a485c7b1f93da81;hb=c7561c631f4bbb94ee0ac9047e953ce56f1df8bb;hp=e98fa80bbce456fbb2bfb274a0fd0b7fcc696483;hpb=c74ea301cd9c71a434685bb75de2b7a2bb2440e4;p=jSite2.git diff --git a/src/net/pterodactylus/jsite/gui/SwingInterface.java b/src/net/pterodactylus/jsite/gui/SwingInterface.java index e98fa80..2f6f965 100644 --- a/src/net/pterodactylus/jsite/gui/SwingInterface.java +++ b/src/net/pterodactylus/jsite/gui/SwingInterface.java @@ -20,21 +20,28 @@ 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$ */ @@ -43,6 +50,9 @@ 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; @@ -76,24 +86,56 @@ public class SwingInterface implements CoreListener { /** 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 antialias the GUI. */ + private boolean antialias; + + /** The control font. */ + private String controlFont; + + /** The user font. */ + private String userFont; + /** * 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 */ - System.setProperty("swing.aatext", "true"); - System.setProperty("swing.plaf.metal.controlFont", "Tahoma"); - System.setProperty("swing.plaf.metal.userFont", "Tahoma"); + this.configDirectory = configDirectory; + I18n.setLocale(Locale.ENGLISH); + loadConfig(); + if (antialias) { + System.setProperty("swing.aatext", "true"); + } + if (controlFont != null) { + System.setProperty("swing.plaf.metal.controlFont", controlFont); + } + if (userFont != null) { + System.setProperty("swing.plaf.metal.userFont", userFont); + } initActions(); initDialogs(); } @@ -104,7 +146,7 @@ public class SwingInterface implements CoreListener { /** * Returns the core that is controlled by the Swing interface. - * + * * @return The core */ Core getCore() { @@ -113,7 +155,7 @@ public class SwingInterface implements CoreListener { /** * Returns the main window of the Swing interface. - * + * * @return The main window */ MainWindow getMainWindow() { @@ -122,7 +164,7 @@ public class SwingInterface implements CoreListener { /** * Returns the “configure” action. - * + * * @return The “configure” action */ I18nAction getConfigureAction() { @@ -131,7 +173,7 @@ public class SwingInterface implements CoreListener { /** * Returns the “import config” action. - * + * * @return The “import config” action */ I18nAction getImportConfigAction() { @@ -140,7 +182,7 @@ public class SwingInterface implements CoreListener { /** * Returns the “quit” action. - * + * * @return The “quit” action */ I18nAction getQuitAction() { @@ -149,7 +191,7 @@ public class SwingInterface implements CoreListener { /** * Returns the “manage nodes” action. - * + * * @return The “manage nodes” action */ I18nAction getManageNodesAction() { @@ -158,7 +200,7 @@ public class SwingInterface implements CoreListener { /** * Returns the “connect to node” action. - * + * * @return The “connect to node” action */ I18nAction getNodeConnectAction() { @@ -167,7 +209,7 @@ public class SwingInterface implements CoreListener { /** * Returns the “disconnect from node” action. - * + * * @return The “disconnect from node” action */ I18nAction getNodeDisconnectAction() { @@ -176,7 +218,7 @@ public class SwingInterface implements CoreListener { /** * Returns all language actions. - * + * * @return All language actions */ List getLanguageActions() { @@ -185,7 +227,7 @@ public class SwingInterface implements CoreListener { /** * Returns the “about” action. - * + * * @return The “about” action */ I18nAction getHelpAboutAction() { @@ -194,13 +236,31 @@ public class SwingInterface implements CoreListener { /** * 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 // @@ -221,6 +281,78 @@ public class SwingInterface implements CoreListener { // /** + * Loads the configuration of the interface. + */ + private void loadConfig() { + /* initialize default stuff. */ + antialias = 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("antialias")) { + antialias = Boolean.valueOf(configProperties.getProperty("antialias")); + } + if (configProperties.containsKey("controlFont")) { + controlFont = configProperties.getProperty("controlFont"); + } + if (configProperties.containsKey("userFont")) { + userFont = configProperties.getProperty("userFont"); + } + if (configProperties.containsKey("language")) { + I18n.setLocale(new Locale(configProperties.getProperty("language"))); + } + } + + /** + * 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("antialias", String.valueOf(antialias)); + if (controlFont != null) { + configProperties.setProperty("controlFont", controlFont); + } + if (userFont != null) { + configProperties.setProperty("userFont", userFont); + } + configProperties.setProperty("language", I18n.getLocale().getLanguage()); + 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() { @@ -317,6 +449,26 @@ public class SwingInterface implements CoreListener { 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(); + } + }; } /** @@ -325,6 +477,7 @@ public class SwingInterface implements CoreListener { private void initDialogs() { manageNodesDialog = new ManageNodesDialog(this); aboutDialog = new AboutDialog(this); + configurationDialog = new ConfigurationDialog(this); } // @@ -335,18 +488,30 @@ public class SwingInterface implements CoreListener { * Shows the configuration dialog. */ private void configure() { + configurationDialog.setAntialias(antialias); + configurationDialog.setControlFont(controlFont); + configurationDialog.setUserFont(userFont); + configurationDialog.setVisible(true); + if (!configurationDialog.wasCancelled()) { + antialias = configurationDialog.isAntialias(); + controlFont = configurationDialog.getControlFont(); + userFont = configurationDialog.getUserFont(); + saveConfig(); + } } /** * Imports old jSite configuration. */ private void importConfig() { + /* TODO */ } /** * Quits jSite. */ private void quit() { + saveConfig(); System.exit(0); } @@ -363,18 +528,20 @@ public class SwingInterface implements CoreListener { * Connects to the node. */ private void nodeConnect() { + /* TODO */ } /** * Disconnects from the node. */ private void nodeDisconnect() { + /* TODO */ } /** * Changes the language of the interface. This method also disables the * action for the newly set language and enables all others. - * + * * @param newLocale * The new language * @param languageAction @@ -398,6 +565,23 @@ public class SwingInterface implements CoreListener { * Adds a project. */ private void addProject() { + Project project = new Project(); + project.setName("New Project"); + project.setDescription(""); + } + + /** + * Clones a project. + */ + private void cloneProject() { + /* TODO */ + } + + /** + * Deletes a project. + */ + private void deleteProject() { + /* TODO */ } // @@ -407,36 +591,60 @@ public class SwingInterface implements CoreListener { /** * {@inheritDoc} */ - public void loadingProjectsFailed(String directory) { + public void loadingProjectsFailed(String directory, Throwable throwable) { JOptionPane.showMessageDialog(mainWindow, I18n.get("mainWindow.error.projectLoadingFailed.message", directory), I18n.get("mainWindow.error.projectLoadingFailed.title"), JOptionPane.ERROR_MESSAGE); } /** * {@inheritDoc} */ + public void savingProjectsDone(String directory) { + mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.projectSavingDone")); + } + + /** + * {@inheritDoc} + */ + public void savingProjectsFailed(String directory, Throwable throwabled) { + /* TODO */ + } + + /** + * {@inheritDoc} + */ public void coreLoaded() { this.nodeList = core.getNodes(); manageNodesDialog.setNodeList(nodeList); mainWindow.setVisible(true); - mainWindow.setStatusBarText("Core loaded."); + mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.coreLoaded")); + } + + /** + * {@inheritDoc} + */ + public void coreStopped() { + mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.coreStopped")); } /** * {@inheritDoc} */ public void nodeConnected(Node node) { + /* TODO */ } /** * {@inheritDoc} */ public void nodeConnecting(Node node) { + /* TODO */ } /** * {@inheritDoc} */ public void nodeDisconnected(Node node) { + /* TODO */ } }