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);
266 void projectInsertStarted(Project project) {
267 int projectIndex = getProjectIndex(project);
268 if (projectIndex == -1) {
271 projectPane.setTitleAt(projectIndex, I18n.get("projectPanel.title.starting", project.getName()));
277 * @param requiredBlocks
278 * @param successfulBlocks
279 * @param failedBlocks
280 * @param fatallyFailedBlocks
281 * @param finalizedTotal
283 void projectInsertProgressed(Project project, int totalBlocks, int requiredBlocks, int successfulBlocks, int failedBlocks, int fatallyFailedBlocks, boolean finalizedTotal) {
284 int projectIndex = getProjectIndex(project);
285 if (projectIndex == -1) {
288 projectPane.setTitleAt(projectIndex, I18n.get("projectPanel.title.progress", project.getName(), requiredBlocks / (double) successfulBlocks));
294 void projectInsertGeneratedURI(Project project) {
295 /* TODO - update panel. */
302 void projectInsertFinished(Project project, boolean success) {
303 int projectIndex = getProjectIndex(project);
304 if (projectIndex == -1) {
307 projectPane.setTitleAt(projectIndex, project.getName());
315 * Returns the index of the project panel that contains the given project.
319 * @return The index of {@link #projectPane}’s tab that contains the given
320 * project, or <code>-1</code> if the project can not be found
322 private int getProjectIndex(Project project) {
323 int tabCount = projectPane.getTabCount();
324 for (int tabIndex = 1; tabIndex < tabCount; tabIndex++) {
325 Component tabComponent = projectPane.getComponentAt(tabIndex);
326 if (tabComponent instanceof ProjectPanel) {
327 if (((ProjectPanel) tabComponent).getProject() == project) {
336 * Initializes the window by creating all its components.
338 private void initWindow() {
339 JMenuBar menuBar = new JMenuBar();
341 jSiteMenu = new I18nMenu("mainWindow.menu.jSite");
342 menuBar.add(jSiteMenu);
344 jSiteMenu.add(new FixedJMenuItem(swingInterface.getConfigureAction()));
345 jSiteMenu.addSeparator();
346 jSiteMenu.add(new FixedJMenuItem(swingInterface.getImportConfigAction()));
347 jSiteMenu.addSeparator();
348 jSiteMenu.add(new FixedJMenuItem(swingInterface.getQuitAction()));
350 connectMenu = new I18nMenu("mainWindow.menu.connect");
351 disconnectMenu = new I18nMenu("mainWindow.menu.disconnect");
353 nodeMenu = new I18nMenu("mainWindow.menu.node");
354 menuBar.add(nodeMenu);
356 nodeMenu.add(new FixedJMenuItem(swingInterface.getManageNodesAction()));
357 nodeMenu.addSeparator();
358 nodeMenu.add(connectMenuItem = new FixedJMenuItem(swingInterface.getNodeConnectAction()));
359 nodeMenu.add(connectMenu);
360 nodeMenu.add(disconnectMenuItem = new FixedJMenuItem(swingInterface.getNodeDisconnectAction()));
361 nodeMenu.add(disconnectMenu);
362 refreshNodeMenuItems();
364 languageMenu = new I18nMenu("mainWindow.menu.language");
365 menuBar.add(languageMenu);
367 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
368 languageMenu.add(new FixedJMenuItem(languageAction));
371 JPanel spacerPanel = new JPanel();
372 spacerPanel.setOpaque(false);
373 menuBar.add(spacerPanel);
375 helpMenu = new I18nMenu("mainWindow.menu.help");
376 menuBar.add(helpMenu);
378 helpMenu.add(new FixedJMenuItem(swingInterface.getHelpAboutAction()));
380 setJMenuBar(menuBar);
382 JToolBar toolBar = new JToolBar(I18n.get("mainWindow.toolbar.name"));
383 toolBar.add(swingInterface.getManageNodesAction());
384 toolBar.addSeparator();
385 toolBar.add(swingInterface.getNodeConnectAction());
386 toolBar.add(swingInterface.getNodeDisconnectAction());
387 super.getContentPane().add(toolBar, BorderLayout.PAGE_START);
389 super.getContentPane().add(contentPane, BorderLayout.CENTER);
391 addWindowListener(new WindowAdapter() {
396 @SuppressWarnings("synthetic-access")
398 public void windowClosing(WindowEvent windowEvent) {
399 swingInterface.getQuitAction().actionPerformed(null);
407 * Initializes all components of this window.
409 private void initComponents() {
410 super.getContentPane().add(statusBar, BorderLayout.PAGE_END);
412 /* TODO - remove upper panel */
413 JPanel upperPanel = new JPanel(new BorderLayout(12, 12));
414 getContentPane().add(upperPanel, BorderLayout.CENTER);
415 contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
417 projectPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
418 upperPanel.add(projectPane, BorderLayout.CENTER);
420 projectOverviewPanel = new Box(BoxLayout.PAGE_AXIS);
421 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
422 projectPane.add(projectOverviewPanel);
423 projectOverviewPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
424 projectOverviewPanel.add(Box.createVerticalGlue());
425 JButton addProjectButton = new JButton(swingInterface.getAddProjectAction());
426 addProjectButton.setAlignmentX(0.5f);
427 projectOverviewPanel.add(addProjectButton);
428 projectOverviewPanel.add(Box.createVerticalGlue());
430 // JPanel lowerPanel = new JPanel(new BorderLayout(12, 12));
431 // getContentPane().add(lowerPanel, BorderLayout.CENTER);
435 // INTERFACE I18nable
441 public void updateI18n() {
442 swingInterface.getConfigureAction().updateI18n();
443 swingInterface.getImportConfigAction().updateI18n();
444 swingInterface.getQuitAction().updateI18n();
445 swingInterface.getManageNodesAction().updateI18n();
446 swingInterface.getNodeConnectAction().updateI18n();
447 connectMenu.updateI18n();
448 swingInterface.getNodeDisconnectAction().updateI18n();
449 disconnectMenu.updateI18n();
450 swingInterface.getAddProjectAction().updateI18n();
451 swingInterface.getCloneProjectAction().updateI18n();
452 swingInterface.getDeleteProjectAction().updateI18n();
453 swingInterface.getHelpAboutAction().updateI18n();
454 jSiteMenu.updateI18n();
455 nodeMenu.updateI18n();
456 languageMenu.updateI18n();
457 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
458 languageAction.updateI18n();
460 helpMenu.updateI18n();
461 getJMenuBar().revalidate();
462 projectPane.setTitleAt(0, I18n.get("mainWindow.pane.overview.title"));
463 for (int componentIndex = 0; componentIndex < projectPane.getTabCount(); componentIndex++) {
464 Component tabComponent = projectPane.getComponentAt(componentIndex);
465 if (tabComponent instanceof ProjectPanel) {
466 ((ProjectPanel) tabComponent).updateI18n();
469 refreshNodeMenuItems();
470 SwingUtils.repackCentered(this);
474 // INTERFACE WindowListener
480 public void windowActivated(WindowEvent e) {
487 public void windowClosed(WindowEvent e) {
494 public void windowClosing(WindowEvent e) {
495 swingInterface.getQuitAction().actionPerformed(null);
501 public void windowDeactivated(WindowEvent e) {
508 public void windowDeiconified(WindowEvent e) {
515 public void windowIconified(WindowEvent e) {
522 public void windowOpened(WindowEvent e) {
527 // INTERFACE PropertyChangeListener
531 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
533 public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
534 Object eventSource = propertyChangeEvent.getSource();
535 String propertyName = propertyChangeEvent.getPropertyName();
536 if (eventSource instanceof Project) {
537 /* if a project was changed, update the tab title and tooltip. */
538 if (Project.PROPERTY_NAME.equals(propertyName) || Project.PROPERTY_DESCRIPTION.equals(propertyName)) {
539 Project project = (Project) eventSource;
540 int tabCount = projectPane.getTabCount();
541 for (int tabIndex = 0; tabIndex < tabCount; tabIndex++) {
542 Component tabComponent = projectPane.getComponentAt(tabIndex);
543 if (tabComponent instanceof ProjectPanel) {
544 Project tabProject = ((ProjectPanel) tabComponent).getProject();
545 if (tabProject.equals(project)) {
546 projectPane.setTitleAt(tabIndex, project.getName());
547 projectPane.setToolTipTextAt(tabIndex, project.getDescription());
548 projectPane.repaint();