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 System.out.println("adding locale “" + locale.getLanguage() + "”");
189 I18nAction languageAction = new I18nAction("general.language." + locale.getLanguage()) {
191 @SuppressWarnings("synthetic-access")
192 public void actionPerformed(ActionEvent e) {
193 System.out.println("changing locale to: " + locale);
194 changeLanguage(locale, this);
198 System.out.println("locale: " + locale + ", i18n: " + I18n.getLocale());
199 if (I18n.getLocale().getLanguage().equals(locale.getLanguage())) {
200 languageAction.setEnabled(false);
202 languageActions.add(languageAction);
208 * Initializes all child dialogs.
210 private void initDialogs() {
211 manageNodesDialog = new ManageNodesDialog(this);
219 * Pops up the “manage nodes” dialog.
221 private void manageNodes() {
222 manageNodesDialog.setNodeList(nodeList);
223 manageNodesDialog.setVisible(true);
224 nodeList = manageNodesDialog.getNodeList();
228 * Connects to the node.
230 private void nodeConnect() {
234 * Disconnects from the node.
236 private void nodeDisconnect() {
240 * Changes the language of the interface. This method also disables the
241 * action for the newly set language and enables all others.
245 * @param languageAction
246 * The action that triggered the change
248 private void changeLanguage(Locale newLocale, I18nAction languageAction) {
249 for (I18nAction i18nAction: languageActions) {
250 i18nAction.setEnabled(i18nAction != languageAction);
252 I18n.setLocale(newLocale);
256 // INTERFACE CoreListener
262 public void coreLoaded() {
263 this.nodeList = core.getNodes();
264 manageNodesDialog.setNodeList(nodeList);
265 mainWindow.setVisible(true);
266 mainWindow.setStatusBarText("Core loaded.");
272 public void nodeConnected(Node node) {
278 public void nodeConnecting(Node node) {
284 public void nodeDisconnected(Node node) {