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