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;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Locale;
27 import javax.swing.JOptionPane;
29 import net.pterodactylus.jsite.core.Core;
30 import net.pterodactylus.jsite.core.CoreListener;
31 import net.pterodactylus.jsite.core.Node;
32 import net.pterodactylus.jsite.i18n.I18n;
33 import net.pterodactylus.jsite.i18n.gui.I18nAction;
38 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
41 public class SwingInterface implements CoreListener {
43 /** The application core. */
44 private final Core core;
46 /** The main window. */
47 private MainWindow mainWindow;
49 /** The “configure” action. */
50 private I18nAction configureAction;
52 /** The “import config” action. */
53 private I18nAction importConfigAction;
55 /** The “quit” action. */
56 private I18nAction quitAction;
58 /** The “manage nodes” action. */
59 private I18nAction manageNodesAction;
61 /** The “connect to node” action. */
62 private I18nAction nodeConnectAction;
64 /** The “disconnect from node” action. */
65 private I18nAction nodeDisconnectAction;
67 /** The node manager dialog. */
68 private ManageNodesDialog manageNodesDialog;
70 /** All lanugage menu items. */
71 private List<I18nAction> languageActions = new ArrayList<I18nAction>();
73 /** The “about” action. */
74 private I18nAction helpAboutAction;
76 /** The “add project” action. */
77 private I18nAction addProjectAction;
79 /** The “about” dialog. */
80 private AboutDialog aboutDialog;
82 /** The list of all defined nodes. */
83 private List<Node> nodeList;
86 * Creates a new swing interface.
89 * The core to operate on
91 public SwingInterface(Core core) {
93 I18n.setLocale(Locale.ENGLISH); /* TODO - load config */
94 System.setProperty("swing.aatext", "true");
95 System.setProperty("swing.plaf.metal.controlFont", "Tahoma");
96 System.setProperty("swing.plaf.metal.userFont", "Tahoma");
106 * Returns the core that is controlled by the Swing interface.
115 * Returns the main window of the Swing interface.
117 * @return The main window
119 MainWindow getMainWindow() {
124 * Returns the “configure” action.
126 * @return The “configure” action
128 I18nAction getConfigureAction() {
129 return configureAction;
133 * Returns the “import config” action.
135 * @return The “import config” action
137 I18nAction getImportConfigAction() {
138 return importConfigAction;
142 * Returns the “quit” action.
144 * @return The “quit” action
146 I18nAction getQuitAction() {
151 * Returns the “manage nodes” action.
153 * @return The “manage nodes” action
155 I18nAction getManageNodesAction() {
156 return manageNodesAction;
160 * Returns the “connect to node” action.
162 * @return The “connect to node” action
164 I18nAction getNodeConnectAction() {
165 return nodeConnectAction;
169 * Returns the “disconnect from node” action.
171 * @return The “disconnect from node” action
173 I18nAction getNodeDisconnectAction() {
174 return nodeDisconnectAction;
178 * Returns all language actions.
180 * @return All language actions
182 List<I18nAction> getLanguageActions() {
183 return languageActions;
187 * Returns the “about” action.
189 * @return The “about” action
191 I18nAction getHelpAboutAction() {
192 return helpAboutAction;
196 * Returns the “add project” action.
198 * @return The “add project” action
200 I18nAction getAddProjectAction() {
201 return addProjectAction;
213 * Starts the interface.
215 public void start() {
216 mainWindow = new MainWindow(this);
224 * Initializes all actions.
226 private void initActions() {
227 configureAction = new I18nAction("mainWindow.menu.jSite.configure") {
232 @SuppressWarnings("synthetic-access")
233 public void actionPerformed(ActionEvent actionEvent) {
237 importConfigAction = new I18nAction("mainWindow.menu.jSite.importConfig") {
242 @SuppressWarnings("synthetic-access")
243 public void actionPerformed(ActionEvent actionEvent) {
247 quitAction = new I18nAction("mainWindow.menu.jSite.quit") {
252 @SuppressWarnings("synthetic-access")
253 public void actionPerformed(ActionEvent actionEvent) {
257 manageNodesAction = new I18nAction("mainWindow.menu.node.item.manageNodes") {
262 @SuppressWarnings("synthetic-access")
263 public void actionPerformed(ActionEvent actionEvent) {
267 nodeConnectAction = new I18nAction("mainWindow.menu.node.item.connect", false) {
269 @SuppressWarnings("synthetic-access")
270 public void actionPerformed(ActionEvent actionEvent) {
275 nodeDisconnectAction = new I18nAction("mainWindow.menu.node.item.disconnect", false) {
280 @SuppressWarnings("synthetic-access")
281 public void actionPerformed(ActionEvent e) {
285 List<Locale> availableLanguages = I18n.findAvailableLanguages();
286 for (final Locale locale: availableLanguages) {
287 I18nAction languageAction = new I18nAction("general.language." + locale.getLanguage()) {
289 @SuppressWarnings("synthetic-access")
290 public void actionPerformed(ActionEvent e) {
291 changeLanguage(locale, this);
295 if (I18n.getLocale().getLanguage().equals(locale.getLanguage())) {
296 languageAction.setEnabled(false);
298 languageActions.add(languageAction);
300 helpAboutAction = new I18nAction("mainWindow.menu.help.item.about") {
305 @SuppressWarnings("synthetic-access")
306 public void actionPerformed(ActionEvent actionEvent) {
310 addProjectAction = new I18nAction("mainWindow.button.addProject") {
315 @SuppressWarnings("synthetic-access")
316 public void actionPerformed(ActionEvent actionEvent) {
323 * Initializes all child dialogs.
325 private void initDialogs() {
326 manageNodesDialog = new ManageNodesDialog(this);
327 aboutDialog = new AboutDialog(this);
335 * Shows the configuration dialog.
337 private void configure() {
341 * Imports old jSite configuration.
343 private void importConfig() {
349 private void quit() {
354 * Pops up the “manage nodes” dialog.
356 private void manageNodes() {
357 manageNodesDialog.setNodeList(nodeList);
358 manageNodesDialog.setVisible(true);
359 nodeList = manageNodesDialog.getNodeList();
363 * Connects to the node.
365 private void nodeConnect() {
369 * Disconnects from the node.
371 private void nodeDisconnect() {
375 * Changes the language of the interface. This method also disables the
376 * action for the newly set language and enables all others.
380 * @param languageAction
381 * The action that triggered the change
383 private void changeLanguage(Locale newLocale, I18nAction languageAction) {
384 for (I18nAction i18nAction: languageActions) {
385 i18nAction.setEnabled(i18nAction != languageAction);
387 I18n.setLocale(newLocale);
391 * Shows the “about” dialog.
393 private void helpAbout() {
394 aboutDialog.setVisible(true);
400 private void addProject() {
404 // INTERFACE CoreListener
410 public void loadingProjectsFailed(String directory) {
411 JOptionPane.showMessageDialog(mainWindow, I18n.get("mainWindow.error.projectLoadingFailed.message", directory), I18n.get("mainWindow.error.projectLoadingFailed.title"), JOptionPane.ERROR_MESSAGE);
417 public void coreLoaded() {
418 this.nodeList = core.getNodes();
419 manageNodesDialog.setNodeList(nodeList);
420 mainWindow.setVisible(true);
421 mainWindow.setStatusBarText("Core loaded.");
427 public void nodeConnected(Node node) {
433 public void nodeConnecting(Node node) {
439 public void nodeDisconnected(Node node) {