add language menu
[jSite2.git] / src / net / pterodactylus / jsite / gui / MainWindow.java
1 /*
2  * jSite2 - MainWindow.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.BorderLayout;
23 import java.awt.Container;
24 import java.awt.Dimension;
25
26 import javax.swing.JFrame;
27 import javax.swing.JMenuBar;
28 import javax.swing.JPanel;
29 import javax.swing.JToolBar;
30
31 import net.pterodactylus.jsite.i18n.I18n;
32 import net.pterodactylus.jsite.i18n.I18nable;
33 import net.pterodactylus.jsite.main.Version;
34 import net.pterodactylus.util.swing.StatusBar;
35 import net.pterodactylus.util.swing.SwingUtils;
36
37 /**
38  * Defines the main window of the application.
39  * 
40  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
41  * @version $Id$
42  */
43 public class MainWindow extends JFrame implements I18nable {
44
45         /** The swing interface that receives all actions. */
46         private final SwingInterface swingInterface;
47
48         /** The status bar. */
49         private StatusBar statusBar = new StatusBar();
50
51         /** The content pane. */
52         private JPanel contentPane = new JPanel();
53
54         /** The node menu. */
55         private I18nMenu nodeMenu;
56
57         /** The language menu. */
58         private I18nMenu languageMenu;
59
60         /**
61          * Creates a new main window that redirects all actions to the given swing
62          * interface.
63          * 
64          * @param swingInterface
65          *            The swing interface to receive all actions
66          */
67         public MainWindow(SwingInterface swingInterface) {
68                 super("jSite " + Version.getVersion());
69                 this.swingInterface = swingInterface;
70                 initWindow();
71                 setPreferredSize(new Dimension(480, 280));
72                 pack();
73                 SwingUtils.center(this);
74                 I18n.registerI18nable(this);
75                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
76         }
77
78         //
79         // ACCESSORS
80         //
81
82         /**
83          * Sets the text of the status bar.
84          * 
85          * @param text
86          *            The text of the status bar
87          */
88         public void setStatusBarText(String text) {
89                 statusBar.setText(text);
90         }
91
92         //
93         // PRIVATE METHODS
94         //
95
96         /**
97          * Initializes the window by creating all its components.
98          */
99         private void initWindow() {
100                 JMenuBar menuBar = new JMenuBar();
101
102                 nodeMenu = new I18nMenu("mainWindow.menu.node");
103                 menuBar.add(nodeMenu);
104
105                 nodeMenu.add(swingInterface.getManageNodesAction());
106                 nodeMenu.addSeparator();
107                 nodeMenu.add(swingInterface.getNodeConnectAction());
108                 nodeMenu.add(swingInterface.getNodeDisconnectAction());
109
110                 languageMenu = new I18nMenu("mainWindow.menu.language");
111                 menuBar.add(languageMenu);
112
113                 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
114                         languageMenu.add(languageAction);
115                 }
116
117                 setJMenuBar(menuBar);
118
119                 JToolBar toolBar = new JToolBar(I18n.get("mainWindow.toolbar.name"));
120                 toolBar.add(swingInterface.getManageNodesAction());
121                 toolBar.addSeparator();
122                 toolBar.add(swingInterface.getNodeConnectAction());
123                 toolBar.add(swingInterface.getNodeDisconnectAction());
124                 super.getContentPane().add(toolBar, BorderLayout.PAGE_START);
125
126                 super.getContentPane().add(contentPane, BorderLayout.CENTER);
127                 initComponents();
128         }
129
130         /**
131          * Initializes all components of this window.
132          */
133         private void initComponents() {
134                 super.getContentPane().add(statusBar, BorderLayout.PAGE_END);
135         }
136
137         /**
138          * {@inheritDoc}
139          */
140         @Override
141         public Container getContentPane() {
142                 return contentPane;
143         }
144
145         //
146         // INTERFACE I18nable
147         //
148
149         /**
150          * {@inheritDoc}
151          */
152         public void updateI18n() {
153                 swingInterface.getManageNodesAction().updateI18n();
154                 swingInterface.getNodeConnectAction().updateI18n();
155                 swingInterface.getNodeDisconnectAction().updateI18n();
156                 nodeMenu.updateI18n();
157                 languageMenu.updateI18n();
158                 getJMenuBar().revalidate();
159                 SwingUtils.repackCentered(this);
160         }
161
162 }