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;
27 import java.awt.event.WindowListener;
29 import javax.swing.Action;
30 import javax.swing.Box;
31 import javax.swing.BoxLayout;
32 import javax.swing.JButton;
33 import javax.swing.JFrame;
34 import javax.swing.JMenuBar;
35 import javax.swing.JMenuItem;
36 import javax.swing.JPanel;
37 import javax.swing.JTabbedPane;
38 import javax.swing.JToolBar;
39 import javax.swing.SwingConstants;
40 import javax.swing.border.EmptyBorder;
42 import net.pterodactylus.jsite.core.Project;
43 import net.pterodactylus.jsite.i18n.I18n;
44 import net.pterodactylus.jsite.i18n.I18nable;
45 import net.pterodactylus.jsite.i18n.gui.I18nAction;
46 import net.pterodactylus.jsite.i18n.gui.I18nMenu;
47 import net.pterodactylus.jsite.main.Version;
48 import net.pterodactylus.util.swing.StatusBar;
49 import net.pterodactylus.util.swing.SwingUtils;
52 * Defines the main window of the application.
54 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
57 public class MainWindow extends JFrame implements WindowListener, I18nable {
59 /** The swing interface that receives all actions. */
60 private final SwingInterface swingInterface;
62 /** The status bar. */
63 private StatusBar statusBar = new StatusBar();
65 /** The content pane. */
66 private JPanel contentPane = new JPanel(new BorderLayout(12, 12));
68 /** The jSite menu. */
69 private I18nMenu jSiteMenu;
72 private I18nMenu nodeMenu;
74 /** The “connect” (advanced mode) menu. */
75 private I18nMenu connectMenu;
77 /** The “connect” (simple mode) menu. */
78 private JMenuItem connectMenuItem;
80 /** The “disconnect” (advanced mode) menu. */
81 private I18nMenu disconnectMenu;
83 /** The “diconnect” (simple mode) menu item. */
84 private JMenuItem disconnectMenuItem;
86 /** The language menu. */
87 private I18nMenu languageMenu;
89 /** The about menu. */
90 private I18nMenu helpMenu;
92 /** The tabbed project pane. */
93 private JTabbedPane projectPane;
95 /** The project overview panel. */
96 private Box projectOverviewPanel;
99 * Creates a new main window that redirects all actions to the given swing
102 * @param swingInterface
103 * The swing interface to receive all actions
105 public MainWindow(SwingInterface swingInterface) {
106 super("jSite " + Version.getVersion());
107 this.swingInterface = swingInterface;
109 setPreferredSize(new Dimension(480, 280));
111 SwingUtils.center(this);
112 I18n.registerI18nable(this);
113 addWindowListener(this);
121 * Sets the text of the status bar.
124 * The text of the status bar
126 public void setStatusBarText(String text) {
127 statusBar.setText(text);
131 * Sets whether the advanced mode is activated.
133 * @param advancedMode
134 * <code>true</code> if the advanced mode is activated,
135 * <code>false</code> if the simple mode is activated
137 public void setAdvancedMode(boolean advancedMode) {
138 connectMenu.setVisible(advancedMode);
139 connectMenuItem.setVisible(!advancedMode);
140 disconnectMenu.setVisible(advancedMode);
141 disconnectMenuItem.setVisible(!advancedMode);
148 public Container getContentPane() {
153 * Returns the currently selected project.
155 * @return The currently selected project
157 public Project getSelectedProject() {
166 * Refreshes the menu items in the “connect” and “disconnect” menus.
168 void refreshNodeMenuItems() {
169 connectMenu.removeAll();
170 for (Action nodeConnectAction: swingInterface.getNodeConnectActions()) {
171 connectMenu.add(nodeConnectAction);
173 if (connectMenu.getMenuComponentCount() == 0) {
174 JMenuItem noNodeAvailableItem = new JMenuItem(I18n.get("mainWindow.menu.connectNoNodeAvailable.name"));
175 noNodeAvailableItem.setEnabled(false);
176 connectMenu.add(noNodeAvailableItem);
178 disconnectMenu.removeAll();
179 for (Action nodeDisconnectAction: swingInterface.getNodeDisconnectActions()) {
180 disconnectMenu.add(nodeDisconnectAction);
182 if (disconnectMenu.getMenuComponentCount() == 0) {
183 JMenuItem noNodeAvailableItem = new JMenuItem(I18n.get("mainWindow.menu.disconnectNoNodeAvailable.name"));
184 noNodeAvailableItem.setEnabled(false);
185 disconnectMenu.add(noNodeAvailableItem);
194 * Initializes the window by creating all its components.
196 private void initWindow() {
197 JMenuBar menuBar = new JMenuBar();
199 jSiteMenu = new I18nMenu("mainWindow.menu.jSite");
200 menuBar.add(jSiteMenu);
202 jSiteMenu.add(new FixedJMenuItem(swingInterface.getConfigureAction()));
203 jSiteMenu.addSeparator();
204 jSiteMenu.add(new FixedJMenuItem(swingInterface.getImportConfigAction()));
205 jSiteMenu.addSeparator();
206 jSiteMenu.add(new FixedJMenuItem(swingInterface.getQuitAction()));
208 connectMenu = new I18nMenu("mainWindow.menu.connect");
209 disconnectMenu = new I18nMenu("mainWindow.menu.disconnect");
211 nodeMenu = new I18nMenu("mainWindow.menu.node");
212 menuBar.add(nodeMenu);
214 nodeMenu.add(new FixedJMenuItem(swingInterface.getManageNodesAction()));
215 nodeMenu.addSeparator();
216 nodeMenu.add(connectMenuItem = new FixedJMenuItem(swingInterface.getNodeConnectAction()));
217 nodeMenu.add(connectMenu);
218 nodeMenu.add(disconnectMenuItem = new FixedJMenuItem(swingInterface.getNodeDisconnectAction()));
219 nodeMenu.add(disconnectMenu);
220 refreshNodeMenuItems();
222 languageMenu = new I18nMenu("mainWindow.menu.language");
223 menuBar.add(languageMenu);
225 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
226 languageMenu.add(new FixedJMenuItem(languageAction));
229 JPanel spacerPanel = new JPanel();
230 spacerPanel.setOpaque(false);
231 menuBar.add(spacerPanel);
233 helpMenu = new I18nMenu("mainWindow.menu.help");
234 menuBar.add(helpMenu);
236 helpMenu.add(new FixedJMenuItem(swingInterface.getHelpAboutAction()));
238 setJMenuBar(menuBar);
240 JToolBar toolBar = new JToolBar(I18n.get("mainWindow.toolbar.name"));
241 toolBar.add(swingInterface.getManageNodesAction());
242 toolBar.addSeparator();
243 toolBar.add(swingInterface.getNodeConnectAction());
244 toolBar.add(swingInterface.getNodeDisconnectAction());
245 super.getContentPane().add(toolBar, BorderLayout.PAGE_START);
247 super.getContentPane().add(contentPane, BorderLayout.CENTER);
249 addWindowListener(new WindowAdapter() {
254 @SuppressWarnings("synthetic-access")
256 public void windowClosing(WindowEvent windowEvent) {
257 swingInterface.getQuitAction().actionPerformed(null);
265 * Initializes all components of this window.
267 private void initComponents() {
268 super.getContentPane().add(statusBar, BorderLayout.PAGE_END);
271 * the main window consists of two panels which are vertically oriented.
272 * the upper panel contains of a tabbed pane, the lower panel consists
273 * of a table that lists the running requests.
276 JPanel upperPanel = new JPanel(new BorderLayout(12, 12));
277 getContentPane().add(upperPanel, BorderLayout.PAGE_START);
278 contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
280 projectPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
281 upperPanel.add(projectPane, BorderLayout.CENTER);
283 projectOverviewPanel = new Box(BoxLayout.PAGE_AXIS);
284 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
285 projectPane.add(projectOverviewPanel);
286 projectOverviewPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
287 projectOverviewPanel.add(Box.createVerticalGlue());
288 JButton addProjectButton = new JButton(swingInterface.getAddProjectAction());
289 addProjectButton.setAlignmentX(0.5f);
290 projectOverviewPanel.add(addProjectButton);
291 projectOverviewPanel.add(Box.createVerticalGlue());
293 // JPanel lowerPanel = new JPanel(new BorderLayout(12, 12));
294 // getContentPane().add(lowerPanel, BorderLayout.CENTER);
298 // INTERFACE I18nable
304 public void updateI18n() {
305 swingInterface.getConfigureAction().updateI18n();
306 swingInterface.getImportConfigAction().updateI18n();
307 swingInterface.getQuitAction().updateI18n();
308 swingInterface.getManageNodesAction().updateI18n();
309 swingInterface.getNodeConnectAction().updateI18n();
310 connectMenu.updateI18n();
311 swingInterface.getNodeDisconnectAction().updateI18n();
312 disconnectMenu.updateI18n();
313 swingInterface.getAddProjectAction().updateI18n();
314 swingInterface.getCloneProjectAction().updateI18n();
315 swingInterface.getDeleteProjectAction().updateI18n();
316 swingInterface.getHelpAboutAction().updateI18n();
317 jSiteMenu.updateI18n();
318 nodeMenu.updateI18n();
319 languageMenu.updateI18n();
320 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
321 languageAction.updateI18n();
323 helpMenu.updateI18n();
324 getJMenuBar().revalidate();
325 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
326 for (int componentIndex = 0; componentIndex < projectPane.getTabCount(); componentIndex++) {
327 projectPane.setTitleAt(componentIndex, projectPane.getComponentAt(componentIndex).getName());
329 refreshNodeMenuItems();
330 SwingUtils.repackCentered(this);
334 // INTERFACE WindowListener
340 public void windowActivated(WindowEvent e) {
347 public void windowClosed(WindowEvent e) {
354 public void windowClosing(WindowEvent e) {
355 swingInterface.getQuitAction().actionPerformed(null);
361 public void windowDeactivated(WindowEvent e) {
368 public void windowDeiconified(WindowEvent e) {
375 public void windowIconified(WindowEvent e) {
382 public void windowOpened(WindowEvent e) {