add actions for "jsite" and "about" menus
[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 javax.swing.JOptionPane;
28
29 import net.pterodactylus.jsite.core.Core;
30 import net.pterodactylus.jsite.core.CoreListener;
31 import net.pterodactylus.jsite.core.Node;
32 import net.pterodactylus.jsite.i18n.I18n;
33 import net.pterodactylus.jsite.i18n.gui.I18nAction;
34
35 /**
36  * TODO
37  * 
38  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
39  * @version $Id$
40  */
41 public class SwingInterface implements CoreListener {
42
43         /** The application core. */
44         private final Core core;
45
46         /** The main window. */
47         private MainWindow mainWindow;
48
49         /** The “configure” action. */
50         private I18nAction configureAction;
51
52         /** The “import config” action. */
53         private I18nAction importConfigAction;
54
55         /** The “quit” action. */
56         private I18nAction quitAction;
57
58         /** The “manage nodes” action. */
59         private I18nAction manageNodesAction;
60
61         /** The “connect to node” action. */
62         private I18nAction nodeConnectAction;
63
64         /** The “disconnect from node” action. */
65         private I18nAction nodeDisconnectAction;
66
67         /** The node manager dialog. */
68         private ManageNodesDialog manageNodesDialog;
69
70         /** All lanugage menu items. */
71         private List<I18nAction> languageActions = new ArrayList<I18nAction>();
72
73         /** The “about” action. */
74         private I18nAction helpAboutAction;
75
76         /** The “add project” action. */
77         private I18nAction addProjectAction;
78
79         /** The list of all defined nodes. */
80         private List<Node> nodeList;
81
82         /**
83          * Creates a new swing interface.
84          * 
85          * @param core
86          *            The core to operate on
87          */
88         public SwingInterface(Core core) {
89                 this.core = core;
90                 I18n.setLocale(Locale.ENGLISH); /* TODO - load config */
91                 initActions();
92                 initDialogs();
93         }
94
95         //
96         // ACCESSORS
97         //
98
99         /**
100          * Returns the core that is controlled by the Swing interface.
101          * 
102          * @return The core
103          */
104         Core getCore() {
105                 return core;
106         }
107
108         /**
109          * Returns the main window of the Swing interface.
110          * 
111          * @return The main window
112          */
113         MainWindow getMainWindow() {
114                 return mainWindow;
115         }
116
117         /**
118          * Returns the “configure” action.
119          * 
120          * @return The “configure” action
121          */
122         I18nAction getConfigureAction() {
123                 return configureAction;
124         }
125
126         /**
127          * Returns the “import config” action.
128          * 
129          * @return The “import config” action
130          */
131         I18nAction getImportConfigAction() {
132                 return importConfigAction;
133         }
134
135         /**
136          * Returns the “quit” action.
137          * 
138          * @return The “quit” action
139          */
140         I18nAction getQuitAction() {
141                 return quitAction;
142         }
143
144         /**
145          * Returns the “manage nodes” action.
146          * 
147          * @return The “manage nodes” action
148          */
149         I18nAction getManageNodesAction() {
150                 return manageNodesAction;
151         }
152
153         /**
154          * Returns the “connect to node” action.
155          * 
156          * @return The “connect to node” action
157          */
158         I18nAction getNodeConnectAction() {
159                 return nodeConnectAction;
160         }
161
162         /**
163          * Returns the “disconnect from node” action.
164          * 
165          * @return The “disconnect from node” action
166          */
167         I18nAction getNodeDisconnectAction() {
168                 return nodeDisconnectAction;
169         }
170
171         /**
172          * Returns all language actions.
173          * 
174          * @return All language actions
175          */
176         List<I18nAction> getLanguageActions() {
177                 return languageActions;
178         }
179
180         /**
181          * Returns the “about” action.
182          * 
183          * @return The “about” action
184          */
185         I18nAction getHelpAboutAction() {
186                 return helpAboutAction;
187         }
188
189         /**
190          * Returns the “add project” action.
191          * 
192          * @return The “add project” action
193          */
194         I18nAction getAddProjectAction() {
195                 return addProjectAction;
196         }
197
198         //
199         // ACTIONS
200         //
201
202         //
203         // SERVICE METHODS
204         //
205
206         /**
207          * Starts the interface.
208          */
209         public void start() {
210                 mainWindow = new MainWindow(this);
211         }
212
213         //
214         // PRIVATE METHODS
215         //
216
217         /**
218          * Initializes all actions.
219          */
220         private void initActions() {
221                 configureAction = new I18nAction("mainWindow.menu.jSite.configure") {
222
223                         /**
224                          * {@inheritDoc}
225                          */
226                         @SuppressWarnings("synthetic-access")
227                         public void actionPerformed(ActionEvent actionEvent) {
228                                 configure();
229                         }
230                 };
231                 importConfigAction = new I18nAction("mainWindow.menu.jSite.importConfig") {
232
233                         /**
234                          * {@inheritDoc}
235                          */
236                         @SuppressWarnings("synthetic-access")
237                         public void actionPerformed(ActionEvent actionEvent) {
238                                 importConfig();
239                         }
240                 };
241                 quitAction = new I18nAction("mainWindow.menu.jSite.quit") {
242
243                         /**
244                          * {@inheritDoc}
245                          */
246                         @SuppressWarnings("synthetic-access")
247                         public void actionPerformed(ActionEvent actionEvent) {
248                                 quit();
249                         }
250                 };
251                 manageNodesAction = new I18nAction("mainWindow.menu.node.item.manageNodes") {
252
253                         /**
254                          * {@inheritDoc}
255                          */
256                         @SuppressWarnings("synthetic-access")
257                         public void actionPerformed(ActionEvent actionEvent) {
258                                 manageNodes();
259                         }
260                 };
261                 nodeConnectAction = new I18nAction("mainWindow.menu.node.item.connect", false) {
262
263                         @SuppressWarnings("synthetic-access")
264                         public void actionPerformed(ActionEvent actionEvent) {
265                                 nodeConnect();
266                         }
267
268                 };
269                 nodeDisconnectAction = new I18nAction("mainWindow.menu.node.item.disconnect", false) {
270
271                         /**
272                          * {@inheritDoc}
273                          */
274                         @SuppressWarnings("synthetic-access")
275                         public void actionPerformed(ActionEvent e) {
276                                 nodeDisconnect();
277                         }
278                 };
279                 List<Locale> availableLanguages = I18n.findAvailableLanguages();
280                 for (final Locale locale: availableLanguages) {
281                         I18nAction languageAction = new I18nAction("general.language." + locale.getLanguage()) {
282
283                                 @SuppressWarnings("synthetic-access")
284                                 public void actionPerformed(ActionEvent e) {
285                                         changeLanguage(locale, this);
286                                 }
287
288                         };
289                         if (I18n.getLocale().getLanguage().equals(locale.getLanguage())) {
290                                 languageAction.setEnabled(false);
291                         }
292                         languageActions.add(languageAction);
293                 }
294                 helpAboutAction = new I18nAction("mainWindow.menu.help.item.about") {
295
296                         /**
297                          * {@inheritDoc}
298                          */
299                         @SuppressWarnings("synthetic-access")
300                         public void actionPerformed(ActionEvent actionEvent) {
301                                 helpAbout();
302                         }
303                 };
304                 addProjectAction = new I18nAction("mainWindow.button.addProject") {
305
306                         /**
307                          * {@inheritDoc}
308                          */
309                         @SuppressWarnings("synthetic-access")
310                         public void actionPerformed(ActionEvent actionEvent) {
311                                 addProject();
312                         }
313                 };
314         }
315
316         /**
317          * Initializes all child dialogs.
318          */
319         private void initDialogs() {
320                 manageNodesDialog = new ManageNodesDialog(this);
321         }
322
323         //
324         // PRIVATE ACTIONS
325         //
326
327         /**
328          * Shows the configuration dialog.
329          */
330         private void configure() {
331         }
332
333         /**
334          * Imports old jSite configuration.
335          */
336         private void importConfig() {
337         }
338
339         /**
340          * Quits jSite.
341          */
342         private void quit() {
343                 System.exit(0);
344         }
345
346         /**
347          * Pops up the “manage nodes” dialog.
348          */
349         private void manageNodes() {
350                 manageNodesDialog.setNodeList(nodeList);
351                 manageNodesDialog.setVisible(true);
352                 nodeList = manageNodesDialog.getNodeList();
353         }
354
355         /**
356          * Connects to the node.
357          */
358         private void nodeConnect() {
359         }
360
361         /**
362          * Disconnects from the node.
363          */
364         private void nodeDisconnect() {
365         }
366
367         /**
368          * Changes the language of the interface. This method also disables the
369          * action for the newly set language and enables all others.
370          * 
371          * @param newLocale
372          *            The new language
373          * @param languageAction
374          *            The action that triggered the change
375          */
376         private void changeLanguage(Locale newLocale, I18nAction languageAction) {
377                 for (I18nAction i18nAction: languageActions) {
378                         i18nAction.setEnabled(i18nAction != languageAction);
379                 }
380                 I18n.setLocale(newLocale);
381         }
382
383         /**
384          * Shows the “about” dialog.
385          */
386         private void helpAbout() {
387         }
388
389         /**
390          * Adds a project.
391          */
392         private void addProject() {
393         }
394
395         //
396         // INTERFACE CoreListener
397         //
398
399         /**
400          * {@inheritDoc}
401          */
402         public void loadingProjectsFailed(String directory) {
403                 JOptionPane.showMessageDialog(mainWindow, I18n.get("mainWindow.error.projectLoadingFailed.message", directory), I18n.get("mainWindow.error.projectLoadingFailed.title"), JOptionPane.ERROR_MESSAGE);
404         }
405
406         /**
407          * {@inheritDoc}
408          */
409         public void coreLoaded() {
410                 this.nodeList = core.getNodes();
411                 manageNodesDialog.setNodeList(nodeList);
412                 mainWindow.setVisible(true);
413                 mainWindow.setStatusBarText("Core loaded.");
414         }
415
416         /**
417          * {@inheritDoc}
418          */
419         public void nodeConnected(Node node) {
420         }
421
422         /**
423          * {@inheritDoc}
424          */
425         public void nodeConnecting(Node node) {
426         }
427
428         /**
429          * {@inheritDoc}
430          */
431         public void nodeDisconnected(Node node) {
432         }
433
434 }