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 net.pterodactylus.jsite.core.Core;
28 import net.pterodactylus.jsite.core.CoreListener;
29 import net.pterodactylus.jsite.core.Node;
30 import net.pterodactylus.jsite.i18n.I18n;
31 import net.pterodactylus.jsite.i18n.gui.I18nAction;
36 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
39 public class SwingInterface implements CoreListener {
41 /** The application core. */
42 private final Core core;
44 /** The main window. */
45 private MainWindow mainWindow;
47 /** The “manage nodes” action. */
48 private I18nAction manageNodesAction;
50 /** The “connect to node” action. */
51 private I18nAction nodeConnectAction;
53 /** The “disconnect from node” action. */
54 private I18nAction nodeDisconnectAction;
56 /** The node manager dialog. */
57 private ManageNodesDialog manageNodesDialog;
59 /** All lanugage menu items. */
60 private List<I18nAction> languageActions = new ArrayList<I18nAction>();
62 /** The list of all defined nodes. */
63 private List<Node> nodeList;
66 * Creates a new swing interface.
69 * The core to operate on
71 public SwingInterface(Core core) {
73 I18n.setLocale(Locale.ENGLISH); /* TODO - load config */
83 * Returns the core that is controlled by the Swing interface.
92 * Returns the main window of the Swing interface.
94 * @return The main window
96 MainWindow getMainWindow() {
101 * Returns the “manage nodes” action.
103 * @return The “manage nodes” action
105 I18nAction getManageNodesAction() {
106 return manageNodesAction;
110 * Returns the “connect to node” action.
112 * @return The “connect to node” action
114 I18nAction getNodeConnectAction() {
115 return nodeConnectAction;
119 * Returns the “disconnect from node” action.
121 * @return The “disconnect from node” action
123 I18nAction getNodeDisconnectAction() {
124 return nodeDisconnectAction;
128 * Returns all language actions.
130 * @return All language actions
132 List<I18nAction> getLanguageActions() {
133 return languageActions;
145 * Starts the interface.
147 public void start() {
148 mainWindow = new MainWindow(this);
156 * Initializes all actions.
158 private void initActions() {
159 manageNodesAction = new I18nAction("mainWindow.menu.node.item.manageNodes") {
164 @SuppressWarnings("synthetic-access")
165 public void actionPerformed(ActionEvent e) {
169 nodeConnectAction = new I18nAction("mainWindow.menu.node.item.connect", false) {
171 @SuppressWarnings("synthetic-access")
172 public void actionPerformed(ActionEvent e) {
177 nodeDisconnectAction = new I18nAction("mainWindow.menu.node.item.disconnect", false) {
182 @SuppressWarnings("synthetic-access")
183 public void actionPerformed(ActionEvent e) {
187 List<Locale> availableLanguages = I18n.findAvailableLanguages();
188 for (final Locale locale: availableLanguages) {
189 I18nAction languageAction = new I18nAction("general.language." + locale.getLanguage()) {
191 @SuppressWarnings("synthetic-access")
192 public void actionPerformed(ActionEvent e) {
193 changeLanguage(locale, this);
197 if (I18n.getLocale().getLanguage().equals(locale.getLanguage())) {
198 languageAction.setEnabled(false);
200 languageActions.add(languageAction);
206 * Initializes all child dialogs.
208 private void initDialogs() {
209 manageNodesDialog = new ManageNodesDialog(this);
217 * Pops up the “manage nodes” dialog.
219 private void manageNodes() {
220 manageNodesDialog.setNodeList(nodeList);
221 manageNodesDialog.setVisible(true);
222 nodeList = manageNodesDialog.getNodeList();
226 * Connects to the node.
228 private void nodeConnect() {
232 * Disconnects from the node.
234 private void nodeDisconnect() {
238 * Changes the language of the interface. This method also disables the
239 * action for the newly set language and enables all others.
243 * @param languageAction
244 * The action that triggered the change
246 private void changeLanguage(Locale newLocale, I18nAction languageAction) {
247 for (I18nAction i18nAction: languageActions) {
248 i18nAction.setEnabled(i18nAction != languageAction);
250 I18n.setLocale(newLocale);
254 // INTERFACE CoreListener
260 public void coreLoaded() {
261 this.nodeList = core.getNodes();
262 manageNodesDialog.setNodeList(nodeList);
263 mainWindow.setVisible(true);
264 mainWindow.setStatusBarText("Core loaded.");
270 public void nodeConnected(Node node) {
276 public void nodeConnecting(Node node) {
282 public void nodeDisconnected(Node node) {