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>
68 public class MainWindow extends JFrame implements WindowListener, I18nable, PropertyChangeListener {
71 @SuppressWarnings("unused")
72 private static final Logger logger = Logging.getLogger(MainWindow.class.getName());
74 /** The swing interface that receives all actions. */
75 private final SwingInterface swingInterface;
77 /** The status bar. */
78 private StatusBar statusBar = new StatusBar();
80 /** Timer for clearing the status bar. */
81 private Timer statusBarClearTimer = new Timer("StatusBar Cleaner", true);
83 /** Object for status bar clearing ticker event. */
84 private TimerTask statusBarClearTimerTask;
86 /** Delay (in seconds) after which to clear status bar. */
87 private int statusBarClearDelay = 5000;
89 /** The icon for offline nodes. */
90 private Icon offlineIcon;
92 /** The icon for online nodes. */
93 private Icon onlineIcon;
95 /** The icon for error nodes. */
96 private Icon errorIcon;
98 /** The content pane. */
99 private JPanel contentPane = new JPanel(new BorderLayout(12, 12));
101 /** The jSite menu. */
102 private I18nMenu jSiteMenu;
104 /** The node menu. */
105 private I18nMenu nodeMenu;
107 /** The language menu. */
108 private I18nMenu languageMenu;
110 /** The about menu. */
111 private I18nMenu helpMenu;
113 /** The tabbed project pane. */
114 private JTabbedPane projectPane;
116 /** The project overview panel. */
117 private Box projectOverviewPanel;
119 /** Maps from node to menus. */
120 private final Map<Node, JMenu> nodeMenus = new HashMap<Node, JMenu>();
122 /** Maps from nodes to node panels. */
123 private final Map<Node, NodeLabel> nodeLabels = new HashMap<Node, NodeLabel>();
126 * Creates a new main window that redirects all actions to the given swing
129 * @param swingInterface
130 * The swing interface to receive all actions
132 public MainWindow(SwingInterface swingInterface) {
133 super("jSite " + Version.getVersion());
134 this.swingInterface = swingInterface;
136 setPreferredSize(new Dimension(480, 280));
138 SwingUtils.center(this);
139 I18n.registerI18nable(this);
140 addWindowListener(this);
148 * Sets the text of the status bar.
151 * The text of the status bar
153 public void setStatusBarText(String text) {
154 statusBar.setText(text);
155 synchronized (statusBar) {
156 if (statusBarClearTimerTask != null) {
157 statusBarClearTimerTask.cancel();
159 statusBarClearTimerTask = new TimerTask() {
161 @SuppressWarnings("synthetic-access")
164 statusBar.setText("\u00a0");
168 statusBarClearTimer.schedule(statusBarClearTimerTask, statusBarClearDelay);
173 * Returns the status bar clear delay (in milliseconds).
175 * @return The status bar clear delay
177 public int getStatusBarClearDelay() {
178 return statusBarClearDelay;
182 * Sets the status bar clear delay (in milliseconds).
184 * @param statusBarClearDelay
185 * The status bar clear delay
187 public void setStatusBarClearDelay(int statusBarClearDelay) {
188 this.statusBarClearDelay = statusBarClearDelay;
192 * Sets whether the advanced mode is activated.
194 * @param advancedMode
195 * <code>true</code> if the advanced mode is activated,
196 * <code>false</code> if the simple mode is activated
198 public void setAdvancedMode(boolean advancedMode) {
199 /* doesn’t do anything right now. */
206 public Container getContentPane() {
211 * Returns the currently selected project.
213 * @return The currently selected project
215 public Project getSelectedProject() {
224 * Adds a node to the menu.
229 void addNode(Node node) {
230 JMenu newNodeMenu = new JMenu(node.getName());
231 nodeMenus.put(node, newNodeMenu);
232 newNodeMenu.add(swingInterface.getNodeConnectAction(node));
233 newNodeMenu.add(swingInterface.getNodeDisconnectAction(node));
234 newNodeMenu.addSeparator();
235 newNodeMenu.add(swingInterface.getNodeEditAction(node));
236 newNodeMenu.add(swingInterface.getNodeDeleteAction(node));
237 nodeMenu.add(newNodeMenu);
238 NodeLabel nodeLabel = new NodeLabel(swingInterface, node, onlineIcon, offlineIcon, errorIcon);
239 nodeLabels.put(node, nodeLabel);
240 statusBar.addSideComponent(nodeLabel);
241 node.addPropertyChangeListener(this);
245 * Removes a node from the menu.
250 void removeNode(Node node) {
251 nodeMenu.remove(nodeMenus.remove(node));
252 statusBar.removeSideComponent(nodeLabels.remove(node));
253 node.removePropertyChangeListener(this);
257 * Adds a project to the project pane.
261 * @param switchToProject
262 * <code>true</code> to switch to the new panel,
263 * <code>false</code> to not change the current panel
265 void addProject(Project project, boolean switchToProject) {
266 ProjectPanel projectPanel = new ProjectPanel(swingInterface, project);
267 int newTabIndex = projectPane.getTabCount();
268 projectPane.add(project.getName(), projectPanel);
269 projectPane.setToolTipTextAt(newTabIndex, project.getDescription());
270 project.addPropertyChangeListener(this);
271 if (switchToProject) {
272 projectPane.setSelectedIndex(newTabIndex);
279 void projectInsertStarted(Project project) {
280 int projectIndex = getProjectIndex(project);
281 if (projectIndex == -1) {
284 projectPane.setTitleAt(projectIndex, I18n.get("projectPanel.title.starting", project.getName()));
290 * @param requiredBlocks
291 * @param successfulBlocks
292 * @param failedBlocks
293 * @param fatallyFailedBlocks
294 * @param finalizedTotal
296 void projectInsertProgressed(Project project, int totalBlocks, int requiredBlocks, int successfulBlocks, int failedBlocks, int fatallyFailedBlocks, boolean finalizedTotal) {
297 int projectIndex = getProjectIndex(project);
298 if (projectIndex == -1) {
301 projectPane.setTitleAt(projectIndex, I18n.get("projectPanel.title.progress", project.getName(), requiredBlocks / (double) successfulBlocks));
307 void projectInsertGeneratedURI(Project project) {
308 /* TODO - update panel. */
315 void projectInsertFinished(Project project, boolean success) {
316 int projectIndex = getProjectIndex(project);
317 if (projectIndex == -1) {
320 projectPane.setTitleAt(projectIndex, project.getName());
328 * Returns the index of the project panel that contains the given project.
332 * @return The index of {@link #projectPane}’s tab that contains the given
333 * project, or <code>-1</code> if the project can not be found
335 private int getProjectIndex(Project project) {
336 int tabCount = projectPane.getTabCount();
337 for (int tabIndex = 1; tabIndex < tabCount; tabIndex++) {
338 Component tabComponent = projectPane.getComponentAt(tabIndex);
339 if (tabComponent instanceof ProjectPanel) {
340 if (((ProjectPanel) tabComponent).getProject() == project) {
349 * Initializes the window by creating all its components.
351 private void initWindow() {
352 onlineIcon = IconLoader.loadIcon("/node-online.png");
353 offlineIcon = IconLoader.loadIcon("/node-offline.png");
354 errorIcon = IconLoader.loadIcon("/node-error.png");
356 JMenuBar menuBar = new JMenuBar();
358 jSiteMenu = new I18nMenu("mainWindow.menu.jSite");
359 menuBar.add(jSiteMenu);
361 jSiteMenu.add(new FixedJMenuItem(swingInterface.getConfigureAction()));
362 jSiteMenu.addSeparator();
363 jSiteMenu.add(new FixedJMenuItem(swingInterface.getImportConfigAction()));
364 jSiteMenu.addSeparator();
365 jSiteMenu.add(new FixedJMenuItem(swingInterface.getQuitAction()));
367 nodeMenu = new I18nMenu("mainWindow.menu.node");
368 menuBar.add(nodeMenu);
370 nodeMenu.add(new FixedJMenuItem(swingInterface.getAddNodeAction()));
371 nodeMenu.addSeparator();
373 languageMenu = new I18nMenu("mainWindow.menu.language");
374 menuBar.add(languageMenu);
376 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
377 languageMenu.add(new FixedJMenuItem(languageAction));
380 menuBar.add(Box.createHorizontalGlue());
382 helpMenu = new I18nMenu("mainWindow.menu.help");
383 menuBar.add(helpMenu);
385 helpMenu.add(new FixedJMenuItem(swingInterface.getHelpAboutAction()));
387 setJMenuBar(menuBar);
389 JToolBar toolBar = new JToolBar(I18n.get("mainWindow.toolbar.name"));
390 toolBar.add(swingInterface.getAddNodeAction());
391 toolBar.addSeparator();
392 toolBar.add(swingInterface.getQuitAction());
393 super.getContentPane().add(toolBar, BorderLayout.PAGE_START);
395 super.getContentPane().add(contentPane, BorderLayout.CENTER);
397 addWindowListener(new WindowAdapter() {
402 @SuppressWarnings("synthetic-access")
404 public void windowClosing(WindowEvent windowEvent) {
405 swingInterface.getQuitAction().actionPerformed(null);
413 * Initializes all components of this window.
415 private void initComponents() {
416 super.getContentPane().add(statusBar, BorderLayout.PAGE_END);
417 contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
419 projectPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
420 contentPane.add(projectPane, BorderLayout.CENTER);
422 projectOverviewPanel = new Box(BoxLayout.PAGE_AXIS);
423 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
424 projectPane.add(projectOverviewPanel);
425 projectOverviewPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
426 projectOverviewPanel.add(Box.createVerticalGlue());
427 JButton addProjectButton = new JButton(swingInterface.getAddProjectAction());
428 addProjectButton.setAlignmentX(0.5f);
429 projectOverviewPanel.add(addProjectButton);
430 projectOverviewPanel.add(Box.createVerticalGlue());
434 // INTERFACE I18nable
440 public void updateI18n() {
441 swingInterface.getConfigureAction().updateI18n();
442 swingInterface.getImportConfigAction().updateI18n();
443 swingInterface.getQuitAction().updateI18n();
444 swingInterface.getAddNodeAction().updateI18n();
445 swingInterface.getAddProjectAction().updateI18n();
446 swingInterface.getCloneProjectAction().updateI18n();
447 swingInterface.getDeleteProjectAction().updateI18n();
448 swingInterface.getHelpAboutAction().updateI18n();
449 jSiteMenu.updateI18n();
450 nodeMenu.updateI18n();
451 languageMenu.updateI18n();
452 for (Node node: swingInterface.getNodes()) {
453 swingInterface.getNodeConnectAction(node).updateI18n();
454 swingInterface.getNodeDisconnectAction(node).updateI18n();
455 swingInterface.getNodeEditAction(node).updateI18n();
456 swingInterface.getNodeDeleteAction(node).updateI18n();
458 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
459 languageAction.updateI18n();
461 helpMenu.updateI18n();
462 getJMenuBar().revalidate();
463 projectPane.setTitleAt(0, I18n.get("mainWindow.pane.overview.title"));
464 for (int componentIndex = 0; componentIndex < projectPane.getTabCount(); componentIndex++) {
465 Component tabComponent = projectPane.getComponentAt(componentIndex);
466 if (tabComponent instanceof ProjectPanel) {
467 ((ProjectPanel) tabComponent).updateI18n();
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();
553 } else if (eventSource instanceof Node) {
554 if (propertyName.equals(Node.PROPERTY_NAME)) {
555 Node changedNode = (Node) eventSource;
556 nodeMenus.get(changedNode).setText(changedNode.getName());