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.HashMap;
33 import java.util.Timer;
34 import java.util.TimerTask;
35 import java.util.logging.Logger;
37 import javax.swing.Box;
38 import javax.swing.BoxLayout;
39 import javax.swing.Icon;
40 import javax.swing.JButton;
41 import javax.swing.JFrame;
42 import javax.swing.JMenu;
43 import javax.swing.JMenuBar;
44 import javax.swing.JPanel;
45 import javax.swing.JTabbedPane;
46 import javax.swing.JToolBar;
47 import javax.swing.SwingConstants;
48 import javax.swing.border.EmptyBorder;
50 import net.pterodactylus.jsite.core.Node;
51 import net.pterodactylus.jsite.i18n.I18n;
52 import net.pterodactylus.jsite.i18n.I18nable;
53 import net.pterodactylus.jsite.i18n.gui.I18nAction;
54 import net.pterodactylus.jsite.i18n.gui.I18nMenu;
55 import net.pterodactylus.jsite.main.Version;
56 import net.pterodactylus.jsite.project.Project;
57 import net.pterodactylus.util.image.IconLoader;
58 import net.pterodactylus.util.logging.Logging;
59 import net.pterodactylus.util.swing.StatusBar;
60 import net.pterodactylus.util.swing.SwingUtils;
63 * Defines the main window of the application.
65 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
67 public class MainWindow extends JFrame implements WindowListener, I18nable, PropertyChangeListener {
70 @SuppressWarnings("unused")
71 private static final Logger logger = Logging.getLogger(MainWindow.class.getName());
73 /** The swing interface that receives all actions. */
74 private final SwingInterface swingInterface;
76 /** The status bar. */
77 private StatusBar statusBar = new StatusBar();
79 /** Timer for clearing the status bar. */
80 private Timer statusBarClearTimer = new Timer("StatusBar Cleaner", true);
82 /** Object for status bar clearing ticker event. */
83 private TimerTask statusBarClearTimerTask;
85 /** Delay (in seconds) after which to clear status bar. */
86 private int statusBarClearDelay = 5000;
88 /** The icon for offline nodes. */
89 private Icon offlineIcon;
91 /** The icon for online nodes. */
92 private Icon onlineIcon;
94 /** The icon for error nodes. */
95 private Icon errorIcon;
97 /** The content pane. */
98 private JPanel contentPane = new JPanel(new BorderLayout(12, 12));
100 /** The jSite menu. */
101 private I18nMenu jSiteMenu;
103 /** The node menu. */
104 private I18nMenu nodeMenu;
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;
118 /** Maps from node to menus. */
119 private final Map<Node, JMenu> nodeMenus = new HashMap<Node, JMenu>();
121 /** Maps from nodes to node panels. */
122 private final Map<Node, NodeLabel> nodeLabels = new HashMap<Node, NodeLabel>();
125 * Creates a new main window that redirects all actions to the given swing
128 * @param swingInterface
129 * The swing interface to receive all actions
131 public MainWindow(SwingInterface swingInterface) {
132 super("jSite " + Version.getVersion());
133 this.swingInterface = swingInterface;
135 setPreferredSize(new Dimension(480, 280));
137 SwingUtils.center(this);
138 I18n.registerI18nable(this);
139 addWindowListener(this);
147 * Sets the text of the status bar.
150 * The text of the status bar
152 public void setStatusBarText(String text) {
153 statusBar.setText(text);
154 synchronized (statusBar) {
155 if (statusBarClearTimerTask != null) {
156 statusBarClearTimerTask.cancel();
158 statusBarClearTimerTask = new TimerTask() {
160 @SuppressWarnings("synthetic-access")
163 statusBar.setText("\u00a0");
167 statusBarClearTimer.schedule(statusBarClearTimerTask, statusBarClearDelay);
172 * Returns the status bar clear delay (in milliseconds).
174 * @return The status bar clear delay
176 public int getStatusBarClearDelay() {
177 return statusBarClearDelay;
181 * Sets the status bar clear delay (in milliseconds).
183 * @param statusBarClearDelay
184 * The status bar clear delay
186 public void setStatusBarClearDelay(int statusBarClearDelay) {
187 this.statusBarClearDelay = statusBarClearDelay;
191 * Sets whether the advanced mode is activated.
193 * @param advancedMode
194 * <code>true</code> if the advanced mode is activated,
195 * <code>false</code> if the simple mode is activated
197 public void setAdvancedMode(boolean advancedMode) {
198 /* doesn’t do anything right now. */
205 public Container getContentPane() {
210 * Returns the currently selected project.
212 * @return The currently selected project
214 public Project getSelectedProject() {
223 * Adds a node to the menu.
228 void addNode(Node node) {
229 JMenu newNodeMenu = new JMenu(node.getName());
230 nodeMenus.put(node, newNodeMenu);
231 newNodeMenu.add(swingInterface.getNodeConnectAction(node));
232 newNodeMenu.add(swingInterface.getNodeDisconnectAction(node));
233 newNodeMenu.addSeparator();
234 newNodeMenu.add(swingInterface.getNodeEditAction(node));
235 newNodeMenu.add(swingInterface.getNodeDeleteAction(node));
236 nodeMenu.add(newNodeMenu);
237 NodeLabel nodeLabel = new NodeLabel(swingInterface, node, onlineIcon, offlineIcon, errorIcon);
238 nodeLabels.put(node, nodeLabel);
239 statusBar.addSideComponent(nodeLabel);
240 node.addPropertyChangeListener(this);
244 * Removes a node from the menu.
249 void removeNode(Node node) {
250 nodeMenu.remove(nodeMenus.remove(node));
251 statusBar.removeSideComponent(nodeLabels.remove(node));
252 node.removePropertyChangeListener(this);
256 * Adds a project to the project pane.
260 * @param switchToProject
261 * <code>true</code> to switch to the new panel,
262 * <code>false</code> to not change the current panel
264 void addProject(Project project, boolean switchToProject) {
265 ProjectPanel projectPanel = new ProjectPanel(swingInterface, project);
266 int newTabIndex = projectPane.getTabCount();
267 projectPane.add(project.getName(), projectPanel);
268 projectPane.setToolTipTextAt(newTabIndex, project.getDescription());
269 project.addPropertyChangeListener(this);
270 if (switchToProject) {
271 projectPane.setSelectedIndex(newTabIndex);
278 void projectInsertStarted(Project project) {
279 int projectIndex = getProjectIndex(project);
280 if (projectIndex == -1) {
283 projectPane.setTitleAt(projectIndex, I18n.get("projectPanel.title.starting", project.getName()));
289 * @param requiredBlocks
290 * @param successfulBlocks
291 * @param failedBlocks
292 * @param fatallyFailedBlocks
293 * @param finalizedTotal
295 void projectInsertProgressed(Project project, int totalBlocks, int requiredBlocks, int successfulBlocks, int failedBlocks, int fatallyFailedBlocks, boolean finalizedTotal) {
296 int projectIndex = getProjectIndex(project);
297 if (projectIndex == -1) {
300 projectPane.setTitleAt(projectIndex, I18n.get("projectPanel.title.progress", project.getName(), requiredBlocks / (double) successfulBlocks));
306 void projectInsertGeneratedURI(Project project) {
307 /* TODO - update panel. */
314 void projectInsertFinished(Project project, boolean success) {
315 int projectIndex = getProjectIndex(project);
316 if (projectIndex == -1) {
319 projectPane.setTitleAt(projectIndex, project.getName());
327 * Returns the index of the project panel that contains the given project.
331 * @return The index of {@link #projectPane}’s tab that contains the given
332 * project, or <code>-1</code> if the project can not be found
334 private int getProjectIndex(Project project) {
335 int tabCount = projectPane.getTabCount();
336 for (int tabIndex = 1; tabIndex < tabCount; tabIndex++) {
337 Component tabComponent = projectPane.getComponentAt(tabIndex);
338 if (tabComponent instanceof ProjectPanel) {
339 if (((ProjectPanel) tabComponent).getProject() == project) {
348 * Initializes the window by creating all its components.
350 private void initWindow() {
351 onlineIcon = IconLoader.loadIcon("/node-online.png");
352 offlineIcon = IconLoader.loadIcon("/node-offline.png");
353 errorIcon = IconLoader.loadIcon("/node-error.png");
355 JMenuBar menuBar = new JMenuBar();
357 jSiteMenu = new I18nMenu("mainWindow.menu.jSite");
358 menuBar.add(jSiteMenu);
360 jSiteMenu.add(new FixedJMenuItem(swingInterface.getConfigureAction()));
361 jSiteMenu.addSeparator();
362 jSiteMenu.add(new FixedJMenuItem(swingInterface.getImportConfigAction()));
363 jSiteMenu.addSeparator();
364 jSiteMenu.add(new FixedJMenuItem(swingInterface.getQuitAction()));
366 nodeMenu = new I18nMenu("mainWindow.menu.node");
367 menuBar.add(nodeMenu);
369 nodeMenu.add(new FixedJMenuItem(swingInterface.getAddNodeAction()));
370 nodeMenu.addSeparator();
372 languageMenu = new I18nMenu("mainWindow.menu.language");
373 menuBar.add(languageMenu);
375 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
376 languageMenu.add(new FixedJMenuItem(languageAction));
379 menuBar.add(Box.createHorizontalGlue());
381 helpMenu = new I18nMenu("mainWindow.menu.help");
382 menuBar.add(helpMenu);
384 helpMenu.add(new FixedJMenuItem(swingInterface.getHelpAboutAction()));
386 setJMenuBar(menuBar);
388 JToolBar toolBar = new JToolBar(I18n.get("mainWindow.toolbar.name"));
389 toolBar.add(swingInterface.getAddNodeAction());
390 toolBar.addSeparator();
391 toolBar.add(swingInterface.getQuitAction());
392 super.getContentPane().add(toolBar, BorderLayout.PAGE_START);
394 super.getContentPane().add(contentPane, BorderLayout.CENTER);
396 addWindowListener(new WindowAdapter() {
401 @SuppressWarnings("synthetic-access")
403 public void windowClosing(WindowEvent windowEvent) {
404 swingInterface.getQuitAction().actionPerformed(null);
412 * Initializes all components of this window.
414 private void initComponents() {
415 super.getContentPane().add(statusBar, BorderLayout.PAGE_END);
416 contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
418 projectPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
419 contentPane.add(projectPane, BorderLayout.CENTER);
421 projectOverviewPanel = new Box(BoxLayout.PAGE_AXIS);
422 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
423 projectPane.add(projectOverviewPanel);
424 projectOverviewPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
425 projectOverviewPanel.add(Box.createVerticalGlue());
426 JButton addProjectButton = new JButton(swingInterface.getAddProjectAction());
427 addProjectButton.setAlignmentX(0.5f);
428 projectOverviewPanel.add(addProjectButton);
429 projectOverviewPanel.add(Box.createVerticalGlue());
433 // INTERFACE I18nable
439 public void updateI18n() {
440 swingInterface.getConfigureAction().updateI18n();
441 swingInterface.getImportConfigAction().updateI18n();
442 swingInterface.getQuitAction().updateI18n();
443 swingInterface.getAddNodeAction().updateI18n();
444 swingInterface.getAddProjectAction().updateI18n();
445 swingInterface.getCloneProjectAction().updateI18n();
446 swingInterface.getDeleteProjectAction().updateI18n();
447 swingInterface.getHelpAboutAction().updateI18n();
448 jSiteMenu.updateI18n();
449 nodeMenu.updateI18n();
450 languageMenu.updateI18n();
451 for (Node node: swingInterface.getNodes()) {
452 swingInterface.getNodeConnectAction(node).updateI18n();
453 swingInterface.getNodeDisconnectAction(node).updateI18n();
454 swingInterface.getNodeEditAction(node).updateI18n();
455 swingInterface.getNodeDeleteAction(node).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 SwingUtils.repackCentered(this);
473 // INTERFACE WindowListener
479 public void windowActivated(WindowEvent e) {
486 public void windowClosed(WindowEvent e) {
493 public void windowClosing(WindowEvent e) {
494 swingInterface.getQuitAction().actionPerformed(null);
500 public void windowDeactivated(WindowEvent e) {
507 public void windowDeiconified(WindowEvent e) {
514 public void windowIconified(WindowEvent e) {
521 public void windowOpened(WindowEvent e) {
526 // INTERFACE PropertyChangeListener
530 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
532 public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
533 Object eventSource = propertyChangeEvent.getSource();
534 String propertyName = propertyChangeEvent.getPropertyName();
535 if (eventSource instanceof Project) {
536 /* if a project was changed, update the tab title and tooltip. */
537 if (Project.PROPERTY_NAME.equals(propertyName) || Project.PROPERTY_DESCRIPTION.equals(propertyName)) {
538 Project project = (Project) eventSource;
539 int tabCount = projectPane.getTabCount();
540 for (int tabIndex = 0; tabIndex < tabCount; tabIndex++) {
541 Component tabComponent = projectPane.getComponentAt(tabIndex);
542 if (tabComponent instanceof ProjectPanel) {
543 Project tabProject = ((ProjectPanel) tabComponent).getProject();
544 if (tabProject.equals(project)) {
545 projectPane.setTitleAt(tabIndex, project.getName());
546 projectPane.setToolTipTextAt(tabIndex, project.getDescription());
547 projectPane.repaint();
552 } else if (eventSource instanceof Node) {
553 if (propertyName.equals(Node.PROPERTY_NAME)) {
554 Node changedNode = (Node) eventSource;
555 nodeMenus.get(changedNode).setText(changedNode.getName());