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 “about” dialog. */
80 private AboutDialog aboutDialog;
82 /** The configuration dialog. */
83 private ConfigurationDialog configurationDialog;
85 /** The list of all defined nodes. */
86 private List<Node> nodeList;
92 /** Whether to beautify the GUI. */
93 private boolean beautify;
96 * Creates a new swing interface.
99 * The core to operate on
101 public SwingInterface(Core core) {
103 I18n.setLocale(Locale.ENGLISH); /* TODO - load config */
106 System.setProperty("swing.aatext", "true");
107 System.setProperty("swing.plaf.metal.controlFont", "Tahoma");
108 System.setProperty("swing.plaf.metal.userFont", "Tahoma");
119 * Returns the core that is controlled by the Swing interface.
128 * Returns the main window of the Swing interface.
130 * @return The main window
132 MainWindow getMainWindow() {
137 * Returns the “configure” action.
139 * @return The “configure” action
141 I18nAction getConfigureAction() {
142 return configureAction;
146 * Returns the “import config” action.
148 * @return The “import config” action
150 I18nAction getImportConfigAction() {
151 return importConfigAction;
155 * Returns the “quit” action.
157 * @return The “quit” action
159 I18nAction getQuitAction() {
164 * Returns the “manage nodes” action.
166 * @return The “manage nodes” action
168 I18nAction getManageNodesAction() {
169 return manageNodesAction;
173 * Returns the “connect to node” action.
175 * @return The “connect to node” action
177 I18nAction getNodeConnectAction() {
178 return nodeConnectAction;
182 * Returns the “disconnect from node” action.
184 * @return The “disconnect from node” action
186 I18nAction getNodeDisconnectAction() {
187 return nodeDisconnectAction;
191 * Returns all language actions.
193 * @return All language actions
195 List<I18nAction> getLanguageActions() {
196 return languageActions;
200 * Returns the “about” action.
202 * @return The “about” action
204 I18nAction getHelpAboutAction() {
205 return helpAboutAction;
209 * Returns the “add project” action.
211 * @return The “add project” action
213 I18nAction getAddProjectAction() {
214 return addProjectAction;
226 * Starts the interface.
228 public void start() {
229 mainWindow = new MainWindow(this);
237 * Loads the configuration of the interface.
239 private void loadConfig() {
244 * Initializes all actions.
246 private void initActions() {
247 configureAction = new I18nAction("mainWindow.menu.jSite.configure") {
252 @SuppressWarnings("synthetic-access")
253 public void actionPerformed(ActionEvent actionEvent) {
257 importConfigAction = new I18nAction("mainWindow.menu.jSite.importConfig") {
262 @SuppressWarnings("synthetic-access")
263 public void actionPerformed(ActionEvent actionEvent) {
267 quitAction = new I18nAction("mainWindow.menu.jSite.quit") {
272 @SuppressWarnings("synthetic-access")
273 public void actionPerformed(ActionEvent actionEvent) {
277 manageNodesAction = new I18nAction("mainWindow.menu.node.item.manageNodes") {
282 @SuppressWarnings("synthetic-access")
283 public void actionPerformed(ActionEvent actionEvent) {
287 nodeConnectAction = new I18nAction("mainWindow.menu.node.item.connect", false) {
289 @SuppressWarnings("synthetic-access")
290 public void actionPerformed(ActionEvent actionEvent) {
295 nodeDisconnectAction = new I18nAction("mainWindow.menu.node.item.disconnect", false) {
300 @SuppressWarnings("synthetic-access")
301 public void actionPerformed(ActionEvent e) {
305 List<Locale> availableLanguages = I18n.findAvailableLanguages();
306 for (final Locale locale: availableLanguages) {
307 I18nAction languageAction = new I18nAction("general.language." + locale.getLanguage()) {
309 @SuppressWarnings("synthetic-access")
310 public void actionPerformed(ActionEvent e) {
311 changeLanguage(locale, this);
315 if (I18n.getLocale().getLanguage().equals(locale.getLanguage())) {
316 languageAction.setEnabled(false);
318 languageActions.add(languageAction);
320 helpAboutAction = new I18nAction("mainWindow.menu.help.item.about") {
325 @SuppressWarnings("synthetic-access")
326 public void actionPerformed(ActionEvent actionEvent) {
330 addProjectAction = new I18nAction("mainWindow.button.addProject") {
335 @SuppressWarnings("synthetic-access")
336 public void actionPerformed(ActionEvent actionEvent) {
343 * Initializes all child dialogs.
345 private void initDialogs() {
346 manageNodesDialog = new ManageNodesDialog(this);
347 aboutDialog = new AboutDialog(this);
348 configurationDialog = new ConfigurationDialog(this);
356 * Shows the configuration dialog.
358 private void configure() {
359 configurationDialog.setBeautify(beautify);
360 configurationDialog.setVisible(true);
361 if (!configurationDialog.wasCancelled()) {
362 beautify = configurationDialog.getBeautify();
367 * Imports old jSite configuration.
369 private void importConfig() {
375 private void quit() {
380 * Pops up the “manage nodes” dialog.
382 private void manageNodes() {
383 manageNodesDialog.setNodeList(nodeList);
384 manageNodesDialog.setVisible(true);
385 nodeList = manageNodesDialog.getNodeList();
389 * Connects to the node.
391 private void nodeConnect() {
395 * Disconnects from the node.
397 private void nodeDisconnect() {
401 * Changes the language of the interface. This method also disables the
402 * action for the newly set language and enables all others.
406 * @param languageAction
407 * The action that triggered the change
409 private void changeLanguage(Locale newLocale, I18nAction languageAction) {
410 for (I18nAction i18nAction: languageActions) {
411 i18nAction.setEnabled(i18nAction != languageAction);
413 I18n.setLocale(newLocale);
417 * Shows the “about” dialog.
419 private void helpAbout() {
420 aboutDialog.setVisible(true);
426 private void addProject() {
430 // INTERFACE CoreListener
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);
443 public void coreLoaded() {
444 this.nodeList = core.getNodes();
445 manageNodesDialog.setNodeList(nodeList);
446 mainWindow.setVisible(true);
447 mainWindow.setStatusBarText("Core loaded.");
453 public void nodeConnected(Node node) {
459 public void nodeConnecting(Node node) {
465 public void nodeDisconnected(Node node) {