implement node addition and removal events
[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
29 import javax.swing.Action;
30 import javax.swing.Box;
31 import javax.swing.BoxLayout;
32 import javax.swing.JButton;
33 import javax.swing.JFrame;
34 import javax.swing.JMenuBar;
35 import javax.swing.JMenuItem;
36 import javax.swing.JPanel;
37 import javax.swing.JTabbedPane;
38 import javax.swing.JToolBar;
39 import javax.swing.SwingConstants;
40 import javax.swing.border.EmptyBorder;
41
42 import net.pterodactylus.jsite.core.Project;
43 import net.pterodactylus.jsite.i18n.I18n;
44 import net.pterodactylus.jsite.i18n.I18nable;
45 import net.pterodactylus.jsite.i18n.gui.I18nAction;
46 import net.pterodactylus.jsite.i18n.gui.I18nMenu;
47 import net.pterodactylus.jsite.main.Version;
48 import net.pterodactylus.util.swing.StatusBar;
49 import net.pterodactylus.util.swing.SwingUtils;
50
51 /**
52  * Defines the main window of the application.
53  *
54  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
55  * @version $Id$
56  */
57 public class MainWindow extends JFrame implements WindowListener, I18nable {
58
59         /** The swing interface that receives all actions. */
60         private final SwingInterface swingInterface;
61
62         /** The status bar. */
63         private StatusBar statusBar = new StatusBar();
64
65         /** The content pane. */
66         private JPanel contentPane = new JPanel(new BorderLayout(12, 12));
67
68         /** The jSite menu. */
69         private I18nMenu jSiteMenu;
70
71         /** The node menu. */
72         private I18nMenu nodeMenu;
73
74         /** The “connect” (advanced mode) menu. */
75         private I18nMenu connectMenu;
76
77         /** The “connect” (simple mode) menu. */
78         private JMenuItem connectMenuItem;
79
80         /** The “disconnect” (advanced mode) menu. */
81         private I18nMenu disconnectMenu;
82
83         /** The “diconnect” (simple mode) menu item. */
84         private JMenuItem disconnectMenuItem;
85
86         /** The language menu. */
87         private I18nMenu languageMenu;
88
89         /** The about menu. */
90         private I18nMenu helpMenu;
91
92         /** The tabbed project pane. */
93         private JTabbedPane projectPane;
94
95         /** The project overview panel. */
96         private Box projectOverviewPanel;
97
98         /**
99          * Creates a new main window that redirects all actions to the given swing
100          * interface.
101          *
102          * @param swingInterface
103          *            The swing interface to receive all actions
104          */
105         public MainWindow(SwingInterface swingInterface) {
106                 super("jSite " + Version.getVersion());
107                 this.swingInterface = swingInterface;
108                 initWindow();
109                 setPreferredSize(new Dimension(480, 280));
110                 pack();
111                 SwingUtils.center(this);
112                 I18n.registerI18nable(this);
113                 addWindowListener(this);
114         }
115
116         //
117         // ACCESSORS
118         //
119
120         /**
121          * Sets the text of the status bar.
122          *
123          * @param text
124          *            The text of the status bar
125          */
126         public void setStatusBarText(String text) {
127                 statusBar.setText(text);
128         }
129
130         /**
131          * Sets whether the advanced mode is activated.
132          *
133          * @param advancedMode
134          *            <code>true</code> if the advanced mode is activated,
135          *            <code>false</code> if the simple mode is activated
136          */
137         public void setAdvancedMode(boolean advancedMode) {
138                 connectMenu.setVisible(advancedMode);
139                 connectMenuItem.setVisible(!advancedMode);
140                 disconnectMenu.setVisible(advancedMode);
141                 disconnectMenuItem.setVisible(!advancedMode);
142         }
143
144         /**
145          * {@inheritDoc}
146          */
147         @Override
148         public Container getContentPane() {
149                 return contentPane;
150         }
151
152         /**
153          * Returns the currently selected project.
154          *
155          * @return The currently selected project
156          */
157         public Project getSelectedProject() {
158                 return null;
159         }
160
161         //
162         // ACTIONS
163         //
164
165         /**
166          * Refreshes the menu items in the “connect” and “disconnect” menus.
167          */
168         void refreshNodeMenuItems() {
169                 connectMenu.removeAll();
170                 for (Action nodeConnectAction: swingInterface.getNodeConnectActions()) {
171                         connectMenu.add(nodeConnectAction);
172                 }
173                 if (connectMenu.getMenuComponentCount() == 0) {
174                         JMenuItem noNodeAvailableItem = new JMenuItem(I18n.get("mainWindow.menu.connectNoNodeAvailable.name"));
175                         noNodeAvailableItem.setEnabled(false);
176                         connectMenu.add(noNodeAvailableItem);
177                 }
178                 disconnectMenu.removeAll();
179                 for (Action nodeDisconnectAction: swingInterface.getNodeDisconnectActions()) {
180                         disconnectMenu.add(nodeDisconnectAction);
181                 }
182                 if (disconnectMenu.getMenuComponentCount() == 0) {
183                         JMenuItem noNodeAvailableItem = new JMenuItem(I18n.get("mainWindow.menu.disconnectNoNodeAvailable.name"));
184                         noNodeAvailableItem.setEnabled(false);
185                         disconnectMenu.add(noNodeAvailableItem);
186                 }
187         }
188
189         //
190         // PRIVATE METHODS
191         //
192
193         /**
194          * Initializes the window by creating all its components.
195          */
196         private void initWindow() {
197                 JMenuBar menuBar = new JMenuBar();
198
199                 jSiteMenu = new I18nMenu("mainWindow.menu.jSite");
200                 menuBar.add(jSiteMenu);
201
202                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getConfigureAction()));
203                 jSiteMenu.addSeparator();
204                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getImportConfigAction()));
205                 jSiteMenu.addSeparator();
206                 jSiteMenu.add(new FixedJMenuItem(swingInterface.getQuitAction()));
207
208                 connectMenu = new I18nMenu("mainWindow.menu.connect");
209                 disconnectMenu = new I18nMenu("mainWindow.menu.disconnect");
210
211                 nodeMenu = new I18nMenu("mainWindow.menu.node");
212                 menuBar.add(nodeMenu);
213
214                 nodeMenu.add(new FixedJMenuItem(swingInterface.getManageNodesAction()));
215                 nodeMenu.addSeparator();
216                 nodeMenu.add(connectMenuItem = new FixedJMenuItem(swingInterface.getNodeConnectAction()));
217                 nodeMenu.add(connectMenu);
218                 nodeMenu.add(disconnectMenuItem = new FixedJMenuItem(swingInterface.getNodeDisconnectAction()));
219                 nodeMenu.add(disconnectMenu);
220                 refreshNodeMenuItems();
221
222                 languageMenu = new I18nMenu("mainWindow.menu.language");
223                 menuBar.add(languageMenu);
224
225                 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
226                         languageMenu.add(new FixedJMenuItem(languageAction));
227                 }
228
229                 JPanel spacerPanel = new JPanel();
230                 spacerPanel.setOpaque(false);
231                 menuBar.add(spacerPanel);
232
233                 helpMenu = new I18nMenu("mainWindow.menu.help");
234                 menuBar.add(helpMenu);
235
236                 helpMenu.add(new FixedJMenuItem(swingInterface.getHelpAboutAction()));
237
238                 setJMenuBar(menuBar);
239
240                 JToolBar toolBar = new JToolBar(I18n.get("mainWindow.toolbar.name"));
241                 toolBar.add(swingInterface.getManageNodesAction());
242                 toolBar.addSeparator();
243                 toolBar.add(swingInterface.getNodeConnectAction());
244                 toolBar.add(swingInterface.getNodeDisconnectAction());
245                 super.getContentPane().add(toolBar, BorderLayout.PAGE_START);
246
247                 super.getContentPane().add(contentPane, BorderLayout.CENTER);
248
249                 addWindowListener(new WindowAdapter() {
250
251                         /**
252                          * {@inheritDoc}
253                          */
254                         @SuppressWarnings("synthetic-access")
255                         @Override
256                         public void windowClosing(WindowEvent windowEvent) {
257                                 swingInterface.getQuitAction().actionPerformed(null);
258                         }
259                 });
260
261                 initComponents();
262         }
263
264         /**
265          * Initializes all components of this window.
266          */
267         private void initComponents() {
268                 super.getContentPane().add(statusBar, BorderLayout.PAGE_END);
269
270                 /*
271                  * the main window consists of two panels which are vertically oriented.
272                  * the upper panel contains of a tabbed pane, the lower panel consists
273                  * of a table that lists the running requests.
274                  */
275
276                 JPanel upperPanel = new JPanel(new BorderLayout(12, 12));
277                 getContentPane().add(upperPanel, BorderLayout.PAGE_START);
278                 contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
279
280                 projectPane = new JTabbedPane(SwingConstants.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
281                 upperPanel.add(projectPane, BorderLayout.CENTER);
282
283                 projectOverviewPanel = new Box(BoxLayout.PAGE_AXIS);
284                 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
285                 projectPane.add(projectOverviewPanel);
286                 projectOverviewPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
287                 projectOverviewPanel.add(Box.createVerticalGlue());
288                 JButton addProjectButton = new JButton(swingInterface.getAddProjectAction());
289                 addProjectButton.setAlignmentX(0.5f);
290                 projectOverviewPanel.add(addProjectButton);
291                 projectOverviewPanel.add(Box.createVerticalGlue());
292
293 // JPanel lowerPanel = new JPanel(new BorderLayout(12, 12));
294 // getContentPane().add(lowerPanel, BorderLayout.CENTER);
295         }
296
297         //
298         // INTERFACE I18nable
299         //
300
301         /**
302          * {@inheritDoc}
303          */
304         public void updateI18n() {
305                 swingInterface.getConfigureAction().updateI18n();
306                 swingInterface.getImportConfigAction().updateI18n();
307                 swingInterface.getQuitAction().updateI18n();
308                 swingInterface.getManageNodesAction().updateI18n();
309                 swingInterface.getNodeConnectAction().updateI18n();
310                 connectMenu.updateI18n();
311                 swingInterface.getNodeDisconnectAction().updateI18n();
312                 disconnectMenu.updateI18n();
313                 swingInterface.getAddProjectAction().updateI18n();
314                 swingInterface.getCloneProjectAction().updateI18n();
315                 swingInterface.getDeleteProjectAction().updateI18n();
316                 swingInterface.getHelpAboutAction().updateI18n();
317                 jSiteMenu.updateI18n();
318                 nodeMenu.updateI18n();
319                 languageMenu.updateI18n();
320                 for (I18nAction languageAction: swingInterface.getLanguageActions()) {
321                         languageAction.updateI18n();
322                 }
323                 helpMenu.updateI18n();
324                 getJMenuBar().revalidate();
325                 projectOverviewPanel.setName(I18n.get("mainWindow.pane.overview.title"));
326                 for (int componentIndex = 0; componentIndex < projectPane.getTabCount(); componentIndex++) {
327                         projectPane.setTitleAt(componentIndex, projectPane.getComponentAt(componentIndex).getName());
328                 }
329                 refreshNodeMenuItems();
330                 SwingUtils.repackCentered(this);
331         }
332
333         //
334         // INTERFACE WindowListener
335         //
336         
337         /**
338          * {@inheritDoc}
339          */
340         public void windowActivated(WindowEvent e) {
341                 /* do nothing. */
342         }
343
344         /**
345          * {@inheritDoc}
346          */
347         public void windowClosed(WindowEvent e) {
348                 /* do nothing. */
349         }
350
351         /**
352          * {@inheritDoc}
353          */
354         public void windowClosing(WindowEvent e) {
355                 swingInterface.getQuitAction().actionPerformed(null);
356         }
357
358         /**
359          * {@inheritDoc}
360          */
361         public void windowDeactivated(WindowEvent e) {
362                 /* do nothing. */
363         }
364
365         /**
366          * {@inheritDoc}
367          */
368         public void windowDeiconified(WindowEvent e) {
369                 /* do nothing. */
370         }
371
372         /**
373          * {@inheritDoc}
374          */
375         public void windowIconified(WindowEvent e) {
376                 /* do nothing. */
377         }
378
379         /**
380          * {@inheritDoc}
381          */
382         public void windowOpened(WindowEvent e) {
383                 /* do nothing. */
384         }
385
386 }