first stab add project addition
[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          * Adds a project to the project pane.
241          * 
242          * @param project
243          *            The project to add
244          */
245         void addProject(Project project) {
246                 ProjectPanel projectPanel = new ProjectPanel(swingInterface, project);
247                 projectPane.add(project.getName(), projectPanel);
248         }
249
250         //
251         // PRIVATE METHODS
252         //
253
254         /**
255          * Initializes the window by creating all its components.
256          */
257         private void initWindow() {
258                 JMenuBar menuBar = new JMenuBar();
259
260                 jSiteMenu = new I18nMenu("mainWindow.menu.jSite");
261                 menuBar.add(jSiteMenu);
262
263                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getConfigureAction()));
264                 jSiteMenu.addSeparator();
265                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getImportConfigAction()));
266                 jSiteMenu.addSeparator();
267                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getQuitAction()));
268
269                 connectMenu = new I18nMenu("mainWindow.menu.connect");
270                 disconnectMenu = new I18nMenu("mainWindow.menu.disconnect");
271
272                 nodeMenu = new I18nMenu("mainWindow.menu.node");
273                 menuBar.add(nodeMenu);
274
275                 nodeMenu.add(new FixedJMenuItem(swingInterface.getManageNodesAction()));
276                 nodeMenu.addSeparator();
277                 nodeMenu.add(connectMenuItem = new FixedJMenuItem(swingInterface.getNodeConnectAction()));
278                 nodeMenu.add(connectMenu);
279                 nodeMenu.add(disconnectMenuItem = new FixedJMenuItem(swingInterface.getNodeDisconnectAction()));
280                 nodeMenu.add(disconnectMenu);
281                 refreshNodeMenuItems();
282
283                 languageMenu = new I18nMenu("mainWindow.menu.language");
284                 menuBar.add(languageMenu);
285
286                 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
287                         languageMenu.add(new FixedJMenuItem(languageAction));
288                 }
289
290                 JPanel spacerPanel = new JPanel();
291                 spacerPanel.setOpaque(false);
292                 menuBar.add(spacerPanel);
293
294                 helpMenu = new I18nMenu("mainWindow.menu.help");
295                 menuBar.add(helpMenu);
296
297                 helpMenu.add(new FixedJMenuItem(swingInterface.getHelpAboutAction()));
298
299                 setJMenuBar(menuBar);
300
301                 JToolBar toolBar = new JToolBar(I18n.get("mainWindow.toolbar.name"));
302                 toolBar.add(swingInterface.getManageNodesAction());
303                 toolBar.addSeparator();
304                 toolBar.add(swingInterface.getNodeConnectAction());
305                 toolBar.add(swingInterface.getNodeDisconnectAction());
306                 super.getContentPane().add(toolBar, BorderLayout.PAGE_START);
307
308                 super.getContentPane().add(contentPane, BorderLayout.CENTER);
309
310                 addWindowListener(new WindowAdapter() {
311
312                         /**
313                          * {@inheritDoc}
314                          */
315                         @SuppressWarnings("synthetic-access")
316                         @Override
317                         public void windowClosing(WindowEvent windowEvent) {
318                                 swingInterface.getQuitAction().actionPerformed(null);
319                         }
320                 });
321
322                 initComponents();
323         }
324
325         /**
326          * Initializes all components of this window.
327          */
328         private void initComponents() {
329                 super.getContentPane().add(statusBar, BorderLayout.PAGE_END);
330
331                 /*
332                  * the main window consists of two panels which are vertically oriented.
333                  * the upper panel contains of a tabbed pane, the lower panel consists
334                  * of a table that lists the running requests.
335                  */
336
337                 JPanel upperPanel = new JPanel(new BorderLayout(12, 12));
338                 getContentPane().add(upperPanel, BorderLayout.PAGE_START);
339                 contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
340
341                 projectPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
342                 upperPanel.add(projectPane, BorderLayout.CENTER);
343
344                 projectOverviewPanel = new Box(BoxLayout.PAGE_AXIS);
345                 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
346                 projectPane.add(projectOverviewPanel);
347                 projectOverviewPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
348                 projectOverviewPanel.add(Box.createVerticalGlue());
349                 JButton addProjectButton = new JButton(swingInterface.getAddProjectAction());
350                 addProjectButton.setAlignmentX(0.5f);
351                 projectOverviewPanel.add(addProjectButton);
352                 projectOverviewPanel.add(Box.createVerticalGlue());
353
354                 requestTable = new JTable(swingInterface.getRequestTableModel());
355                 getContentPane().add(new JScrollPane(requestTable), BorderLayout.CENTER);
356
357 // JPanel lowerPanel = new JPanel(new BorderLayout(12, 12));
358 // getContentPane().add(lowerPanel, BorderLayout.CENTER);
359         }
360
361         //
362         // INTERFACE I18nable
363         //
364
365         /**
366          * {@inheritDoc}
367          */
368         public void updateI18n() {
369                 swingInterface.getConfigureAction().updateI18n();
370                 swingInterface.getImportConfigAction().updateI18n();
371                 swingInterface.getQuitAction().updateI18n();
372                 swingInterface.getManageNodesAction().updateI18n();
373                 swingInterface.getNodeConnectAction().updateI18n();
374                 connectMenu.updateI18n();
375                 swingInterface.getNodeDisconnectAction().updateI18n();
376                 disconnectMenu.updateI18n();
377                 swingInterface.getAddProjectAction().updateI18n();
378                 swingInterface.getCloneProjectAction().updateI18n();
379                 swingInterface.getDeleteProjectAction().updateI18n();
380                 swingInterface.getHelpAboutAction().updateI18n();
381                 jSiteMenu.updateI18n();
382                 nodeMenu.updateI18n();
383                 languageMenu.updateI18n();
384                 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
385                         languageAction.updateI18n();
386                 }
387                 helpMenu.updateI18n();
388                 getJMenuBar().revalidate();
389                 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
390                 for (int componentIndex = 0; componentIndex < projectPane.getTabCount(); componentIndex++) {
391                         projectPane.setTitleAt(componentIndex, projectPane.getComponentAt(componentIndex).getName());
392                 }
393                 refreshNodeMenuItems();
394                 SwingUtils.repackCentered(this);
395         }
396
397         //
398         // INTERFACE WindowListener
399         //
400
401         /**
402          * {@inheritDoc}
403          */
404         public void windowActivated(WindowEvent e) {
405                 /* do nothing. */
406         }
407
408         /**
409          * {@inheritDoc}
410          */
411         public void windowClosed(WindowEvent e) {
412                 /* do nothing. */
413         }
414
415         /**
416          * {@inheritDoc}
417          */
418         public void windowClosing(WindowEvent e) {
419                 swingInterface.getQuitAction().actionPerformed(null);
420         }
421
422         /**
423          * {@inheritDoc}
424          */
425         public void windowDeactivated(WindowEvent e) {
426                 /* do nothing. */
427         }
428
429         /**
430          * {@inheritDoc}
431          */
432         public void windowDeiconified(WindowEvent e) {
433                 /* do nothing. */
434         }
435
436         /**
437          * {@inheritDoc}
438          */
439         public void windowIconified(WindowEvent e) {
440                 /* do nothing. */
441         }
442
443         /**
444          * {@inheritDoc}
445          */
446         public void windowOpened(WindowEvent e) {
447                 /* do nothing. */
448         }
449
450 }