fix warning
[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                                 @SuppressWarnings("synthetic-access")
146                                 @Override
147                                 public void run() {
148                                         statusBar.setText("\u00a0");
149                                 }
150
151                         };
152                         statusBarClearTimer.schedule(statusBarClearTimerTask, statusBarClearDelay);
153                 }
154         }
155
156         /**
157          * Returns the status bar clear delay (in milliseconds).
158          * 
159          * @return The status bar clear delay
160          */
161         public int getStatusBarClearDelay() {
162                 return statusBarClearDelay;
163         }
164
165         /**
166          * Sets the status bar clear delay (in milliseconds).
167          * 
168          * @param statusBarClearDelay
169          *            The status bar clear delay
170          */
171         public void setStatusBarClearDelay(int statusBarClearDelay) {
172                 this.statusBarClearDelay = statusBarClearDelay;
173         }
174
175         /**
176          * Sets whether the advanced mode is activated.
177          * 
178          * @param advancedMode
179          *            <code>true</code> if the advanced mode is activated,
180          *            <code>false</code> if the simple mode is activated
181          */
182         public void setAdvancedMode(boolean advancedMode) {
183                 connectMenu.setVisible(advancedMode);
184                 connectMenuItem.setVisible(!advancedMode);
185                 disconnectMenu.setVisible(advancedMode);
186                 disconnectMenuItem.setVisible(!advancedMode);
187         }
188
189         /**
190          * {@inheritDoc}
191          */
192         @Override
193         public Container getContentPane() {
194                 return contentPane;
195         }
196
197         /**
198          * Returns the currently selected project.
199          * 
200          * @return The currently selected project
201          */
202         public Project getSelectedProject() {
203                 return null;
204         }
205
206         //
207         // ACTIONS
208         //
209
210         /**
211          * Refreshes the menu items in the “connect” and “disconnect” menus.
212          */
213         void refreshNodeMenuItems() {
214                 connectMenu.removeAll();
215                 for (Action nodeConnectAction: swingInterface.getNodeConnectActions()) {
216                         connectMenu.add(nodeConnectAction);
217                 }
218                 if (connectMenu.getMenuComponentCount() == 0) {
219                         JMenuItem noNodeAvailableItem = new JMenuItem(I18n.get("mainWindow.menu.connectNoNodeAvailable.name"));
220                         noNodeAvailableItem.setEnabled(false);
221                         connectMenu.add(noNodeAvailableItem);
222                 }
223                 disconnectMenu.removeAll();
224                 for (Action nodeDisconnectAction: swingInterface.getNodeDisconnectActions()) {
225                         disconnectMenu.add(nodeDisconnectAction);
226                 }
227                 if (disconnectMenu.getMenuComponentCount() == 0) {
228                         JMenuItem noNodeAvailableItem = new JMenuItem(I18n.get("mainWindow.menu.disconnectNoNodeAvailable.name"));
229                         noNodeAvailableItem.setEnabled(false);
230                         disconnectMenu.add(noNodeAvailableItem);
231                 }
232         }
233
234         //
235         // PRIVATE METHODS
236         //
237
238         /**
239          * Initializes the window by creating all its components.
240          */
241         private void initWindow() {
242                 JMenuBar menuBar = new JMenuBar();
243
244                 jSiteMenu = new I18nMenu("mainWindow.menu.jSite");
245                 menuBar.add(jSiteMenu);
246
247                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getConfigureAction()));
248                 jSiteMenu.addSeparator();
249                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getImportConfigAction()));
250                 jSiteMenu.addSeparator();
251                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getQuitAction()));
252
253                 connectMenu = new I18nMenu("mainWindow.menu.connect");
254                 disconnectMenu = new I18nMenu("mainWindow.menu.disconnect");
255
256                 nodeMenu = new I18nMenu("mainWindow.menu.node");
257                 menuBar.add(nodeMenu);
258
259                 nodeMenu.add(new FixedJMenuItem(swingInterface.getManageNodesAction()));
260                 nodeMenu.addSeparator();
261                 nodeMenu.add(connectMenuItem = new FixedJMenuItem(swingInterface.getNodeConnectAction()));
262                 nodeMenu.add(connectMenu);
263                 nodeMenu.add(disconnectMenuItem = new FixedJMenuItem(swingInterface.getNodeDisconnectAction()));
264                 nodeMenu.add(disconnectMenu);
265                 refreshNodeMenuItems();
266
267                 languageMenu = new I18nMenu("mainWindow.menu.language");
268                 menuBar.add(languageMenu);
269
270                 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
271                         languageMenu.add(new FixedJMenuItem(languageAction));
272                 }
273
274                 JPanel spacerPanel = new JPanel();
275                 spacerPanel.setOpaque(false);
276                 menuBar.add(spacerPanel);
277
278                 helpMenu = new I18nMenu("mainWindow.menu.help");
279                 menuBar.add(helpMenu);
280
281                 helpMenu.add(new FixedJMenuItem(swingInterface.getHelpAboutAction()));
282
283                 setJMenuBar(menuBar);
284
285                 JToolBar toolBar = new JToolBar(I18n.get("mainWindow.toolbar.name"));
286                 toolBar.add(swingInterface.getManageNodesAction());
287                 toolBar.addSeparator();
288                 toolBar.add(swingInterface.getNodeConnectAction());
289                 toolBar.add(swingInterface.getNodeDisconnectAction());
290                 super.getContentPane().add(toolBar, BorderLayout.PAGE_START);
291
292                 super.getContentPane().add(contentPane, BorderLayout.CENTER);
293
294                 addWindowListener(new WindowAdapter() {
295
296                         /**
297                          * {@inheritDoc}
298                          */
299                         @SuppressWarnings("synthetic-access")
300                         @Override
301                         public void windowClosing(WindowEvent windowEvent) {
302                                 swingInterface.getQuitAction().actionPerformed(null);
303                         }
304                 });
305
306                 initComponents();
307         }
308
309         /**
310          * Initializes all components of this window.
311          */
312         private void initComponents() {
313                 super.getContentPane().add(statusBar, BorderLayout.PAGE_END);
314
315                 /*
316                  * the main window consists of two panels which are vertically oriented.
317                  * the upper panel contains of a tabbed pane, the lower panel consists
318                  * of a table that lists the running requests.
319                  */
320
321                 JPanel upperPanel = new JPanel(new BorderLayout(12, 12));
322                 getContentPane().add(upperPanel, BorderLayout.PAGE_START);
323                 contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
324
325                 projectPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
326                 upperPanel.add(projectPane, BorderLayout.CENTER);
327
328                 projectOverviewPanel = new Box(BoxLayout.PAGE_AXIS);
329                 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
330                 projectPane.add(projectOverviewPanel);
331                 projectOverviewPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
332                 projectOverviewPanel.add(Box.createVerticalGlue());
333                 JButton addProjectButton = new JButton(swingInterface.getAddProjectAction());
334                 addProjectButton.setAlignmentX(0.5f);
335                 projectOverviewPanel.add(addProjectButton);
336                 projectOverviewPanel.add(Box.createVerticalGlue());
337
338 // JPanel lowerPanel = new JPanel(new BorderLayout(12, 12));
339 // getContentPane().add(lowerPanel, BorderLayout.CENTER);
340         }
341
342         //
343         // INTERFACE I18nable
344         //
345
346         /**
347          * {@inheritDoc}
348          */
349         public void updateI18n() {
350                 swingInterface.getConfigureAction().updateI18n();
351                 swingInterface.getImportConfigAction().updateI18n();
352                 swingInterface.getQuitAction().updateI18n();
353                 swingInterface.getManageNodesAction().updateI18n();
354                 swingInterface.getNodeConnectAction().updateI18n();
355                 connectMenu.updateI18n();
356                 swingInterface.getNodeDisconnectAction().updateI18n();
357                 disconnectMenu.updateI18n();
358                 swingInterface.getAddProjectAction().updateI18n();
359                 swingInterface.getCloneProjectAction().updateI18n();
360                 swingInterface.getDeleteProjectAction().updateI18n();
361                 swingInterface.getHelpAboutAction().updateI18n();
362                 jSiteMenu.updateI18n();
363                 nodeMenu.updateI18n();
364                 languageMenu.updateI18n();
365                 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
366                         languageAction.updateI18n();
367                 }
368                 helpMenu.updateI18n();
369                 getJMenuBar().revalidate();
370                 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
371                 for (int componentIndex = 0; componentIndex < projectPane.getTabCount(); componentIndex++) {
372                         projectPane.setTitleAt(componentIndex, projectPane.getComponentAt(componentIndex).getName());
373                 }
374                 refreshNodeMenuItems();
375                 SwingUtils.repackCentered(this);
376         }
377
378         //
379         // INTERFACE WindowListener
380         //
381
382         /**
383          * {@inheritDoc}
384          */
385         public void windowActivated(WindowEvent e) {
386                 /* do nothing. */
387         }
388
389         /**
390          * {@inheritDoc}
391          */
392         public void windowClosed(WindowEvent e) {
393                 /* do nothing. */
394         }
395
396         /**
397          * {@inheritDoc}
398          */
399         public void windowClosing(WindowEvent e) {
400                 swingInterface.getQuitAction().actionPerformed(null);
401         }
402
403         /**
404          * {@inheritDoc}
405          */
406         public void windowDeactivated(WindowEvent e) {
407                 /* do nothing. */
408         }
409
410         /**
411          * {@inheritDoc}
412          */
413         public void windowDeiconified(WindowEvent e) {
414                 /* do nothing. */
415         }
416
417         /**
418          * {@inheritDoc}
419          */
420         public void windowIconified(WindowEvent e) {
421                 /* do nothing. */
422         }
423
424         /**
425          * {@inheritDoc}
426          */
427         public void windowOpened(WindowEvent e) {
428                 /* do nothing. */
429         }
430
431 }