starting project notifications
[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                 ProjectPanel projectPanel = findProjectPanel(project);
268                 if (projectPanel == null) {
269                         return;
270                 }
271                 
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
285         }
286
287         /**
288          * @param project
289          */
290         void projectInsertGeneratedURI(Project project) {
291
292         }
293
294         /**
295          * @param project
296          * @param success
297          */
298         void projectInsertFinished(Project project, boolean success) {
299
300         }
301
302         //
303         // PRIVATE METHODS
304         //
305
306         /**
307          * Locates the project panel that contains the given project.
308          * 
309          * @param project
310          *            The wanted project
311          */
312         private ProjectPanel findProjectPanel(Project project) {
313                 int tabCount = projectPane.getTabCount();
314                 for (int tabIndex = 1; tabIndex < tabCount; tabIndex++) {
315                         Component tabComponent = projectPane.getComponentAt(tabIndex);
316                         if (tabComponent instanceof ProjectPanel) {
317                                 if (((ProjectPanel) tabComponent).getProject() == project) {
318                                         return (ProjectPanel) tabComponent;
319                                 }
320                         }
321                 }
322                 return null;
323         }
324
325         /**
326          * Initializes the window by creating all its components.
327          */
328         private void initWindow() {
329                 JMenuBar menuBar = new JMenuBar();
330
331                 jSiteMenu = new I18nMenu("mainWindow.menu.jSite");
332                 menuBar.add(jSiteMenu);
333
334                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getConfigureAction()));
335                 jSiteMenu.addSeparator();
336                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getImportConfigAction()));
337                 jSiteMenu.addSeparator();
338                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getQuitAction()));
339
340                 connectMenu = new I18nMenu("mainWindow.menu.connect");
341                 disconnectMenu = new I18nMenu("mainWindow.menu.disconnect");
342
343                 nodeMenu = new I18nMenu("mainWindow.menu.node");
344                 menuBar.add(nodeMenu);
345
346                 nodeMenu.add(new FixedJMenuItem(swingInterface.getManageNodesAction()));
347                 nodeMenu.addSeparator();
348                 nodeMenu.add(connectMenuItem = new FixedJMenuItem(swingInterface.getNodeConnectAction()));
349                 nodeMenu.add(connectMenu);
350                 nodeMenu.add(disconnectMenuItem = new FixedJMenuItem(swingInterface.getNodeDisconnectAction()));
351                 nodeMenu.add(disconnectMenu);
352                 refreshNodeMenuItems();
353
354                 languageMenu = new I18nMenu("mainWindow.menu.language");
355                 menuBar.add(languageMenu);
356
357                 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
358                         languageMenu.add(new FixedJMenuItem(languageAction));
359                 }
360
361                 JPanel spacerPanel = new JPanel();
362                 spacerPanel.setOpaque(false);
363                 menuBar.add(spacerPanel);
364
365                 helpMenu = new I18nMenu("mainWindow.menu.help");
366                 menuBar.add(helpMenu);
367
368                 helpMenu.add(new FixedJMenuItem(swingInterface.getHelpAboutAction()));
369
370                 setJMenuBar(menuBar);
371
372                 JToolBar toolBar = new JToolBar(I18n.get("mainWindow.toolbar.name"));
373                 toolBar.add(swingInterface.getManageNodesAction());
374                 toolBar.addSeparator();
375                 toolBar.add(swingInterface.getNodeConnectAction());
376                 toolBar.add(swingInterface.getNodeDisconnectAction());
377                 super.getContentPane().add(toolBar, BorderLayout.PAGE_START);
378
379                 super.getContentPane().add(contentPane, BorderLayout.CENTER);
380
381                 addWindowListener(new WindowAdapter() {
382
383                         /**
384                          * {@inheritDoc}
385                          */
386                         @SuppressWarnings("synthetic-access")
387                         @Override
388                         public void windowClosing(WindowEvent windowEvent) {
389                                 swingInterface.getQuitAction().actionPerformed(null);
390                         }
391                 });
392
393                 initComponents();
394         }
395
396         /**
397          * Initializes all components of this window.
398          */
399         private void initComponents() {
400                 super.getContentPane().add(statusBar, BorderLayout.PAGE_END);
401
402                 /* TODO - remove upper panel */
403                 JPanel upperPanel = new JPanel(new BorderLayout(12, 12));
404                 getContentPane().add(upperPanel, BorderLayout.CENTER);
405                 contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
406
407                 projectPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
408                 upperPanel.add(projectPane, BorderLayout.CENTER);
409
410                 projectOverviewPanel = new Box(BoxLayout.PAGE_AXIS);
411                 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
412                 projectPane.add(projectOverviewPanel);
413                 projectOverviewPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
414                 projectOverviewPanel.add(Box.createVerticalGlue());
415                 JButton addProjectButton = new JButton(swingInterface.getAddProjectAction());
416                 addProjectButton.setAlignmentX(0.5f);
417                 projectOverviewPanel.add(addProjectButton);
418                 projectOverviewPanel.add(Box.createVerticalGlue());
419
420                 // JPanel lowerPanel = new JPanel(new BorderLayout(12, 12));
421                 // getContentPane().add(lowerPanel, BorderLayout.CENTER);
422         }
423
424         //
425         // INTERFACE I18nable
426         //
427
428         /**
429          * {@inheritDoc}
430          */
431         public void updateI18n() {
432                 swingInterface.getConfigureAction().updateI18n();
433                 swingInterface.getImportConfigAction().updateI18n();
434                 swingInterface.getQuitAction().updateI18n();
435                 swingInterface.getManageNodesAction().updateI18n();
436                 swingInterface.getNodeConnectAction().updateI18n();
437                 connectMenu.updateI18n();
438                 swingInterface.getNodeDisconnectAction().updateI18n();
439                 disconnectMenu.updateI18n();
440                 swingInterface.getAddProjectAction().updateI18n();
441                 swingInterface.getCloneProjectAction().updateI18n();
442                 swingInterface.getDeleteProjectAction().updateI18n();
443                 swingInterface.getHelpAboutAction().updateI18n();
444                 jSiteMenu.updateI18n();
445                 nodeMenu.updateI18n();
446                 languageMenu.updateI18n();
447                 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
448                         languageAction.updateI18n();
449                 }
450                 helpMenu.updateI18n();
451                 getJMenuBar().revalidate();
452                 projectPane.setTitleAt(0, I18n.get("mainWindow.pane.overview.title"));
453                 for (int componentIndex = 0; componentIndex < projectPane.getTabCount(); componentIndex++) {
454                         Component tabComponent = projectPane.getComponentAt(componentIndex);
455                         if (tabComponent instanceof ProjectPanel) {
456                                 ((ProjectPanel) tabComponent).updateI18n();
457                         }
458                 }
459                 refreshNodeMenuItems();
460                 SwingUtils.repackCentered(this);
461         }
462
463         //
464         // INTERFACE WindowListener
465         //
466
467         /**
468          * {@inheritDoc}
469          */
470         public void windowActivated(WindowEvent e) {
471                 /* do nothing. */
472         }
473
474         /**
475          * {@inheritDoc}
476          */
477         public void windowClosed(WindowEvent e) {
478                 /* do nothing. */
479         }
480
481         /**
482          * {@inheritDoc}
483          */
484         public void windowClosing(WindowEvent e) {
485                 swingInterface.getQuitAction().actionPerformed(null);
486         }
487
488         /**
489          * {@inheritDoc}
490          */
491         public void windowDeactivated(WindowEvent e) {
492                 /* do nothing. */
493         }
494
495         /**
496          * {@inheritDoc}
497          */
498         public void windowDeiconified(WindowEvent e) {
499                 /* do nothing. */
500         }
501
502         /**
503          * {@inheritDoc}
504          */
505         public void windowIconified(WindowEvent e) {
506                 /* do nothing. */
507         }
508
509         /**
510          * {@inheritDoc}
511          */
512         public void windowOpened(WindowEvent e) {
513                 /* do nothing. */
514         }
515
516         //
517         // INTERFACE PropertyChangeListener
518         //
519
520         /**
521          * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
522          */
523         public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
524                 Object eventSource = propertyChangeEvent.getSource();
525                 String propertyName = propertyChangeEvent.getPropertyName();
526                 if (eventSource instanceof Project) {
527                         /* if a project was changed, update the tab title and tooltip. */
528                         if (Project.PROPERTY_NAME.equals(propertyName) || Project.PROPERTY_DESCRIPTION.equals(propertyName)) {
529                                 Project project = (Project) eventSource;
530                                 int tabCount = projectPane.getTabCount();
531                                 for (int tabIndex = 0; tabIndex < tabCount; tabIndex++) {
532                                         Component tabComponent = projectPane.getComponentAt(tabIndex);
533                                         if (tabComponent instanceof ProjectPanel) {
534                                                 Project tabProject = ((ProjectPanel) tabComponent).getProject();
535                                                 if (tabProject.equals(project)) {
536                                                         projectPane.setTitleAt(tabIndex, project.getName());
537                                                         projectPane.setToolTipTextAt(tabIndex, project.getDescription());
538                                                         projectPane.repaint();
539                                                 }
540                                         }
541                                 }
542                         }
543                 }
544         }
545
546 }