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;
28 import java.util.Timer;
29 import java.util.TimerTask;
31 import javax.swing.Action;
32 import javax.swing.Box;
33 import javax.swing.BoxLayout;
34 import javax.swing.JButton;
35 import javax.swing.JFrame;
36 import javax.swing.JMenuBar;
37 import javax.swing.JMenuItem;
38 import javax.swing.JPanel;
39 import javax.swing.JTabbedPane;
40 import javax.swing.JToolBar;
41 import javax.swing.SwingConstants;
42 import javax.swing.border.EmptyBorder;
44 import net.pterodactylus.jsite.core.Project;
45 import net.pterodactylus.jsite.i18n.I18n;
46 import net.pterodactylus.jsite.i18n.I18nable;
47 import net.pterodactylus.jsite.i18n.gui.I18nAction;
48 import net.pterodactylus.jsite.i18n.gui.I18nMenu;
49 import net.pterodactylus.jsite.main.Version;
50 import net.pterodactylus.util.swing.StatusBar;
51 import net.pterodactylus.util.swing.SwingUtils;
54 * Defines the main window of the application.
56 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
59 public class MainWindow extends JFrame implements WindowListener, I18nable {
61 /** The swing interface that receives all actions. */
62 private final SwingInterface swingInterface;
64 /** The status bar. */
65 private StatusBar statusBar = new StatusBar();
67 /** Timer for clearing the status bar. */
68 private Timer statusBarClearTimer = new Timer("StatusBar Cleaner", true);
70 /** Object for status bar clearing ticker event. */
71 private TimerTask statusBarClearTimerTask;
73 /** Delay (in seconds) after which to clear status bar. */
74 private int statusBarClearDelay = 5000;
76 /** The content pane. */
77 private JPanel contentPane = new JPanel(new BorderLayout(12, 12));
79 /** The jSite menu. */
80 private I18nMenu jSiteMenu;
83 private I18nMenu nodeMenu;
85 /** The “connect” (advanced mode) menu. */
86 private I18nMenu connectMenu;
88 /** The “connect” (simple mode) menu. */
89 private JMenuItem connectMenuItem;
91 /** The “disconnect” (advanced mode) menu. */
92 private I18nMenu disconnectMenu;
94 /** The “diconnect” (simple mode) menu item. */
95 private JMenuItem disconnectMenuItem;
97 /** The language menu. */
98 private I18nMenu languageMenu;
100 /** The about menu. */
101 private I18nMenu helpMenu;
103 /** The tabbed project pane. */
104 private JTabbedPane projectPane;
106 /** The project overview panel. */
107 private Box projectOverviewPanel;
110 * Creates a new main window that redirects all actions to the given swing
113 * @param swingInterface
114 * The swing interface to receive all actions
116 public MainWindow(SwingInterface swingInterface) {
117 super("jSite " + Version.getVersion());
118 this.swingInterface = swingInterface;
120 setPreferredSize(new Dimension(480, 280));
122 SwingUtils.center(this);
123 I18n.registerI18nable(this);
124 addWindowListener(this);
132 * Sets the text of the status bar.
135 * The text of the status bar
137 public void setStatusBarText(String text) {
138 statusBar.setText(text);
139 synchronized (statusBar) {
140 if (statusBarClearTimerTask != null) {
141 statusBarClearTimerTask.cancel();
143 statusBarClearTimerTask = new TimerTask() {
147 statusBar.setText("\u00a0");
151 statusBarClearTimer.schedule(statusBarClearTimerTask, statusBarClearDelay);
156 * Returns the status bar clear delay (in milliseconds).
158 * @return The status bar clear delay
160 public int getStatusBarClearDelay() {
161 return statusBarClearDelay;
165 * Sets the status bar clear delay (in milliseconds).
167 * @param statusBarClearDelay
168 * The status bar clear delay
170 public void setStatusBarClearDelay(int statusBarClearDelay) {
171 this.statusBarClearDelay = statusBarClearDelay;
175 * Sets whether the advanced mode is activated.
177 * @param advancedMode
178 * <code>true</code> if the advanced mode is activated,
179 * <code>false</code> if the simple mode is activated
181 public void setAdvancedMode(boolean advancedMode) {
182 connectMenu.setVisible(advancedMode);
183 connectMenuItem.setVisible(!advancedMode);
184 disconnectMenu.setVisible(advancedMode);
185 disconnectMenuItem.setVisible(!advancedMode);
192 public Container getContentPane() {
197 * Returns the currently selected project.
199 * @return The currently selected project
201 public Project getSelectedProject() {
210 * Refreshes the menu items in the “connect” and “disconnect” menus.
212 void refreshNodeMenuItems() {
213 connectMenu.removeAll();
214 for (Action nodeConnectAction: swingInterface.getNodeConnectActions()) {
215 connectMenu.add(nodeConnectAction);
217 if (connectMenu.getMenuComponentCount() == 0) {
218 JMenuItem noNodeAvailableItem = new JMenuItem(I18n.get("mainWindow.menu.connectNoNodeAvailable.name"));
219 noNodeAvailableItem.setEnabled(false);
220 connectMenu.add(noNodeAvailableItem);
222 disconnectMenu.removeAll();
223 for (Action nodeDisconnectAction: swingInterface.getNodeDisconnectActions()) {
224 disconnectMenu.add(nodeDisconnectAction);
226 if (disconnectMenu.getMenuComponentCount() == 0) {
227 JMenuItem noNodeAvailableItem = new JMenuItem(I18n.get("mainWindow.menu.disconnectNoNodeAvailable.name"));
228 noNodeAvailableItem.setEnabled(false);
229 disconnectMenu.add(noNodeAvailableItem);
238 * Initializes the window by creating all its components.
240 private void initWindow() {
241 JMenuBar menuBar = new JMenuBar();
243 jSiteMenu = new I18nMenu("mainWindow.menu.jSite");
244 menuBar.add(jSiteMenu);
246 jSiteMenu.add(new FixedJMenuItem(swingInterface.getConfigureAction()));
247 jSiteMenu.addSeparator();
248 jSiteMenu.add(new FixedJMenuItem(swingInterface.getImportConfigAction()));
249 jSiteMenu.addSeparator();
250 jSiteMenu.add(new FixedJMenuItem(swingInterface.getQuitAction()));
252 connectMenu = new I18nMenu("mainWindow.menu.connect");
253 disconnectMenu = new I18nMenu("mainWindow.menu.disconnect");
255 nodeMenu = new I18nMenu("mainWindow.menu.node");
256 menuBar.add(nodeMenu);
258 nodeMenu.add(new FixedJMenuItem(swingInterface.getManageNodesAction()));
259 nodeMenu.addSeparator();
260 nodeMenu.add(connectMenuItem = new FixedJMenuItem(swingInterface.getNodeConnectAction()));
261 nodeMenu.add(connectMenu);
262 nodeMenu.add(disconnectMenuItem = new FixedJMenuItem(swingInterface.getNodeDisconnectAction()));
263 nodeMenu.add(disconnectMenu);
264 refreshNodeMenuItems();
266 languageMenu = new I18nMenu("mainWindow.menu.language");
267 menuBar.add(languageMenu);
269 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
270 languageMenu.add(new FixedJMenuItem(languageAction));
273 JPanel spacerPanel = new JPanel();
274 spacerPanel.setOpaque(false);
275 menuBar.add(spacerPanel);
277 helpMenu = new I18nMenu("mainWindow.menu.help");
278 menuBar.add(helpMenu);
280 helpMenu.add(new FixedJMenuItem(swingInterface.getHelpAboutAction()));
282 setJMenuBar(menuBar);
284 JToolBar toolBar = new JToolBar(I18n.get("mainWindow.toolbar.name"));
285 toolBar.add(swingInterface.getManageNodesAction());
286 toolBar.addSeparator();
287 toolBar.add(swingInterface.getNodeConnectAction());
288 toolBar.add(swingInterface.getNodeDisconnectAction());
289 super.getContentPane().add(toolBar, BorderLayout.PAGE_START);
291 super.getContentPane().add(contentPane, BorderLayout.CENTER);
293 addWindowListener(new WindowAdapter() {
298 @SuppressWarnings("synthetic-access")
300 public void windowClosing(WindowEvent windowEvent) {
301 swingInterface.getQuitAction().actionPerformed(null);
309 * Initializes all components of this window.
311 private void initComponents() {
312 super.getContentPane().add(statusBar, BorderLayout.PAGE_END);
315 * the main window consists of two panels which are vertically oriented.
316 * the upper panel contains of a tabbed pane, the lower panel consists
317 * of a table that lists the running requests.
320 JPanel upperPanel = new JPanel(new BorderLayout(12, 12));
321 getContentPane().add(upperPanel, BorderLayout.PAGE_START);
322 contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
324 projectPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
325 upperPanel.add(projectPane, BorderLayout.CENTER);
327 projectOverviewPanel = new Box(BoxLayout.PAGE_AXIS);
328 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
329 projectPane.add(projectOverviewPanel);
330 projectOverviewPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
331 projectOverviewPanel.add(Box.createVerticalGlue());
332 JButton addProjectButton = new JButton(swingInterface.getAddProjectAction());
333 addProjectButton.setAlignmentX(0.5f);
334 projectOverviewPanel.add(addProjectButton);
335 projectOverviewPanel.add(Box.createVerticalGlue());
337 // JPanel lowerPanel = new JPanel(new BorderLayout(12, 12));
338 // getContentPane().add(lowerPanel, BorderLayout.CENTER);
342 // INTERFACE I18nable
348 public void updateI18n() {
349 swingInterface.getConfigureAction().updateI18n();
350 swingInterface.getImportConfigAction().updateI18n();
351 swingInterface.getQuitAction().updateI18n();
352 swingInterface.getManageNodesAction().updateI18n();
353 swingInterface.getNodeConnectAction().updateI18n();
354 connectMenu.updateI18n();
355 swingInterface.getNodeDisconnectAction().updateI18n();
356 disconnectMenu.updateI18n();
357 swingInterface.getAddProjectAction().updateI18n();
358 swingInterface.getCloneProjectAction().updateI18n();
359 swingInterface.getDeleteProjectAction().updateI18n();
360 swingInterface.getHelpAboutAction().updateI18n();
361 jSiteMenu.updateI18n();
362 nodeMenu.updateI18n();
363 languageMenu.updateI18n();
364 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
365 languageAction.updateI18n();
367 helpMenu.updateI18n();
368 getJMenuBar().revalidate();
369 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
370 for (int componentIndex = 0; componentIndex < projectPane.getTabCount(); componentIndex++) {
371 projectPane.setTitleAt(componentIndex, projectPane.getComponentAt(componentIndex).getName());
373 refreshNodeMenuItems();
374 SwingUtils.repackCentered(this);
378 // INTERFACE WindowListener
384 public void windowActivated(WindowEvent e) {
391 public void windowClosed(WindowEvent e) {
398 public void windowClosing(WindowEvent e) {
399 swingInterface.getQuitAction().actionPerformed(null);
405 public void windowDeactivated(WindowEvent e) {
412 public void windowDeiconified(WindowEvent e) {
419 public void windowIconified(WindowEvent e) {
426 public void windowOpened(WindowEvent e) {