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");
317 * Saves the configuration.
319 private void saveConfig() {
320 File configDirectory = new File(this.configDirectory);
321 if (!configDirectory.exists()) {
322 if (!configDirectory.mkdirs()) {
323 System.err.println("could not create “" + this.configDirectory + "”!");
327 if (!configDirectory.exists() || !configDirectory.isDirectory() || !configDirectory.canWrite()) {
328 System.err.println("can not access “" + this.configDirectory + "”!");
331 File configFile = new File(configDirectory, "swing-interface.properties");
332 Properties configProperties = new Properties();
333 configProperties.setProperty("antialias", String.valueOf(antialias));
334 if (controlFont != null) {
335 configProperties.setProperty("controlFont", controlFont);
337 if (userFont != null) {
338 configProperties.setProperty("userFont", userFont);
340 FileOutputStream configOutputStream = null;
342 configOutputStream = new FileOutputStream(configFile);
343 configProperties.store(configOutputStream, "configuration of swing interface");
344 } catch (IOException ioe1) {
345 System.err.println("could not save config, " + ioe1.getMessage());
347 Closer.close(configOutputStream);
352 * Initializes all actions.
354 private void initActions() {
355 configureAction = new I18nAction("mainWindow.menu.jSite.configure") {
360 @SuppressWarnings("synthetic-access")
361 public void actionPerformed(ActionEvent actionEvent) {
365 importConfigAction = new I18nAction("mainWindow.menu.jSite.importConfig") {
370 @SuppressWarnings("synthetic-access")
371 public void actionPerformed(ActionEvent actionEvent) {
375 quitAction = new I18nAction("mainWindow.menu.jSite.quit") {
380 @SuppressWarnings("synthetic-access")
381 public void actionPerformed(ActionEvent actionEvent) {
385 manageNodesAction = new I18nAction("mainWindow.menu.node.item.manageNodes") {
390 @SuppressWarnings("synthetic-access")
391 public void actionPerformed(ActionEvent actionEvent) {
395 nodeConnectAction = new I18nAction("mainWindow.menu.node.item.connect", false) {
397 @SuppressWarnings("synthetic-access")
398 public void actionPerformed(ActionEvent actionEvent) {
403 nodeDisconnectAction = new I18nAction("mainWindow.menu.node.item.disconnect", false) {
408 @SuppressWarnings("synthetic-access")
409 public void actionPerformed(ActionEvent e) {
413 List<Locale> availableLanguages = I18n.findAvailableLanguages();
414 for (final Locale locale: availableLanguages) {
415 I18nAction languageAction = new I18nAction("general.language." + locale.getLanguage()) {
417 @SuppressWarnings("synthetic-access")
418 public void actionPerformed(ActionEvent e) {
419 changeLanguage(locale, this);
423 if (I18n.getLocale().getLanguage().equals(locale.getLanguage())) {
424 languageAction.setEnabled(false);
426 languageActions.add(languageAction);
428 helpAboutAction = new I18nAction("mainWindow.menu.help.item.about") {
433 @SuppressWarnings("synthetic-access")
434 public void actionPerformed(ActionEvent actionEvent) {
438 addProjectAction = new I18nAction("mainWindow.button.addProject") {
443 @SuppressWarnings("synthetic-access")
444 public void actionPerformed(ActionEvent actionEvent) {
448 cloneProjectAction = new I18nAction("mainWindow.button.cloneProject") {
453 @SuppressWarnings("synthetic-access")
454 public void actionPerformed(ActionEvent actionEvent) {
458 deleteProjectAction = new I18nAction("mainWindow.button.deleteProject") {
463 @SuppressWarnings("synthetic-access")
464 public void actionPerformed(ActionEvent actionEvent) {
471 * Initializes all child dialogs.
473 private void initDialogs() {
474 manageNodesDialog = new ManageNodesDialog(this);
475 aboutDialog = new AboutDialog(this);
476 configurationDialog = new ConfigurationDialog(this);
484 * Shows the configuration dialog.
486 private void configure() {
487 configurationDialog.setAntialias(antialias);
488 configurationDialog.setControlFont(controlFont);
489 configurationDialog.setUserFont(userFont);
490 configurationDialog.setVisible(true);
491 if (!configurationDialog.wasCancelled()) {
492 antialias = configurationDialog.isAntialias();
493 controlFont = configurationDialog.getControlFont();
494 userFont = configurationDialog.getUserFont();
500 * Imports old jSite configuration.
502 private void importConfig() {
508 private void quit() {
514 * Pops up the “manage nodes” dialog.
516 private void manageNodes() {
517 manageNodesDialog.setNodeList(nodeList);
518 manageNodesDialog.setVisible(true);
519 nodeList = manageNodesDialog.getNodeList();
523 * Connects to the node.
525 private void nodeConnect() {
529 * Disconnects from the node.
531 private void nodeDisconnect() {
535 * Changes the language of the interface. This method also disables the
536 * action for the newly set language and enables all others.
540 * @param languageAction
541 * The action that triggered the change
543 private void changeLanguage(Locale newLocale, I18nAction languageAction) {
544 for (I18nAction i18nAction: languageActions) {
545 i18nAction.setEnabled(i18nAction != languageAction);
547 I18n.setLocale(newLocale);
551 * Shows the “about” dialog.
553 private void helpAbout() {
554 aboutDialog.setVisible(true);
560 private void addProject() {
561 Project project = new Project();
562 project.setName("New Project");
563 project.setDescription("");
569 private void cloneProject() {
575 private void deleteProject() {
579 // INTERFACE CoreListener
585 public void loadingProjectsFailed(String directory) {
586 JOptionPane.showMessageDialog(mainWindow, I18n.get("mainWindow.error.projectLoadingFailed.message", directory), I18n.get("mainWindow.error.projectLoadingFailed.title"), JOptionPane.ERROR_MESSAGE);
592 public void coreLoaded() {
593 this.nodeList = core.getNodes();
594 manageNodesDialog.setNodeList(nodeList);
595 mainWindow.setVisible(true);
596 mainWindow.setStatusBarText("Core loaded.");
602 public void nodeConnected(Node node) {
608 public void nodeConnecting(Node node) {
614 public void nodeDisconnected(Node node) {