]> git.mxchange.org Git - addressbook-lib.git/blob - Addressbook/src/org/mxchange/addressbook/client/gui/AddressbookFrame.java
679684641897d4b3963daf725b13775f1612de93
[addressbook-lib.git] / Addressbook / src / org / mxchange / addressbook / client / gui / AddressbookFrame.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.addressbook.client.gui;
18
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;
37
38 /**
39  *
40  * @author Roland Haeder
41  */
42 public class AddressbookFrame extends BaseFrameworkSystem implements ClientFrame {
43
44     /**
45      * Own instance
46      */
47     private static ClientFrame self;
48
49     /**
50      * Singelton getter for this frame instance.
51      *
52      * @param client Client instance
53      * @return Returns a singelton instance of this frame
54      */
55     public static final ClientFrame getSelfInstance (final Client client) {
56         // Is it set?
57         if (!(self instanceof ClientFrame)) {
58             // Create new instance
59             self = new AddressbookFrame(client);
60         }
61         
62         // Return instance
63         return self;
64     }
65
66     /**
67      * Frame instance
68      */
69     private final JFrame frame;
70
71     /**
72      * Whether this frame has been initialized
73      */
74     private boolean isInitialized;
75
76     /**
77      * Status label needs to be updated
78      */
79     private JLabel statusLabel;
80
81     /**
82      * Creates an instance of this frame with a client instance
83      * 
84      * @param client
85      */
86     private AddressbookFrame (final Client client) {
87         // Debug line
88         this.getLogger().trace(MessageFormat.format("client={0}: CALLED!", client));
89
90         // Set frame instance
91         this.frame = new JFrame(AddressbookApplication.printableTitle());
92
93         // Set client here
94         this.setClient(client);
95     }
96
97     /**
98      * Shutdown this frame
99      */
100     @Override
101     public void doShutdown () {
102         // First only show shutdown status
103         this.statusLabel.setText(this.getBundle().getString("AddressbookFrame.statusLabel.shutdown.text"));
104     }
105
106     /**
107      * Setups the frame, do not set isInitialized here
108      * 
109      * @param client Client instance
110      */
111     @Override
112     public void setupFrame (final Client client) {
113         // Debug line
114         this.getLogger().trace(MessageFormat.format("client={0}: CALLED!", client));
115
116         // Has the user entered own data?
117         if (this.getClient().getContactManager().isOwnContactAdded()) {
118             // Debug message
119             this.getLogger().debug("Disabling menus: isOwnContactAdded()=false");
120
121             // Not entered yet, so enable menu
122             //addOwnData.setEnabled(false);
123         }
124
125         // Make the frame visible
126         this.frame.setVisible(true);
127
128         // All done here
129         this.statusLabel.setText(this.getBundle().getString("AddressbookFrame.statusLabel.done.text"));
130     }
131
132     /**
133      * Initalizes this frame. Having initComponents() exposed (publicly
134      * accessible) means that any other object can initialize components which
135      * you may not want.
136      * 
137      * @throws org.mxchange.addressbook.FrameAlreadyInitializedException If this method has been called twice
138      */
139     @Override
140     public void initFrame () throws FrameAlreadyInitializedException {
141         // Debug line
142         this.getLogger().trace("CALLED!");
143
144         // Has this frame been initialized?
145         if (this.isInitialized()) {
146             // Throw exception
147             throw new FrameAlreadyInitializedException();
148         }
149
150         // Init components
151         this.initComponents();
152
153         // Set flag
154         this.isInitialized = true;
155     }
156
157     /**
158      * Returns field isInitialized. This flag indicates whether this frame has been initialized or not.
159      * 
160      * @return Field isInitialized
161      */
162     @Override
163     public final boolean isInitialized () {
164         return this.isInitialized;
165     }
166
167     /**
168      * Initialize components
169      */
170     private void initComponents () {
171         // Debug line
172         this.getLogger().trace("CALLED!");
173
174         // Set default close operation
175         this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
176
177         // Register shutdown listener
178         this.frame.addWindowListener(new WindowAdapter() {
179             /**
180              * Invoked when a window has been closed.
181              */
182             @Override
183             public void windowClosed(final WindowEvent e) {
184                 // Shutdown application cleanly
185                 self.getClient().getApplication().doShutdown();
186             }
187
188             /**
189              * Invoked when a window is in the process of being closed.
190              * The close operation can be overridden at this point.
191              */
192             @Override
193             public void windowClosing(final WindowEvent e) {
194                 // Also shutdown cleanly here
195                 self.getClient().getApplication().doShutdown();
196             }
197         });
198
199         // Setup layout manager
200         this.frame.setLayout(new BorderLayout(2, 2));
201
202         // Set window size
203         this.frame.setSize(700, 400);
204
205         // Center window in middle of screen, instead of top-left corner
206         this.frame.setLocationRelativeTo(null);
207
208         // Init menu bar, menu and item instances
209         JMenuBar menuBar = new JMenuBar();
210         JMenu menu;
211         JMenuItem item;
212
213         // Init some menus:
214         // 1) File menu
215         menu = new JMenu(this.getBundle().getString("AddressbookFrame.menu.file.text"));
216
217         // Add menu items:
218         // 1.x) Exit program (should be last)
219         item = new JMenuItem(this.getBundle().getString("AddressbookFrame.menuItem.exitProgram.text"));
220         item.setToolTipText(this.getBundle().getString("AddressbookFrame.menuItem.exitProgram.toolTipText"));
221
222         // Add listener to exit menu
223         item.addActionListener(new ActionListener() {
224             /**
225              * If the user has performed this action
226              *
227              * @param e An instance of an ActionEvent class
228              */
229             @Override
230             public void actionPerformed (final ActionEvent e) {
231                 self.getClient().getApplication().doShutdown();
232             }
233         });
234
235         // Add item -> menu
236         menu.add(item);
237
238         // Add menu -> menu bar
239         menuBar.add(menu);
240
241         // Add menu bar -> frame
242         this.frame.add(menuBar, BorderLayout.NORTH);
243
244         // Init status label (which needs to be updated
245         this.statusLabel = new JLabel(this.getBundle().getString("AddressbookFrame.statusLabel.initializing.text"));
246
247         // Init status bar in south
248         JPanel panel = new JPanel();
249         panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
250         panel.add(this.statusLabel);
251         panel.setBorder(BorderFactory.createEtchedBorder());
252
253         // Add panel to frame
254         this.frame.add(panel, BorderLayout.SOUTH);
255     }
256
257 }