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.Component;
24 import java.awt.Container;
25 import java.awt.Dimension;
26 import java.awt.event.WindowAdapter;
27 import java.awt.event.WindowEvent;
28 import java.awt.event.WindowListener;
29 import java.beans.PropertyChangeEvent;
30 import java.beans.PropertyChangeListener;
31 import java.util.Timer;
32 import java.util.TimerTask;
33 import java.util.logging.Logger;
35 import javax.swing.Action;
36 import javax.swing.Box;
37 import javax.swing.BoxLayout;
38 import javax.swing.JButton;
39 import javax.swing.JFrame;
40 import javax.swing.JMenuBar;
41 import javax.swing.JMenuItem;
42 import javax.swing.JPanel;
43 import javax.swing.JScrollPane;
44 import javax.swing.JTabbedPane;
45 import javax.swing.JTable;
46 import javax.swing.JToolBar;
47 import javax.swing.SwingConstants;
48 import javax.swing.border.EmptyBorder;
50 import net.pterodactylus.jsite.i18n.I18n;
51 import net.pterodactylus.jsite.i18n.I18nable;
52 import net.pterodactylus.jsite.i18n.gui.I18nAction;
53 import net.pterodactylus.jsite.i18n.gui.I18nMenu;
54 import net.pterodactylus.jsite.main.Version;
55 import net.pterodactylus.jsite.project.Project;
56 import net.pterodactylus.util.logging.Logging;
57 import net.pterodactylus.util.swing.StatusBar;
58 import net.pterodactylus.util.swing.SwingUtils;
61 * Defines the main window of the application.
63 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
66 public class MainWindow extends JFrame implements WindowListener, I18nable, PropertyChangeListener {
69 @SuppressWarnings("unused")
70 private static final Logger logger = Logging.getLogger(MainWindow.class.getName());
72 /** The swing interface that receives all actions. */
73 private final SwingInterface swingInterface;
75 /** The status bar. */
76 private StatusBar statusBar = new StatusBar();
78 /** Timer for clearing the status bar. */
79 private Timer statusBarClearTimer = new Timer("StatusBar Cleaner", true);
81 /** Object for status bar clearing ticker event. */
82 private TimerTask statusBarClearTimerTask;
84 /** Delay (in seconds) after which to clear status bar. */
85 private int statusBarClearDelay = 5000;
87 /** The content pane. */
88 private JPanel contentPane = new JPanel(new BorderLayout(12, 12));
90 /** The jSite menu. */
91 private I18nMenu jSiteMenu;
94 private I18nMenu nodeMenu;
96 /** The “connect” (advanced mode) menu. */
97 private I18nMenu connectMenu;
99 /** The “connect” (simple mode) menu. */
100 private JMenuItem connectMenuItem;
102 /** The “disconnect” (advanced mode) menu. */
103 private I18nMenu disconnectMenu;
105 /** The “diconnect” (simple mode) menu item. */
106 private JMenuItem disconnectMenuItem;
108 /** The language menu. */
109 private I18nMenu languageMenu;
111 /** The about menu. */
112 private I18nMenu helpMenu;
114 /** The tabbed project pane. */
115 private JTabbedPane projectPane;
117 /** The project overview panel. */
118 private Box projectOverviewPanel;
120 /** The request table. */
121 private JTable requestTable;
124 * Creates a new main window that redirects all actions to the given swing
127 * @param swingInterface
128 * The swing interface to receive all actions
130 public MainWindow(SwingInterface swingInterface) {
131 super("jSite " + Version.getVersion());
132 this.swingInterface = swingInterface;
134 setPreferredSize(new Dimension(480, 280));
136 SwingUtils.center(this);
137 I18n.registerI18nable(this);
138 addWindowListener(this);
146 * Sets the text of the status bar.
149 * The text of the status bar
151 public void setStatusBarText(String text) {
152 statusBar.setText(text);
153 synchronized (statusBar) {
154 if (statusBarClearTimerTask != null) {
155 statusBarClearTimerTask.cancel();
157 statusBarClearTimerTask = new TimerTask() {
159 @SuppressWarnings("synthetic-access")
162 statusBar.setText("\u00a0");
166 statusBarClearTimer.schedule(statusBarClearTimerTask, statusBarClearDelay);
171 * Returns the status bar clear delay (in milliseconds).
173 * @return The status bar clear delay
175 public int getStatusBarClearDelay() {
176 return statusBarClearDelay;
180 * Sets the status bar clear delay (in milliseconds).
182 * @param statusBarClearDelay
183 * The status bar clear delay
185 public void setStatusBarClearDelay(int statusBarClearDelay) {
186 this.statusBarClearDelay = statusBarClearDelay;
190 * Sets whether the advanced mode is activated.
192 * @param advancedMode
193 * <code>true</code> if the advanced mode is activated,
194 * <code>false</code> if the simple mode is activated
196 public void setAdvancedMode(boolean advancedMode) {
197 connectMenu.setVisible(advancedMode);
198 connectMenuItem.setVisible(!advancedMode);
199 disconnectMenu.setVisible(advancedMode);
200 disconnectMenuItem.setVisible(!advancedMode);
207 public Container getContentPane() {
212 * Returns the currently selected project.
214 * @return The currently selected project
216 public Project getSelectedProject() {
225 * Refreshes the menu items in the “connect” and “disconnect” menus.
227 void refreshNodeMenuItems() {
228 connectMenu.removeAll();
229 for (Action nodeConnectAction: swingInterface.getNodeConnectActions()) {
230 connectMenu.add(nodeConnectAction);
232 if (connectMenu.getMenuComponentCount() == 0) {
233 JMenuItem noNodeAvailableItem = new JMenuItem(I18n.get("mainWindow.menu.connectNoNodeAvailable.name"));
234 noNodeAvailableItem.setEnabled(false);
235 connectMenu.add(noNodeAvailableItem);
237 disconnectMenu.removeAll();
238 for (Action nodeDisconnectAction: swingInterface.getNodeDisconnectActions()) {
239 disconnectMenu.add(nodeDisconnectAction);
241 if (disconnectMenu.getMenuComponentCount() == 0) {
242 JMenuItem noNodeAvailableItem = new JMenuItem(I18n.get("mainWindow.menu.disconnectNoNodeAvailable.name"));
243 noNodeAvailableItem.setEnabled(false);
244 disconnectMenu.add(noNodeAvailableItem);
249 * Adds a project to the project pane.
254 void addProject(Project project) {
255 ProjectPanel projectPanel = new ProjectPanel(swingInterface, project);
256 int newTabIndex = projectPane.getTabCount();
257 projectPane.add(project.getName(), projectPanel);
258 projectPane.setToolTipTextAt(newTabIndex, project.getDescription());
259 project.addPropertyChangeListener(this);
267 * Initializes the window by creating all its components.
269 private void initWindow() {
270 JMenuBar menuBar = new JMenuBar();
272 jSiteMenu = new I18nMenu("mainWindow.menu.jSite");
273 menuBar.add(jSiteMenu);
275 jSiteMenu.add(new FixedJMenuItem(swingInterface.getConfigureAction()));
276 jSiteMenu.addSeparator();
277 jSiteMenu.add(new FixedJMenuItem(swingInterface.getImportConfigAction()));
278 jSiteMenu.addSeparator();
279 jSiteMenu.add(new FixedJMenuItem(swingInterface.getQuitAction()));
281 connectMenu = new I18nMenu("mainWindow.menu.connect");
282 disconnectMenu = new I18nMenu("mainWindow.menu.disconnect");
284 nodeMenu = new I18nMenu("mainWindow.menu.node");
285 menuBar.add(nodeMenu);
287 nodeMenu.add(new FixedJMenuItem(swingInterface.getManageNodesAction()));
288 nodeMenu.addSeparator();
289 nodeMenu.add(connectMenuItem = new FixedJMenuItem(swingInterface.getNodeConnectAction()));
290 nodeMenu.add(connectMenu);
291 nodeMenu.add(disconnectMenuItem = new FixedJMenuItem(swingInterface.getNodeDisconnectAction()));
292 nodeMenu.add(disconnectMenu);
293 refreshNodeMenuItems();
295 languageMenu = new I18nMenu("mainWindow.menu.language");
296 menuBar.add(languageMenu);
298 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
299 languageMenu.add(new FixedJMenuItem(languageAction));
302 JPanel spacerPanel = new JPanel();
303 spacerPanel.setOpaque(false);
304 menuBar.add(spacerPanel);
306 helpMenu = new I18nMenu("mainWindow.menu.help");
307 menuBar.add(helpMenu);
309 helpMenu.add(new FixedJMenuItem(swingInterface.getHelpAboutAction()));
311 setJMenuBar(menuBar);
313 JToolBar toolBar = new JToolBar(I18n.get("mainWindow.toolbar.name"));
314 toolBar.add(swingInterface.getManageNodesAction());
315 toolBar.addSeparator();
316 toolBar.add(swingInterface.getNodeConnectAction());
317 toolBar.add(swingInterface.getNodeDisconnectAction());
318 super.getContentPane().add(toolBar, BorderLayout.PAGE_START);
320 super.getContentPane().add(contentPane, BorderLayout.CENTER);
322 addWindowListener(new WindowAdapter() {
327 @SuppressWarnings("synthetic-access")
329 public void windowClosing(WindowEvent windowEvent) {
330 swingInterface.getQuitAction().actionPerformed(null);
338 * Initializes all components of this window.
340 private void initComponents() {
341 super.getContentPane().add(statusBar, BorderLayout.PAGE_END);
344 * the main window consists of two panels which are vertically oriented.
345 * the upper panel contains of a tabbed pane, the lower panel consists
346 * of a table that lists the running requests.
349 JPanel upperPanel = new JPanel(new BorderLayout(12, 12));
350 getContentPane().add(upperPanel, BorderLayout.PAGE_START);
351 contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
353 projectPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
354 upperPanel.add(projectPane, BorderLayout.CENTER);
356 projectOverviewPanel = new Box(BoxLayout.PAGE_AXIS);
357 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
358 projectPane.add(projectOverviewPanel);
359 projectOverviewPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
360 projectOverviewPanel.add(Box.createVerticalGlue());
361 JButton addProjectButton = new JButton(swingInterface.getAddProjectAction());
362 addProjectButton.setAlignmentX(0.5f);
363 projectOverviewPanel.add(addProjectButton);
364 projectOverviewPanel.add(Box.createVerticalGlue());
366 requestTable = new JTable(swingInterface.getRequestTableModel());
367 getContentPane().add(new JScrollPane(requestTable), BorderLayout.CENTER);
369 // JPanel lowerPanel = new JPanel(new BorderLayout(12, 12));
370 // getContentPane().add(lowerPanel, BorderLayout.CENTER);
374 // INTERFACE I18nable
380 public void updateI18n() {
381 swingInterface.getConfigureAction().updateI18n();
382 swingInterface.getImportConfigAction().updateI18n();
383 swingInterface.getQuitAction().updateI18n();
384 swingInterface.getManageNodesAction().updateI18n();
385 swingInterface.getNodeConnectAction().updateI18n();
386 connectMenu.updateI18n();
387 swingInterface.getNodeDisconnectAction().updateI18n();
388 disconnectMenu.updateI18n();
389 swingInterface.getAddProjectAction().updateI18n();
390 swingInterface.getCloneProjectAction().updateI18n();
391 swingInterface.getDeleteProjectAction().updateI18n();
392 swingInterface.getHelpAboutAction().updateI18n();
393 jSiteMenu.updateI18n();
394 nodeMenu.updateI18n();
395 languageMenu.updateI18n();
396 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
397 languageAction.updateI18n();
399 helpMenu.updateI18n();
400 getJMenuBar().revalidate();
401 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
402 for (int componentIndex = 0; componentIndex < projectPane.getTabCount(); componentIndex++) {
403 projectPane.setTitleAt(componentIndex, projectPane.getComponentAt(componentIndex).getName());
405 refreshNodeMenuItems();
406 SwingUtils.repackCentered(this);
410 // INTERFACE WindowListener
416 public void windowActivated(WindowEvent e) {
423 public void windowClosed(WindowEvent e) {
430 public void windowClosing(WindowEvent e) {
431 swingInterface.getQuitAction().actionPerformed(null);
437 public void windowDeactivated(WindowEvent e) {
444 public void windowDeiconified(WindowEvent e) {
451 public void windowIconified(WindowEvent e) {
458 public void windowOpened(WindowEvent e) {
463 // INTERFACE PropertyChangeListener
467 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
469 public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
470 Object eventSource = propertyChangeEvent.getSource();
471 String propertyName = propertyChangeEvent.getPropertyName();
472 if (eventSource instanceof Project) {
473 /* if a project was changed, update the tab title and tooltip. */
474 if (Project.PROPERTY_NAME.equals(propertyName) || Project.PROPERTY_DESCRIPTION.equals(propertyName)) {
475 Project project = (Project) eventSource;
476 int tabCount = projectPane.getTabCount();
477 for (int tabIndex = 0; tabIndex < tabCount; tabIndex++) {
478 Component tabComponent = projectPane.getComponentAt(tabIndex);
479 if (tabComponent instanceof ProjectPanel) {
480 Project tabProject = ((ProjectPanel) tabComponent).getProject();
481 if (tabProject.equals(project)) {
482 projectPane.setTitleAt(tabIndex, project.getName());
483 projectPane.setToolTipTextAt(tabIndex, project.getDescription());
484 projectPane.repaint();