2 * jSite2 - SwingInterface.java -
3 * Copyright © 2008 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 net.pterodactylus.jsite.gui;
22 import java.awt.event.ActionEvent;
24 import java.io.FileInputStream;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Locale;
30 import java.util.Properties;
32 import javax.swing.JOptionPane;
34 import net.pterodactylus.jsite.core.Core;
35 import net.pterodactylus.jsite.core.CoreListener;
36 import net.pterodactylus.jsite.core.Node;
37 import net.pterodactylus.jsite.core.Project;
38 import net.pterodactylus.jsite.i18n.I18n;
39 import net.pterodactylus.jsite.i18n.gui.I18nAction;
40 import net.pterodactylus.util.io.Closer;
43 * The Swing user interface.
45 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
48 public class SwingInterface implements CoreListener {
50 /** The application core. */
51 private final Core core;
53 /** The configuration directory. */
54 private final String configDirectory;
56 /** The main window. */
57 private MainWindow mainWindow;
59 /** The “configure” action. */
60 private I18nAction configureAction;
62 /** The “import config” action. */
63 private I18nAction importConfigAction;
65 /** The “quit” action. */
66 private I18nAction quitAction;
68 /** The “manage nodes” action. */
69 private I18nAction manageNodesAction;
71 /** The “connect to node” action. */
72 private I18nAction nodeConnectAction;
74 /** The “disconnect from node” action. */
75 private I18nAction nodeDisconnectAction;
77 /** The node manager dialog. */
78 private ManageNodesDialog manageNodesDialog;
80 /** All lanugage menu items. */
81 private List<I18nAction> languageActions = new ArrayList<I18nAction>();
83 /** The “about” action. */
84 private I18nAction helpAboutAction;
86 /** The “add project” action. */
87 private I18nAction addProjectAction;
89 /** The “clone project” action. */
90 private I18nAction cloneProjectAction;
92 /** The “delete project” action. */
93 private I18nAction deleteProjectAction;
95 /** The “about” dialog. */
96 private AboutDialog aboutDialog;
98 /** The configuration dialog. */
99 private ConfigurationDialog configurationDialog;
101 /** The list of all defined nodes. */
102 private List<Node> nodeList;
108 /** Whether to antialias the GUI. */
109 private boolean antialias;
111 /** The control font. */
112 private String controlFont;
114 /** The user font. */
115 private String userFont;
118 * Creates a new swing interface.
121 * The core to operate on
122 * @param configDirectory
123 * The directory the configuration is stored in
125 public SwingInterface(Core core, String configDirectory) {
127 this.configDirectory = configDirectory;
128 I18n.setLocale(Locale.ENGLISH);
131 System.setProperty("swing.aatext", "true");
133 if (controlFont != null) {
134 System.setProperty("swing.plaf.metal.controlFont", controlFont);
136 if (userFont != null) {
137 System.setProperty("swing.plaf.metal.userFont", userFont);
148 * Returns the core that is controlled by the Swing interface.
157 * Returns the main window of the Swing interface.
159 * @return The main window
161 MainWindow getMainWindow() {
166 * Returns the “configure” action.
168 * @return The “configure” action
170 I18nAction getConfigureAction() {
171 return configureAction;
175 * Returns the “import config” action.
177 * @return The “import config” action
179 I18nAction getImportConfigAction() {
180 return importConfigAction;
184 * Returns the “quit” action.
186 * @return The “quit” action
188 I18nAction getQuitAction() {
193 * Returns the “manage nodes” action.
195 * @return The “manage nodes” action
197 I18nAction getManageNodesAction() {
198 return manageNodesAction;
202 * Returns the “connect to node” action.
204 * @return The “connect to node” action
206 I18nAction getNodeConnectAction() {
207 return nodeConnectAction;
211 * Returns the “disconnect from node” action.
213 * @return The “disconnect from node” action
215 I18nAction getNodeDisconnectAction() {
216 return nodeDisconnectAction;
220 * Returns all language actions.
222 * @return All language actions
224 List<I18nAction> getLanguageActions() {
225 return languageActions;
229 * Returns the “about” action.
231 * @return The “about” action
233 I18nAction getHelpAboutAction() {
234 return helpAboutAction;
238 * Returns the “add project” action.
240 * @return The “add project” action
242 I18nAction getAddProjectAction() {
243 return addProjectAction;
247 * Returns the “clone project” action.
249 * @return The “clone project” action
251 I18nAction getCloneProjectAction() {
252 return cloneProjectAction;
256 * Returns the “delete project” action.
258 * @return The “delete project” action
260 I18nAction getDeleteProjectAction() {
261 return deleteProjectAction;
273 * Starts the interface.
275 public void start() {
276 mainWindow = new MainWindow(this);
284 * Loads the configuration of the interface.
286 private void loadConfig() {
287 /* initialize default stuff. */
289 /* now read config. */
290 File configFile = new File(configDirectory, "swing-interface.properties");
291 if (!configFile.exists() || !configFile.canRead() || !configFile.isFile()) {
292 System.err.println("could not find “" + configFile.getAbsolutePath() + "”!");
295 Properties configProperties = new Properties();
296 FileInputStream configInputStream = null;
298 configInputStream = new FileInputStream(configFile);
299 configProperties.load(configInputStream);
300 } catch (IOException ioe1) {
301 System.err.println("could not load config, " + ioe1.getMessage());
303 Closer.close(configInputStream);
305 if (configProperties.containsKey("antialias")) {
306 antialias = Boolean.valueOf(configProperties.getProperty("antialias"));
308 if (configProperties.containsKey("controlFont")) {
309 controlFont = configProperties.getProperty("controlFont");
311 if (configProperties.containsKey("userFont")) {
312 userFont = configProperties.getProperty("userFont");
314 if (configProperties.containsKey("language")) {
315 I18n.setLocale(new Locale(configProperties.getProperty("language")));
320 * Saves the configuration.
322 private void saveConfig() {
323 File configDirectory = new File(this.configDirectory);
324 if (!configDirectory.exists()) {
325 if (!configDirectory.mkdirs()) {
326 System.err.println("could not create “" + this.configDirectory + "”!");
330 if (!configDirectory.exists() || !configDirectory.isDirectory() || !configDirectory.canWrite()) {
331 System.err.println("can not access “" + this.configDirectory + "”!");
334 File configFile = new File(configDirectory, "swing-interface.properties");
335 Properties configProperties = new Properties();
336 configProperties.setProperty("antialias", String.valueOf(antialias));
337 if (controlFont != null) {
338 configProperties.setProperty("controlFont", controlFont);
340 if (userFont != null) {
341 configProperties.setProperty("userFont", userFont);
343 configProperties.setProperty("language", I18n.getLocale().getLanguage());
344 FileOutputStream configOutputStream = null;
346 configOutputStream = new FileOutputStream(configFile);
347 configProperties.store(configOutputStream, "configuration of swing interface");
348 } catch (IOException ioe1) {
349 System.err.println("could not save config, " + ioe1.getMessage());
351 Closer.close(configOutputStream);
356 * Initializes all actions.
358 private void initActions() {
359 configureAction = new I18nAction("mainWindow.menu.jSite.configure") {
364 @SuppressWarnings("synthetic-access")
365 public void actionPerformed(ActionEvent actionEvent) {
369 importConfigAction = new I18nAction("mainWindow.menu.jSite.importConfig") {
374 @SuppressWarnings("synthetic-access")
375 public void actionPerformed(ActionEvent actionEvent) {
379 quitAction = new I18nAction("mainWindow.menu.jSite.quit") {
384 @SuppressWarnings("synthetic-access")
385 public void actionPerformed(ActionEvent actionEvent) {
389 manageNodesAction = new I18nAction("mainWindow.menu.node.item.manageNodes") {
394 @SuppressWarnings("synthetic-access")
395 public void actionPerformed(ActionEvent actionEvent) {
399 nodeConnectAction = new I18nAction("mainWindow.menu.node.item.connect", false) {
401 @SuppressWarnings("synthetic-access")
402 public void actionPerformed(ActionEvent actionEvent) {
407 nodeDisconnectAction = new I18nAction("mainWindow.menu.node.item.disconnect", false) {
412 @SuppressWarnings("synthetic-access")
413 public void actionPerformed(ActionEvent e) {
417 List<Locale> availableLanguages = I18n.findAvailableLanguages();
418 for (final Locale locale: availableLanguages) {
419 I18nAction languageAction = new I18nAction("general.language." + locale.getLanguage()) {
421 @SuppressWarnings("synthetic-access")
422 public void actionPerformed(ActionEvent e) {
423 changeLanguage(locale, this);
427 if (I18n.getLocale().getLanguage().equals(locale.getLanguage())) {
428 languageAction.setEnabled(false);
430 languageActions.add(languageAction);
432 helpAboutAction = new I18nAction("mainWindow.menu.help.item.about") {
437 @SuppressWarnings("synthetic-access")
438 public void actionPerformed(ActionEvent actionEvent) {
442 addProjectAction = new I18nAction("mainWindow.button.addProject") {
447 @SuppressWarnings("synthetic-access")
448 public void actionPerformed(ActionEvent actionEvent) {
452 cloneProjectAction = new I18nAction("mainWindow.button.cloneProject") {
457 @SuppressWarnings("synthetic-access")
458 public void actionPerformed(ActionEvent actionEvent) {
462 deleteProjectAction = new I18nAction("mainWindow.button.deleteProject") {
467 @SuppressWarnings("synthetic-access")
468 public void actionPerformed(ActionEvent actionEvent) {
475 * Initializes all child dialogs.
477 private void initDialogs() {
478 manageNodesDialog = new ManageNodesDialog(this);
479 aboutDialog = new AboutDialog(this);
480 configurationDialog = new ConfigurationDialog(this);
488 * Shows the configuration dialog.
490 private void configure() {
491 configurationDialog.setAntialias(antialias);
492 configurationDialog.setControlFont(controlFont);
493 configurationDialog.setUserFont(userFont);
494 configurationDialog.setVisible(true);
495 if (!configurationDialog.wasCancelled()) {
496 antialias = configurationDialog.isAntialias();
497 controlFont = configurationDialog.getControlFont();
498 userFont = configurationDialog.getUserFont();
504 * Imports old jSite configuration.
506 private void importConfig() {
513 private void quit() {
519 * Pops up the “manage nodes” dialog.
521 private void manageNodes() {
522 manageNodesDialog.setNodeList(nodeList);
523 manageNodesDialog.setVisible(true);
524 nodeList = manageNodesDialog.getNodeList();
528 * Connects to the node.
530 private void nodeConnect() {
535 * Disconnects from the node.
537 private void nodeDisconnect() {
542 * Changes the language of the interface. This method also disables the
543 * action for the newly set language and enables all others.
547 * @param languageAction
548 * The action that triggered the change
550 private void changeLanguage(Locale newLocale, I18nAction languageAction) {
551 for (I18nAction i18nAction: languageActions) {
552 i18nAction.setEnabled(i18nAction != languageAction);
554 I18n.setLocale(newLocale);
558 * Shows the “about” dialog.
560 private void helpAbout() {
561 aboutDialog.setVisible(true);
567 private void addProject() {
568 Project project = new Project();
569 project.setName("New Project");
570 project.setDescription("");
576 private void cloneProject() {
583 private void deleteProject() {
588 // INTERFACE CoreListener
594 public void loadingProjectsFailed(String directory) {
595 JOptionPane.showMessageDialog(mainWindow, I18n.get("mainWindow.error.projectLoadingFailed.message", directory), I18n.get("mainWindow.error.projectLoadingFailed.title"), JOptionPane.ERROR_MESSAGE);
601 public void coreLoaded() {
602 this.nodeList = core.getNodes();
603 manageNodesDialog.setNodeList(nodeList);
604 mainWindow.setVisible(true);
605 mainWindow.setStatusBarText("Core loaded.");
611 public void nodeConnected(Node node) {
618 public void nodeConnecting(Node node) {
625 public void nodeDisconnected(Node node) {