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.JTabbedPane;
44 import javax.swing.JToolBar;
45 import javax.swing.SwingConstants;
46 import javax.swing.border.EmptyBorder;
48 import net.pterodactylus.jsite.i18n.I18n;
49 import net.pterodactylus.jsite.i18n.I18nable;
50 import net.pterodactylus.jsite.i18n.gui.I18nAction;
51 import net.pterodactylus.jsite.i18n.gui.I18nMenu;
52 import net.pterodactylus.jsite.main.Version;
53 import net.pterodactylus.jsite.project.Project;
54 import net.pterodactylus.util.logging.Logging;
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 {
67 @SuppressWarnings("unused")
68 private static final Logger logger = Logging.getLogger(MainWindow.class.getName());
70 /** The swing interface that receives all actions. */
71 private final SwingInterface swingInterface;
73 /** The status bar. */
74 private StatusBar statusBar = new StatusBar();
76 /** Timer for clearing the status bar. */
77 private Timer statusBarClearTimer = new Timer("StatusBar Cleaner", true);
79 /** Object for status bar clearing ticker event. */
80 private TimerTask statusBarClearTimerTask;
82 /** Delay (in seconds) after which to clear status bar. */
83 private int statusBarClearDelay = 5000;
85 /** The content pane. */
86 private JPanel contentPane = new JPanel(new BorderLayout(12, 12));
88 /** The jSite menu. */
89 private I18nMenu jSiteMenu;
92 private I18nMenu nodeMenu;
94 /** The “connect” (advanced mode) menu. */
95 private I18nMenu connectMenu;
97 /** The “connect” (simple mode) menu. */
98 private JMenuItem connectMenuItem;
100 /** The “disconnect” (advanced mode) menu. */
101 private I18nMenu disconnectMenu;
103 /** The “diconnect” (simple mode) menu item. */
104 private JMenuItem disconnectMenuItem;
106 /** The language menu. */
107 private I18nMenu languageMenu;
109 /** The about menu. */
110 private I18nMenu helpMenu;
112 /** The tabbed project pane. */
113 private JTabbedPane projectPane;
115 /** The project overview panel. */
116 private Box projectOverviewPanel;
119 * Creates a new main window that redirects all actions to the given swing
122 * @param swingInterface
123 * The swing interface to receive all actions
125 public MainWindow(SwingInterface swingInterface) {
126 super("jSite " + Version.getVersion());
127 this.swingInterface = swingInterface;
129 setPreferredSize(new Dimension(480, 280));
131 SwingUtils.center(this);
132 I18n.registerI18nable(this);
133 addWindowListener(this);
141 * Sets the text of the status bar.
144 * The text of the status bar
146 public void setStatusBarText(String text) {
147 statusBar.setText(text);
148 synchronized (statusBar) {
149 if (statusBarClearTimerTask != null) {
150 statusBarClearTimerTask.cancel();
152 statusBarClearTimerTask = new TimerTask() {
154 @SuppressWarnings("synthetic-access")
157 statusBar.setText("\u00a0");
161 statusBarClearTimer.schedule(statusBarClearTimerTask, statusBarClearDelay);
166 * Returns the status bar clear delay (in milliseconds).
168 * @return The status bar clear delay
170 public int getStatusBarClearDelay() {
171 return statusBarClearDelay;
175 * Sets the status bar clear delay (in milliseconds).
177 * @param statusBarClearDelay
178 * The status bar clear delay
180 public void setStatusBarClearDelay(int statusBarClearDelay) {
181 this.statusBarClearDelay = statusBarClearDelay;
185 * Sets whether the advanced mode is activated.
187 * @param advancedMode
188 * <code>true</code> if the advanced mode is activated,
189 * <code>false</code> if the simple mode is activated
191 public void setAdvancedMode(boolean advancedMode) {
192 connectMenu.setVisible(advancedMode);
193 connectMenuItem.setVisible(!advancedMode);
194 disconnectMenu.setVisible(advancedMode);
195 disconnectMenuItem.setVisible(!advancedMode);
202 public Container getContentPane() {
207 * Returns the currently selected project.
209 * @return The currently selected project
211 public Project getSelectedProject() {
220 * Refreshes the menu items in the “connect” and “disconnect” menus.
222 void refreshNodeMenuItems() {
223 connectMenu.removeAll();
224 for (Action nodeConnectAction: swingInterface.getNodeConnectActions()) {
225 connectMenu.add(nodeConnectAction);
227 if (connectMenu.getMenuComponentCount() == 0) {
228 JMenuItem noNodeAvailableItem = new JMenuItem(I18n.get("mainWindow.menu.connectNoNodeAvailable.name"));
229 noNodeAvailableItem.setEnabled(false);
230 connectMenu.add(noNodeAvailableItem);
232 disconnectMenu.removeAll();
233 for (Action nodeDisconnectAction: swingInterface.getNodeDisconnectActions()) {
234 disconnectMenu.add(nodeDisconnectAction);
236 if (disconnectMenu.getMenuComponentCount() == 0) {
237 JMenuItem noNodeAvailableItem = new JMenuItem(I18n.get("mainWindow.menu.disconnectNoNodeAvailable.name"));
238 noNodeAvailableItem.setEnabled(false);
239 disconnectMenu.add(noNodeAvailableItem);
244 * Adds a project to the project pane.
248 * @param switchToProject
249 * <code>true</code> to switch to the new panel,
250 * <code>false</code> to not change the current panel
252 void addProject(Project project, boolean switchToProject) {
253 ProjectPanel projectPanel = new ProjectPanel(swingInterface, project);
254 int newTabIndex = projectPane.getTabCount();
255 projectPane.add(project.getName(), projectPanel);
256 projectPane.setToolTipTextAt(newTabIndex, project.getDescription());
257 project.addPropertyChangeListener(this);
258 if (switchToProject) {
259 projectPane.setSelectedIndex(newTabIndex);
268 * Initializes the window by creating all its components.
270 private void initWindow() {
271 JMenuBar menuBar = new JMenuBar();
273 jSiteMenu = new I18nMenu("mainWindow.menu.jSite");
274 menuBar.add(jSiteMenu);
276 jSiteMenu.add(new FixedJMenuItem(swingInterface.getConfigureAction()));
277 jSiteMenu.addSeparator();
278 jSiteMenu.add(new FixedJMenuItem(swingInterface.getImportConfigAction()));
279 jSiteMenu.addSeparator();
280 jSiteMenu.add(new FixedJMenuItem(swingInterface.getQuitAction()));
282 connectMenu = new I18nMenu("mainWindow.menu.connect");
283 disconnectMenu = new I18nMenu("mainWindow.menu.disconnect");
285 nodeMenu = new I18nMenu("mainWindow.menu.node");
286 menuBar.add(nodeMenu);
288 nodeMenu.add(new FixedJMenuItem(swingInterface.getManageNodesAction()));
289 nodeMenu.addSeparator();
290 nodeMenu.add(connectMenuItem = new FixedJMenuItem(swingInterface.getNodeConnectAction()));
291 nodeMenu.add(connectMenu);
292 nodeMenu.add(disconnectMenuItem = new FixedJMenuItem(swingInterface.getNodeDisconnectAction()));
293 nodeMenu.add(disconnectMenu);
294 refreshNodeMenuItems();
296 languageMenu = new I18nMenu("mainWindow.menu.language");
297 menuBar.add(languageMenu);
299 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
300 languageMenu.add(new FixedJMenuItem(languageAction));
303 JPanel spacerPanel = new JPanel();
304 spacerPanel.setOpaque(false);
305 menuBar.add(spacerPanel);
307 helpMenu = new I18nMenu("mainWindow.menu.help");
308 menuBar.add(helpMenu);
310 helpMenu.add(new FixedJMenuItem(swingInterface.getHelpAboutAction()));
312 setJMenuBar(menuBar);
314 JToolBar toolBar = new JToolBar(I18n.get("mainWindow.toolbar.name"));
315 toolBar.add(swingInterface.getManageNodesAction());
316 toolBar.addSeparator();
317 toolBar.add(swingInterface.getNodeConnectAction());
318 toolBar.add(swingInterface.getNodeDisconnectAction());
319 super.getContentPane().add(toolBar, BorderLayout.PAGE_START);
321 super.getContentPane().add(contentPane, BorderLayout.CENTER);
323 addWindowListener(new WindowAdapter() {
328 @SuppressWarnings("synthetic-access")
330 public void windowClosing(WindowEvent windowEvent) {
331 swingInterface.getQuitAction().actionPerformed(null);
339 * Initializes all components of this window.
341 private void initComponents() {
342 super.getContentPane().add(statusBar, BorderLayout.PAGE_END);
344 /* TODO - remove upper panel */
345 JPanel upperPanel = new JPanel(new BorderLayout(12, 12));
346 getContentPane().add(upperPanel, BorderLayout.CENTER);
347 contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
349 projectPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
350 upperPanel.add(projectPane, BorderLayout.CENTER);
352 projectOverviewPanel = new Box(BoxLayout.PAGE_AXIS);
353 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
354 projectPane.add(projectOverviewPanel);
355 projectOverviewPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
356 projectOverviewPanel.add(Box.createVerticalGlue());
357 JButton addProjectButton = new JButton(swingInterface.getAddProjectAction());
358 addProjectButton.setAlignmentX(0.5f);
359 projectOverviewPanel.add(addProjectButton);
360 projectOverviewPanel.add(Box.createVerticalGlue());
362 // JPanel lowerPanel = new JPanel(new BorderLayout(12, 12));
363 // getContentPane().add(lowerPanel, BorderLayout.CENTER);
367 // INTERFACE I18nable
373 public void updateI18n() {
374 swingInterface.getConfigureAction().updateI18n();
375 swingInterface.getImportConfigAction().updateI18n();
376 swingInterface.getQuitAction().updateI18n();
377 swingInterface.getManageNodesAction().updateI18n();
378 swingInterface.getNodeConnectAction().updateI18n();
379 connectMenu.updateI18n();
380 swingInterface.getNodeDisconnectAction().updateI18n();
381 disconnectMenu.updateI18n();
382 swingInterface.getAddProjectAction().updateI18n();
383 swingInterface.getCloneProjectAction().updateI18n();
384 swingInterface.getDeleteProjectAction().updateI18n();
385 swingInterface.getHelpAboutAction().updateI18n();
386 jSiteMenu.updateI18n();
387 nodeMenu.updateI18n();
388 languageMenu.updateI18n();
389 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
390 languageAction.updateI18n();
392 helpMenu.updateI18n();
393 getJMenuBar().revalidate();
394 projectPane.setTitleAt(0, I18n.get("mainWindow.pane.overview.title"));
395 for (int componentIndex = 0; componentIndex < projectPane.getTabCount(); componentIndex++) {
396 Component tabComponent = projectPane.getComponentAt(componentIndex);
397 if (tabComponent instanceof ProjectPanel) {
398 ((ProjectPanel) tabComponent).updateI18n();
401 refreshNodeMenuItems();
402 SwingUtils.repackCentered(this);
406 // INTERFACE WindowListener
412 public void windowActivated(WindowEvent e) {
419 public void windowClosed(WindowEvent e) {
426 public void windowClosing(WindowEvent e) {
427 swingInterface.getQuitAction().actionPerformed(null);
433 public void windowDeactivated(WindowEvent e) {
440 public void windowDeiconified(WindowEvent e) {
447 public void windowIconified(WindowEvent e) {
454 public void windowOpened(WindowEvent e) {
459 // INTERFACE PropertyChangeListener
463 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
465 public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
466 Object eventSource = propertyChangeEvent.getSource();
467 String propertyName = propertyChangeEvent.getPropertyName();
468 if (eventSource instanceof Project) {
469 /* if a project was changed, update the tab title and tooltip. */
470 if (Project.PROPERTY_NAME.equals(propertyName) || Project.PROPERTY_DESCRIPTION.equals(propertyName)) {
471 Project project = (Project) eventSource;
472 int tabCount = projectPane.getTabCount();
473 for (int tabIndex = 0; tabIndex < tabCount; tabIndex++) {
474 Component tabComponent = projectPane.getComponentAt(tabIndex);
475 if (tabComponent instanceof ProjectPanel) {
476 Project tabProject = ((ProjectPanel) tabComponent).getProject();
477 if (tabProject.equals(project)) {
478 projectPane.setTitleAt(tabIndex, project.getName());
479 projectPane.setToolTipTextAt(tabIndex, project.getDescription());
480 projectPane.repaint();