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