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