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