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