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;
35 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
38 public class SwingInterface implements CoreListener {
40 /** The application core. */
41 private final Core core;
43 /** The main window. */
44 private MainWindow mainWindow;
46 /** The “manage nodes” action. */
47 private I18nAction manageNodesAction;
49 /** The “connect to node” action. */
50 private I18nAction nodeConnectAction;
52 /** The “disconnect from node” action. */
53 private I18nAction nodeDisconnectAction;
55 /** The node manager dialog. */
56 private ManageNodesDialog manageNodesDialog;
58 /** All lanugage menu items. */
59 private List<I18nAction> languageActions = new ArrayList<I18nAction>();
61 /** The list of all defined nodes. */
62 private List<Node> nodeList;
65 * Creates a new swing interface.
68 * The core to operate on
70 public SwingInterface(Core core) {
72 I18n.setLocale(Locale.ENGLISH); /* TODO - load config */
82 * Returns the core that is controlled by the Swing interface.
91 * Returns the main window of the Swing interface.
93 * @return The main window
95 MainWindow getMainWindow() {
100 * Returns the “manage nodes” action.
102 * @return The “manage nodes” action
104 I18nAction getManageNodesAction() {
105 return manageNodesAction;
109 * Returns the “connect to node” action.
111 * @return The “connect to node” action
113 I18nAction getNodeConnectAction() {
114 return nodeConnectAction;
118 * Returns the “disconnect from node” action.
120 * @return The “disconnect from node” action
122 I18nAction getNodeDisconnectAction() {
123 return nodeDisconnectAction;
127 * Returns all language actions.
129 * @return All language actions
131 List<I18nAction> getLanguageActions() {
132 return languageActions;
144 * Starts the interface.
146 public void start() {
147 mainWindow = new MainWindow(this);
155 * Initializes all actions.
157 private void initActions() {
158 manageNodesAction = new I18nAction("mainWindow.menu.node.item.manageNodes") {
163 @SuppressWarnings("synthetic-access")
164 public void actionPerformed(ActionEvent e) {
168 nodeConnectAction = new I18nAction("mainWindow.menu.node.item.connect", false) {
170 @SuppressWarnings("synthetic-access")
171 public void actionPerformed(ActionEvent e) {
176 nodeDisconnectAction = new I18nAction("mainWindow.menu.node.item.disconnect", false) {
181 @SuppressWarnings("synthetic-access")
182 public void actionPerformed(ActionEvent e) {
186 List<Locale> availableLanguages = I18n.findAvailableLanguages();
187 for (final Locale locale: availableLanguages) {
188 I18nAction languageAction = new I18nAction("general.language." + locale.getLanguage()) {
190 @SuppressWarnings("synthetic-access")
191 public void actionPerformed(ActionEvent e) {
192 changeLanguage(locale, this);
196 if (I18n.getLocale().getLanguage().equals(locale.getLanguage())) {
197 languageAction.setEnabled(false);
199 languageActions.add(languageAction);
205 * Initializes all child dialogs.
207 private void initDialogs() {
208 manageNodesDialog = new ManageNodesDialog(this);
216 * Pops up the “manage nodes” dialog.
218 private void manageNodes() {
219 manageNodesDialog.setNodeList(nodeList);
220 manageNodesDialog.setVisible(true);
221 nodeList = manageNodesDialog.getNodeList();
225 * Connects to the node.
227 private void nodeConnect() {
231 * Disconnects from the node.
233 private void nodeDisconnect() {
237 * Changes the language of the interface. This method also disables the
238 * action for the newly set language and enables all others.
242 * @param languageAction
243 * The action that triggered the change
245 private void changeLanguage(Locale newLocale, I18nAction languageAction) {
246 for (I18nAction i18nAction: languageActions) {
247 i18nAction.setEnabled(i18nAction != languageAction);
249 I18n.setLocale(newLocale);
253 // INTERFACE CoreListener
259 public void coreLoaded() {
260 this.nodeList = core.getNodes();
261 manageNodesDialog.setNodeList(nodeList);
262 mainWindow.setVisible(true);
263 mainWindow.setStatusBarText("Core loaded.");
269 public void nodeConnected(Node node) {
275 public void nodeConnecting(Node node) {
281 public void nodeDisconnected(Node node) {