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