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