clear status bar after delay
[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.Container;
24 import java.awt.Dimension;
25 import java.awt.event.WindowAdapter;
26 import java.awt.event.WindowEvent;
27 import java.awt.event.WindowListener;
28 import java.util.Timer;
29 import java.util.TimerTask;
30
31 import javax.swing.Action;
32 import javax.swing.Box;
33 import javax.swing.BoxLayout;
34 import javax.swing.JButton;
35 import javax.swing.JFrame;
36 import javax.swing.JMenuBar;
37 import javax.swing.JMenuItem;
38 import javax.swing.JPanel;
39 import javax.swing.JTabbedPane;
40 import javax.swing.JToolBar;
41 import javax.swing.SwingConstants;
42 import javax.swing.border.EmptyBorder;
43
44 import net.pterodactylus.jsite.core.Project;
45 import net.pterodactylus.jsite.i18n.I18n;
46 import net.pterodactylus.jsite.i18n.I18nable;
47 import net.pterodactylus.jsite.i18n.gui.I18nAction;
48 import net.pterodactylus.jsite.i18n.gui.I18nMenu;
49 import net.pterodactylus.jsite.main.Version;
50 import net.pterodactylus.util.swing.StatusBar;
51 import net.pterodactylus.util.swing.SwingUtils;
52
53 /**
54  * Defines the main window of the application.
55  * 
56  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
57  * @version $Id$
58  */
59 public class MainWindow extends JFrame implements WindowListener, I18nable {
60
61         /** The swing interface that receives all actions. */
62         private final SwingInterface swingInterface;
63
64         /** The status bar. */
65         private StatusBar statusBar = new StatusBar();
66
67         /** Timer for clearing the status bar. */
68         private Timer statusBarClearTimer = new Timer("StatusBar Cleaner", true);
69
70         /** Object for status bar clearing ticker event. */
71         private TimerTask statusBarClearTimerTask;
72
73         /** Delay (in seconds) after which to clear status bar. */
74         private int statusBarClearDelay = 5000;
75
76         /** The content pane. */
77         private JPanel contentPane = new JPanel(new BorderLayout(12, 12));
78
79         /** The jSite menu. */
80         private I18nMenu jSiteMenu;
81
82         /** The node menu. */
83         private I18nMenu nodeMenu;
84
85         /** The “connect” (advanced mode) menu. */
86         private I18nMenu connectMenu;
87
88         /** The “connect” (simple mode) menu. */
89         private JMenuItem connectMenuItem;
90
91         /** The “disconnect” (advanced mode) menu. */
92         private I18nMenu disconnectMenu;
93
94         /** The “diconnect” (simple mode) menu item. */
95         private JMenuItem disconnectMenuItem;
96
97         /** The language menu. */
98         private I18nMenu languageMenu;
99
100         /** The about menu. */
101         private I18nMenu helpMenu;
102
103         /** The tabbed project pane. */
104         private JTabbedPane projectPane;
105
106         /** The project overview panel. */
107         private Box projectOverviewPanel;
108
109         /**
110          * Creates a new main window that redirects all actions to the given swing
111          * interface.
112          * 
113          * @param swingInterface
114          *            The swing interface to receive all actions
115          */
116         public MainWindow(SwingInterface swingInterface) {
117                 super("jSite " + Version.getVersion());
118                 this.swingInterface = swingInterface;
119                 initWindow();
120                 setPreferredSize(new Dimension(480, 280));
121                 pack();
122                 SwingUtils.center(this);
123                 I18n.registerI18nable(this);
124                 addWindowListener(this);
125         }
126
127         //
128         // ACCESSORS
129         //
130
131         /**
132          * Sets the text of the status bar.
133          * 
134          * @param text
135          *            The text of the status bar
136          */
137         public void setStatusBarText(String text) {
138                 statusBar.setText(text);
139                 synchronized (statusBar) {
140                         if (statusBarClearTimerTask != null) {
141                                 statusBarClearTimerTask.cancel();
142                         }
143                         statusBarClearTimerTask = new TimerTask() {
144
145                                 @Override
146                                 public void run() {
147                                         statusBar.setText("\u00a0");
148                                 }
149
150                         };
151                         statusBarClearTimer.schedule(statusBarClearTimerTask, statusBarClearDelay);
152                 }
153         }
154
155         /**
156          * Returns the status bar clear delay (in milliseconds).
157          * 
158          * @return The status bar clear delay
159          */
160         public int getStatusBarClearDelay() {
161                 return statusBarClearDelay;
162         }
163
164         /**
165          * Sets the status bar clear delay (in milliseconds).
166          * 
167          * @param statusBarClearDelay
168          *            The status bar clear delay
169          */
170         public void setStatusBarClearDelay(int statusBarClearDelay) {
171                 this.statusBarClearDelay = statusBarClearDelay;
172         }
173
174         /**
175          * Sets whether the advanced mode is activated.
176          * 
177          * @param advancedMode
178          *            <code>true</code> if the advanced mode is activated,
179          *            <code>false</code> if the simple mode is activated
180          */
181         public void setAdvancedMode(boolean advancedMode) {
182                 connectMenu.setVisible(advancedMode);
183                 connectMenuItem.setVisible(!advancedMode);
184                 disconnectMenu.setVisible(advancedMode);
185                 disconnectMenuItem.setVisible(!advancedMode);
186         }
187
188         /**
189          * {@inheritDoc}
190          */
191         @Override
192         public Container getContentPane() {
193                 return contentPane;
194         }
195
196         /**
197          * Returns the currently selected project.
198          * 
199          * @return The currently selected project
200          */
201         public Project getSelectedProject() {
202                 return null;
203         }
204
205         //
206         // ACTIONS
207         //
208
209         /**
210          * Refreshes the menu items in the “connect” and “disconnect” menus.
211          */
212         void refreshNodeMenuItems() {
213                 connectMenu.removeAll();
214                 for (Action nodeConnectAction: swingInterface.getNodeConnectActions()) {
215                         connectMenu.add(nodeConnectAction);
216                 }
217                 if (connectMenu.getMenuComponentCount() == 0) {
218                         JMenuItem noNodeAvailableItem = new JMenuItem(I18n.get("mainWindow.menu.connectNoNodeAvailable.name"));
219                         noNodeAvailableItem.setEnabled(false);
220                         connectMenu.add(noNodeAvailableItem);
221                 }
222                 disconnectMenu.removeAll();
223                 for (Action nodeDisconnectAction: swingInterface.getNodeDisconnectActions()) {
224                         disconnectMenu.add(nodeDisconnectAction);
225                 }
226                 if (disconnectMenu.getMenuComponentCount() == 0) {
227                         JMenuItem noNodeAvailableItem = new JMenuItem(I18n.get("mainWindow.menu.disconnectNoNodeAvailable.name"));
228                         noNodeAvailableItem.setEnabled(false);
229                         disconnectMenu.add(noNodeAvailableItem);
230                 }
231         }
232
233         //
234         // PRIVATE METHODS
235         //
236
237         /**
238          * Initializes the window by creating all its components.
239          */
240         private void initWindow() {
241                 JMenuBar menuBar = new JMenuBar();
242
243                 jSiteMenu = new I18nMenu("mainWindow.menu.jSite");
244                 menuBar.add(jSiteMenu);
245
246                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getConfigureAction()));
247                 jSiteMenu.addSeparator();
248                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getImportConfigAction()));
249                 jSiteMenu.addSeparator();
250                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getQuitAction()));
251
252                 connectMenu = new I18nMenu("mainWindow.menu.connect");
253                 disconnectMenu = new I18nMenu("mainWindow.menu.disconnect");
254
255                 nodeMenu = new I18nMenu("mainWindow.menu.node");
256                 menuBar.add(nodeMenu);
257
258                 nodeMenu.add(new FixedJMenuItem(swingInterface.getManageNodesAction()));
259                 nodeMenu.addSeparator();
260                 nodeMenu.add(connectMenuItem = new FixedJMenuItem(swingInterface.getNodeConnectAction()));
261                 nodeMenu.add(connectMenu);
262                 nodeMenu.add(disconnectMenuItem = new FixedJMenuItem(swingInterface.getNodeDisconnectAction()));
263                 nodeMenu.add(disconnectMenu);
264                 refreshNodeMenuItems();
265
266                 languageMenu = new I18nMenu("mainWindow.menu.language");
267                 menuBar.add(languageMenu);
268
269                 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
270                         languageMenu.add(new FixedJMenuItem(languageAction));
271                 }
272
273                 JPanel spacerPanel = new JPanel();
274                 spacerPanel.setOpaque(false);
275                 menuBar.add(spacerPanel);
276
277                 helpMenu = new I18nMenu("mainWindow.menu.help");
278                 menuBar.add(helpMenu);
279
280                 helpMenu.add(new FixedJMenuItem(swingInterface.getHelpAboutAction()));
281
282                 setJMenuBar(menuBar);
283
284                 JToolBar toolBar = new JToolBar(I18n.get("mainWindow.toolbar.name"));
285                 toolBar.add(swingInterface.getManageNodesAction());
286                 toolBar.addSeparator();
287                 toolBar.add(swingInterface.getNodeConnectAction());
288                 toolBar.add(swingInterface.getNodeDisconnectAction());
289                 super.getContentPane().add(toolBar, BorderLayout.PAGE_START);
290
291                 super.getContentPane().add(contentPane, BorderLayout.CENTER);
292
293                 addWindowListener(new WindowAdapter() {
294
295                         /**
296                          * {@inheritDoc}
297                          */
298                         @SuppressWarnings("synthetic-access")
299                         @Override
300                         public void windowClosing(WindowEvent windowEvent) {
301                                 swingInterface.getQuitAction().actionPerformed(null);
302                         }
303                 });
304
305                 initComponents();
306         }
307
308         /**
309          * Initializes all components of this window.
310          */
311         private void initComponents() {
312                 super.getContentPane().add(statusBar, BorderLayout.PAGE_END);
313
314                 /*
315                  * the main window consists of two panels which are vertically oriented.
316                  * the upper panel contains of a tabbed pane, the lower panel consists
317                  * of a table that lists the running requests.
318                  */
319
320                 JPanel upperPanel = new JPanel(new BorderLayout(12, 12));
321                 getContentPane().add(upperPanel, BorderLayout.PAGE_START);
322                 contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
323
324                 projectPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
325                 upperPanel.add(projectPane, BorderLayout.CENTER);
326
327                 projectOverviewPanel = new Box(BoxLayout.PAGE_AXIS);
328                 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
329                 projectPane.add(projectOverviewPanel);
330                 projectOverviewPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
331                 projectOverviewPanel.add(Box.createVerticalGlue());
332                 JButton addProjectButton = new JButton(swingInterface.getAddProjectAction());
333                 addProjectButton.setAlignmentX(0.5f);
334                 projectOverviewPanel.add(addProjectButton);
335                 projectOverviewPanel.add(Box.createVerticalGlue());
336
337 // JPanel lowerPanel = new JPanel(new BorderLayout(12, 12));
338 // getContentPane().add(lowerPanel, BorderLayout.CENTER);
339         }
340
341         //
342         // INTERFACE I18nable
343         //
344
345         /**
346          * {@inheritDoc}
347          */
348         public void updateI18n() {
349                 swingInterface.getConfigureAction().updateI18n();
350                 swingInterface.getImportConfigAction().updateI18n();
351                 swingInterface.getQuitAction().updateI18n();
352                 swingInterface.getManageNodesAction().updateI18n();
353                 swingInterface.getNodeConnectAction().updateI18n();
354                 connectMenu.updateI18n();
355                 swingInterface.getNodeDisconnectAction().updateI18n();
356                 disconnectMenu.updateI18n();
357                 swingInterface.getAddProjectAction().updateI18n();
358                 swingInterface.getCloneProjectAction().updateI18n();
359                 swingInterface.getDeleteProjectAction().updateI18n();
360                 swingInterface.getHelpAboutAction().updateI18n();
361                 jSiteMenu.updateI18n();
362                 nodeMenu.updateI18n();
363                 languageMenu.updateI18n();
364                 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
365                         languageAction.updateI18n();
366                 }
367                 helpMenu.updateI18n();
368                 getJMenuBar().revalidate();
369                 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
370                 for (int componentIndex = 0; componentIndex < projectPane.getTabCount(); componentIndex++) {
371                         projectPane.setTitleAt(componentIndex, projectPane.getComponentAt(componentIndex).getName());
372                 }
373                 refreshNodeMenuItems();
374                 SwingUtils.repackCentered(this);
375         }
376
377         //
378         // INTERFACE WindowListener
379         //
380
381         /**
382          * {@inheritDoc}
383          */
384         public void windowActivated(WindowEvent e) {
385                 /* do nothing. */
386         }
387
388         /**
389          * {@inheritDoc}
390          */
391         public void windowClosed(WindowEvent e) {
392                 /* do nothing. */
393         }
394
395         /**
396          * {@inheritDoc}
397          */
398         public void windowClosing(WindowEvent e) {
399                 swingInterface.getQuitAction().actionPerformed(null);
400         }
401
402         /**
403          * {@inheritDoc}
404          */
405         public void windowDeactivated(WindowEvent e) {
406                 /* do nothing. */
407         }
408
409         /**
410          * {@inheritDoc}
411          */
412         public void windowDeiconified(WindowEvent e) {
413                 /* do nothing. */
414         }
415
416         /**
417          * {@inheritDoc}
418          */
419         public void windowIconified(WindowEvent e) {
420                 /* do nothing. */
421         }
422
423         /**
424          * {@inheritDoc}
425          */
426         public void windowOpened(WindowEvent e) {
427                 /* do nothing. */
428         }
429
430 }