remove debug output
[jSite2.git] / src / net / pterodactylus / jsite / gui / SwingInterface.java
1 /*
2  * jSite2 - SwingInterface.java -
3  * Copyright © 2008 David Roden
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 package net.pterodactylus.jsite.gui;
21
22 import java.awt.event.ActionEvent;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Locale;
26
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
32 /**
33  * TODO
34  * 
35  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
36  * @version $Id$
37  */
38 public class SwingInterface implements CoreListener {
39
40         /** The application core. */
41         private final Core core;
42
43         /** The main window. */
44         private MainWindow mainWindow;
45
46         /** The “manage nodes” action. */
47         private I18nAction manageNodesAction;
48
49         /** The “connect to node” action. */
50         private I18nAction nodeConnectAction;
51
52         /** The “disconnect from node” action. */
53         private I18nAction nodeDisconnectAction;
54
55         /** The node manager dialog. */
56         private ManageNodesDialog manageNodesDialog;
57
58         /** All lanugage menu items. */
59         private List<I18nAction> languageActions = new ArrayList<I18nAction>();
60
61         /** The list of all defined nodes. */
62         private List<Node> nodeList;
63
64         /**
65          * Creates a new swing interface.
66          * 
67          * @param core
68          *            The core to operate on
69          */
70         public SwingInterface(Core core) {
71                 this.core = core;
72                 I18n.setLocale(Locale.ENGLISH); /* TODO - load config */
73                 initActions();
74                 initDialogs();
75         }
76
77         //
78         // ACCESSORS
79         //
80
81         /**
82          * Returns the core that is controlled by the Swing interface.
83          * 
84          * @return The core
85          */
86         Core getCore() {
87                 return core;
88         }
89
90         /**
91          * Returns the main window of the Swing interface.
92          * 
93          * @return The main window
94          */
95         MainWindow getMainWindow() {
96                 return mainWindow;
97         }
98
99         /**
100          * Returns the “manage nodes” action.
101          * 
102          * @return The “manage nodes” action
103          */
104         I18nAction getManageNodesAction() {
105                 return manageNodesAction;
106         }
107
108         /**
109          * Returns the “connect to node” action.
110          * 
111          * @return The “connect to node” action
112          */
113         I18nAction getNodeConnectAction() {
114                 return nodeConnectAction;
115         }
116
117         /**
118          * Returns the “disconnect from node” action.
119          * 
120          * @return The “disconnect from node” action
121          */
122         I18nAction getNodeDisconnectAction() {
123                 return nodeDisconnectAction;
124         }
125
126         /**
127          * Returns all language actions.
128          * 
129          * @return All language actions
130          */
131         List<I18nAction> getLanguageActions() {
132                 return languageActions;
133         }
134
135         //
136         // ACTIONS
137         //
138
139         //
140         // SERVICE METHODS
141         //
142
143         /**
144          * Starts the interface.
145          */
146         public void start() {
147                 mainWindow = new MainWindow(this);
148         }
149
150         //
151         // PRIVATE METHODS
152         //
153
154         /**
155          * Initializes all actions.
156          */
157         private void initActions() {
158                 manageNodesAction = new I18nAction("mainWindow.menu.node.item.manageNodes") {
159
160                         /**
161                          * {@inheritDoc}
162                          */
163                         @SuppressWarnings("synthetic-access")
164                         public void actionPerformed(ActionEvent e) {
165                                 manageNodes();
166                         }
167                 };
168                 nodeConnectAction = new I18nAction("mainWindow.menu.node.item.connect", false) {
169
170                         @SuppressWarnings("synthetic-access")
171                         public void actionPerformed(ActionEvent e) {
172                                 nodeConnect();
173                         }
174
175                 };
176                 nodeDisconnectAction = new I18nAction("mainWindow.menu.node.item.disconnect", false) {
177
178                         /**
179                          * {@inheritDoc}
180                          */
181                         @SuppressWarnings("synthetic-access")
182                         public void actionPerformed(ActionEvent e) {
183                                 nodeDisconnect();
184                         }
185                 };
186                 List<Locale> availableLanguages = I18n.findAvailableLanguages();
187                 for (final Locale locale: availableLanguages) {
188                         I18nAction languageAction = new I18nAction("general.language." + locale.getLanguage()) {
189
190                                 @SuppressWarnings("synthetic-access")
191                                 public void actionPerformed(ActionEvent e) {
192                                         changeLanguage(locale, this);
193                                 }
194
195                         };
196                         if (I18n.getLocale().getLanguage().equals(locale.getLanguage())) {
197                                 languageAction.setEnabled(false);
198                         }
199                         languageActions.add(languageAction);
200                 }
201
202         }
203
204         /**
205          * Initializes all child dialogs.
206          */
207         private void initDialogs() {
208                 manageNodesDialog = new ManageNodesDialog(this);
209         }
210
211         //
212         // PRIVATE ACTIONS
213         //
214
215         /**
216          * Pops up the “manage nodes” dialog.
217          */
218         private void manageNodes() {
219                 manageNodesDialog.setNodeList(nodeList);
220                 manageNodesDialog.setVisible(true);
221                 nodeList = manageNodesDialog.getNodeList();
222         }
223
224         /**
225          * Connects to the node.
226          */
227         private void nodeConnect() {
228         }
229
230         /**
231          * Disconnects from the node.
232          */
233         private void nodeDisconnect() {
234         }
235
236         /**
237          * Changes the language of the interface. This method also disables the
238          * action for the newly set language and enables all others.
239          * 
240          * @param newLocale
241          *            The new language
242          * @param languageAction
243          *            The action that triggered the change
244          */
245         private void changeLanguage(Locale newLocale, I18nAction languageAction) {
246                 for (I18nAction i18nAction: languageActions) {
247                         i18nAction.setEnabled(i18nAction != languageAction);
248                 }
249                 I18n.setLocale(newLocale);
250         }
251
252         //
253         // INTERFACE CoreListener
254         //
255
256         /**
257          * {@inheritDoc}
258          */
259         public void coreLoaded() {
260                 this.nodeList = core.getNodes();
261                 manageNodesDialog.setNodeList(nodeList);
262                 mainWindow.setVisible(true);
263                 mainWindow.setStatusBarText("Core loaded.");
264         }
265
266         /**
267          * {@inheritDoc}
268          */
269         public void nodeConnected(Node node) {
270         }
271
272         /**
273          * {@inheritDoc}
274          */
275         public void nodeConnecting(Node node) {
276         }
277
278         /**
279          * {@inheritDoc}
280          */
281         public void nodeDisconnected(Node node) {
282         }
283
284 }