]> git.mxchange.org Git - jaddressbook-share-lib.git/blob - Addressbook/src/org/mxchange/addressbook/client/gui/AddressbookFrame.java
0cd0a3fc48eb6dc3b99e1a531454ad1747e2c961
[jaddressbook-share-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      * Frame instance
83      */
84     private JMenuItem addOwnItem;
85
86     /**
87      * Creates an instance of this frame with a client instance
88      * 
89      * @param client
90      */
91     private AddressbookFrame (final Client client) {
92         // Debug line
93         this.getLogger().trace(MessageFormat.format("client={0}: CALLED!", client));
94
95         // Set frame instance
96         this.frame = new JFrame(AddressbookApplication.printableTitle());
97
98         // Set client here
99         this.setClient(client);
100     }
101
102     /**
103      * Shutdown this frame
104      */
105     @Override
106     public void doShutdown () {
107         // First only show shutdown status
108         this.statusLabel.setText(this.getBundle().getString("AddressbookFrame.statusLabel.shutdown.text"));
109     }
110
111     /**
112      * Setups the frame, do not set isInitialized here
113      * 
114      * @param client Client instance
115      */
116     @Override
117     public void setupFrame (final Client client) {
118         // Debug line
119         this.getLogger().trace(MessageFormat.format("client={0}: CALLED!", client));
120
121         // Has the user entered own data?
122         if (this.getClient().getContactManager().isOwnContactAdded()) {
123             // Debug message
124             this.getLogger().debug("Disabling menus: isOwnContactAdded()=false");
125
126             // Not entered yet, so enable menu
127             this.addOwnItem.setEnabled(false);
128         }
129
130         // Make the frame visible
131         this.frame.setVisible(true);
132
133         // All done here
134         this.statusLabel.setText(this.getBundle().getString("AddressbookFrame.statusLabel.done.text"));
135     }
136
137     /**
138      * Initalizes this frame. Having initComponents() exposed (publicly
139      * accessible) means that any other object can initialize components which
140      * you may not want.
141      * 
142      * @throws org.mxchange.addressbook.FrameAlreadyInitializedException If this method has been called twice
143      */
144     @Override
145     public void initFrame () throws FrameAlreadyInitializedException {
146         // Debug line
147         this.getLogger().trace("CALLED!");
148
149         // Has this frame been initialized?
150         if (this.isInitialized()) {
151             // Throw exception
152             throw new FrameAlreadyInitializedException();
153         }
154
155         // Init components
156         this.initComponents();
157
158         // Set flag
159         this.isInitialized = true;
160     }
161
162     /**
163      * Returns field isInitialized. This flag indicates whether this frame has been initialized or not.
164      * 
165      * @return Field isInitialized
166      */
167     @Override
168     public final boolean isInitialized () {
169         return this.isInitialized;
170     }
171
172     /**
173      * Initialize components
174      */
175     private void initComponents () {
176         // Debug line
177         this.getLogger().trace("CALLED!");
178
179         // Set default close operation
180         this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
181
182         // Register shutdown listener
183         this.frame.addWindowListener(new WindowAdapter() {
184             /**
185              * Invoked when a window has been closed.
186              */
187             @Override
188             public void windowClosed(final WindowEvent e) {
189                 // Shutdown application cleanly
190                 self.getClient().getApplication().doShutdown();
191             }
192
193             /**
194              * Invoked when a window is in the process of being closed.
195              * The close operation can be overridden at this point.
196              */
197             @Override
198             public void windowClosing(final WindowEvent e) {
199                 // Also shutdown cleanly here
200                 self.getClient().getApplication().doShutdown();
201             }
202         });
203
204         // Setup layout manager
205         this.frame.setLayout(new BorderLayout(2, 2));
206
207         // Set window size
208         this.frame.setSize(700, 400);
209
210         // Center window in middle of screen, instead of top-left corner
211         this.frame.setLocationRelativeTo(null);
212
213         // Init menu system
214         initMenuSystem();
215
216         // Init status label (which needs to be updated
217         this.statusLabel = new JLabel(this.getBundle().getString("AddressbookFrame.statusLabel.initializing.text"));
218
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());
224
225         // Add panel to frame
226         this.frame.add(panel, BorderLayout.SOUTH);
227     }
228
229     /**
230      * Initializes the menu system
231      */
232     private void initMenuSystem () {
233         // Init menu bar, menu and item instances
234         JMenuBar menuBar = new JMenuBar();
235         JMenu menu;
236         JMenuItem item;
237
238         // Init some menus:
239         // 1) File menu
240         menu = new JMenu(this.getBundle().getString("AddressbookFrame.menu.file.text"));
241
242         // Add menu items:
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"));
246
247         // Add listener to exit menu
248         item.addActionListener(new ActionListener() {
249             /**
250              * If the user has performed this action
251              *
252              * @param e An instance of an ActionEvent class
253              */
254             @Override
255             public void actionPerformed (final ActionEvent e) {
256                 self.getClient().getApplication().doShutdown();
257             }
258         });
259
260         // Add item -> menu
261         menu.add(item);
262
263         // Add menu -> menu bar
264         menuBar.add(menu);
265
266         // Init some menus:
267         // 2) Addressbook menu
268         menu = new JMenu(this.getBundle().getString("AddressbookFrame.menu.addressbook.text"));
269
270         // 2.1) Add own data
271         this.addOwnItem = new JMenuItem(this.getBundle().getString("AddressbookFrame.menuItem.addOwnData.text"));
272         this.addOwnItem.setToolTipText(this.getBundle().getString("AddressbookFrame.menuItem.addOwnData.toolTipText"));
273
274         // Add listener to exit menu
275         this.addOwnItem.addActionListener(new ActionListener() {
276             /**
277              * If the user has performed this action
278              *
279              * @param e An instance of an ActionEvent class
280              */
281             @Override
282             public void actionPerformed (final ActionEvent e) {
283                 self.getClient().getContactManager().doEnterOwnData();
284             }
285         });
286
287         // Add item -> menu
288         menu.add(this.addOwnItem);
289
290         // Add menu -> menu bar
291         menuBar.add(menu);
292
293         // Add menu bar -> frame
294         this.frame.add(menuBar, BorderLayout.NORTH);
295     }
296
297 }