switch to new project when project is created
[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          * @param switchToProject
254          *            <code>true</code> to switch to the new panel,
255          *            <code>false</code> to not change the current panel
256          */
257         void addProject(Project project, boolean switchToProject) {
258                 ProjectPanel projectPanel = new ProjectPanel(swingInterface, project);
259                 int newTabIndex = projectPane.getTabCount();
260                 projectPane.add(project.getName(), projectPanel);
261                 projectPane.setToolTipTextAt(newTabIndex, project.getDescription());
262                 project.addPropertyChangeListener(this);
263                 if (switchToProject) {
264                         projectPane.setSelectedIndex(newTabIndex);
265                 }
266         }
267
268         //
269         // PRIVATE METHODS
270         //
271
272         /**
273          * Initializes the window by creating all its components.
274          */
275         private void initWindow() {
276                 JMenuBar menuBar = new JMenuBar();
277
278                 jSiteMenu = new I18nMenu("mainWindow.menu.jSite");
279                 menuBar.add(jSiteMenu);
280
281                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getConfigureAction()));
282                 jSiteMenu.addSeparator();
283                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getImportConfigAction()));
284                 jSiteMenu.addSeparator();
285                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getQuitAction()));
286
287                 connectMenu = new I18nMenu("mainWindow.menu.connect");
288                 disconnectMenu = new I18nMenu("mainWindow.menu.disconnect");
289
290                 nodeMenu = new I18nMenu("mainWindow.menu.node");
291                 menuBar.add(nodeMenu);
292
293                 nodeMenu.add(new FixedJMenuItem(swingInterface.getManageNodesAction()));
294                 nodeMenu.addSeparator();
295                 nodeMenu.add(connectMenuItem = new FixedJMenuItem(swingInterface.getNodeConnectAction()));
296                 nodeMenu.add(connectMenu);
297                 nodeMenu.add(disconnectMenuItem = new FixedJMenuItem(swingInterface.getNodeDisconnectAction()));
298                 nodeMenu.add(disconnectMenu);
299                 refreshNodeMenuItems();
300
301                 languageMenu = new I18nMenu("mainWindow.menu.language");
302                 menuBar.add(languageMenu);
303
304                 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
305                         languageMenu.add(new FixedJMenuItem(languageAction));
306                 }
307
308                 JPanel spacerPanel = new JPanel();
309                 spacerPanel.setOpaque(false);
310                 menuBar.add(spacerPanel);
311
312                 helpMenu = new I18nMenu("mainWindow.menu.help");
313                 menuBar.add(helpMenu);
314
315                 helpMenu.add(new FixedJMenuItem(swingInterface.getHelpAboutAction()));
316
317                 setJMenuBar(menuBar);
318
319                 JToolBar toolBar = new JToolBar(I18n.get("mainWindow.toolbar.name"));
320                 toolBar.add(swingInterface.getManageNodesAction());
321                 toolBar.addSeparator();
322                 toolBar.add(swingInterface.getNodeConnectAction());
323                 toolBar.add(swingInterface.getNodeDisconnectAction());
324                 super.getContentPane().add(toolBar, BorderLayout.PAGE_START);
325
326                 super.getContentPane().add(contentPane, BorderLayout.CENTER);
327
328                 addWindowListener(new WindowAdapter() {
329
330                         /**
331                          * {@inheritDoc}
332                          */
333                         @SuppressWarnings("synthetic-access")
334                         @Override
335                         public void windowClosing(WindowEvent windowEvent) {
336                                 swingInterface.getQuitAction().actionPerformed(null);
337                         }
338                 });
339
340                 initComponents();
341         }
342
343         /**
344          * Initializes all components of this window.
345          */
346         private void initComponents() {
347                 super.getContentPane().add(statusBar, BorderLayout.PAGE_END);
348
349                 /*
350                  * the main window consists of two panels which are vertically oriented.
351                  * the upper panel contains of a tabbed pane, the lower panel consists
352                  * of a table that lists the running requests.
353                  */
354
355                 JPanel upperPanel = new JPanel(new BorderLayout(12, 12));
356                 getContentPane().add(upperPanel, BorderLayout.PAGE_START);
357                 contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
358
359                 projectPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
360                 upperPanel.add(projectPane, BorderLayout.CENTER);
361
362                 projectOverviewPanel = new Box(BoxLayout.PAGE_AXIS);
363                 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
364                 projectPane.add(projectOverviewPanel);
365                 projectOverviewPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
366                 projectOverviewPanel.add(Box.createVerticalGlue());
367                 JButton addProjectButton = new JButton(swingInterface.getAddProjectAction());
368                 addProjectButton.setAlignmentX(0.5f);
369                 projectOverviewPanel.add(addProjectButton);
370                 projectOverviewPanel.add(Box.createVerticalGlue());
371
372                 requestTable = new JTable(swingInterface.getRequestTableModel());
373                 getContentPane().add(new JScrollPane(requestTable), BorderLayout.CENTER);
374
375                 // JPanel lowerPanel = new JPanel(new BorderLayout(12, 12));
376                 // getContentPane().add(lowerPanel, BorderLayout.CENTER);
377         }
378
379         //
380         // INTERFACE I18nable
381         //
382
383         /**
384          * {@inheritDoc}
385          */
386         public void updateI18n() {
387                 swingInterface.getConfigureAction().updateI18n();
388                 swingInterface.getImportConfigAction().updateI18n();
389                 swingInterface.getQuitAction().updateI18n();
390                 swingInterface.getManageNodesAction().updateI18n();
391                 swingInterface.getNodeConnectAction().updateI18n();
392                 connectMenu.updateI18n();
393                 swingInterface.getNodeDisconnectAction().updateI18n();
394                 disconnectMenu.updateI18n();
395                 swingInterface.getAddProjectAction().updateI18n();
396                 swingInterface.getCloneProjectAction().updateI18n();
397                 swingInterface.getDeleteProjectAction().updateI18n();
398                 swingInterface.getHelpAboutAction().updateI18n();
399                 jSiteMenu.updateI18n();
400                 nodeMenu.updateI18n();
401                 languageMenu.updateI18n();
402                 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
403                         languageAction.updateI18n();
404                 }
405                 helpMenu.updateI18n();
406                 getJMenuBar().revalidate();
407                 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
408                 for (int componentIndex = 0; componentIndex < projectPane.getTabCount(); componentIndex++) {
409                         projectPane.setTitleAt(componentIndex, projectPane.getComponentAt(componentIndex).getName());
410                 }
411                 refreshNodeMenuItems();
412                 SwingUtils.repackCentered(this);
413         }
414
415         //
416         // INTERFACE WindowListener
417         //
418
419         /**
420          * {@inheritDoc}
421          */
422         public void windowActivated(WindowEvent e) {
423                 /* do nothing. */
424         }
425
426         /**
427          * {@inheritDoc}
428          */
429         public void windowClosed(WindowEvent e) {
430                 /* do nothing. */
431         }
432
433         /**
434          * {@inheritDoc}
435          */
436         public void windowClosing(WindowEvent e) {
437                 swingInterface.getQuitAction().actionPerformed(null);
438         }
439
440         /**
441          * {@inheritDoc}
442          */
443         public void windowDeactivated(WindowEvent e) {
444                 /* do nothing. */
445         }
446
447         /**
448          * {@inheritDoc}
449          */
450         public void windowDeiconified(WindowEvent e) {
451                 /* do nothing. */
452         }
453
454         /**
455          * {@inheritDoc}
456          */
457         public void windowIconified(WindowEvent e) {
458                 /* do nothing. */
459         }
460
461         /**
462          * {@inheritDoc}
463          */
464         public void windowOpened(WindowEvent e) {
465                 /* do nothing. */
466         }
467
468         //
469         // INTERFACE PropertyChangeListener
470         //
471
472         /**
473          * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
474          */
475         public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
476                 Object eventSource = propertyChangeEvent.getSource();
477                 String propertyName = propertyChangeEvent.getPropertyName();
478                 if (eventSource instanceof Project) {
479                         /* if a project was changed, update the tab title and tooltip. */
480                         if (Project.PROPERTY_NAME.equals(propertyName) || Project.PROPERTY_DESCRIPTION.equals(propertyName)) {
481                                 Project project = (Project) eventSource;
482                                 int tabCount = projectPane.getTabCount();
483                                 for (int tabIndex = 0; tabIndex < tabCount; tabIndex++) {
484                                         Component tabComponent = projectPane.getComponentAt(tabIndex);
485                                         if (tabComponent instanceof ProjectPanel) {
486                                                 Project tabProject = ((ProjectPanel) tabComponent).getProject();
487                                                 if (tabProject.equals(project)) {
488                                                         projectPane.setTitleAt(tabIndex, project.getName());
489                                                         projectPane.setToolTipTextAt(tabIndex, project.getDescription());
490                                                         projectPane.repaint();
491                                                 }
492                                         }
493                                 }
494                         }
495                 }
496         }
497
498 }