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 if (connectMenu.getMenuComponentCount() == 0) {
173 JMenuItem noNodeAvailableItem = new JMenuItem(I18n.get("mainWindow.menu.connectNoNodeAvailable.name"));
174 noNodeAvailableItem.setEnabled(false);
175 connectMenu.add(noNodeAvailableItem);
177 disconnectMenu.removeAll();
178 for (Action nodeDisconnectAction: swingInterface.getNodeDisconnectActions()) {
179 disconnectMenu.add(nodeDisconnectAction);
181 if (disconnectMenu.getMenuComponentCount() == 0) {
182 JMenuItem noNodeAvailableItem = new JMenuItem(I18n.get("mainWindow.menu.disconnectNoNodeAvailable.name"));
183 noNodeAvailableItem.setEnabled(false);
184 disconnectMenu.add(noNodeAvailableItem);
193 * Initializes the window by creating all its components.
195 private void initWindow() {
196 JMenuBar menuBar = new JMenuBar();
198 jSiteMenu = new I18nMenu("mainWindow.menu.jSite");
199 menuBar.add(jSiteMenu);
201 jSiteMenu.add(new FixedJMenuItem(swingInterface.getConfigureAction()));
202 jSiteMenu.addSeparator();
203 jSiteMenu.add(new FixedJMenuItem(swingInterface.getImportConfigAction()));
204 jSiteMenu.addSeparator();
205 jSiteMenu.add(new FixedJMenuItem(swingInterface.getQuitAction()));
207 connectMenu = new I18nMenu("mainWindow.menu.connect");
208 disconnectMenu = new I18nMenu("mainWindow.menu.disconnect");
210 nodeMenu = new I18nMenu("mainWindow.menu.node");
211 menuBar.add(nodeMenu);
213 nodeMenu.add(new FixedJMenuItem(swingInterface.getManageNodesAction()));
214 nodeMenu.addSeparator();
215 nodeMenu.add(connectMenuItem = new FixedJMenuItem(swingInterface.getNodeConnectAction()));
216 nodeMenu.add(connectMenu);
217 nodeMenu.add(disconnectMenuItem = new FixedJMenuItem(swingInterface.getNodeDisconnectAction()));
218 nodeMenu.add(disconnectMenu);
219 refreshNodeMenuItems();
221 languageMenu = new I18nMenu("mainWindow.menu.language");
222 menuBar.add(languageMenu);
224 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
225 languageMenu.add(new FixedJMenuItem(languageAction));
228 JPanel spacerPanel = new JPanel();
229 spacerPanel.setOpaque(false);
230 menuBar.add(spacerPanel);
232 helpMenu = new I18nMenu("mainWindow.menu.help");
233 menuBar.add(helpMenu);
235 helpMenu.add(new FixedJMenuItem(swingInterface.getHelpAboutAction()));
237 setJMenuBar(menuBar);
239 JToolBar toolBar = new JToolBar(I18n.get("mainWindow.toolbar.name"));
240 toolBar.add(swingInterface.getManageNodesAction());
241 toolBar.addSeparator();
242 toolBar.add(swingInterface.getNodeConnectAction());
243 toolBar.add(swingInterface.getNodeDisconnectAction());
244 super.getContentPane().add(toolBar, BorderLayout.PAGE_START);
246 super.getContentPane().add(contentPane, BorderLayout.CENTER);
248 addWindowListener(new WindowAdapter() {
253 @SuppressWarnings("synthetic-access")
255 public void windowClosing(WindowEvent windowEvent) {
256 swingInterface.getQuitAction().actionPerformed(null);
264 * Initializes all components of this window.
266 private void initComponents() {
267 super.getContentPane().add(statusBar, BorderLayout.PAGE_END);
270 * the main window consists of two panels which are vertically oriented.
271 * the upper panel contains of a tabbed pane, the lower panel consists
272 * of a table that lists the running requests.
275 JPanel upperPanel = new JPanel(new BorderLayout(12, 12));
276 getContentPane().add(upperPanel, BorderLayout.PAGE_START);
277 contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
279 projectPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
280 upperPanel.add(projectPane, BorderLayout.CENTER);
282 projectOverviewPanel = new Box(BoxLayout.PAGE_AXIS);
283 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
284 projectPane.add(projectOverviewPanel);
285 projectOverviewPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
286 projectOverviewPanel.add(Box.createVerticalGlue());
287 JButton addProjectButton = new JButton(swingInterface.getAddProjectAction());
288 addProjectButton.setAlignmentX(0.5f);
289 projectOverviewPanel.add(addProjectButton);
290 projectOverviewPanel.add(Box.createVerticalGlue());
292 // JPanel lowerPanel = new JPanel(new BorderLayout(12, 12));
293 // getContentPane().add(lowerPanel, BorderLayout.CENTER);
297 // INTERFACE I18nable
303 public void updateI18n() {
304 swingInterface.getConfigureAction().updateI18n();
305 swingInterface.getImportConfigAction().updateI18n();
306 swingInterface.getQuitAction().updateI18n();
307 swingInterface.getManageNodesAction().updateI18n();
308 swingInterface.getNodeConnectAction().updateI18n();
309 connectMenu.updateI18n();
310 swingInterface.getNodeDisconnectAction().updateI18n();
311 disconnectMenu.updateI18n();
312 swingInterface.getAddProjectAction().updateI18n();
313 swingInterface.getCloneProjectAction().updateI18n();
314 swingInterface.getDeleteProjectAction().updateI18n();
315 swingInterface.getHelpAboutAction().updateI18n();
316 jSiteMenu.updateI18n();
317 nodeMenu.updateI18n();
318 languageMenu.updateI18n();
319 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
320 languageAction.updateI18n();
322 helpMenu.updateI18n();
323 getJMenuBar().revalidate();
324 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
325 for (int componentIndex = 0; componentIndex < projectPane.getTabCount(); componentIndex++) {
326 projectPane.setTitleAt(componentIndex, projectPane.getComponentAt(componentIndex).getName());
328 refreshNodeMenuItems();
329 SwingUtils.repackCentered(this);