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