add "jsite" and "about" 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 import java.awt.event.WindowAdapter;
26 import java.awt.event.WindowEvent;
27
28 import javax.swing.Box;
29 import javax.swing.BoxLayout;
30 import javax.swing.JButton;
31 import javax.swing.JFrame;
32 import javax.swing.JMenuBar;
33 import javax.swing.JPanel;
34 import javax.swing.JTabbedPane;
35 import javax.swing.JToolBar;
36 import javax.swing.SwingConstants;
37 import javax.swing.border.EmptyBorder;
38
39 import net.pterodactylus.jsite.i18n.I18n;
40 import net.pterodactylus.jsite.i18n.I18nable;
41 import net.pterodactylus.jsite.i18n.gui.I18nAction;
42 import net.pterodactylus.jsite.i18n.gui.I18nMenu;
43 import net.pterodactylus.jsite.main.Version;
44 import net.pterodactylus.util.swing.StatusBar;
45 import net.pterodactylus.util.swing.SwingUtils;
46
47 /**
48  * Defines the main window of the application.
49  * 
50  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
51  * @version $Id$
52  */
53 public class MainWindow extends JFrame implements I18nable {
54
55         /** The swing interface that receives all actions. */
56         private final SwingInterface swingInterface;
57
58         /** The status bar. */
59         private StatusBar statusBar = new StatusBar();
60
61         /** The content pane. */
62         private JPanel contentPane = new JPanel(new BorderLayout(12, 12));
63
64         /** The jSite menu. */
65         private I18nMenu jSiteMenu;
66
67         /** The node menu. */
68         private I18nMenu nodeMenu;
69
70         /** The language menu. */
71         private I18nMenu languageMenu;
72
73         /** The about menu. */
74         private I18nMenu aboutMenu;
75
76         /**
77          * Creates a new main window that redirects all actions to the given swing
78          * interface.
79          * 
80          * @param swingInterface
81          *            The swing interface to receive all actions
82          */
83         public MainWindow(SwingInterface swingInterface) {
84                 super("jSite " + Version.getVersion());
85                 this.swingInterface = swingInterface;
86                 initWindow();
87                 setPreferredSize(new Dimension(480, 280));
88                 pack();
89                 SwingUtils.center(this);
90                 I18n.registerI18nable(this);
91                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
92         }
93
94         //
95         // ACCESSORS
96         //
97
98         /**
99          * Sets the text of the status bar.
100          * 
101          * @param text
102          *            The text of the status bar
103          */
104         public void setStatusBarText(String text) {
105                 statusBar.setText(text);
106         }
107
108         //
109         // PRIVATE METHODS
110         //
111
112         /**
113          * Initializes the window by creating all its components.
114          */
115         private void initWindow() {
116                 JMenuBar menuBar = new JMenuBar();
117
118                 jSiteMenu = new I18nMenu("mainWindow.menu.jSite");
119                 menuBar.add(jSiteMenu);
120
121                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getConfigureAction()));
122                 jSiteMenu.addSeparator();
123                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getImportConfigAction()));
124                 jSiteMenu.addSeparator();
125                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getQuitAction()));
126
127                 nodeMenu = new I18nMenu("mainWindow.menu.node");
128                 menuBar.add(nodeMenu);
129
130                 nodeMenu.add(new FixedJMenuItem(swingInterface.getManageNodesAction()));
131                 nodeMenu.addSeparator();
132                 nodeMenu.add(new FixedJMenuItem(swingInterface.getNodeConnectAction()));
133                 nodeMenu.add(new FixedJMenuItem(swingInterface.getNodeDisconnectAction()));
134
135                 languageMenu = new I18nMenu("mainWindow.menu.language");
136                 menuBar.add(languageMenu);
137
138                 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
139                         languageMenu.add(languageAction);
140                 }
141
142                 aboutMenu = new I18nMenu("mainWindow.menu.about");
143                 menuBar.add(aboutMenu, BorderLayout.LINE_END);
144
145                 aboutMenu.add(new FixedJMenuItem(swingInterface.getHelpAboutAction()));
146
147                 setJMenuBar(menuBar);
148
149                 JToolBar toolBar = new JToolBar(I18n.get("mainWindow.toolbar.name"));
150                 toolBar.add(swingInterface.getManageNodesAction());
151                 toolBar.addSeparator();
152                 toolBar.add(swingInterface.getNodeConnectAction());
153                 toolBar.add(swingInterface.getNodeDisconnectAction());
154                 super.getContentPane().add(toolBar, BorderLayout.PAGE_START);
155
156                 super.getContentPane().add(contentPane, BorderLayout.CENTER);
157
158                 addWindowListener(new WindowAdapter() {
159
160                         /**
161                          * {@inheritDoc}
162                          */
163                         @SuppressWarnings("synthetic-access")
164                         @Override
165                         public void windowClosing(WindowEvent windowEvent) {
166                                 swingInterface.getQuitAction().actionPerformed(null);
167                         }
168                 });
169
170                 initComponents();
171         }
172
173         /**
174          * Initializes all components of this window.
175          */
176         private void initComponents() {
177                 super.getContentPane().add(statusBar, BorderLayout.PAGE_END);
178
179                 /*
180                  * the main window consists of two panels which are vertically oriented.
181                  * the upper panel contains of a tabbed pane, the lower panel consists
182                  * of a table that lists the running requests.
183                  */
184
185                 JPanel upperPanel = new JPanel(new BorderLayout(12, 12));
186                 getContentPane().add(upperPanel, BorderLayout.PAGE_START);
187                 contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
188
189                 JTabbedPane tabbedPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
190                 upperPanel.add(tabbedPane, BorderLayout.CENTER);
191
192                 Box projectOverviewPanel = new Box(BoxLayout.PAGE_AXIS);
193                 tabbedPane.add(I18n.get("mainWindow.pane.overview.title"), projectOverviewPanel);
194                 projectOverviewPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
195                 projectOverviewPanel.add(Box.createVerticalGlue());
196                 JButton addProjectButton = new JButton(swingInterface.getAddProjectAction());
197                 addProjectButton.setAlignmentX(0.5f);
198                 projectOverviewPanel.add(addProjectButton);
199                 projectOverviewPanel.add(Box.createVerticalGlue());
200
201 // JPanel lowerPanel = new JPanel(new BorderLayout(12, 12));
202 // getContentPane().add(lowerPanel, BorderLayout.CENTER);
203         }
204
205         /**
206          * {@inheritDoc}
207          */
208         @Override
209         public Container getContentPane() {
210                 return contentPane;
211         }
212
213         //
214         // INTERFACE I18nable
215         //
216
217         /**
218          * {@inheritDoc}
219          */
220         public void updateI18n() {
221                 swingInterface.getManageNodesAction().updateI18n();
222                 swingInterface.getNodeConnectAction().updateI18n();
223                 swingInterface.getNodeDisconnectAction().updateI18n();
224                 nodeMenu.updateI18n();
225                 languageMenu.updateI18n();
226                 getJMenuBar().revalidate();
227                 SwingUtils.repackCentered(this);
228         }
229
230 }