add language menu
[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                         System.out.println("adding locale “" + locale.getLanguage() + "”");
189                         I18nAction languageAction = new I18nAction("general.language." + locale.getLanguage()) {
190
191                                 @SuppressWarnings("synthetic-access")
192                                 public void actionPerformed(ActionEvent e) {
193                                         System.out.println("changing locale to: " + locale);
194                                         changeLanguage(locale, this);
195                                 }
196
197                         };
198                         System.out.println("locale: " + locale + ", i18n: " + I18n.getLocale());
199                         if (I18n.getLocale().getLanguage().equals(locale.getLanguage())) {
200                                 languageAction.setEnabled(false);
201                         }
202                         languageActions.add(languageAction);
203                 }
204
205         }
206
207         /**
208          * Initializes all child dialogs.
209          */
210         private void initDialogs() {
211                 manageNodesDialog = new ManageNodesDialog(this);
212         }
213
214         //
215         // PRIVATE ACTIONS
216         //
217
218         /**
219          * Pops up the “manage nodes” dialog.
220          */
221         private void manageNodes() {
222                 manageNodesDialog.setNodeList(nodeList);
223                 manageNodesDialog.setVisible(true);
224                 nodeList = manageNodesDialog.getNodeList();
225         }
226
227         /**
228          * Connects to the node.
229          */
230         private void nodeConnect() {
231         }
232
233         /**
234          * Disconnects from the node.
235          */
236         private void nodeDisconnect() {
237         }
238
239         /**
240          * Changes the language of the interface. This method also disables the
241          * action for the newly set language and enables all others.
242          * 
243          * @param newLocale
244          *            The new language
245          * @param languageAction
246          *            The action that triggered the change
247          */
248         private void changeLanguage(Locale newLocale, I18nAction languageAction) {
249                 for (I18nAction i18nAction: languageActions) {
250                         i18nAction.setEnabled(i18nAction != languageAction);
251                 }
252                 I18n.setLocale(newLocale);
253         }
254
255         //
256         // INTERFACE CoreListener
257         //
258
259         /**
260          * {@inheritDoc}
261          */
262         public void coreLoaded() {
263                 this.nodeList = core.getNodes();
264                 manageNodesDialog.setNodeList(nodeList);
265                 mainWindow.setVisible(true);
266                 mainWindow.setStatusBarText("Core loaded.");
267         }
268
269         /**
270          * {@inheritDoc}
271          */
272         public void nodeConnected(Node node) {
273         }
274
275         /**
276          * {@inheritDoc}
277          */
278         public void nodeConnecting(Node node) {
279         }
280
281         /**
282          * {@inheritDoc}
283          */
284         public void nodeDisconnected(Node node) {
285         }
286
287 }