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;
34 import javax.swing.Action;
35 import javax.swing.Box;
36 import javax.swing.BoxLayout;
37 import javax.swing.JButton;
38 import javax.swing.JFrame;
39 import javax.swing.JMenuBar;
40 import javax.swing.JMenuItem;
41 import javax.swing.JPanel;
42 import javax.swing.JScrollPane;
43 import javax.swing.JTabbedPane;
44 import javax.swing.JTable;
45 import javax.swing.JToolBar;
46 import javax.swing.SwingConstants;
47 import javax.swing.border.EmptyBorder;
49 import net.pterodactylus.jsite.core.Project;
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.util.swing.StatusBar;
56 import net.pterodactylus.util.swing.SwingUtils;
59 * Defines the main window of the application.
61 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
64 public class MainWindow extends JFrame implements WindowListener, I18nable, PropertyChangeListener {
66 /** The swing interface that receives all actions. */
67 private final SwingInterface swingInterface;
69 /** The status bar. */
70 private StatusBar statusBar = new StatusBar();
72 /** Timer for clearing the status bar. */
73 private Timer statusBarClearTimer = new Timer("StatusBar Cleaner", true);
75 /** Object for status bar clearing ticker event. */
76 private TimerTask statusBarClearTimerTask;
78 /** Delay (in seconds) after which to clear status bar. */
79 private int statusBarClearDelay = 5000;
81 /** The content pane. */
82 private JPanel contentPane = new JPanel(new BorderLayout(12, 12));
84 /** The jSite menu. */
85 private I18nMenu jSiteMenu;
88 private I18nMenu nodeMenu;
90 /** The “connect” (advanced mode) menu. */
91 private I18nMenu connectMenu;
93 /** The “connect” (simple mode) menu. */
94 private JMenuItem connectMenuItem;
96 /** The “disconnect” (advanced mode) menu. */
97 private I18nMenu disconnectMenu;
99 /** The “diconnect” (simple mode) menu item. */
100 private JMenuItem disconnectMenuItem;
102 /** The language menu. */
103 private I18nMenu languageMenu;
105 /** The about menu. */
106 private I18nMenu helpMenu;
108 /** The tabbed project pane. */
109 private JTabbedPane projectPane;
111 /** The project overview panel. */
112 private Box projectOverviewPanel;
114 /** The request table. */
115 private JTable requestTable;
118 * Creates a new main window that redirects all actions to the given swing
121 * @param swingInterface
122 * The swing interface to receive all actions
124 public MainWindow(SwingInterface swingInterface) {
125 super("jSite " + Version.getVersion());
126 this.swingInterface = swingInterface;
128 setPreferredSize(new Dimension(480, 280));
130 SwingUtils.center(this);
131 I18n.registerI18nable(this);
132 addWindowListener(this);
140 * Sets the text of the status bar.
143 * The text of the status bar
145 public void setStatusBarText(String text) {
146 statusBar.setText(text);
147 synchronized (statusBar) {
148 if (statusBarClearTimerTask != null) {
149 statusBarClearTimerTask.cancel();
151 statusBarClearTimerTask = new TimerTask() {
153 @SuppressWarnings("synthetic-access")
156 statusBar.setText("\u00a0");
160 statusBarClearTimer.schedule(statusBarClearTimerTask, statusBarClearDelay);
165 * Returns the status bar clear delay (in milliseconds).
167 * @return The status bar clear delay
169 public int getStatusBarClearDelay() {
170 return statusBarClearDelay;
174 * Sets the status bar clear delay (in milliseconds).
176 * @param statusBarClearDelay
177 * The status bar clear delay
179 public void setStatusBarClearDelay(int statusBarClearDelay) {
180 this.statusBarClearDelay = statusBarClearDelay;
184 * Sets whether the advanced mode is activated.
186 * @param advancedMode
187 * <code>true</code> if the advanced mode is activated,
188 * <code>false</code> if the simple mode is activated
190 public void setAdvancedMode(boolean advancedMode) {
191 connectMenu.setVisible(advancedMode);
192 connectMenuItem.setVisible(!advancedMode);
193 disconnectMenu.setVisible(advancedMode);
194 disconnectMenuItem.setVisible(!advancedMode);
201 public Container getContentPane() {
206 * Returns the currently selected project.
208 * @return The currently selected project
210 public Project getSelectedProject() {
219 * Refreshes the menu items in the “connect” and “disconnect” menus.
221 void refreshNodeMenuItems() {
222 connectMenu.removeAll();
223 for (Action nodeConnectAction: swingInterface.getNodeConnectActions()) {
224 connectMenu.add(nodeConnectAction);
226 if (connectMenu.getMenuComponentCount() == 0) {
227 JMenuItem noNodeAvailableItem = new JMenuItem(I18n.get("mainWindow.menu.connectNoNodeAvailable.name"));
228 noNodeAvailableItem.setEnabled(false);
229 connectMenu.add(noNodeAvailableItem);
231 disconnectMenu.removeAll();
232 for (Action nodeDisconnectAction: swingInterface.getNodeDisconnectActions()) {
233 disconnectMenu.add(nodeDisconnectAction);
235 if (disconnectMenu.getMenuComponentCount() == 0) {
236 JMenuItem noNodeAvailableItem = new JMenuItem(I18n.get("mainWindow.menu.disconnectNoNodeAvailable.name"));
237 noNodeAvailableItem.setEnabled(false);
238 disconnectMenu.add(noNodeAvailableItem);
243 * Adds a project to the project pane.
248 void addProject(Project project) {
249 ProjectPanel projectPanel = new ProjectPanel(swingInterface, project);
250 int newTabIndex = projectPane.getTabCount();
251 projectPane.add(project.getName(), projectPanel);
252 projectPane.setToolTipTextAt(newTabIndex, project.getDescription());
253 project.addPropertyChangeListener(this);
261 * Initializes the window by creating all its components.
263 private void initWindow() {
264 JMenuBar menuBar = new JMenuBar();
266 jSiteMenu = new I18nMenu("mainWindow.menu.jSite");
267 menuBar.add(jSiteMenu);
269 jSiteMenu.add(new FixedJMenuItem(swingInterface.getConfigureAction()));
270 jSiteMenu.addSeparator();
271 jSiteMenu.add(new FixedJMenuItem(swingInterface.getImportConfigAction()));
272 jSiteMenu.addSeparator();
273 jSiteMenu.add(new FixedJMenuItem(swingInterface.getQuitAction()));
275 connectMenu = new I18nMenu("mainWindow.menu.connect");
276 disconnectMenu = new I18nMenu("mainWindow.menu.disconnect");
278 nodeMenu = new I18nMenu("mainWindow.menu.node");
279 menuBar.add(nodeMenu);
281 nodeMenu.add(new FixedJMenuItem(swingInterface.getManageNodesAction()));
282 nodeMenu.addSeparator();
283 nodeMenu.add(connectMenuItem = new FixedJMenuItem(swingInterface.getNodeConnectAction()));
284 nodeMenu.add(connectMenu);
285 nodeMenu.add(disconnectMenuItem = new FixedJMenuItem(swingInterface.getNodeDisconnectAction()));
286 nodeMenu.add(disconnectMenu);
287 refreshNodeMenuItems();
289 languageMenu = new I18nMenu("mainWindow.menu.language");
290 menuBar.add(languageMenu);
292 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
293 languageMenu.add(new FixedJMenuItem(languageAction));
296 JPanel spacerPanel = new JPanel();
297 spacerPanel.setOpaque(false);
298 menuBar.add(spacerPanel);
300 helpMenu = new I18nMenu("mainWindow.menu.help");
301 menuBar.add(helpMenu);
303 helpMenu.add(new FixedJMenuItem(swingInterface.getHelpAboutAction()));
305 setJMenuBar(menuBar);
307 JToolBar toolBar = new JToolBar(I18n.get("mainWindow.toolbar.name"));
308 toolBar.add(swingInterface.getManageNodesAction());
309 toolBar.addSeparator();
310 toolBar.add(swingInterface.getNodeConnectAction());
311 toolBar.add(swingInterface.getNodeDisconnectAction());
312 super.getContentPane().add(toolBar, BorderLayout.PAGE_START);
314 super.getContentPane().add(contentPane, BorderLayout.CENTER);
316 addWindowListener(new WindowAdapter() {
321 @SuppressWarnings("synthetic-access")
323 public void windowClosing(WindowEvent windowEvent) {
324 swingInterface.getQuitAction().actionPerformed(null);
332 * Initializes all components of this window.
334 private void initComponents() {
335 super.getContentPane().add(statusBar, BorderLayout.PAGE_END);
338 * the main window consists of two panels which are vertically oriented.
339 * the upper panel contains of a tabbed pane, the lower panel consists
340 * of a table that lists the running requests.
343 JPanel upperPanel = new JPanel(new BorderLayout(12, 12));
344 getContentPane().add(upperPanel, BorderLayout.PAGE_START);
345 contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
347 projectPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
348 upperPanel.add(projectPane, BorderLayout.CENTER);
350 projectOverviewPanel = new Box(BoxLayout.PAGE_AXIS);
351 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
352 projectPane.add(projectOverviewPanel);
353 projectOverviewPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
354 projectOverviewPanel.add(Box.createVerticalGlue());
355 JButton addProjectButton = new JButton(swingInterface.getAddProjectAction());
356 addProjectButton.setAlignmentX(0.5f);
357 projectOverviewPanel.add(addProjectButton);
358 projectOverviewPanel.add(Box.createVerticalGlue());
360 requestTable = new JTable(swingInterface.getRequestTableModel());
361 getContentPane().add(new JScrollPane(requestTable), BorderLayout.CENTER);
363 // JPanel lowerPanel = new JPanel(new BorderLayout(12, 12));
364 // getContentPane().add(lowerPanel, BorderLayout.CENTER);
368 // INTERFACE I18nable
374 public void updateI18n() {
375 swingInterface.getConfigureAction().updateI18n();
376 swingInterface.getImportConfigAction().updateI18n();
377 swingInterface.getQuitAction().updateI18n();
378 swingInterface.getManageNodesAction().updateI18n();
379 swingInterface.getNodeConnectAction().updateI18n();
380 connectMenu.updateI18n();
381 swingInterface.getNodeDisconnectAction().updateI18n();
382 disconnectMenu.updateI18n();
383 swingInterface.getAddProjectAction().updateI18n();
384 swingInterface.getCloneProjectAction().updateI18n();
385 swingInterface.getDeleteProjectAction().updateI18n();
386 swingInterface.getHelpAboutAction().updateI18n();
387 jSiteMenu.updateI18n();
388 nodeMenu.updateI18n();
389 languageMenu.updateI18n();
390 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
391 languageAction.updateI18n();
393 helpMenu.updateI18n();
394 getJMenuBar().revalidate();
395 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
396 for (int componentIndex = 0; componentIndex < projectPane.getTabCount(); componentIndex++) {
397 projectPane.setTitleAt(componentIndex, projectPane.getComponentAt(componentIndex).getName());
399 refreshNodeMenuItems();
400 SwingUtils.repackCentered(this);
404 // INTERFACE WindowListener
410 public void windowActivated(WindowEvent e) {
417 public void windowClosed(WindowEvent e) {
424 public void windowClosing(WindowEvent e) {
425 swingInterface.getQuitAction().actionPerformed(null);
431 public void windowDeactivated(WindowEvent e) {
438 public void windowDeiconified(WindowEvent e) {
445 public void windowIconified(WindowEvent e) {
452 public void windowOpened(WindowEvent e) {
457 // INTERFACE PropertyChangeListener
461 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
463 public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
464 Object eventSource = propertyChangeEvent.getSource();
465 String propertyName = propertyChangeEvent.getPropertyName();
466 if (eventSource instanceof Project) {
467 /* if a project was changed, update the tab title and tooltip. */
468 if (Project.PROPERTY_NAME.equals(propertyName) || Project.PROPERTY_DESCRIPTION.equals(propertyName)) {
469 Project project = (Project) eventSource;
470 int tabCount = projectPane.getTabCount();
471 for (int tabIndex = 0; tabIndex < tabCount; tabIndex++) {
472 Component tabComponent = projectPane.getComponentAt(tabIndex);
473 if (tabComponent instanceof ProjectPanel) {
474 Project tabProject = ((ProjectPanel) tabComponent).getProject();
475 if (tabProject.equals(project)) {
476 projectPane.setTitleAt(tabIndex, project.getName());
477 projectPane.setToolTipTextAt(tabIndex, project.getDescription());
478 projectPane.repaint();