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