2 * Copyright (C) 2015 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.addressbook.client.gui;
19 import java.awt.BorderLayout;
20 import java.awt.event.ActionEvent;
21 import java.awt.event.ActionListener;
22 import java.awt.event.WindowAdapter;
23 import java.awt.event.WindowEvent;
24 import java.text.MessageFormat;
25 import javax.swing.BorderFactory;
26 import javax.swing.BoxLayout;
27 import javax.swing.JFrame;
28 import javax.swing.JLabel;
29 import javax.swing.JMenu;
30 import javax.swing.JMenuBar;
31 import javax.swing.JMenuItem;
32 import javax.swing.JPanel;
33 import org.mxchange.addressbook.BaseFrameworkSystem;
34 import org.mxchange.addressbook.FrameAlreadyInitializedException;
35 import org.mxchange.addressbook.application.AddressbookApplication;
36 import org.mxchange.addressbook.client.Client;
40 * @author Roland Haeder
42 public class AddressbookFrame extends BaseFrameworkSystem implements ClientFrame {
47 private static ClientFrame self;
50 * Singelton getter for this frame instance.
52 * @param client Client instance
53 * @return Returns a singelton instance of this frame
55 public static final ClientFrame getSelfInstance (final Client client) {
57 if (!(self instanceof ClientFrame)) {
58 // Create new instance
59 self = new AddressbookFrame(client);
69 private final JFrame frame;
72 * Whether this frame has been initialized
74 private boolean isInitialized;
77 * Status label needs to be updated
79 private JLabel statusLabel;
84 private JMenuItem addOwnItem;
87 * Creates an instance of this frame with a client instance
91 private AddressbookFrame (final Client client) {
93 this.getLogger().trace(MessageFormat.format("client={0}: CALLED!", client));
96 this.frame = new JFrame(AddressbookApplication.printableTitle());
99 this.setClient(client);
103 * Shutdown this frame
106 public void doShutdown () {
107 // First only show shutdown status
108 this.statusLabel.setText(this.getBundle().getString("AddressbookFrame.statusLabel.shutdown.text"));
112 * Setups the frame, do not set isInitialized here
114 * @param client Client instance
117 public void setupFrame (final Client client) {
119 this.getLogger().trace(MessageFormat.format("client={0}: CALLED!", client));
121 // Has the user entered own data?
122 if (this.getClient().getContactManager().isOwnContactAdded()) {
124 this.getLogger().debug("Disabling menus: isOwnContactAdded()=false");
126 // Not entered yet, so enable menu
127 this.addOwnItem.setEnabled(false);
130 // Make the frame visible
131 this.frame.setVisible(true);
134 this.statusLabel.setText(this.getBundle().getString("AddressbookFrame.statusLabel.done.text"));
138 * Initalizes this frame. Having initComponents() exposed (publicly
139 * accessible) means that any other object can initialize components which
142 * @throws org.mxchange.addressbook.FrameAlreadyInitializedException If this method has been called twice
145 public void initFrame () throws FrameAlreadyInitializedException {
147 this.getLogger().trace("CALLED!");
149 // Has this frame been initialized?
150 if (this.isInitialized()) {
152 throw new FrameAlreadyInitializedException();
156 this.initComponents();
159 this.isInitialized = true;
163 * Returns field isInitialized. This flag indicates whether this frame has been initialized or not.
165 * @return Field isInitialized
168 public final boolean isInitialized () {
169 return this.isInitialized;
173 * Initialize components
175 private void initComponents () {
177 this.getLogger().trace("CALLED!");
179 // Set default close operation
180 this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
182 // Register shutdown listener
183 this.frame.addWindowListener(new WindowAdapter() {
185 * Invoked when a window has been closed.
188 public void windowClosed(final WindowEvent e) {
189 // Shutdown application cleanly
190 self.getClient().getApplication().doShutdown();
194 * Invoked when a window is in the process of being closed.
195 * The close operation can be overridden at this point.
198 public void windowClosing(final WindowEvent e) {
199 // Also shutdown cleanly here
200 self.getClient().getApplication().doShutdown();
204 // Setup layout manager
205 this.frame.setLayout(new BorderLayout(2, 2));
208 this.frame.setSize(700, 400);
210 // Center window in middle of screen, instead of top-left corner
211 this.frame.setLocationRelativeTo(null);
216 // Init status label (which needs to be updated
217 this.statusLabel = new JLabel(this.getBundle().getString("AddressbookFrame.statusLabel.initializing.text"));
219 // Init status bar in south
220 JPanel panel = new JPanel();
221 panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
222 panel.add(this.statusLabel);
223 panel.setBorder(BorderFactory.createEtchedBorder());
225 // Add panel to frame
226 this.frame.add(panel, BorderLayout.SOUTH);
230 * Initializes the menu system
232 private void initMenuSystem () {
233 // Init menu bar, menu and item instances
234 JMenuBar menuBar = new JMenuBar();
240 menu = new JMenu(this.getBundle().getString("AddressbookFrame.menu.file.text"));
243 // 1.x) Exit program (should be last)
244 item = new JMenuItem(this.getBundle().getString("AddressbookFrame.menuItem.exitProgram.text"));
245 item.setToolTipText(this.getBundle().getString("AddressbookFrame.menuItem.exitProgram.toolTipText"));
247 // Add listener to exit menu
248 item.addActionListener(new ActionListener() {
250 * If the user has performed this action
252 * @param e An instance of an ActionEvent class
255 public void actionPerformed (final ActionEvent e) {
256 self.getClient().getApplication().doShutdown();
263 // Add menu -> menu bar
267 // 2) Addressbook menu
268 menu = new JMenu(this.getBundle().getString("AddressbookFrame.menu.addressbook.text"));
271 this.addOwnItem = new JMenuItem(this.getBundle().getString("AddressbookFrame.menuItem.addOwnData.text"));
272 this.addOwnItem.setToolTipText(this.getBundle().getString("AddressbookFrame.menuItem.addOwnData.toolTipText"));
274 // Add listener to exit menu
275 this.addOwnItem.addActionListener(new ActionListener() {
277 * If the user has performed this action
279 * @param e An instance of an ActionEvent class
282 public void actionPerformed (final ActionEvent e) {
283 self.getClient().getContactManager().doEnterOwnData();
288 menu.add(this.addOwnItem);
290 // Add menu -> menu bar
293 // Add menu bar -> frame
294 this.frame.add(menuBar, BorderLayout.NORTH);