]> git.mxchange.org Git - addressbook-lib.git/blob - Addressbook/src/org/mxchange/addressbook/client/gui/AddressbookFrame.java
Introduced initTable()
[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.MouseAdapter;
23 import java.awt.event.MouseEvent;
24 import java.awt.event.WindowAdapter;
25 import java.awt.event.WindowEvent;
26 import java.text.MessageFormat;
27 import javax.swing.BorderFactory;
28 import javax.swing.BoxLayout;
29 import javax.swing.JFrame;
30 import javax.swing.JLabel;
31 import javax.swing.JMenu;
32 import javax.swing.JMenuBar;
33 import javax.swing.JMenuItem;
34 import javax.swing.JPanel;
35 import javax.swing.JTable;
36 import javax.swing.table.TableModel;
37 import org.mxchange.addressbook.BaseFrameworkSystem;
38 import org.mxchange.addressbook.application.AddressbookApplication;
39 import org.mxchange.addressbook.client.Client;
40 import org.mxchange.addressbook.exceptions.FrameAlreadyInitializedException;
41 import org.mxchange.addressbook.model.address.AddressTableModel;
42
43 /**
44  *
45  * @author Roland Haeder
46  */
47 public class AddressbookFrame extends BaseFrameworkSystem implements ClientFrame {
48
49     /**
50      * Own instance
51      */
52     private static ClientFrame self;
53
54     /**
55      * Singelton getter for this frame instance.
56      *
57      * @param client Client instance
58      * @return Returns a singelton instance of this frame
59      */
60     public static final ClientFrame getSelfInstance (final Client client) {
61         // Is it set?
62         if (!(self instanceof ClientFrame)) {
63             // Create new instance
64             self = new AddressbookFrame(client);
65         }
66         
67         // Return instance
68         return self;
69     }
70     /**
71      * Frame instance for "add own data"
72      */
73     private JMenuItem addOwnItem;
74
75     /**
76      * Instance to table model
77      */
78     private TableModel dataModel;
79
80     /**
81      * Table instance
82      */
83     private JTable dataTable;
84
85     /**
86      * Frame instance for "edit own data"
87      */
88     private JMenuItem editOwnItem;
89
90     /**
91      * Frame instance
92      */
93     private final JFrame frame;
94
95     /**
96      * Whether this frame has been initialized
97      */
98     private boolean isInitialized;
99
100     /**
101      * Status label needs to be updated
102      */
103     private JLabel statusLabel;
104
105     /**
106      * Creates an instance of this frame with a client instance
107      * 
108      * @param client
109      */
110     private AddressbookFrame (final Client client) {
111         // Debug line
112         this.getLogger().trace(MessageFormat.format("client={0}: CALLED!", client));
113
114         // Set frame instance
115         this.frame = new JFrame(AddressbookApplication.printableTitle());
116
117         // Set client here
118         this.setClient(client);
119     }
120
121     /**
122      * Shutdown this frame
123      */
124     @Override
125     public void doShutdown () {
126         // First only show shutdown status
127         this.updateStatus("shutdown");
128     }
129
130     /**
131      * Setups the frame, do not set isInitialized here
132      * 
133      * @param client Client instance
134      */
135     @Override
136     public void setupFrame (final Client client) {
137         // Debug line
138         this.getLogger().trace(MessageFormat.format("client={0}: CALLED!", client));
139
140         // Has the user entered own data?
141         if (this.getClient().getContactManager().isOwnContactAdded()) {
142             // Debug message
143             this.getLogger().debug("Disabling menus: isOwnContactAdded()=false");
144
145             // Not entered yet, so disable "add" menu
146             this.addOwnItem.setEnabled(false);
147         } else {
148             // Disable "edit"
149             this.editOwnItem.setEnabled(false);
150         }
151
152         // Make the frame visible
153         this.frame.setVisible(true);
154
155         // All done here
156         this.updateStatus("done");
157     }
158
159     /**
160      * Initalizes this frame. Having initComponents() exposed (publicly
161      * accessible) means that any other object can initialize components which
162      * you may not want.
163      * 
164      * @throws org.mxchange.addressbook.exceptions.FrameAlreadyInitializedException If this method has been called twice
165      */
166     @Override
167     public void init () throws FrameAlreadyInitializedException {
168         // Debug line
169         this.getLogger().trace("CALLED!");
170
171         // Has this frame been initialized?
172         if (this.isInitialized()) {
173             // Throw exception
174             throw new FrameAlreadyInitializedException();
175         }
176
177         // Init components
178         this.initComponents();
179
180         // Set flag
181         this.isInitialized = true;
182     }
183
184     /**
185      * Returns field isInitialized. This flag indicates whether this frame has been initialized or not.
186      * 
187      * @return Field isInitialized
188      */
189     @Override
190     public final boolean isInitialized () {
191         return this.isInitialized;
192     }
193
194     /**
195      * Shuts down the application.
196      */
197     @Override
198     public void shutdownApplication () {
199         // To do this, the frame must be initialized
200         if (!this.isInitialized()) {
201             // Not initalized, so bad call
202             this.getLogger().fatal("Bad call of shutdownApplication(). Please report this.");
203             return;
204         }
205         this.getClient().getApplication().doShutdown();
206     }
207
208     /**
209      * Initialize components
210      */
211     private void initComponents () {
212         // Debug line
213         this.getLogger().trace("CALLED!");
214
215         // Set default close operation
216         this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
217
218         // Register shutdown listener
219         this.frame.addWindowListener(new WindowAdapter() {
220             /**
221              * Invoked when a window has been closed.
222              */
223             @Override
224             public void windowClosed(final WindowEvent e) {
225                 // Shutdown application cleanly
226                 self.shutdownApplication();
227             }
228
229             /**
230              * Invoked when a window is in the process of being closed.
231              * The close operation can be overridden at this point.
232              */
233             @Override
234             public void windowClosing(final WindowEvent e) {
235                 // Also shutdown cleanly here
236                 self.shutdownApplication();
237             }
238         });
239
240         // Setup layout manager
241         this.frame.setLayout(new BorderLayout(2, 2));
242
243         // Set window size
244         this.frame.setSize(700, 400);
245
246         // Center window in middle of screen, instead of top-left corner
247         this.frame.setLocationRelativeTo(null);
248
249         // Init menu system
250         initMenuSystem();
251
252         // Init table
253         initTable();
254
255         // Init status panel
256         initStatusPanel();
257     }
258
259     /**
260      * Initializes the menu system
261      */
262     private void initMenuSystem () {
263         // Init menu bar, menu and item instances
264         JMenuBar menuBar = new JMenuBar();
265         JMenu menu;
266         JMenuItem item;
267
268         // Init some menus:
269         // 1) File menu
270         menu = new JMenu(this.getBundle().getString("AddressbookFrame.menu.file.text"));
271
272         // Add menu items:
273         // 1.x) Exit program (should be last)
274         item = new JMenuItem(this.getBundle().getString("AddressbookFrame.menuItem.exitProgram.text"));
275         item.setToolTipText(this.getBundle().getString("AddressbookFrame.menuItem.exitProgram.toolTipText"));
276
277         // Add listener to exit menu
278         item.addActionListener(new ActionListener() {
279             /**
280              * If the user has performed this action
281              *
282              * @param e An instance of an ActionEvent class
283              */
284             @Override
285             public void actionPerformed (final ActionEvent e) {
286                 self.shutdownApplication();
287             }
288         });
289
290         // Add item -> menu
291         menu.add(item);
292
293         // Add menu -> menu bar
294         menuBar.add(menu);
295
296         // Init some menus:
297         // 2) Addressbook menu
298         menu = new JMenu(this.getBundle().getString("AddressbookFrame.menu.addressbook.text"));
299
300         // 2.1) Add own data
301         this.addOwnItem = new JMenuItem(this.getBundle().getString("AddressbookFrame.menuItem.addOwnData.text"));
302         this.addOwnItem.setToolTipText(this.getBundle().getString("AddressbookFrame.menuItem.addOwnData.toolTipText"));
303
304         // Add listener to exit menu
305         this.addOwnItem.addActionListener(new ActionListener() {
306             /**
307              * If the user has performed this action
308              *
309              * @param e An instance of an ActionEvent class
310              */
311             @Override
312             public void actionPerformed (final ActionEvent e) {
313                 self.getClient().getContactManager().doEnterOwnData();
314             }
315         });
316
317         // Add item -> menu
318         menu.add(this.addOwnItem);
319
320         // 2.2) Edit own data
321         this.editOwnItem = new JMenuItem(this.getBundle().getString("AddressbookFrame.menuItem.editOwnData.text"));
322         this.editOwnItem.setToolTipText(this.getBundle().getString("AddressbookFrame.menuItem.editOwnData.toolTipText"));
323
324         // Add listener to exit menu
325         this.editOwnItem.addActionListener(new ActionListener() {
326             /**
327              * If the user has performed this action
328              *
329              * @param e An instance of an ActionEvent class
330              */
331             @Override
332             public void actionPerformed (final ActionEvent e) {
333                 self.getClient().getContactManager().doChangeOwnData();
334             }
335         });
336
337         // Add item -> menu
338         menu.add(this.editOwnItem);
339
340         // Add menu -> menu bar
341         menuBar.add(menu);
342
343         // Add menu bar -> frame
344         this.frame.add(menuBar, BorderLayout.NORTH);
345     }
346
347     /**
348      * Initializes status panel
349      */
350     private void initStatusPanel () {
351         // Init status label (which needs to be updated
352         this.statusLabel = new JLabel();
353         this.updateStatus("initializing");
354
355         // Init status bar in south
356         JPanel panel = new JPanel();
357         panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
358         panel.add(this.statusLabel);
359         panel.setBorder(BorderFactory.createEtchedBorder());
360         
361         // Add panel to frame
362         this.frame.add(panel, BorderLayout.SOUTH);
363     }
364
365     /**
366      * Initializes the table which will show all contacts
367      */
368     private void initTable () {
369         // Instance table model
370         this.dataModel = new AddressTableModel(this.getClient());
371
372         // Instance table
373         this.dataTable = new JTable(this.dataModel);
374
375         // Add mouse listener
376         this.dataTable.addMouseListener(new MouseAdapter() {
377             /**
378              * If the user peformed a click on a cell
379              * 
380              * @param e Mouse event instance
381              */
382             @Override
383             public void mouseClicked(final MouseEvent e) {
384             }
385         });
386
387         // Add table to frame
388         this.frame.add(this.dataTable);
389     }
390
391     /**
392      * Updates status to given type
393      * 
394      * @param type Status type
395      */
396     private void updateStatus (final String type) {
397         // Set status message
398         this.statusLabel.setText(this.getBundle().getString("AddressbookFrame.statusLabel." + type + ".text"));
399     }
400 }