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() {
145 @SuppressWarnings("synthetic-access")
148 statusBar.setText("\u00a0");
152 statusBarClearTimer.schedule(statusBarClearTimerTask, statusBarClearDelay);
157 * Returns the status bar clear delay (in milliseconds).
159 * @return The status bar clear delay
161 public int getStatusBarClearDelay() {
162 return statusBarClearDelay;
166 * Sets the status bar clear delay (in milliseconds).
168 * @param statusBarClearDelay
169 * The status bar clear delay
171 public void setStatusBarClearDelay(int statusBarClearDelay) {
172 this.statusBarClearDelay = statusBarClearDelay;
176 * Sets whether the advanced mode is activated.
178 * @param advancedMode
179 * <code>true</code> if the advanced mode is activated,
180 * <code>false</code> if the simple mode is activated
182 public void setAdvancedMode(boolean advancedMode) {
183 connectMenu.setVisible(advancedMode);
184 connectMenuItem.setVisible(!advancedMode);
185 disconnectMenu.setVisible(advancedMode);
186 disconnectMenuItem.setVisible(!advancedMode);
193 public Container getContentPane() {
198 * Returns the currently selected project.
200 * @return The currently selected project
202 public Project getSelectedProject() {
211 * Refreshes the menu items in the “connect” and “disconnect” menus.
213 void refreshNodeMenuItems() {
214 connectMenu.removeAll();
215 for (Action nodeConnectAction: swingInterface.getNodeConnectActions()) {
216 connectMenu.add(nodeConnectAction);
218 if (connectMenu.getMenuComponentCount() == 0) {
219 JMenuItem noNodeAvailableItem = new JMenuItem(I18n.get("mainWindow.menu.connectNoNodeAvailable.name"));
220 noNodeAvailableItem.setEnabled(false);
221 connectMenu.add(noNodeAvailableItem);
223 disconnectMenu.removeAll();
224 for (Action nodeDisconnectAction: swingInterface.getNodeDisconnectActions()) {
225 disconnectMenu.add(nodeDisconnectAction);
227 if (disconnectMenu.getMenuComponentCount() == 0) {
228 JMenuItem noNodeAvailableItem = new JMenuItem(I18n.get("mainWindow.menu.disconnectNoNodeAvailable.name"));
229 noNodeAvailableItem.setEnabled(false);
230 disconnectMenu.add(noNodeAvailableItem);
239 * Initializes the window by creating all its components.
241 private void initWindow() {
242 JMenuBar menuBar = new JMenuBar();
244 jSiteMenu = new I18nMenu("mainWindow.menu.jSite");
245 menuBar.add(jSiteMenu);
247 jSiteMenu.add(new FixedJMenuItem(swingInterface.getConfigureAction()));
248 jSiteMenu.addSeparator();
249 jSiteMenu.add(new FixedJMenuItem(swingInterface.getImportConfigAction()));
250 jSiteMenu.addSeparator();
251 jSiteMenu.add(new FixedJMenuItem(swingInterface.getQuitAction()));
253 connectMenu = new I18nMenu("mainWindow.menu.connect");
254 disconnectMenu = new I18nMenu("mainWindow.menu.disconnect");
256 nodeMenu = new I18nMenu("mainWindow.menu.node");
257 menuBar.add(nodeMenu);
259 nodeMenu.add(new FixedJMenuItem(swingInterface.getManageNodesAction()));
260 nodeMenu.addSeparator();
261 nodeMenu.add(connectMenuItem = new FixedJMenuItem(swingInterface.getNodeConnectAction()));
262 nodeMenu.add(connectMenu);
263 nodeMenu.add(disconnectMenuItem = new FixedJMenuItem(swingInterface.getNodeDisconnectAction()));
264 nodeMenu.add(disconnectMenu);
265 refreshNodeMenuItems();
267 languageMenu = new I18nMenu("mainWindow.menu.language");
268 menuBar.add(languageMenu);
270 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
271 languageMenu.add(new FixedJMenuItem(languageAction));
274 JPanel spacerPanel = new JPanel();
275 spacerPanel.setOpaque(false);
276 menuBar.add(spacerPanel);
278 helpMenu = new I18nMenu("mainWindow.menu.help");
279 menuBar.add(helpMenu);
281 helpMenu.add(new FixedJMenuItem(swingInterface.getHelpAboutAction()));
283 setJMenuBar(menuBar);
285 JToolBar toolBar = new JToolBar(I18n.get("mainWindow.toolbar.name"));
286 toolBar.add(swingInterface.getManageNodesAction());
287 toolBar.addSeparator();
288 toolBar.add(swingInterface.getNodeConnectAction());
289 toolBar.add(swingInterface.getNodeDisconnectAction());
290 super.getContentPane().add(toolBar, BorderLayout.PAGE_START);
292 super.getContentPane().add(contentPane, BorderLayout.CENTER);
294 addWindowListener(new WindowAdapter() {
299 @SuppressWarnings("synthetic-access")
301 public void windowClosing(WindowEvent windowEvent) {
302 swingInterface.getQuitAction().actionPerformed(null);
310 * Initializes all components of this window.
312 private void initComponents() {
313 super.getContentPane().add(statusBar, BorderLayout.PAGE_END);
316 * the main window consists of two panels which are vertically oriented.
317 * the upper panel contains of a tabbed pane, the lower panel consists
318 * of a table that lists the running requests.
321 JPanel upperPanel = new JPanel(new BorderLayout(12, 12));
322 getContentPane().add(upperPanel, BorderLayout.PAGE_START);
323 contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
325 projectPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
326 upperPanel.add(projectPane, BorderLayout.CENTER);
328 projectOverviewPanel = new Box(BoxLayout.PAGE_AXIS);
329 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
330 projectPane.add(projectOverviewPanel);
331 projectOverviewPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
332 projectOverviewPanel.add(Box.createVerticalGlue());
333 JButton addProjectButton = new JButton(swingInterface.getAddProjectAction());
334 addProjectButton.setAlignmentX(0.5f);
335 projectOverviewPanel.add(addProjectButton);
336 projectOverviewPanel.add(Box.createVerticalGlue());
338 // JPanel lowerPanel = new JPanel(new BorderLayout(12, 12));
339 // getContentPane().add(lowerPanel, BorderLayout.CENTER);
343 // INTERFACE I18nable
349 public void updateI18n() {
350 swingInterface.getConfigureAction().updateI18n();
351 swingInterface.getImportConfigAction().updateI18n();
352 swingInterface.getQuitAction().updateI18n();
353 swingInterface.getManageNodesAction().updateI18n();
354 swingInterface.getNodeConnectAction().updateI18n();
355 connectMenu.updateI18n();
356 swingInterface.getNodeDisconnectAction().updateI18n();
357 disconnectMenu.updateI18n();
358 swingInterface.getAddProjectAction().updateI18n();
359 swingInterface.getCloneProjectAction().updateI18n();
360 swingInterface.getDeleteProjectAction().updateI18n();
361 swingInterface.getHelpAboutAction().updateI18n();
362 jSiteMenu.updateI18n();
363 nodeMenu.updateI18n();
364 languageMenu.updateI18n();
365 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
366 languageAction.updateI18n();
368 helpMenu.updateI18n();
369 getJMenuBar().revalidate();
370 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
371 for (int componentIndex = 0; componentIndex < projectPane.getTabCount(); componentIndex++) {
372 projectPane.setTitleAt(componentIndex, projectPane.getComponentAt(componentIndex).getName());
374 refreshNodeMenuItems();
375 SwingUtils.repackCentered(this);
379 // INTERFACE WindowListener
385 public void windowActivated(WindowEvent e) {
392 public void windowClosed(WindowEvent e) {
399 public void windowClosing(WindowEvent e) {
400 swingInterface.getQuitAction().actionPerformed(null);
406 public void windowDeactivated(WindowEvent e) {
413 public void windowDeiconified(WindowEvent e) {
420 public void windowIconified(WindowEvent e) {
427 public void windowOpened(WindowEvent e) {