2 * jSite2 - MainWindow.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.BorderLayout;
23 import java.awt.Container;
24 import java.awt.Dimension;
25 import java.awt.event.WindowAdapter;
26 import java.awt.event.WindowEvent;
28 import javax.swing.Action;
29 import javax.swing.Box;
30 import javax.swing.BoxLayout;
31 import javax.swing.JButton;
32 import javax.swing.JFrame;
33 import javax.swing.JMenuBar;
34 import javax.swing.JMenuItem;
35 import javax.swing.JPanel;
36 import javax.swing.JTabbedPane;
37 import javax.swing.JToolBar;
38 import javax.swing.SwingConstants;
39 import javax.swing.border.EmptyBorder;
41 import net.pterodactylus.jsite.core.Project;
42 import net.pterodactylus.jsite.i18n.I18n;
43 import net.pterodactylus.jsite.i18n.I18nable;
44 import net.pterodactylus.jsite.i18n.gui.I18nAction;
45 import net.pterodactylus.jsite.i18n.gui.I18nMenu;
46 import net.pterodactylus.jsite.main.Version;
47 import net.pterodactylus.util.swing.StatusBar;
48 import net.pterodactylus.util.swing.SwingUtils;
51 * Defines the main window of the application.
53 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
56 public class MainWindow extends JFrame implements I18nable {
58 /** The swing interface that receives all actions. */
59 private final SwingInterface swingInterface;
61 /** The status bar. */
62 private StatusBar statusBar = new StatusBar();
64 /** The content pane. */
65 private JPanel contentPane = new JPanel(new BorderLayout(12, 12));
67 /** The jSite menu. */
68 private I18nMenu jSiteMenu;
71 private I18nMenu nodeMenu;
73 /** The “connect” (advanced mode) menu. */
74 private I18nMenu connectMenu;
76 /** The “connect” (simple mode) menu. */
77 private JMenuItem connectMenuItem;
79 /** The “disconnect” (advanced mode) menu. */
80 private I18nMenu disconnectMenu;
82 /** The “diconnect” (simple mode) menu item. */
83 private JMenuItem disconnectMenuItem;
85 /** The language menu. */
86 private I18nMenu languageMenu;
88 /** The about menu. */
89 private I18nMenu helpMenu;
91 /** The tabbed project pane. */
92 private JTabbedPane projectPane;
94 /** The project overview panel. */
95 private Box projectOverviewPanel;
98 * Creates a new main window that redirects all actions to the given swing
101 * @param swingInterface
102 * The swing interface to receive all actions
104 public MainWindow(SwingInterface swingInterface) {
105 super("jSite " + Version.getVersion());
106 this.swingInterface = swingInterface;
108 setPreferredSize(new Dimension(480, 280));
110 SwingUtils.center(this);
111 I18n.registerI18nable(this);
112 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
120 * Sets the text of the status bar.
123 * The text of the status bar
125 public void setStatusBarText(String text) {
126 statusBar.setText(text);
130 * Sets whether the advanced mode is activated.
132 * @param advancedMode
133 * <code>true</code> if the advanced mode is activated,
134 * <code>false</code> if the simple mode is activated
136 public void setAdvancedMode(boolean advancedMode) {
137 connectMenu.setVisible(advancedMode);
138 connectMenuItem.setVisible(!advancedMode);
139 disconnectMenu.setVisible(advancedMode);
140 disconnectMenuItem.setVisible(!advancedMode);
147 public Container getContentPane() {
152 * Returns the currently selected project.
154 * @return The currently selected project
156 public Project getSelectedProject() {
165 * Refreshes the menu items in the “connect” and “disconnect” menus.
167 void refreshNodeMenuItems() {
168 connectMenu.removeAll();
169 for (Action nodeConnectAction: swingInterface.getNodeConnectActions()) {
170 connectMenu.add(nodeConnectAction);
172 disconnectMenu.removeAll();
173 for (Action nodeDisconnectAction: swingInterface.getNodeDisconnectActions()) {
174 disconnectMenu.add(nodeDisconnectAction);
183 * Initializes the window by creating all its components.
185 private void initWindow() {
186 JMenuBar menuBar = new JMenuBar();
188 jSiteMenu = new I18nMenu("mainWindow.menu.jSite");
189 menuBar.add(jSiteMenu);
191 jSiteMenu.add(new FixedJMenuItem(swingInterface.getConfigureAction()));
192 jSiteMenu.addSeparator();
193 jSiteMenu.add(new FixedJMenuItem(swingInterface.getImportConfigAction()));
194 jSiteMenu.addSeparator();
195 jSiteMenu.add(new FixedJMenuItem(swingInterface.getQuitAction()));
197 connectMenu = new I18nMenu("mainWindow.menu.connect");
198 disconnectMenu = new I18nMenu("mainWindow.menu.disconnect");
200 nodeMenu = new I18nMenu("mainWindow.menu.node");
201 menuBar.add(nodeMenu);
203 nodeMenu.add(new FixedJMenuItem(swingInterface.getManageNodesAction()));
204 nodeMenu.addSeparator();
205 nodeMenu.add(connectMenuItem = new FixedJMenuItem(swingInterface.getNodeConnectAction()));
206 nodeMenu.add(connectMenu);
207 nodeMenu.add(disconnectMenuItem = new FixedJMenuItem(swingInterface.getNodeDisconnectAction()));
208 nodeMenu.add(disconnectMenu);
210 languageMenu = new I18nMenu("mainWindow.menu.language");
211 menuBar.add(languageMenu);
213 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
214 languageMenu.add(new FixedJMenuItem(languageAction));
217 JPanel spacerPanel = new JPanel();
218 spacerPanel.setOpaque(false);
219 menuBar.add(spacerPanel);
221 helpMenu = new I18nMenu("mainWindow.menu.help");
222 menuBar.add(helpMenu);
224 helpMenu.add(new FixedJMenuItem(swingInterface.getHelpAboutAction()));
226 setJMenuBar(menuBar);
228 JToolBar toolBar = new JToolBar(I18n.get("mainWindow.toolbar.name"));
229 toolBar.add(swingInterface.getManageNodesAction());
230 toolBar.addSeparator();
231 toolBar.add(swingInterface.getNodeConnectAction());
232 toolBar.add(swingInterface.getNodeDisconnectAction());
233 super.getContentPane().add(toolBar, BorderLayout.PAGE_START);
235 super.getContentPane().add(contentPane, BorderLayout.CENTER);
237 addWindowListener(new WindowAdapter() {
242 @SuppressWarnings("synthetic-access")
244 public void windowClosing(WindowEvent windowEvent) {
245 swingInterface.getQuitAction().actionPerformed(null);
253 * Initializes all components of this window.
255 private void initComponents() {
256 super.getContentPane().add(statusBar, BorderLayout.PAGE_END);
259 * the main window consists of two panels which are vertically oriented.
260 * the upper panel contains of a tabbed pane, the lower panel consists
261 * of a table that lists the running requests.
264 JPanel upperPanel = new JPanel(new BorderLayout(12, 12));
265 getContentPane().add(upperPanel, BorderLayout.PAGE_START);
266 contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
268 projectPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
269 upperPanel.add(projectPane, BorderLayout.CENTER);
271 projectOverviewPanel = new Box(BoxLayout.PAGE_AXIS);
272 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
273 projectPane.add(projectOverviewPanel);
274 projectOverviewPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
275 projectOverviewPanel.add(Box.createVerticalGlue());
276 JButton addProjectButton = new JButton(swingInterface.getAddProjectAction());
277 addProjectButton.setAlignmentX(0.5f);
278 projectOverviewPanel.add(addProjectButton);
279 projectOverviewPanel.add(Box.createVerticalGlue());
281 // JPanel lowerPanel = new JPanel(new BorderLayout(12, 12));
282 // getContentPane().add(lowerPanel, BorderLayout.CENTER);
286 // INTERFACE I18nable
292 public void updateI18n() {
293 swingInterface.getConfigureAction().updateI18n();
294 swingInterface.getImportConfigAction().updateI18n();
295 swingInterface.getQuitAction().updateI18n();
296 swingInterface.getManageNodesAction().updateI18n();
297 swingInterface.getNodeConnectAction().updateI18n();
298 connectMenu.updateI18n();
299 swingInterface.getNodeDisconnectAction().updateI18n();
300 disconnectMenu.updateI18n();
301 swingInterface.getAddProjectAction().updateI18n();
302 swingInterface.getCloneProjectAction().updateI18n();
303 swingInterface.getDeleteProjectAction().updateI18n();
304 swingInterface.getHelpAboutAction().updateI18n();
305 jSiteMenu.updateI18n();
306 nodeMenu.updateI18n();
307 languageMenu.updateI18n();
308 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
309 languageAction.updateI18n();
311 helpMenu.updateI18n();
312 getJMenuBar().revalidate();
313 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
314 for (int componentIndex = 0; componentIndex < projectPane.getTabCount(); componentIndex++) {
315 projectPane.setTitleAt(componentIndex, projectPane.getComponentAt(componentIndex).getName());
317 SwingUtils.repackCentered(this);