react to node name changes
[jSite2.git] / src / net / pterodactylus / jsite / gui / MainWindow.java
1 /*
2  * jSite2 - MainWindow.java -
3  * Copyright © 2008 David Roden
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 package net.pterodactylus.jsite.gui;
21
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;
32 import java.util.Map;
33 import java.util.Timer;
34 import java.util.TimerTask;
35 import java.util.logging.Logger;
36
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;
49
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;
61
62 /**
63  * Defines the main window of the application.
64  * 
65  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
66  * @version $Id$
67  */
68 public class MainWindow extends JFrame implements WindowListener, I18nable, PropertyChangeListener {
69
70         /** Logger. */
71         @SuppressWarnings("unused")
72         private static final Logger logger = Logging.getLogger(MainWindow.class.getName());
73
74         /** The swing interface that receives all actions. */
75         private final SwingInterface swingInterface;
76
77         /** The status bar. */
78         private StatusBar statusBar = new StatusBar();
79
80         /** Timer for clearing the status bar. */
81         private Timer statusBarClearTimer = new Timer("StatusBar Cleaner", true);
82
83         /** Object for status bar clearing ticker event. */
84         private TimerTask statusBarClearTimerTask;
85
86         /** Delay (in seconds) after which to clear status bar. */
87         private int statusBarClearDelay = 5000;
88
89         /** The icon for offline nodes. */
90         private Icon offlineIcon;
91
92         /** The icon for online nodes. */
93         private Icon onlineIcon;
94
95         /** The icon for error nodes. */
96         private Icon errorIcon;
97
98         /** The content pane. */
99         private JPanel contentPane = new JPanel(new BorderLayout(12, 12));
100
101         /** The jSite menu. */
102         private I18nMenu jSiteMenu;
103
104         /** The node menu. */
105         private I18nMenu nodeMenu;
106
107         /** The language menu. */
108         private I18nMenu languageMenu;
109
110         /** The about menu. */
111         private I18nMenu helpMenu;
112
113         /** The tabbed project pane. */
114         private JTabbedPane projectPane;
115
116         /** The project overview panel. */
117         private Box projectOverviewPanel;
118
119         /** Maps from node to menus. */
120         private final Map<Node, JMenu> nodeMenus = new HashMap<Node, JMenu>();
121
122         /** Maps from nodes to node panels. */
123         private final Map<Node, NodeLabel> nodeLabels = new HashMap<Node, NodeLabel>();
124
125         /**
126          * Creates a new main window that redirects all actions to the given swing
127          * interface.
128          * 
129          * @param swingInterface
130          *            The swing interface to receive all actions
131          */
132         public MainWindow(SwingInterface swingInterface) {
133                 super("jSite " + Version.getVersion());
134                 this.swingInterface = swingInterface;
135                 initWindow();
136                 setPreferredSize(new Dimension(480, 280));
137                 pack();
138                 SwingUtils.center(this);
139                 I18n.registerI18nable(this);
140                 addWindowListener(this);
141         }
142
143         //
144         // ACCESSORS
145         //
146
147         /**
148          * Sets the text of the status bar.
149          * 
150          * @param text
151          *            The text of the status bar
152          */
153         public void setStatusBarText(String text) {
154                 statusBar.setText(text);
155                 synchronized (statusBar) {
156                         if (statusBarClearTimerTask != null) {
157                                 statusBarClearTimerTask.cancel();
158                         }
159                         statusBarClearTimerTask = new TimerTask() {
160
161                                 @SuppressWarnings("synthetic-access")
162                                 @Override
163                                 public void run() {
164                                         statusBar.setText("\u00a0");
165                                 }
166
167                         };
168                         statusBarClearTimer.schedule(statusBarClearTimerTask, statusBarClearDelay);
169                 }
170         }
171
172         /**
173          * Returns the status bar clear delay (in milliseconds).
174          * 
175          * @return The status bar clear delay
176          */
177         public int getStatusBarClearDelay() {
178                 return statusBarClearDelay;
179         }
180
181         /**
182          * Sets the status bar clear delay (in milliseconds).
183          * 
184          * @param statusBarClearDelay
185          *            The status bar clear delay
186          */
187         public void setStatusBarClearDelay(int statusBarClearDelay) {
188                 this.statusBarClearDelay = statusBarClearDelay;
189         }
190
191         /**
192          * Sets whether the advanced mode is activated.
193          * 
194          * @param advancedMode
195          *            <code>true</code> if the advanced mode is activated,
196          *            <code>false</code> if the simple mode is activated
197          */
198         public void setAdvancedMode(boolean advancedMode) {
199                 /* doesn’t do anything right now. */
200         }
201
202         /**
203          * {@inheritDoc}
204          */
205         @Override
206         public Container getContentPane() {
207                 return contentPane;
208         }
209
210         /**
211          * Returns the currently selected project.
212          * 
213          * @return The currently selected project
214          */
215         public Project getSelectedProject() {
216                 return null;
217         }
218
219         //
220         // ACTIONS
221         //
222
223         /**
224          * Adds a node to the menu.
225          * 
226          * @param node
227          *            The node to add
228          */
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                 nodeMenu.add(newNodeMenu);
235                 NodeLabel nodeLabel = new NodeLabel(swingInterface, node, onlineIcon, offlineIcon, errorIcon);
236                 nodeLabels.put(node, nodeLabel);
237                 statusBar.addSideComponent(nodeLabel);
238                 node.addPropertyChangeListener(this);
239         }
240
241         /**
242          * Removes a node from the menu.
243          * 
244          * @param node
245          *            The node to remove
246          */
247         void removeNode(Node node) {
248                 nodeMenu.remove(nodeMenus.remove(node));
249                 statusBar.removeSideComponent(nodeLabels.remove(node));
250                 node.removePropertyChangeListener(this);
251         }
252
253         /**
254          * Adds a project to the project pane.
255          * 
256          * @param project
257          *            The project to add
258          * @param switchToProject
259          *            <code>true</code> to switch to the new panel,
260          *            <code>false</code> to not change the current panel
261          */
262         void addProject(Project project, boolean switchToProject) {
263                 ProjectPanel projectPanel = new ProjectPanel(swingInterface, project);
264                 int newTabIndex = projectPane.getTabCount();
265                 projectPane.add(project.getName(), projectPanel);
266                 projectPane.setToolTipTextAt(newTabIndex, project.getDescription());
267                 project.addPropertyChangeListener(this);
268                 if (switchToProject) {
269                         projectPane.setSelectedIndex(newTabIndex);
270                 }
271         }
272
273         /**
274          * @param project
275          */
276         void projectInsertStarted(Project project) {
277                 int projectIndex = getProjectIndex(project);
278                 if (projectIndex == -1) {
279                         return;
280                 }
281                 projectPane.setTitleAt(projectIndex, I18n.get("projectPanel.title.starting", project.getName()));
282         }
283
284         /**
285          * @param project
286          * @param totalBlocks
287          * @param requiredBlocks
288          * @param successfulBlocks
289          * @param failedBlocks
290          * @param fatallyFailedBlocks
291          * @param finalizedTotal
292          */
293         void projectInsertProgressed(Project project, int totalBlocks, int requiredBlocks, int successfulBlocks, int failedBlocks, int fatallyFailedBlocks, boolean finalizedTotal) {
294                 int projectIndex = getProjectIndex(project);
295                 if (projectIndex == -1) {
296                         return;
297                 }
298                 projectPane.setTitleAt(projectIndex, I18n.get("projectPanel.title.progress", project.getName(), requiredBlocks / (double) successfulBlocks));
299         }
300
301         /**
302          * @param project
303          */
304         void projectInsertGeneratedURI(Project project) {
305                 /* TODO - update panel. */
306         }
307
308         /**
309          * @param project
310          * @param success
311          */
312         void projectInsertFinished(Project project, boolean success) {
313                 int projectIndex = getProjectIndex(project);
314                 if (projectIndex == -1) {
315                         return;
316                 }
317                 projectPane.setTitleAt(projectIndex, project.getName());
318         }
319
320         //
321         // PRIVATE METHODS
322         //
323
324         /**
325          * Returns the index of the project panel that contains the given project.
326          * 
327          * @param project
328          *            The wanted project
329          * @return The index of {@link #projectPane}’s tab that contains the given
330          *         project, or <code>-1</code> if the project can not be found
331          */
332         private int getProjectIndex(Project project) {
333                 int tabCount = projectPane.getTabCount();
334                 for (int tabIndex = 1; tabIndex < tabCount; tabIndex++) {
335                         Component tabComponent = projectPane.getComponentAt(tabIndex);
336                         if (tabComponent instanceof ProjectPanel) {
337                                 if (((ProjectPanel) tabComponent).getProject() == project) {
338                                         return tabIndex;
339                                 }
340                         }
341                 }
342                 return -1;
343         }
344
345         /**
346          * Initializes the window by creating all its components.
347          */
348         private void initWindow() {
349                 onlineIcon = IconLoader.loadIcon("/node-online.png");
350                 offlineIcon = IconLoader.loadIcon("/node-offline.png");
351                 errorIcon = IconLoader.loadIcon("/node-error.png");
352
353                 JMenuBar menuBar = new JMenuBar();
354
355                 jSiteMenu = new I18nMenu("mainWindow.menu.jSite");
356                 menuBar.add(jSiteMenu);
357
358                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getConfigureAction()));
359                 jSiteMenu.addSeparator();
360                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getImportConfigAction()));
361                 jSiteMenu.addSeparator();
362                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getQuitAction()));
363
364                 nodeMenu = new I18nMenu("mainWindow.menu.node");
365                 menuBar.add(nodeMenu);
366
367                 nodeMenu.add(new FixedJMenuItem(swingInterface.getManageNodesAction()));
368                 nodeMenu.addSeparator();
369
370                 languageMenu = new I18nMenu("mainWindow.menu.language");
371                 menuBar.add(languageMenu);
372
373                 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
374                         languageMenu.add(new FixedJMenuItem(languageAction));
375                 }
376
377                 menuBar.add(Box.createHorizontalGlue());
378
379                 helpMenu = new I18nMenu("mainWindow.menu.help");
380                 menuBar.add(helpMenu);
381
382                 helpMenu.add(new FixedJMenuItem(swingInterface.getHelpAboutAction()));
383
384                 setJMenuBar(menuBar);
385
386                 JToolBar toolBar = new JToolBar(I18n.get("mainWindow.toolbar.name"));
387                 toolBar.add(swingInterface.getManageNodesAction());
388                 toolBar.addSeparator();
389                 toolBar.add(swingInterface.getQuitAction());
390                 super.getContentPane().add(toolBar, BorderLayout.PAGE_START);
391
392                 super.getContentPane().add(contentPane, BorderLayout.CENTER);
393
394                 addWindowListener(new WindowAdapter() {
395
396                         /**
397                          * {@inheritDoc}
398                          */
399                         @SuppressWarnings("synthetic-access")
400                         @Override
401                         public void windowClosing(WindowEvent windowEvent) {
402                                 swingInterface.getQuitAction().actionPerformed(null);
403                         }
404                 });
405
406                 initComponents();
407         }
408
409         /**
410          * Initializes all components of this window.
411          */
412         private void initComponents() {
413                 super.getContentPane().add(statusBar, BorderLayout.PAGE_END);
414                 contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
415
416                 projectPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
417                 contentPane.add(projectPane, BorderLayout.CENTER);
418
419                 projectOverviewPanel = new Box(BoxLayout.PAGE_AXIS);
420                 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
421                 projectPane.add(projectOverviewPanel);
422                 projectOverviewPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
423                 projectOverviewPanel.add(Box.createVerticalGlue());
424                 JButton addProjectButton = new JButton(swingInterface.getAddProjectAction());
425                 addProjectButton.setAlignmentX(0.5f);
426                 projectOverviewPanel.add(addProjectButton);
427                 projectOverviewPanel.add(Box.createVerticalGlue());
428         }
429
430         //
431         // INTERFACE I18nable
432         //
433
434         /**
435          * {@inheritDoc}
436          */
437         public void updateI18n() {
438                 swingInterface.getConfigureAction().updateI18n();
439                 swingInterface.getImportConfigAction().updateI18n();
440                 swingInterface.getQuitAction().updateI18n();
441                 swingInterface.getManageNodesAction().updateI18n();
442                 swingInterface.getAddProjectAction().updateI18n();
443                 swingInterface.getCloneProjectAction().updateI18n();
444                 swingInterface.getDeleteProjectAction().updateI18n();
445                 swingInterface.getHelpAboutAction().updateI18n();
446                 jSiteMenu.updateI18n();
447                 nodeMenu.updateI18n();
448                 languageMenu.updateI18n();
449                 for (Node node: swingInterface.getNodes()) {
450                         swingInterface.getNodeConnectAction(node).updateI18n();
451                         swingInterface.getNodeDisconnectAction(node).updateI18n();
452                 }
453                 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
454                         languageAction.updateI18n();
455                 }
456                 helpMenu.updateI18n();
457                 getJMenuBar().revalidate();
458                 projectPane.setTitleAt(0, I18n.get("mainWindow.pane.overview.title"));
459                 for (int componentIndex = 0; componentIndex < projectPane.getTabCount(); componentIndex++) {
460                         Component tabComponent = projectPane.getComponentAt(componentIndex);
461                         if (tabComponent instanceof ProjectPanel) {
462                                 ((ProjectPanel) tabComponent).updateI18n();
463                         }
464                 }
465                 SwingUtils.repackCentered(this);
466         }
467
468         //
469         // INTERFACE WindowListener
470         //
471
472         /**
473          * {@inheritDoc}
474          */
475         public void windowActivated(WindowEvent e) {
476                 /* do nothing. */
477         }
478
479         /**
480          * {@inheritDoc}
481          */
482         public void windowClosed(WindowEvent e) {
483                 /* do nothing. */
484         }
485
486         /**
487          * {@inheritDoc}
488          */
489         public void windowClosing(WindowEvent e) {
490                 swingInterface.getQuitAction().actionPerformed(null);
491         }
492
493         /**
494          * {@inheritDoc}
495          */
496         public void windowDeactivated(WindowEvent e) {
497                 /* do nothing. */
498         }
499
500         /**
501          * {@inheritDoc}
502          */
503         public void windowDeiconified(WindowEvent e) {
504                 /* do nothing. */
505         }
506
507         /**
508          * {@inheritDoc}
509          */
510         public void windowIconified(WindowEvent e) {
511                 /* do nothing. */
512         }
513
514         /**
515          * {@inheritDoc}
516          */
517         public void windowOpened(WindowEvent e) {
518                 /* do nothing. */
519         }
520
521         //
522         // INTERFACE PropertyChangeListener
523         //
524
525         /**
526          * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
527          */
528         public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
529                 Object eventSource = propertyChangeEvent.getSource();
530                 String propertyName = propertyChangeEvent.getPropertyName();
531                 if (eventSource instanceof Project) {
532                         /* if a project was changed, update the tab title and tooltip. */
533                         if (Project.PROPERTY_NAME.equals(propertyName) || Project.PROPERTY_DESCRIPTION.equals(propertyName)) {
534                                 Project project = (Project) eventSource;
535                                 int tabCount = projectPane.getTabCount();
536                                 for (int tabIndex = 0; tabIndex < tabCount; tabIndex++) {
537                                         Component tabComponent = projectPane.getComponentAt(tabIndex);
538                                         if (tabComponent instanceof ProjectPanel) {
539                                                 Project tabProject = ((ProjectPanel) tabComponent).getProject();
540                                                 if (tabProject.equals(project)) {
541                                                         projectPane.setTitleAt(tabIndex, project.getName());
542                                                         projectPane.setToolTipTextAt(tabIndex, project.getDescription());
543                                                         projectPane.repaint();
544                                                 }
545                                         }
546                                 }
547                         }
548                 } else if (eventSource instanceof Node) {
549                         if (propertyName.equals(Node.PROPERTY_NAME)) {
550                                 Node changedNode = (Node) eventSource;
551                                 nodeMenus.get(changedNode).setText(changedNode.getName());
552                         }
553                 }
554         }
555
556 }