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);
240 * Adds a project to the project pane.
245 void addProject(Project project) {
246 ProjectPanel projectPanel = new ProjectPanel(swingInterface, project);
247 projectPane.add(project.getName(), projectPanel);
255 * Initializes the window by creating all its components.
257 private void initWindow() {
258 JMenuBar menuBar = new JMenuBar();
260 jSiteMenu = new I18nMenu("mainWindow.menu.jSite");
261 menuBar.add(jSiteMenu);
263 jSiteMenu.add(new FixedJMenuItem(swingInterface.getConfigureAction()));
264 jSiteMenu.addSeparator();
265 jSiteMenu.add(new FixedJMenuItem(swingInterface.getImportConfigAction()));
266 jSiteMenu.addSeparator();
267 jSiteMenu.add(new FixedJMenuItem(swingInterface.getQuitAction()));
269 connectMenu = new I18nMenu("mainWindow.menu.connect");
270 disconnectMenu = new I18nMenu("mainWindow.menu.disconnect");
272 nodeMenu = new I18nMenu("mainWindow.menu.node");
273 menuBar.add(nodeMenu);
275 nodeMenu.add(new FixedJMenuItem(swingInterface.getManageNodesAction()));
276 nodeMenu.addSeparator();
277 nodeMenu.add(connectMenuItem = new FixedJMenuItem(swingInterface.getNodeConnectAction()));
278 nodeMenu.add(connectMenu);
279 nodeMenu.add(disconnectMenuItem = new FixedJMenuItem(swingInterface.getNodeDisconnectAction()));
280 nodeMenu.add(disconnectMenu);
281 refreshNodeMenuItems();
283 languageMenu = new I18nMenu("mainWindow.menu.language");
284 menuBar.add(languageMenu);
286 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
287 languageMenu.add(new FixedJMenuItem(languageAction));
290 JPanel spacerPanel = new JPanel();
291 spacerPanel.setOpaque(false);
292 menuBar.add(spacerPanel);
294 helpMenu = new I18nMenu("mainWindow.menu.help");
295 menuBar.add(helpMenu);
297 helpMenu.add(new FixedJMenuItem(swingInterface.getHelpAboutAction()));
299 setJMenuBar(menuBar);
301 JToolBar toolBar = new JToolBar(I18n.get("mainWindow.toolbar.name"));
302 toolBar.add(swingInterface.getManageNodesAction());
303 toolBar.addSeparator();
304 toolBar.add(swingInterface.getNodeConnectAction());
305 toolBar.add(swingInterface.getNodeDisconnectAction());
306 super.getContentPane().add(toolBar, BorderLayout.PAGE_START);
308 super.getContentPane().add(contentPane, BorderLayout.CENTER);
310 addWindowListener(new WindowAdapter() {
315 @SuppressWarnings("synthetic-access")
317 public void windowClosing(WindowEvent windowEvent) {
318 swingInterface.getQuitAction().actionPerformed(null);
326 * Initializes all components of this window.
328 private void initComponents() {
329 super.getContentPane().add(statusBar, BorderLayout.PAGE_END);
332 * the main window consists of two panels which are vertically oriented.
333 * the upper panel contains of a tabbed pane, the lower panel consists
334 * of a table that lists the running requests.
337 JPanel upperPanel = new JPanel(new BorderLayout(12, 12));
338 getContentPane().add(upperPanel, BorderLayout.PAGE_START);
339 contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
341 projectPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
342 upperPanel.add(projectPane, BorderLayout.CENTER);
344 projectOverviewPanel = new Box(BoxLayout.PAGE_AXIS);
345 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
346 projectPane.add(projectOverviewPanel);
347 projectOverviewPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
348 projectOverviewPanel.add(Box.createVerticalGlue());
349 JButton addProjectButton = new JButton(swingInterface.getAddProjectAction());
350 addProjectButton.setAlignmentX(0.5f);
351 projectOverviewPanel.add(addProjectButton);
352 projectOverviewPanel.add(Box.createVerticalGlue());
354 requestTable = new JTable(swingInterface.getRequestTableModel());
355 getContentPane().add(new JScrollPane(requestTable), BorderLayout.CENTER);
357 // JPanel lowerPanel = new JPanel(new BorderLayout(12, 12));
358 // getContentPane().add(lowerPanel, BorderLayout.CENTER);
362 // INTERFACE I18nable
368 public void updateI18n() {
369 swingInterface.getConfigureAction().updateI18n();
370 swingInterface.getImportConfigAction().updateI18n();
371 swingInterface.getQuitAction().updateI18n();
372 swingInterface.getManageNodesAction().updateI18n();
373 swingInterface.getNodeConnectAction().updateI18n();
374 connectMenu.updateI18n();
375 swingInterface.getNodeDisconnectAction().updateI18n();
376 disconnectMenu.updateI18n();
377 swingInterface.getAddProjectAction().updateI18n();
378 swingInterface.getCloneProjectAction().updateI18n();
379 swingInterface.getDeleteProjectAction().updateI18n();
380 swingInterface.getHelpAboutAction().updateI18n();
381 jSiteMenu.updateI18n();
382 nodeMenu.updateI18n();
383 languageMenu.updateI18n();
384 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
385 languageAction.updateI18n();
387 helpMenu.updateI18n();
388 getJMenuBar().revalidate();
389 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
390 for (int componentIndex = 0; componentIndex < projectPane.getTabCount(); componentIndex++) {
391 projectPane.setTitleAt(componentIndex, projectPane.getComponentAt(componentIndex).getName());
393 refreshNodeMenuItems();
394 SwingUtils.repackCentered(this);
398 // INTERFACE WindowListener
404 public void windowActivated(WindowEvent e) {
411 public void windowClosed(WindowEvent e) {
418 public void windowClosing(WindowEvent e) {
419 swingInterface.getQuitAction().actionPerformed(null);
425 public void windowDeactivated(WindowEvent e) {
432 public void windowDeiconified(WindowEvent e) {
439 public void windowIconified(WindowEvent e) {
446 public void windowOpened(WindowEvent e) {