2 * jSite2 - SwingInterface.java -
3 * Copyright © 2008 David Roden
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.
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.
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.
20 package net.pterodactylus.jsite.gui;
22 import java.awt.event.ActionEvent;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Locale;
27 import javax.swing.JOptionPane;
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;
38 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
41 public class SwingInterface implements CoreListener {
43 /** The application core. */
44 private final Core core;
46 /** The main window. */
47 private MainWindow mainWindow;
49 /** The “configure” action. */
50 private I18nAction configureAction;
52 /** The “import config” action. */
53 private I18nAction importConfigAction;
55 /** The “quit” action. */
56 private I18nAction quitAction;
58 /** The “manage nodes” action. */
59 private I18nAction manageNodesAction;
61 /** The “connect to node” action. */
62 private I18nAction nodeConnectAction;
64 /** The “disconnect from node” action. */
65 private I18nAction nodeDisconnectAction;
67 /** The node manager dialog. */
68 private ManageNodesDialog manageNodesDialog;
70 /** All lanugage menu items. */
71 private List<I18nAction> languageActions = new ArrayList<I18nAction>();
73 /** The “about” action. */
74 private I18nAction helpAboutAction;
76 /** The “add project” action. */
77 private I18nAction addProjectAction;
79 /** The list of all defined nodes. */
80 private List<Node> nodeList;
83 * Creates a new swing interface.
86 * The core to operate on
88 public SwingInterface(Core core) {
90 I18n.setLocale(Locale.ENGLISH); /* TODO - load config */
100 * Returns the core that is controlled by the Swing interface.
109 * Returns the main window of the Swing interface.
111 * @return The main window
113 MainWindow getMainWindow() {
118 * Returns the “configure” action.
120 * @return The “configure” action
122 I18nAction getConfigureAction() {
123 return configureAction;
127 * Returns the “import config” action.
129 * @return The “import config” action
131 I18nAction getImportConfigAction() {
132 return importConfigAction;
136 * Returns the “quit” action.
138 * @return The “quit” action
140 I18nAction getQuitAction() {
145 * Returns the “manage nodes” action.
147 * @return The “manage nodes” action
149 I18nAction getManageNodesAction() {
150 return manageNodesAction;
154 * Returns the “connect to node” action.
156 * @return The “connect to node” action
158 I18nAction getNodeConnectAction() {
159 return nodeConnectAction;
163 * Returns the “disconnect from node” action.
165 * @return The “disconnect from node” action
167 I18nAction getNodeDisconnectAction() {
168 return nodeDisconnectAction;
172 * Returns all language actions.
174 * @return All language actions
176 List<I18nAction> getLanguageActions() {
177 return languageActions;
181 * Returns the “about” action.
183 * @return The “about” action
185 I18nAction getHelpAboutAction() {
186 return helpAboutAction;
190 * Returns the “add project” action.
192 * @return The “add project” action
194 I18nAction getAddProjectAction() {
195 return addProjectAction;
207 * Starts the interface.
209 public void start() {
210 mainWindow = new MainWindow(this);
218 * Initializes all actions.
220 private void initActions() {
221 configureAction = new I18nAction("mainWindow.menu.jSite.configure") {
226 @SuppressWarnings("synthetic-access")
227 public void actionPerformed(ActionEvent actionEvent) {
231 importConfigAction = new I18nAction("mainWindow.menu.jSite.importConfig") {
236 @SuppressWarnings("synthetic-access")
237 public void actionPerformed(ActionEvent actionEvent) {
241 quitAction = new I18nAction("mainWindow.menu.jSite.quit") {
246 @SuppressWarnings("synthetic-access")
247 public void actionPerformed(ActionEvent actionEvent) {
251 manageNodesAction = new I18nAction("mainWindow.menu.node.item.manageNodes") {
256 @SuppressWarnings("synthetic-access")
257 public void actionPerformed(ActionEvent actionEvent) {
261 nodeConnectAction = new I18nAction("mainWindow.menu.node.item.connect", false) {
263 @SuppressWarnings("synthetic-access")
264 public void actionPerformed(ActionEvent actionEvent) {
269 nodeDisconnectAction = new I18nAction("mainWindow.menu.node.item.disconnect", false) {
274 @SuppressWarnings("synthetic-access")
275 public void actionPerformed(ActionEvent e) {
279 List<Locale> availableLanguages = I18n.findAvailableLanguages();
280 for (final Locale locale: availableLanguages) {
281 I18nAction languageAction = new I18nAction("general.language." + locale.getLanguage()) {
283 @SuppressWarnings("synthetic-access")
284 public void actionPerformed(ActionEvent e) {
285 changeLanguage(locale, this);
289 if (I18n.getLocale().getLanguage().equals(locale.getLanguage())) {
290 languageAction.setEnabled(false);
292 languageActions.add(languageAction);
294 helpAboutAction = new I18nAction("mainWindow.menu.help.item.about") {
299 @SuppressWarnings("synthetic-access")
300 public void actionPerformed(ActionEvent actionEvent) {
304 addProjectAction = new I18nAction("mainWindow.button.addProject") {
309 @SuppressWarnings("synthetic-access")
310 public void actionPerformed(ActionEvent actionEvent) {
317 * Initializes all child dialogs.
319 private void initDialogs() {
320 manageNodesDialog = new ManageNodesDialog(this);
328 * Shows the configuration dialog.
330 private void configure() {
334 * Imports old jSite configuration.
336 private void importConfig() {
342 private void quit() {
347 * Pops up the “manage nodes” dialog.
349 private void manageNodes() {
350 manageNodesDialog.setNodeList(nodeList);
351 manageNodesDialog.setVisible(true);
352 nodeList = manageNodesDialog.getNodeList();
356 * Connects to the node.
358 private void nodeConnect() {
362 * Disconnects from the node.
364 private void nodeDisconnect() {
368 * Changes the language of the interface. This method also disables the
369 * action for the newly set language and enables all others.
373 * @param languageAction
374 * The action that triggered the change
376 private void changeLanguage(Locale newLocale, I18nAction languageAction) {
377 for (I18nAction i18nAction: languageActions) {
378 i18nAction.setEnabled(i18nAction != languageAction);
380 I18n.setLocale(newLocale);
384 * Shows the “about” dialog.
386 private void helpAbout() {
392 private void addProject() {
396 // INTERFACE CoreListener
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);
409 public void coreLoaded() {
410 this.nodeList = core.getNodes();
411 manageNodesDialog.setNodeList(nodeList);
412 mainWindow.setVisible(true);
413 mainWindow.setStatusBarText("Core loaded.");
419 public void nodeConnected(Node node) {
425 public void nodeConnecting(Node node) {
431 public void nodeDisconnected(Node node) {