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