X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Fjsite%2Fgui%2FSwingInterface.java;h=4fb71390be8b1455547ea8a02162e966791d5f19;hb=99ef3764897825a3154cb662b2b81f51c16fdbd2;hp=b2c7c92fed9ed521278a08944b646f71792ac670;hpb=238ddd807f329f3bca74572a2be011dd42167f73;p=jSite2.git diff --git a/src/net/pterodactylus/jsite/gui/SwingInterface.java b/src/net/pterodactylus/jsite/gui/SwingInterface.java index b2c7c92..4fb7139 100644 --- a/src/net/pterodactylus/jsite/gui/SwingInterface.java +++ b/src/net/pterodactylus/jsite/gui/SwingInterface.java @@ -26,6 +26,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; +import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -40,13 +41,17 @@ import java.util.logging.Logger; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.JOptionPane; +import javax.swing.UIManager; +import javax.swing.UnsupportedLookAndFeelException; import net.pterodactylus.jsite.core.Core; import net.pterodactylus.jsite.core.CoreListener; +import net.pterodactylus.jsite.core.JSiteException; 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.jsite.project.Project; +import net.pterodactylus.util.image.IconLoader; import net.pterodactylus.util.io.Closer; import net.pterodactylus.util.logging.Logging; import net.pterodactylus.util.logging.LoggingListener; @@ -138,7 +143,7 @@ public class SwingInterface implements CoreListener, LoggingListener { private ConfigurationDialog configurationDialog; /** The list of all defined nodes. */ - private List nodeList; + private List nodeList = Collections.synchronizedList(new ArrayList()); // // CONFIGURATION @@ -156,6 +161,21 @@ public class SwingInterface implements CoreListener, LoggingListener { /** The user font. */ private String userFont; + /** The class name of the look and feel. */ + private String lookAndFeel; + + /** X coordinate of the main window. */ + private int mainWindowX = -1; + + /** Y coordinate of the main window. */ + private int mainWindowY = -1; + + /** Width of the main window. */ + private int mainWindowWidth = -1; + + /** Height of the main window. */ + private int mainWindowHeight = -1; + /** * Creates a new swing interface. * @@ -169,6 +189,19 @@ public class SwingInterface implements CoreListener, LoggingListener { this.configDirectory = configDirectory; I18n.setLocale(Locale.ENGLISH); loadConfig(); + if (lookAndFeel != null) { + try { + UIManager.setLookAndFeel(lookAndFeel); + } catch (ClassNotFoundException cnfe1) { + logger.log(Level.WARNING, "could not load look and feel", cnfe1); + } catch (InstantiationException ie1) { + logger.log(Level.WARNING, "could not load look and feel", ie1); + } catch (IllegalAccessException iae1) { + logger.log(Level.WARNING, "could not load look and feel", iae1); + } catch (UnsupportedLookAndFeelException ulafe1) { + logger.log(Level.WARNING, "could not load look and feel", ulafe1); + } + } if (antialias) { System.setProperty("swing.aatext", "true"); } @@ -182,6 +215,10 @@ public class SwingInterface implements CoreListener, LoggingListener { initDialogs(); mainWindow = new MainWindow(this); mainWindow.setAdvancedMode(advancedMode); + if ((mainWindowX != -1) && (mainWindowY != -1) && (mainWindowWidth != -1) && (mainWindowHeight != -1)) { + mainWindow.setLocation(mainWindowX, mainWindowY); + mainWindow.setSize(mainWindowWidth, mainWindowHeight); + } logWindow = new LogWindow(); } @@ -380,9 +417,24 @@ public class SwingInterface implements CoreListener, LoggingListener { if (configProperties.containsKey("userFont")) { userFont = configProperties.getProperty("userFont"); } + if (configProperties.containsKey("lookAndFeel")) { + lookAndFeel = configProperties.getProperty("lookAndFeel"); + } if (configProperties.containsKey("language")) { I18n.setLocale(new Locale(configProperties.getProperty("language"))); } + if (configProperties.containsKey("mainWindowX")) { + mainWindowX = Integer.valueOf(configProperties.getProperty("mainWindowX")); + } + if (configProperties.containsKey("mainWindowY")) { + mainWindowY = Integer.valueOf(configProperties.getProperty("mainWindowY")); + } + if (configProperties.containsKey("mainWindowWidth")) { + mainWindowWidth = Integer.valueOf(configProperties.getProperty("mainWindowWidth")); + } + if (configProperties.containsKey("mainWindowHeight")) { + mainWindowHeight = Integer.valueOf(configProperties.getProperty("mainWindowHeight")); + } } /** @@ -410,7 +462,14 @@ public class SwingInterface implements CoreListener, LoggingListener { if (userFont != null) { configProperties.setProperty("userFont", userFont); } + if (lookAndFeel != null) { + configProperties.setProperty("lookAndFeel", lookAndFeel); + } configProperties.setProperty("language", I18n.getLocale().getLanguage()); + configProperties.setProperty("mainWindowX", String.valueOf(mainWindowX)); + configProperties.setProperty("mainWindowY", String.valueOf(mainWindowY)); + configProperties.setProperty("mainWindowWidth", String.valueOf(mainWindowWidth)); + configProperties.setProperty("mainWindowHeight", String.valueOf(mainWindowHeight)); FileOutputStream configOutputStream = null; try { configOutputStream = new FileOutputStream(configFile); @@ -426,7 +485,7 @@ public class SwingInterface implements CoreListener, LoggingListener { * Initializes all actions. */ private void initActions() { - configureAction = new I18nAction("mainWindow.menu.jSite.configure") { + configureAction = new I18nAction("mainWindow.menu.jSite.configure", IconLoader.loadIcon("/preferences-system.png")) { /** * {@inheritDoc} @@ -446,7 +505,7 @@ public class SwingInterface implements CoreListener, LoggingListener { importConfig(); } }; - quitAction = new I18nAction("mainWindow.menu.jSite.quit") { + quitAction = new I18nAction("mainWindow.menu.jSite.quit", IconLoader.loadIcon("/system-log-out.png")) { /** * {@inheritDoc} @@ -571,6 +630,7 @@ public class SwingInterface implements CoreListener, LoggingListener { configurationDialog.setAntialias(antialias); configurationDialog.setControlFont(controlFont); configurationDialog.setUserFont(userFont); + configurationDialog.setLookAndFeel(lookAndFeel); configurationDialog.setVisible(true); if (!configurationDialog.wasCancelled()) { advancedMode = configurationDialog.isAdvancedMode(); @@ -581,6 +641,7 @@ public class SwingInterface implements CoreListener, LoggingListener { antialias = configurationDialog.isAntialias(); controlFont = configurationDialog.getControlFont(); userFont = configurationDialog.getUserFont(); + lookAndFeel = configurationDialog.getLookAndFeel(); saveConfig(); } } @@ -596,6 +657,12 @@ public class SwingInterface implements CoreListener, LoggingListener { * Quits jSite. */ private void quit() { + /* TODO - ask */ + core.stop(); + mainWindowX = mainWindow.getX(); + mainWindowY = mainWindow.getY(); + mainWindowWidth = mainWindow.getWidth(); + mainWindowHeight = mainWindow.getHeight(); saveConfig(); System.exit(0); } @@ -607,6 +674,7 @@ public class SwingInterface implements CoreListener, LoggingListener { * The list of nodes */ private void rebuildNodeActions(List nodes) { + logger.fine("rebuilding node actions…"); nodeConnectActions.clear(); nodeNodeConnectActions.clear(); nodeConnectActionNodes.clear(); @@ -614,6 +682,7 @@ public class SwingInterface implements CoreListener, LoggingListener { nodeNodeDisconnectActions.clear(); nodeDisconnectActionNodes.clear(); for (Node node: nodes) { + logger.finer("adding node “" + node + "” to menus"); Action nodeConnectAction = new AbstractAction(node.getName()) { /** @@ -639,7 +708,7 @@ public class SwingInterface implements CoreListener, LoggingListener { nodeDisconnect(node); } }; -// nodeDisconnectActions.add(nodeDisconnectAction); + // nodeDisconnectActions.add(nodeDisconnectAction); nodeDisconnectActionNodes.put(nodeDisconnectAction, node); nodeNodeDisconnectActions.put(node, nodeDisconnectAction); } @@ -736,9 +805,18 @@ public class SwingInterface implements CoreListener, LoggingListener { * Adds a project. */ private void addProject() { - Project project = new Project(); - project.setName("New Project"); - project.setDescription(""); + try { + Project project = core.createProject(); + project.setName(I18n.get("general.newProject.name")); + project.setDescription(I18n.get("general.newProject.description", new Date())); + mainWindow.addProject(project, true); + } catch (JSiteException nne1) { + /* TODO - add i18n */ + JOptionPane.showMessageDialog(mainWindow, I18n.get(""), I18n.get(""), JOptionPane.ERROR_MESSAGE); + } catch (IOException e) { + /* TODO - add i18n */ + JOptionPane.showMessageDialog(mainWindow, I18n.get(""), I18n.get(""), JOptionPane.ERROR_MESSAGE); + } } /** @@ -764,6 +842,9 @@ public class SwingInterface implements CoreListener, LoggingListener { */ public void loadingProjectsDone(String directory) { mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.projectLoadingDone")); + for (Project project: core.getProjects()) { + mainWindow.addProject(project, false); + } } /** @@ -819,8 +900,6 @@ public class SwingInterface implements CoreListener, LoggingListener { * {@inheritDoc} */ public void coreLoaded() { - this.nodeList = core.getNodes(); - manageNodesDialog.setNodeList(nodeList); mainWindow.setVisible(true); mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.coreLoaded")); } @@ -835,6 +914,28 @@ public class SwingInterface implements CoreListener, LoggingListener { /** * {@inheritDoc} */ + public void nodeAdded(Node node) { + logger.log(Level.INFO, "node added: " + node); + nodeList.add(node); + logger.log(Level.FINE, "nodeList.size(): " + nodeList.size()); + manageNodesDialog.setNodeList(nodeList); + rebuildNodeActions(nodeList); + mainWindow.refreshNodeMenuItems(); + } + + /** + * {@inheritDoc} + */ + public void nodeRemoved(Node node) { + logger.log(Level.INFO, "node removed: " + node); + nodeList.remove(node); + rebuildNodeActions(nodeList); + mainWindow.refreshNodeMenuItems(); + } + + /** + * {@inheritDoc} + */ public void nodeConnecting(Node node) { Action nodeConnectAction = nodeNodeConnectActions.get(node); nodeConnectActions.remove(nodeConnectAction); @@ -848,6 +949,18 @@ public class SwingInterface implements CoreListener, LoggingListener { public void nodeConnected(Node node) { Action nodeDisconnectAction = nodeNodeDisconnectActions.get(node); nodeDisconnectActions.add(nodeDisconnectAction); + mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.connectedToNode", node.getName(), node.getHostname(), node.getPort())); + mainWindow.refreshNodeMenuItems(); + } + + /** + * {@inheritDoc} + */ + public void nodeConnectionFailed(Node node, Throwable cause) { + Action nodeConnectAction = nodeNodeConnectActions.get(node); + nodeConnectActions.add(nodeConnectAction); + mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.connectionToNodeFailed", node.getName(), node.getHostname(), node.getPort(), (cause != null) ? cause.getMessage() : "no reason given")); + JOptionPane.showMessageDialog(mainWindow, I18n.get("mainWindow.error.nodeConnectionFailed.message", node.getName(), node.getHostname(), node.getPort(), (cause != null) ? cause.getMessage() : "no reason given"), I18n.get("mainWindow.error.nodeConnectionFailed.title"), JOptionPane.ERROR_MESSAGE); mainWindow.refreshNodeMenuItems(); } @@ -859,9 +972,41 @@ public class SwingInterface implements CoreListener, LoggingListener { nodeConnectActions.add(nodeConnectAction); Action nodeDisconnectAction = nodeNodeDisconnectActions.get(node); nodeDisconnectActions.remove(nodeDisconnectAction); + mainWindow.setStatusBarText(I18n.get("mainWindow.statusBar.disconnectedFromNode", node.getName(), node.getHostname(), node.getPort())); mainWindow.refreshNodeMenuItems(); } + /** + * @see net.pterodactylus.jsite.core.CoreListener#projectInsertStarted(net.pterodactylus.jsite.project.Project) + */ + public void projectInsertStarted(Project project) { + mainWindow.projectInsertStarted(project); + } + + /** + * @see net.pterodactylus.jsite.core.CoreListener#projectInsertProgressed(net.pterodactylus.jsite.project.Project, + * int, int, int, int, int, boolean) + */ + public void projectInsertProgressed(Project project, int totalBlocks, int requiredBlocks, int successfulBlocks, int failedBlocks, int fatallyFailedBlocks, boolean finalizedTotal) { + mainWindow.projectInsertProgressed(project, totalBlocks, requiredBlocks, successfulBlocks, failedBlocks, fatallyFailedBlocks, finalizedTotal); + } + + /** + * @see net.pterodactylus.jsite.core.CoreListener#projectInsertGeneratedURI(net.pterodactylus.jsite.project.Project, + * java.lang.String) + */ + public void projectInsertGeneratedURI(Project project, String uri) { + mainWindow.projectInsertGeneratedURI(project); + } + + /** + * @see net.pterodactylus.jsite.core.CoreListener#projectInsertFinished(net.pterodactylus.jsite.project.Project, + * boolean) + */ + public void projectInsertFinished(Project project, boolean success) { + mainWindow.projectInsertFinished(project, success); + } + // // INTERFACE LoggingListener //