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.JScrollPane;
40 import javax.swing.JTabbedPane;
41 import javax.swing.JTable;
42 import javax.swing.JToolBar;
43 import javax.swing.SwingConstants;
44 import javax.swing.border.EmptyBorder;
46 import net.pterodactylus.jsite.core.Project;
47 import net.pterodactylus.jsite.i18n.I18n;
48 import net.pterodactylus.jsite.i18n.I18nable;
49 import net.pterodactylus.jsite.i18n.gui.I18nAction;
50 import net.pterodactylus.jsite.i18n.gui.I18nMenu;
51 import net.pterodactylus.jsite.main.Version;
52 import net.pterodactylus.util.swing.StatusBar;
53 import net.pterodactylus.util.swing.SwingUtils;
56 * Defines the main window of the application.
58 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
61 public class MainWindow extends JFrame implements WindowListener, I18nable {
63 /** The swing interface that receives all actions. */
64 private final SwingInterface swingInterface;
66 /** The status bar. */
67 private StatusBar statusBar = new StatusBar();
69 /** Timer for clearing the status bar. */
70 private Timer statusBarClearTimer = new Timer("StatusBar Cleaner", true);
72 /** Object for status bar clearing ticker event. */
73 private TimerTask statusBarClearTimerTask;
75 /** Delay (in seconds) after which to clear status bar. */
76 private int statusBarClearDelay = 5000;
78 /** The content pane. */
79 private JPanel contentPane = new JPanel(new BorderLayout(12, 12));
81 /** The jSite menu. */
82 private I18nMenu jSiteMenu;
85 private I18nMenu nodeMenu;
87 /** The “connect” (advanced mode) menu. */
88 private I18nMenu connectMenu;
90 /** The “connect” (simple mode) menu. */
91 private JMenuItem connectMenuItem;
93 /** The “disconnect” (advanced mode) menu. */
94 private I18nMenu disconnectMenu;
96 /** The “diconnect” (simple mode) menu item. */
97 private JMenuItem disconnectMenuItem;
99 /** The language menu. */
100 private I18nMenu languageMenu;
102 /** The about menu. */
103 private I18nMenu helpMenu;
105 /** The tabbed project pane. */
106 private JTabbedPane projectPane;
108 /** The project overview panel. */
109 private Box projectOverviewPanel;
111 /** The request table. */
112 private JTable requestTable;
115 * Creates a new main window that redirects all actions to the given swing
118 * @param swingInterface
119 * The swing interface to receive all actions
121 public MainWindow(SwingInterface swingInterface) {
122 super("jSite " + Version.getVersion());
123 this.swingInterface = swingInterface;
125 setPreferredSize(new Dimension(480, 280));
127 SwingUtils.center(this);
128 I18n.registerI18nable(this);
129 addWindowListener(this);
137 * Sets the text of the status bar.
140 * The text of the status bar
142 public void setStatusBarText(String text) {
143 statusBar.setText(text);
144 synchronized (statusBar) {
145 if (statusBarClearTimerTask != null) {
146 statusBarClearTimerTask.cancel();
148 statusBarClearTimerTask = new TimerTask() {
150 @SuppressWarnings("synthetic-access")
153 statusBar.setText("\u00a0");
157 statusBarClearTimer.schedule(statusBarClearTimerTask, statusBarClearDelay);
162 * Returns the status bar clear delay (in milliseconds).
164 * @return The status bar clear delay
166 public int getStatusBarClearDelay() {
167 return statusBarClearDelay;
171 * Sets the status bar clear delay (in milliseconds).
173 * @param statusBarClearDelay
174 * The status bar clear delay
176 public void setStatusBarClearDelay(int statusBarClearDelay) {
177 this.statusBarClearDelay = statusBarClearDelay;
181 * Sets whether the advanced mode is activated.
183 * @param advancedMode
184 * <code>true</code> if the advanced mode is activated,
185 * <code>false</code> if the simple mode is activated
187 public void setAdvancedMode(boolean advancedMode) {
188 connectMenu.setVisible(advancedMode);
189 connectMenuItem.setVisible(!advancedMode);
190 disconnectMenu.setVisible(advancedMode);
191 disconnectMenuItem.setVisible(!advancedMode);
198 public Container getContentPane() {
203 * Returns the currently selected project.
205 * @return The currently selected project
207 public Project getSelectedProject() {
216 * Refreshes the menu items in the “connect” and “disconnect” menus.
218 void refreshNodeMenuItems() {
219 connectMenu.removeAll();
220 for (Action nodeConnectAction: swingInterface.getNodeConnectActions()) {
221 connectMenu.add(nodeConnectAction);
223 if (connectMenu.getMenuComponentCount() == 0) {
224 JMenuItem noNodeAvailableItem = new JMenuItem(I18n.get("mainWindow.menu.connectNoNodeAvailable.name"));
225 noNodeAvailableItem.setEnabled(false);
226 connectMenu.add(noNodeAvailableItem);
228 disconnectMenu.removeAll();
229 for (Action nodeDisconnectAction: swingInterface.getNodeDisconnectActions()) {
230 disconnectMenu.add(nodeDisconnectAction);
232 if (disconnectMenu.getMenuComponentCount() == 0) {
233 JMenuItem noNodeAvailableItem = new JMenuItem(I18n.get("mainWindow.menu.disconnectNoNodeAvailable.name"));
234 noNodeAvailableItem.setEnabled(false);
235 disconnectMenu.add(noNodeAvailableItem);
244 * Initializes the window by creating all its components.
246 private void initWindow() {
247 JMenuBar menuBar = new JMenuBar();
249 jSiteMenu = new I18nMenu("mainWindow.menu.jSite");
250 menuBar.add(jSiteMenu);
252 jSiteMenu.add(new FixedJMenuItem(swingInterface.getConfigureAction()));
253 jSiteMenu.addSeparator();
254 jSiteMenu.add(new FixedJMenuItem(swingInterface.getImportConfigAction()));
255 jSiteMenu.addSeparator();
256 jSiteMenu.add(new FixedJMenuItem(swingInterface.getQuitAction()));
258 connectMenu = new I18nMenu("mainWindow.menu.connect");
259 disconnectMenu = new I18nMenu("mainWindow.menu.disconnect");
261 nodeMenu = new I18nMenu("mainWindow.menu.node");
262 menuBar.add(nodeMenu);
264 nodeMenu.add(new FixedJMenuItem(swingInterface.getManageNodesAction()));
265 nodeMenu.addSeparator();
266 nodeMenu.add(connectMenuItem = new FixedJMenuItem(swingInterface.getNodeConnectAction()));
267 nodeMenu.add(connectMenu);
268 nodeMenu.add(disconnectMenuItem = new FixedJMenuItem(swingInterface.getNodeDisconnectAction()));
269 nodeMenu.add(disconnectMenu);
270 refreshNodeMenuItems();
272 languageMenu = new I18nMenu("mainWindow.menu.language");
273 menuBar.add(languageMenu);
275 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
276 languageMenu.add(new FixedJMenuItem(languageAction));
279 JPanel spacerPanel = new JPanel();
280 spacerPanel.setOpaque(false);
281 menuBar.add(spacerPanel);
283 helpMenu = new I18nMenu("mainWindow.menu.help");
284 menuBar.add(helpMenu);
286 helpMenu.add(new FixedJMenuItem(swingInterface.getHelpAboutAction()));
288 setJMenuBar(menuBar);
290 JToolBar toolBar = new JToolBar(I18n.get("mainWindow.toolbar.name"));
291 toolBar.add(swingInterface.getManageNodesAction());
292 toolBar.addSeparator();
293 toolBar.add(swingInterface.getNodeConnectAction());
294 toolBar.add(swingInterface.getNodeDisconnectAction());
295 super.getContentPane().add(toolBar, BorderLayout.PAGE_START);
297 super.getContentPane().add(contentPane, BorderLayout.CENTER);
299 addWindowListener(new WindowAdapter() {
304 @SuppressWarnings("synthetic-access")
306 public void windowClosing(WindowEvent windowEvent) {
307 swingInterface.getQuitAction().actionPerformed(null);
315 * Initializes all components of this window.
317 private void initComponents() {
318 super.getContentPane().add(statusBar, BorderLayout.PAGE_END);
321 * the main window consists of two panels which are vertically oriented.
322 * the upper panel contains of a tabbed pane, the lower panel consists
323 * of a table that lists the running requests.
326 JPanel upperPanel = new JPanel(new BorderLayout(12, 12));
327 getContentPane().add(upperPanel, BorderLayout.PAGE_START);
328 contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
330 projectPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
331 upperPanel.add(projectPane, BorderLayout.CENTER);
333 projectOverviewPanel = new Box(BoxLayout.PAGE_AXIS);
334 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
335 projectPane.add(projectOverviewPanel);
336 projectOverviewPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
337 projectOverviewPanel.add(Box.createVerticalGlue());
338 JButton addProjectButton = new JButton(swingInterface.getAddProjectAction());
339 addProjectButton.setAlignmentX(0.5f);
340 projectOverviewPanel.add(addProjectButton);
341 projectOverviewPanel.add(Box.createVerticalGlue());
343 requestTable = new JTable(swingInterface.getRequestTableModel());
344 getContentPane().add(new JScrollPane(requestTable), BorderLayout.CENTER);
346 // JPanel lowerPanel = new JPanel(new BorderLayout(12, 12));
347 // getContentPane().add(lowerPanel, BorderLayout.CENTER);
351 // INTERFACE I18nable
357 public void updateI18n() {
358 swingInterface.getConfigureAction().updateI18n();
359 swingInterface.getImportConfigAction().updateI18n();
360 swingInterface.getQuitAction().updateI18n();
361 swingInterface.getManageNodesAction().updateI18n();
362 swingInterface.getNodeConnectAction().updateI18n();
363 connectMenu.updateI18n();
364 swingInterface.getNodeDisconnectAction().updateI18n();
365 disconnectMenu.updateI18n();
366 swingInterface.getAddProjectAction().updateI18n();
367 swingInterface.getCloneProjectAction().updateI18n();
368 swingInterface.getDeleteProjectAction().updateI18n();
369 swingInterface.getHelpAboutAction().updateI18n();
370 jSiteMenu.updateI18n();
371 nodeMenu.updateI18n();
372 languageMenu.updateI18n();
373 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
374 languageAction.updateI18n();
376 helpMenu.updateI18n();
377 getJMenuBar().revalidate();
378 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
379 for (int componentIndex = 0; componentIndex < projectPane.getTabCount(); componentIndex++) {
380 projectPane.setTitleAt(componentIndex, projectPane.getComponentAt(componentIndex).getName());
382 refreshNodeMenuItems();
383 SwingUtils.repackCentered(this);
387 // INTERFACE WindowListener
393 public void windowActivated(WindowEvent e) {
400 public void windowClosed(WindowEvent e) {
407 public void windowClosing(WindowEvent e) {
408 swingInterface.getQuitAction().actionPerformed(null);
414 public void windowDeactivated(WindowEvent e) {
421 public void windowDeiconified(WindowEvent e) {
428 public void windowIconified(WindowEvent e) {
435 public void windowOpened(WindowEvent e) {