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.i18n.I18n;
38 import net.pterodactylus.jsite.i18n.gui.I18nAction;
39 import net.pterodactylus.util.io.Closer;
44 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
47 public class SwingInterface implements CoreListener {
49 /** The application core. */
50 private final Core core;
52 /** The configuration directory. */
53 private String configDirectory;
55 /** The main window. */
56 private MainWindow mainWindow;
58 /** The “configure” action. */
59 private I18nAction configureAction;
61 /** The “import config” action. */
62 private I18nAction importConfigAction;
64 /** The “quit” action. */
65 private I18nAction quitAction;
67 /** The “manage nodes” action. */
68 private I18nAction manageNodesAction;
70 /** The “connect to node” action. */
71 private I18nAction nodeConnectAction;
73 /** The “disconnect from node” action. */
74 private I18nAction nodeDisconnectAction;
76 /** The node manager dialog. */
77 private ManageNodesDialog manageNodesDialog;
79 /** All lanugage menu items. */
80 private List<I18nAction> languageActions = new ArrayList<I18nAction>();
82 /** The “about” action. */
83 private I18nAction helpAboutAction;
85 /** The “add project” action. */
86 private I18nAction addProjectAction;
88 /** The “about” dialog. */
89 private AboutDialog aboutDialog;
91 /** The configuration dialog. */
92 private ConfigurationDialog configurationDialog;
94 /** The list of all defined nodes. */
95 private List<Node> nodeList;
101 /** Whether to beautify the GUI. */
102 private boolean beautify;
105 * Creates a new swing interface.
108 * The core to operate on
110 public SwingInterface(Core core) {
112 I18n.setLocale(Locale.ENGLISH); /* TODO - load config */
115 System.setProperty("swing.aatext", "true");
116 System.setProperty("swing.plaf.metal.controlFont", "Tahoma");
117 System.setProperty("swing.plaf.metal.userFont", "Tahoma");
128 * Returns the core that is controlled by the Swing interface.
137 * Returns the main window of the Swing interface.
139 * @return The main window
141 MainWindow getMainWindow() {
146 * Returns the “configure” action.
148 * @return The “configure” action
150 I18nAction getConfigureAction() {
151 return configureAction;
155 * Returns the “import config” action.
157 * @return The “import config” action
159 I18nAction getImportConfigAction() {
160 return importConfigAction;
164 * Returns the “quit” action.
166 * @return The “quit” action
168 I18nAction getQuitAction() {
173 * Returns the “manage nodes” action.
175 * @return The “manage nodes” action
177 I18nAction getManageNodesAction() {
178 return manageNodesAction;
182 * Returns the “connect to node” action.
184 * @return The “connect to node” action
186 I18nAction getNodeConnectAction() {
187 return nodeConnectAction;
191 * Returns the “disconnect from node” action.
193 * @return The “disconnect from node” action
195 I18nAction getNodeDisconnectAction() {
196 return nodeDisconnectAction;
200 * Returns all language actions.
202 * @return All language actions
204 List<I18nAction> getLanguageActions() {
205 return languageActions;
209 * Returns the “about” action.
211 * @return The “about” action
213 I18nAction getHelpAboutAction() {
214 return helpAboutAction;
218 * Returns the “add project” action.
220 * @return The “add project” action
222 I18nAction getAddProjectAction() {
223 return addProjectAction;
227 * Sets the configuration directory.
229 * @param configDirectory
230 * The directory the configuration is stored in
232 public void setConfigDirectory(String configDirectory) {
233 this.configDirectory = configDirectory;
245 * Starts the interface.
247 public void start() {
248 mainWindow = new MainWindow(this);
256 * Loads the configuration of the interface.
258 private void loadConfig() {
259 /* initialize default stuff. */
261 /* now read config. */
262 File configFile = new File(configDirectory, "swing-interface.properties");
263 if (!configFile.exists() || !configFile.canRead() || !configFile.isFile()) {
264 System.err.println("could not find “" + configFile.getAbsolutePath() + "”!");
267 Properties configProperties = new Properties();
268 FileInputStream configInputStream = null;
270 configInputStream = new FileInputStream(configFile);
271 configProperties.load(configInputStream);
272 } catch (IOException ioe1) {
273 System.err.println("could not load config, " + ioe1.getMessage());
275 Closer.close(configInputStream);
277 if (configProperties.containsKey("beautify")) {
278 beautify = Boolean.valueOf(configProperties.getProperty("beautify"));
283 * Saves the configuration.
285 private void saveConfig() {
286 File configDirectory = new File(this.configDirectory);
287 if (!configDirectory.exists()) {
288 if (!configDirectory.mkdirs()) {
289 System.err.println("could not create “" + this.configDirectory + "”!");
293 if (!configDirectory.exists() || !configDirectory.isDirectory() || !configDirectory.canWrite()) {
294 System.err.println("can not access “" + this.configDirectory + "”!");
297 File configFile = new File(configDirectory, "swing-interface.properties");
298 Properties configProperties = new Properties();
299 configProperties.setProperty("beautify", String.valueOf(beautify));
300 FileOutputStream configOutputStream = null;
302 configOutputStream = new FileOutputStream(configFile);
303 configProperties.store(configOutputStream, "configuration of swing interface");
304 } catch (IOException ioe1) {
305 System.err.println("could not save config, " + ioe1.getMessage());
307 Closer.close(configOutputStream);
312 * Initializes all actions.
314 private void initActions() {
315 configureAction = new I18nAction("mainWindow.menu.jSite.configure") {
320 @SuppressWarnings("synthetic-access")
321 public void actionPerformed(ActionEvent actionEvent) {
325 importConfigAction = new I18nAction("mainWindow.menu.jSite.importConfig") {
330 @SuppressWarnings("synthetic-access")
331 public void actionPerformed(ActionEvent actionEvent) {
335 quitAction = new I18nAction("mainWindow.menu.jSite.quit") {
340 @SuppressWarnings("synthetic-access")
341 public void actionPerformed(ActionEvent actionEvent) {
345 manageNodesAction = new I18nAction("mainWindow.menu.node.item.manageNodes") {
350 @SuppressWarnings("synthetic-access")
351 public void actionPerformed(ActionEvent actionEvent) {
355 nodeConnectAction = new I18nAction("mainWindow.menu.node.item.connect", false) {
357 @SuppressWarnings("synthetic-access")
358 public void actionPerformed(ActionEvent actionEvent) {
363 nodeDisconnectAction = new I18nAction("mainWindow.menu.node.item.disconnect", false) {
368 @SuppressWarnings("synthetic-access")
369 public void actionPerformed(ActionEvent e) {
373 List<Locale> availableLanguages = I18n.findAvailableLanguages();
374 for (final Locale locale: availableLanguages) {
375 I18nAction languageAction = new I18nAction("general.language." + locale.getLanguage()) {
377 @SuppressWarnings("synthetic-access")
378 public void actionPerformed(ActionEvent e) {
379 changeLanguage(locale, this);
383 if (I18n.getLocale().getLanguage().equals(locale.getLanguage())) {
384 languageAction.setEnabled(false);
386 languageActions.add(languageAction);
388 helpAboutAction = new I18nAction("mainWindow.menu.help.item.about") {
393 @SuppressWarnings("synthetic-access")
394 public void actionPerformed(ActionEvent actionEvent) {
398 addProjectAction = new I18nAction("mainWindow.button.addProject") {
403 @SuppressWarnings("synthetic-access")
404 public void actionPerformed(ActionEvent actionEvent) {
411 * Initializes all child dialogs.
413 private void initDialogs() {
414 manageNodesDialog = new ManageNodesDialog(this);
415 aboutDialog = new AboutDialog(this);
416 configurationDialog = new ConfigurationDialog(this);
424 * Shows the configuration dialog.
426 private void configure() {
427 configurationDialog.setBeautify(beautify);
428 configurationDialog.setVisible(true);
429 if (!configurationDialog.wasCancelled()) {
430 beautify = configurationDialog.getBeautify();
436 * Imports old jSite configuration.
438 private void importConfig() {
444 private void quit() {
450 * Pops up the “manage nodes” dialog.
452 private void manageNodes() {
453 manageNodesDialog.setNodeList(nodeList);
454 manageNodesDialog.setVisible(true);
455 nodeList = manageNodesDialog.getNodeList();
459 * Connects to the node.
461 private void nodeConnect() {
465 * Disconnects from the node.
467 private void nodeDisconnect() {
471 * Changes the language of the interface. This method also disables the
472 * action for the newly set language and enables all others.
476 * @param languageAction
477 * The action that triggered the change
479 private void changeLanguage(Locale newLocale, I18nAction languageAction) {
480 for (I18nAction i18nAction: languageActions) {
481 i18nAction.setEnabled(i18nAction != languageAction);
483 I18n.setLocale(newLocale);
487 * Shows the “about” dialog.
489 private void helpAbout() {
490 aboutDialog.setVisible(true);
496 private void addProject() {
500 // INTERFACE CoreListener
506 public void loadingProjectsFailed(String directory) {
507 JOptionPane.showMessageDialog(mainWindow, I18n.get("mainWindow.error.projectLoadingFailed.message", directory), I18n.get("mainWindow.error.projectLoadingFailed.title"), JOptionPane.ERROR_MESSAGE);
513 public void coreLoaded() {
514 this.nodeList = core.getNodes();
515 manageNodesDialog.setNodeList(nodeList);
516 mainWindow.setVisible(true);
517 mainWindow.setStatusBarText("Core loaded.");
523 public void nodeConnected(Node node) {
529 public void nodeConnecting(Node node) {
535 public void nodeDisconnected(Node node) {