add progress updates to project tab titles
[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.Timer;
32 import java.util.TimerTask;
33 import java.util.logging.Logger;
34
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;
47
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;
57
58 /**
59  * Defines the main window of the application.
60  * 
61  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
62  * @version $Id$
63  */
64 public class MainWindow extends JFrame implements WindowListener, I18nable, PropertyChangeListener {
65
66         /** Logger. */
67         @SuppressWarnings("unused")
68         private static final Logger logger = Logging.getLogger(MainWindow.class.getName());
69
70         /** The swing interface that receives all actions. */
71         private final SwingInterface swingInterface;
72
73         /** The status bar. */
74         private StatusBar statusBar = new StatusBar();
75
76         /** Timer for clearing the status bar. */
77         private Timer statusBarClearTimer = new Timer("StatusBar Cleaner", true);
78
79         /** Object for status bar clearing ticker event. */
80         private TimerTask statusBarClearTimerTask;
81
82         /** Delay (in seconds) after which to clear status bar. */
83         private int statusBarClearDelay = 5000;
84
85         /** The content pane. */
86         private JPanel contentPane = new JPanel(new BorderLayout(12, 12));
87
88         /** The jSite menu. */
89         private I18nMenu jSiteMenu;
90
91         /** The node menu. */
92         private I18nMenu nodeMenu;
93
94         /** The “connect” (advanced mode) menu. */
95         private I18nMenu connectMenu;
96
97         /** The “connect” (simple mode) menu. */
98         private JMenuItem connectMenuItem;
99
100         /** The “disconnect” (advanced mode) menu. */
101         private I18nMenu disconnectMenu;
102
103         /** The “diconnect” (simple mode) menu item. */
104         private JMenuItem disconnectMenuItem;
105
106         /** The language menu. */
107         private I18nMenu languageMenu;
108
109         /** The about menu. */
110         private I18nMenu helpMenu;
111
112         /** The tabbed project pane. */
113         private JTabbedPane projectPane;
114
115         /** The project overview panel. */
116         private Box projectOverviewPanel;
117
118         /**
119          * Creates a new main window that redirects all actions to the given swing
120          * interface.
121          * 
122          * @param swingInterface
123          *            The swing interface to receive all actions
124          */
125         public MainWindow(SwingInterface swingInterface) {
126                 super("jSite " + Version.getVersion());
127                 this.swingInterface = swingInterface;
128                 initWindow();
129                 setPreferredSize(new Dimension(480, 280));
130                 pack();
131                 SwingUtils.center(this);
132                 I18n.registerI18nable(this);
133                 addWindowListener(this);
134         }
135
136         //
137         // ACCESSORS
138         //
139
140         /**
141          * Sets the text of the status bar.
142          * 
143          * @param text
144          *            The text of the status bar
145          */
146         public void setStatusBarText(String text) {
147                 statusBar.setText(text);
148                 synchronized (statusBar) {
149                         if (statusBarClearTimerTask != null) {
150                                 statusBarClearTimerTask.cancel();
151                         }
152                         statusBarClearTimerTask = new TimerTask() {
153
154                                 @SuppressWarnings("synthetic-access")
155                                 @Override
156                                 public void run() {
157                                         statusBar.setText("\u00a0");
158                                 }
159
160                         };
161                         statusBarClearTimer.schedule(statusBarClearTimerTask, statusBarClearDelay);
162                 }
163         }
164
165         /**
166          * Returns the status bar clear delay (in milliseconds).
167          * 
168          * @return The status bar clear delay
169          */
170         public int getStatusBarClearDelay() {
171                 return statusBarClearDelay;
172         }
173
174         /**
175          * Sets the status bar clear delay (in milliseconds).
176          * 
177          * @param statusBarClearDelay
178          *            The status bar clear delay
179          */
180         public void setStatusBarClearDelay(int statusBarClearDelay) {
181                 this.statusBarClearDelay = statusBarClearDelay;
182         }
183
184         /**
185          * Sets whether the advanced mode is activated.
186          * 
187          * @param advancedMode
188          *            <code>true</code> if the advanced mode is activated,
189          *            <code>false</code> if the simple mode is activated
190          */
191         public void setAdvancedMode(boolean advancedMode) {
192                 connectMenu.setVisible(advancedMode);
193                 connectMenuItem.setVisible(!advancedMode);
194                 disconnectMenu.setVisible(advancedMode);
195                 disconnectMenuItem.setVisible(!advancedMode);
196         }
197
198         /**
199          * {@inheritDoc}
200          */
201         @Override
202         public Container getContentPane() {
203                 return contentPane;
204         }
205
206         /**
207          * Returns the currently selected project.
208          * 
209          * @return The currently selected project
210          */
211         public Project getSelectedProject() {
212                 return null;
213         }
214
215         //
216         // ACTIONS
217         //
218
219         /**
220          * Refreshes the menu items in the “connect” and “disconnect” menus.
221          */
222         void refreshNodeMenuItems() {
223                 connectMenu.removeAll();
224                 for (Action nodeConnectAction: swingInterface.getNodeConnectActions()) {
225                         connectMenu.add(nodeConnectAction);
226                 }
227                 if (connectMenu.getMenuComponentCount() == 0) {
228                         JMenuItem noNodeAvailableItem = new JMenuItem(I18n.get("mainWindow.menu.connectNoNodeAvailable.name"));
229                         noNodeAvailableItem.setEnabled(false);
230                         connectMenu.add(noNodeAvailableItem);
231                 }
232                 disconnectMenu.removeAll();
233                 for (Action nodeDisconnectAction: swingInterface.getNodeDisconnectActions()) {
234                         disconnectMenu.add(nodeDisconnectAction);
235                 }
236                 if (disconnectMenu.getMenuComponentCount() == 0) {
237                         JMenuItem noNodeAvailableItem = new JMenuItem(I18n.get("mainWindow.menu.disconnectNoNodeAvailable.name"));
238                         noNodeAvailableItem.setEnabled(false);
239                         disconnectMenu.add(noNodeAvailableItem);
240                 }
241         }
242
243         /**
244          * Adds a project to the project pane.
245          * 
246          * @param project
247          *            The project to add
248          * @param switchToProject
249          *            <code>true</code> to switch to the new panel,
250          *            <code>false</code> to not change the current panel
251          */
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);
260                 }
261         }
262
263         /**
264          * @param project
265          */
266         void projectInsertStarted(Project project) {
267                 int projectIndex = getProjectIndex(project);
268                 if (projectIndex == -1) {
269                         return;
270                 }
271                 projectPane.setTitleAt(projectIndex, I18n.get("projectPanel.title.starting", project.getName()));
272         }
273
274         /**
275          * @param project
276          * @param totalBlocks
277          * @param requiredBlocks
278          * @param successfulBlocks
279          * @param failedBlocks
280          * @param fatallyFailedBlocks
281          * @param finalizedTotal
282          */
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) {
286                         return;
287                 }
288                 projectPane.setTitleAt(projectIndex, I18n.get("projectPanel.title.progress", project.getName(), requiredBlocks / (double) successfulBlocks));
289         }
290
291         /**
292          * @param project
293          */
294         void projectInsertGeneratedURI(Project project) {
295                 /* TODO - update panel. */
296         }
297
298         /**
299          * @param project
300          * @param success
301          */
302         void projectInsertFinished(Project project, boolean success) {
303                 int projectIndex = getProjectIndex(project);
304                 if (projectIndex == -1) {
305                         return;
306                 }
307                 projectPane.setTitleAt(projectIndex, project.getName());
308         }
309
310         //
311         // PRIVATE METHODS
312         //
313
314         /**
315          * Returns the index of the project panel that contains the given project.
316          * 
317          * @param project
318          *            The wanted 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
321          */
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) {
328                                         return tabIndex;
329                                 }
330                         }
331                 }
332                 return -1;
333         }
334
335         /**
336          * Initializes the window by creating all its components.
337          */
338         private void initWindow() {
339                 JMenuBar menuBar = new JMenuBar();
340
341                 jSiteMenu = new I18nMenu("mainWindow.menu.jSite");
342                 menuBar.add(jSiteMenu);
343
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()));
349
350                 connectMenu = new I18nMenu("mainWindow.menu.connect");
351                 disconnectMenu = new I18nMenu("mainWindow.menu.disconnect");
352
353                 nodeMenu = new I18nMenu("mainWindow.menu.node");
354                 menuBar.add(nodeMenu);
355
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();
363
364                 languageMenu = new I18nMenu("mainWindow.menu.language");
365                 menuBar.add(languageMenu);
366
367                 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
368                         languageMenu.add(new FixedJMenuItem(languageAction));
369                 }
370
371                 JPanel spacerPanel = new JPanel();
372                 spacerPanel.setOpaque(false);
373                 menuBar.add(spacerPanel);
374
375                 helpMenu = new I18nMenu("mainWindow.menu.help");
376                 menuBar.add(helpMenu);
377
378                 helpMenu.add(new FixedJMenuItem(swingInterface.getHelpAboutAction()));
379
380                 setJMenuBar(menuBar);
381
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);
388
389                 super.getContentPane().add(contentPane, BorderLayout.CENTER);
390
391                 addWindowListener(new WindowAdapter() {
392
393                         /**
394                          * {@inheritDoc}
395                          */
396                         @SuppressWarnings("synthetic-access")
397                         @Override
398                         public void windowClosing(WindowEvent windowEvent) {
399                                 swingInterface.getQuitAction().actionPerformed(null);
400                         }
401                 });
402
403                 initComponents();
404         }
405
406         /**
407          * Initializes all components of this window.
408          */
409         private void initComponents() {
410                 super.getContentPane().add(statusBar, BorderLayout.PAGE_END);
411
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));
416
417                 projectPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
418                 upperPanel.add(projectPane, BorderLayout.CENTER);
419
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());
429
430                 // JPanel lowerPanel = new JPanel(new BorderLayout(12, 12));
431                 // getContentPane().add(lowerPanel, BorderLayout.CENTER);
432         }
433
434         //
435         // INTERFACE I18nable
436         //
437
438         /**
439          * {@inheritDoc}
440          */
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();
459                 }
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();
467                         }
468                 }
469                 refreshNodeMenuItems();
470                 SwingUtils.repackCentered(this);
471         }
472
473         //
474         // INTERFACE WindowListener
475         //
476
477         /**
478          * {@inheritDoc}
479          */
480         public void windowActivated(WindowEvent e) {
481                 /* do nothing. */
482         }
483
484         /**
485          * {@inheritDoc}
486          */
487         public void windowClosed(WindowEvent e) {
488                 /* do nothing. */
489         }
490
491         /**
492          * {@inheritDoc}
493          */
494         public void windowClosing(WindowEvent e) {
495                 swingInterface.getQuitAction().actionPerformed(null);
496         }
497
498         /**
499          * {@inheritDoc}
500          */
501         public void windowDeactivated(WindowEvent e) {
502                 /* do nothing. */
503         }
504
505         /**
506          * {@inheritDoc}
507          */
508         public void windowDeiconified(WindowEvent e) {
509                 /* do nothing. */
510         }
511
512         /**
513          * {@inheritDoc}
514          */
515         public void windowIconified(WindowEvent e) {
516                 /* do nothing. */
517         }
518
519         /**
520          * {@inheritDoc}
521          */
522         public void windowOpened(WindowEvent e) {
523                 /* do nothing. */
524         }
525
526         //
527         // INTERFACE PropertyChangeListener
528         //
529
530         /**
531          * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
532          */
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();
549                                                 }
550                                         }
551                                 }
552                         }
553                 }
554         }
555
556 }