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);
66 * Frame instance for "add own data"
68 private JMenuItem addOwnItem;
71 * Frame instance for "edit own data"
73 private JMenuItem editOwnItem;
78 private final JFrame frame;
81 * Whether this frame has been initialized
83 private boolean isInitialized;
86 * Status label needs to be updated
88 private JLabel statusLabel;
91 * Creates an instance of this frame with a client instance
95 private AddressbookFrame (final Client client) {
97 this.getLogger().trace(MessageFormat.format("client={0}: CALLED!", client));
100 this.frame = new JFrame(AddressbookApplication.printableTitle());
103 this.setClient(client);
107 * Shutdown this frame
110 public void doShutdown () {
111 // First only show shutdown status
112 this.statusLabel.setText(this.getBundle().getString("AddressbookFrame.statusLabel.shutdown.text"));
116 * Setups the frame, do not set isInitialized here
118 * @param client Client instance
121 public void setupFrame (final Client client) {
123 this.getLogger().trace(MessageFormat.format("client={0}: CALLED!", client));
125 // Has the user entered own data?
126 if (this.getClient().getContactManager().isOwnContactAdded()) {
128 this.getLogger().debug("Disabling menus: isOwnContactAdded()=false");
130 // Not entered yet, so disable "add" menu
131 this.addOwnItem.setEnabled(false);
134 this.editOwnItem.setEnabled(false);
137 // Make the frame visible
138 this.frame.setVisible(true);
141 this.statusLabel.setText(this.getBundle().getString("AddressbookFrame.statusLabel.done.text"));
145 * Initalizes this frame. Having initComponents() exposed (publicly
146 * accessible) means that any other object can initialize components which
149 * @throws org.mxchange.addressbook.FrameAlreadyInitializedException If this method has been called twice
152 public void init () throws FrameAlreadyInitializedException {
154 this.getLogger().trace("CALLED!");
156 // Has this frame been initialized?
157 if (this.isInitialized()) {
159 throw new FrameAlreadyInitializedException();
163 this.initComponents();
166 this.isInitialized = true;
170 * Returns field isInitialized. This flag indicates whether this frame has been initialized or not.
172 * @return Field isInitialized
175 public final boolean isInitialized () {
176 return this.isInitialized;
180 * Initialize components
182 private void initComponents () {
184 this.getLogger().trace("CALLED!");
186 // Set default close operation
187 this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
189 // Register shutdown listener
190 this.frame.addWindowListener(new WindowAdapter() {
192 * Invoked when a window has been closed.
195 public void windowClosed(final WindowEvent e) {
196 // Shutdown application cleanly
197 self.getClient().getApplication().doShutdown();
201 * Invoked when a window is in the process of being closed.
202 * The close operation can be overridden at this point.
205 public void windowClosing(final WindowEvent e) {
206 // Also shutdown cleanly here
207 self.getClient().getApplication().doShutdown();
211 // Setup layout manager
212 this.frame.setLayout(new BorderLayout(2, 2));
215 this.frame.setSize(700, 400);
217 // Center window in middle of screen, instead of top-left corner
218 this.frame.setLocationRelativeTo(null);
223 // Init status label (which needs to be updated
224 this.statusLabel = new JLabel(this.getBundle().getString("AddressbookFrame.statusLabel.initializing.text"));
231 * Initializes the menu system
233 private void initMenuSystem () {
234 // Init menu bar, menu and item instances
235 JMenuBar menuBar = new JMenuBar();
241 menu = new JMenu(this.getBundle().getString("AddressbookFrame.menu.file.text"));
244 // 1.x) Exit program (should be last)
245 item = new JMenuItem(this.getBundle().getString("AddressbookFrame.menuItem.exitProgram.text"));
246 item.setToolTipText(this.getBundle().getString("AddressbookFrame.menuItem.exitProgram.toolTipText"));
248 // Add listener to exit menu
249 item.addActionListener(new ActionListener() {
251 * If the user has performed this action
253 * @param e An instance of an ActionEvent class
256 public void actionPerformed (final ActionEvent e) {
257 self.getClient().getApplication().doShutdown();
264 // Add menu -> menu bar
268 // 2) Addressbook menu
269 menu = new JMenu(this.getBundle().getString("AddressbookFrame.menu.addressbook.text"));
272 this.addOwnItem = new JMenuItem(this.getBundle().getString("AddressbookFrame.menuItem.addOwnData.text"));
273 this.addOwnItem.setToolTipText(this.getBundle().getString("AddressbookFrame.menuItem.addOwnData.toolTipText"));
275 // Add listener to exit menu
276 this.addOwnItem.addActionListener(new ActionListener() {
278 * If the user has performed this action
280 * @param e An instance of an ActionEvent class
283 public void actionPerformed (final ActionEvent e) {
284 self.getClient().getContactManager().doEnterOwnData();
289 menu.add(this.addOwnItem);
291 // 2.2) Edit own data
292 this.editOwnItem = new JMenuItem(this.getBundle().getString("AddressbookFrame.menuItem.editOwnData.text"));
293 this.editOwnItem.setToolTipText(this.getBundle().getString("AddressbookFrame.menuItem.editOwnData.toolTipText"));
295 // Add listener to exit menu
296 this.editOwnItem.addActionListener(new ActionListener() {
298 * If the user has performed this action
300 * @param e An instance of an ActionEvent class
303 public void actionPerformed (final ActionEvent e) {
304 self.getClient().getContactManager().doChangeOwnData();
309 menu.add(this.editOwnItem);
311 // Add menu -> menu bar
314 // Add menu bar -> frame
315 this.frame.add(menuBar, BorderLayout.NORTH);
319 * Initializes status panel
321 private void initStatusPanel () {
322 // Init status bar in south
323 JPanel panel = new JPanel();
324 panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
325 panel.add(this.statusLabel);
326 panel.setBorder(BorderFactory.createEtchedBorder());
328 // Add panel to frame
329 this.frame.add(panel, BorderLayout.SOUTH);