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