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;
24 import java.io.FileInputStream;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Locale;
30 import java.util.Properties;
32 import javax.swing.JOptionPane;
34 import net.pterodactylus.jsite.core.Core;
35 import net.pterodactylus.jsite.core.CoreListener;
36 import net.pterodactylus.jsite.core.Node;
37 import net.pterodactylus.jsite.i18n.I18n;
38 import net.pterodactylus.jsite.i18n.gui.I18nAction;
39 import net.pterodactylus.util.io.Closer;
42 * The Swing user interface.
44 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
47 public class SwingInterface implements CoreListener {
49 /** The application core. */
50 private final Core core;
52 /** The configuration directory. */
53 private final String configDirectory;
55 /** The main window. */
56 private MainWindow mainWindow;
58 /** The “configure” action. */
59 private I18nAction configureAction;
61 /** The “import config” action. */
62 private I18nAction importConfigAction;
64 /** The “quit” action. */
65 private I18nAction quitAction;
67 /** The “manage nodes” action. */
68 private I18nAction manageNodesAction;
70 /** The “connect to node” action. */
71 private I18nAction nodeConnectAction;
73 /** The “disconnect from node” action. */
74 private I18nAction nodeDisconnectAction;
76 /** The node manager dialog. */
77 private ManageNodesDialog manageNodesDialog;
79 /** All lanugage menu items. */
80 private List<I18nAction> languageActions = new ArrayList<I18nAction>();
82 /** The “about” action. */
83 private I18nAction helpAboutAction;
85 /** The “add project” action. */
86 private I18nAction addProjectAction;
88 /** The “about” dialog. */
89 private AboutDialog aboutDialog;
91 /** The configuration dialog. */
92 private ConfigurationDialog configurationDialog;
94 /** The list of all defined nodes. */
95 private List<Node> nodeList;
101 /** Whether to beautify the GUI. */
102 private boolean beautify;
105 * Creates a new swing interface.
108 * The core to operate on
109 * @param configDirectory
110 * The directory the configuration is stored in
112 public SwingInterface(Core core, String configDirectory) {
114 this.configDirectory = configDirectory;
115 I18n.setLocale(Locale.ENGLISH);
118 System.setProperty("swing.aatext", "true");
119 System.setProperty("swing.plaf.metal.controlFont", "Tahoma");
120 System.setProperty("swing.plaf.metal.userFont", "Tahoma");
131 * Returns the core that is controlled by the Swing interface.
140 * Returns the main window of the Swing interface.
142 * @return The main window
144 MainWindow getMainWindow() {
149 * Returns the “configure” action.
151 * @return The “configure” action
153 I18nAction getConfigureAction() {
154 return configureAction;
158 * Returns the “import config” action.
160 * @return The “import config” action
162 I18nAction getImportConfigAction() {
163 return importConfigAction;
167 * Returns the “quit” action.
169 * @return The “quit” action
171 I18nAction getQuitAction() {
176 * Returns the “manage nodes” action.
178 * @return The “manage nodes” action
180 I18nAction getManageNodesAction() {
181 return manageNodesAction;
185 * Returns the “connect to node” action.
187 * @return The “connect to node” action
189 I18nAction getNodeConnectAction() {
190 return nodeConnectAction;
194 * Returns the “disconnect from node” action.
196 * @return The “disconnect from node” action
198 I18nAction getNodeDisconnectAction() {
199 return nodeDisconnectAction;
203 * Returns all language actions.
205 * @return All language actions
207 List<I18nAction> getLanguageActions() {
208 return languageActions;
212 * Returns the “about” action.
214 * @return The “about” action
216 I18nAction getHelpAboutAction() {
217 return helpAboutAction;
221 * Returns the “add project” action.
223 * @return The “add project” action
225 I18nAction getAddProjectAction() {
226 return addProjectAction;
238 * Starts the interface.
240 public void start() {
241 mainWindow = new MainWindow(this);
249 * Loads the configuration of the interface.
251 private void loadConfig() {
252 /* initialize default stuff. */
254 /* now read config. */
255 System.out.println("configDirectory: “" + configDirectory + "”");
256 File configFile = new File(configDirectory, "swing-interface.properties");
257 if (!configFile.exists() || !configFile.canRead() || !configFile.isFile()) {
258 System.err.println("could not find “" + configFile.getAbsolutePath() + "”!");
261 Properties configProperties = new Properties();
262 FileInputStream configInputStream = null;
264 configInputStream = new FileInputStream(configFile);
265 configProperties.load(configInputStream);
266 } catch (IOException ioe1) {
267 System.err.println("could not load config, " + ioe1.getMessage());
269 Closer.close(configInputStream);
271 if (configProperties.containsKey("beautify")) {
272 beautify = Boolean.valueOf(configProperties.getProperty("beautify"));
277 * Saves the configuration.
279 private void saveConfig() {
280 File configDirectory = new File(this.configDirectory);
281 if (!configDirectory.exists()) {
282 if (!configDirectory.mkdirs()) {
283 System.err.println("could not create “" + this.configDirectory + "”!");
287 if (!configDirectory.exists() || !configDirectory.isDirectory() || !configDirectory.canWrite()) {
288 System.err.println("can not access “" + this.configDirectory + "”!");
291 File configFile = new File(configDirectory, "swing-interface.properties");
292 Properties configProperties = new Properties();
293 configProperties.setProperty("beautify", String.valueOf(beautify));
294 FileOutputStream configOutputStream = null;
296 configOutputStream = new FileOutputStream(configFile);
297 configProperties.store(configOutputStream, "configuration of swing interface");
298 } catch (IOException ioe1) {
299 System.err.println("could not save config, " + ioe1.getMessage());
301 Closer.close(configOutputStream);
306 * Initializes all actions.
308 private void initActions() {
309 configureAction = new I18nAction("mainWindow.menu.jSite.configure") {
314 @SuppressWarnings("synthetic-access")
315 public void actionPerformed(ActionEvent actionEvent) {
319 importConfigAction = new I18nAction("mainWindow.menu.jSite.importConfig") {
324 @SuppressWarnings("synthetic-access")
325 public void actionPerformed(ActionEvent actionEvent) {
329 quitAction = new I18nAction("mainWindow.menu.jSite.quit") {
334 @SuppressWarnings("synthetic-access")
335 public void actionPerformed(ActionEvent actionEvent) {
339 manageNodesAction = new I18nAction("mainWindow.menu.node.item.manageNodes") {
344 @SuppressWarnings("synthetic-access")
345 public void actionPerformed(ActionEvent actionEvent) {
349 nodeConnectAction = new I18nAction("mainWindow.menu.node.item.connect", false) {
351 @SuppressWarnings("synthetic-access")
352 public void actionPerformed(ActionEvent actionEvent) {
357 nodeDisconnectAction = new I18nAction("mainWindow.menu.node.item.disconnect", false) {
362 @SuppressWarnings("synthetic-access")
363 public void actionPerformed(ActionEvent e) {
367 List<Locale> availableLanguages = I18n.findAvailableLanguages();
368 for (final Locale locale: availableLanguages) {
369 I18nAction languageAction = new I18nAction("general.language." + locale.getLanguage()) {
371 @SuppressWarnings("synthetic-access")
372 public void actionPerformed(ActionEvent e) {
373 changeLanguage(locale, this);
377 if (I18n.getLocale().getLanguage().equals(locale.getLanguage())) {
378 languageAction.setEnabled(false);
380 languageActions.add(languageAction);
382 helpAboutAction = new I18nAction("mainWindow.menu.help.item.about") {
387 @SuppressWarnings("synthetic-access")
388 public void actionPerformed(ActionEvent actionEvent) {
392 addProjectAction = new I18nAction("mainWindow.button.addProject") {
397 @SuppressWarnings("synthetic-access")
398 public void actionPerformed(ActionEvent actionEvent) {
405 * Initializes all child dialogs.
407 private void initDialogs() {
408 manageNodesDialog = new ManageNodesDialog(this);
409 aboutDialog = new AboutDialog(this);
410 configurationDialog = new ConfigurationDialog(this);
418 * Shows the configuration dialog.
420 private void configure() {
421 configurationDialog.setBeautify(beautify);
422 configurationDialog.setVisible(true);
423 if (!configurationDialog.wasCancelled()) {
424 beautify = configurationDialog.getBeautify();
430 * Imports old jSite configuration.
432 private void importConfig() {
438 private void quit() {
444 * Pops up the “manage nodes” dialog.
446 private void manageNodes() {
447 manageNodesDialog.setNodeList(nodeList);
448 manageNodesDialog.setVisible(true);
449 nodeList = manageNodesDialog.getNodeList();
453 * Connects to the node.
455 private void nodeConnect() {
459 * Disconnects from the node.
461 private void nodeDisconnect() {
465 * Changes the language of the interface. This method also disables the
466 * action for the newly set language and enables all others.
470 * @param languageAction
471 * The action that triggered the change
473 private void changeLanguage(Locale newLocale, I18nAction languageAction) {
474 for (I18nAction i18nAction: languageActions) {
475 i18nAction.setEnabled(i18nAction != languageAction);
477 I18n.setLocale(newLocale);
481 * Shows the “about” dialog.
483 private void helpAbout() {
484 aboutDialog.setVisible(true);
490 private void addProject() {
494 // INTERFACE CoreListener
500 public void loadingProjectsFailed(String directory) {
501 JOptionPane.showMessageDialog(mainWindow, I18n.get("mainWindow.error.projectLoadingFailed.message", directory), I18n.get("mainWindow.error.projectLoadingFailed.title"), JOptionPane.ERROR_MESSAGE);
507 public void coreLoaded() {
508 this.nodeList = core.getNodes();
509 manageNodesDialog.setNodeList(nodeList);
510 mainWindow.setVisible(true);
511 mainWindow.setStatusBarText("Core loaded.");
517 public void nodeConnected(Node node) {
523 public void nodeConnecting(Node node) {
529 public void nodeDisconnected(Node node) {