]> git.mxchange.org Git - addressbook-lib.git/blob - Addressbook/src/org/mxchange/addressbook/client/gui/AddressbookFrame.java
Introduced Gender enum which replaces the old char
[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.GridLayout;
21 import java.awt.event.ActionEvent;
22 import java.awt.event.ActionListener;
23 import java.awt.event.MouseAdapter;
24 import java.awt.event.MouseEvent;
25 import java.awt.event.WindowAdapter;
26 import java.awt.event.WindowEvent;
27 import java.text.MessageFormat;
28 import javax.swing.BorderFactory;
29 import javax.swing.BoxLayout;
30 import javax.swing.DefaultComboBoxModel;
31 import javax.swing.JComboBox;
32 import javax.swing.JDialog;
33 import javax.swing.JFrame;
34 import javax.swing.JLabel;
35 import javax.swing.JMenu;
36 import javax.swing.JMenuBar;
37 import javax.swing.JMenuItem;
38 import javax.swing.JPanel;
39 import javax.swing.JScrollPane;
40 import javax.swing.JTable;
41 import javax.swing.JTextField;
42 import javax.swing.border.TitledBorder;
43 import javax.swing.table.TableModel;
44 import org.mxchange.addressbook.BaseFrameworkSystem;
45 import org.mxchange.addressbook.application.AddressbookApplication;
46 import org.mxchange.addressbook.client.Client;
47 import org.mxchange.addressbook.contact.Gender;
48 import org.mxchange.addressbook.exceptions.FrameAlreadyInitializedException;
49 import org.mxchange.addressbook.model.contact.ContactTableModel;
50
51 /**
52  *
53  * @author Roland Haeder
54  */
55 public class AddressbookFrame extends BaseFrameworkSystem implements ClientFrame {
56
57         /**
58          * Own instance
59          */
60         private static ClientFrame self;
61
62         /**
63          * Singelton getter for this frame instance.
64          *
65          * @param client Client instance
66          * @return Returns a singelton instance of this frame
67          */
68         public static final ClientFrame getSelfInstance (final Client client) {
69                 // Is it set?
70                 if (!(self instanceof ClientFrame)) {
71                         // Create new instance
72                         self = new AddressbookFrame(client);
73                 }
74
75                 // Return instance
76                 return self;
77         }
78
79         /**
80          * Dialog box "add contact"
81          */
82         private JDialog addContact;
83
84         /**
85          * Frame instance for "add own data"
86          */
87         private JMenuItem addOwnItem;
88
89         /**
90          * Instance to table model
91          */
92         private TableModel dataModel;
93
94         /**
95          * Table instance
96          */
97         private JTable dataTable;
98
99         /**
100          * Frame instance for "edit own data"
101          */
102         private JMenuItem editOwnItem;
103
104         /**
105          * Frame instance
106          */
107         private final JFrame frame;
108
109         /**
110          * Whether this frame has been initialized
111          */
112         private boolean isInitialized;
113
114         /**
115          * Status label needs to be updated
116          */
117         private JLabel statusLabel;
118
119         /**
120          * Creates an instance of this frame with a client instance
121          *
122          * @param client
123          */
124         private AddressbookFrame (final Client client) {
125                 // Debug line
126                 this.getLogger().trace(MessageFormat.format("client={0}: CALLED!", client));
127
128                 // Set frame instance
129                 this.frame = new JFrame();
130                 this.frame.setTitle(this.generateFrameTitle("main"));
131
132                 // Set client here
133                 this.setClient(client);
134         }
135
136         /**
137          * Shutdown this frame
138          */
139         @Override
140         public void doShutdown () {
141                 // First only show shutdown status
142                 this.updateStatus("shutdown");
143         }
144
145         /**
146          * Setups the frame, do not set isInitialized here
147          *
148          * @param client Client instance
149          */
150         @Override
151         public void setupFrame (final Client client) {
152                 // Debug line
153                 this.getLogger().trace(MessageFormat.format("client={0}: CALLED!", client));
154
155                 // Has the user entered own data?
156                 if (this.getClient().getContactManager().isOwnContactAdded()) {
157                         // Debug message
158                         this.getLogger().debug("Disabling menus: isOwnContactAdded()=false");
159
160                         // Not entered yet, so disable "add" menu
161                         this.addOwnItem.setEnabled(false);
162                 } else {
163                         // Disable "edit"
164                         this.editOwnItem.setEnabled(false);
165                 }
166
167                 // Make the frame visible
168                 this.frame.setVisible(true);
169
170                 // All done here
171                 this.updateStatus("done");
172         }
173
174         /**
175          * Initalizes this frame. Having initComponents() exposed (publicly
176          * accessible) means that any other object can initialize components which
177          * you may not want.
178          *
179          * @throws
180          * org.mxchange.addressbook.exceptions.FrameAlreadyInitializedException If
181          * this method has been called twice
182          */
183         @Override
184         public void init () throws FrameAlreadyInitializedException {
185                 // Debug line
186                 this.getLogger().trace("CALLED!");
187
188                 // Has this frame been initialized?
189                 if (this.isInitialized()) {
190                         // Throw exception
191                         throw new FrameAlreadyInitializedException();
192                 }
193
194                 // Init components
195                 this.initComponents();
196
197                 // Set flag
198                 this.isInitialized = true;
199         }
200
201         /**
202          * Returns field isInitialized. This flag indicates whether this frame has
203          * been initialized or not.
204          *
205          * @return Field isInitialized
206          */
207         @Override
208         public final boolean isInitialized () {
209                 return this.isInitialized;
210         }
211
212         /**
213          * Shuts down the application.
214          */
215         @Override
216         public void shutdownApplication () {
217                 // To do this, the frame must be initialized
218                 if (!this.isInitialized()) {
219                         // Not initalized, so bad call
220                         this.getLogger().fatal("Bad call of shutdownApplication(). Please report this.");
221                         return;
222                 }
223                 this.getClient().getApplication().doShutdown();
224         }
225
226         /**
227          * Generates a title for borders
228          * @param key Key part to look for
229          * @return Human-readable title
230          */
231         private String generateBorderTitle (final String key) {
232                 // Call bundle instance
233                 return this.getBundle().getString(String.format("AddressbookFrame.border.%s.title.text", key));
234         }
235
236         /**
237          * Generates a title for all frames based on given sub title key. If null is
238          * given, the sub title is not generated.
239          * 
240          * @param subKey Key for sub title resource
241          * @return A full application title
242          */
243         private String generateFrameTitle (final String subKey) {
244                 // Base title
245                 String title = AddressbookApplication.printableTitle();
246
247                 // Is key given?
248                 if (subKey != null) {
249                         // Add sub title
250                         title = String.format("%s - %s", title, this.getBundle().getString(String.format("AddressbookFrame.%s.title.text", subKey)));
251                 }
252
253                 // Return it
254                 return title;
255         }
256
257         /**
258          * Initializes "add contact" dialog
259          */
260         private void initAddContactDialog () {
261                 // Instance dialog and set title
262                 this.addContact = new JDialog();
263                 this.addContact.setTitle(this.generateFrameTitle("dialog.addContact"));
264                 this.addContact.setLayout(new GridLayout(4, 1));
265                 
266                 // Only hide it on close and make it appear in middle of screen
267                 this.addContact.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
268                 this.addContact.setLocationRelativeTo(null);
269                 
270                 // Set always on top and auto-focus
271                 this.addContact.setAlwaysOnTop(true);
272                 this.addContact.setAutoRequestFocus(true);
273                 
274                 // Initial dimension
275                 this.addContact.setSize(400, 300);
276                 
277                 /*
278                  * Add listener which asks for confirmation, if data has been entered
279                  * but not saved yet. The user may appriciate this ... ;-)
280                  *
281                  * @TODO Unfinished
282                  */
283                 
284                 // Init 3 panels:
285                 // 1) Panel "name" input boxes
286                 JPanel namePanel = new JPanel();
287                 namePanel.setLayout(new BoxLayout(namePanel, BoxLayout.Y_AXIS));
288                 
289                 // Set border to titled version
290                 namePanel.setBorder(new TitledBorder(this.generateBorderTitle("name")));
291
292                 // Add some input boxes for "name" panel
293                 JComboBox<Gender> gender = new JComboBox<>(new DefaultComboBoxModel<Gender>());
294
295                 // Panel for gender
296                 JPanel gPanel = new JPanel();
297                 gPanel.setLayout(new GridLayout(1, 2));
298
299                 // Set tooltip text
300                 gPanel.setToolTipText(this.getBundle().getString("AddressbookFrame.gender.tooltipText"));
301
302                 // Gender text field
303                 JLabel gLabel = new JLabel(this.getBundle().getString("AddressbookFrame.gender.text"));
304
305                 // Add both to gender panel
306                 gPanel.add(gLabel);
307                 gPanel.add(gender);
308
309                 // Add panel to north of "name" panel
310                 namePanel.add(gPanel, BorderLayout.NORTH);
311
312                 // Panel for surname
313                 JPanel sPanel = new JPanel();
314                 sPanel.setLayout(new GridLayout(1, 2));
315
316                 // Set too tip text
317                 sPanel.setToolTipText(this.getBundle().getString("AddressbookFrame.surname.tooltipText"));
318
319                 // New label for surname is not needed
320                 JLabel sLabel = new JLabel(this.getBundle().getString("AddressbookFrame.surname.text"));
321
322                 // And input box
323                 JTextField surname = new JTextField(20);
324
325                 // Add both to surname panel
326                 sPanel.add(sLabel);
327                 sPanel.add(surname);
328
329                 // Add surname panel to "name" panel
330                 namePanel.add(sPanel, BorderLayout.CENTER);
331
332                 // Finally add panel to dialog
333                 this.addContact.add(namePanel);
334                 
335                 // Only for developing:
336                 /* DEBUG: */ this.addContact.setVisible(true);
337         }
338
339         /**
340          * Initialize components
341          */
342         private void initComponents () {
343                 // Debug line
344                 this.getLogger().trace("CALLED!");
345
346                 // Set default close operation
347                 this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
348
349                 // Register shutdown listener
350                 this.frame.addWindowListener(new WindowAdapter() {
351                         /**
352                          * Invoked when a window has been closed.
353                          */
354                         @Override
355                         public void windowClosed (final WindowEvent e) {
356                                 // Shutdown application cleanly
357                                 self.shutdownApplication();
358                         }
359
360                         /**
361                          * Invoked when a window is in the process of being closed. The
362                          * close operation can be overridden at this point.
363                          */
364                         @Override
365                         public void windowClosing (final WindowEvent e) {
366                                 // Also shutdown cleanly here
367                                 self.shutdownApplication();
368                         }
369                 });
370
371                 // Setup layout manager
372                 this.frame.setLayout(new BorderLayout(2, 2));
373
374                 // Set window size
375                 this.frame.setSize(700, 400);
376
377                 // Center window in middle of screen, instead of top-left corner
378                 this.frame.setLocationRelativeTo(null);
379
380                 // Init menu system
381                 initMenuSystem();
382
383                 // Init table
384                 initTable();
385
386                 // Init status panel
387                 initStatusPanel();
388
389                 // Init other windows
390                 initOtherDialogs();
391         }
392
393         /**
394          * Initializes the menu system
395          */
396         private void initMenuSystem () {
397                 // Init menu bar, menu and item instances
398                 JMenuBar menuBar = new JMenuBar();
399                 JMenu menu;
400                 JMenuItem item;
401                 
402                 // Init some menus:
403                 // 1) File menu
404                 menu = new JMenu(this.getBundle().getString("AddressbookFrame.menu.file.text"));
405                 
406                 // Add menu items:
407                 // 1.x) Exit program (should be last)
408                 item = new JMenuItem(this.getBundle().getString("AddressbookFrame.menuItem.exitProgram.text"));
409                 item.setToolTipText(this.getBundle().getString("AddressbookFrame.menuItem.exitProgram.toolTipText"));
410
411                 // Add listener to exit menu
412                 item.addActionListener(new ActionListener() {
413                         /**
414                          * If the user has performed this action
415                          *
416                          * @param e An instance of an ActionEvent class
417                          */
418                         @Override
419                         public void actionPerformed (final ActionEvent e) {
420                                 self.shutdownApplication();
421                         }
422                 });
423
424                 // Add item -> menu
425                 menu.add(item);
426
427                 // Add menu -> menu bar
428                 menuBar.add(menu);
429                 
430                 // Init some menus:
431                 // 2) Addressbook menu
432                 menu = new JMenu(this.getBundle().getString("AddressbookFrame.menu.addressbook.text"));
433
434                 // 2.1) Add own data
435                 this.addOwnItem = new JMenuItem(this.getBundle().getString("AddressbookFrame.menuItem.addOwnData.text"));
436                 this.addOwnItem.setToolTipText(this.getBundle().getString("AddressbookFrame.menuItem.addOwnData.toolTipText"));
437
438                 // Add listener to exit menu
439                 this.addOwnItem.addActionListener(new ActionListener() {
440                         /**
441                          * If the user has performed this action
442                          *
443                          * @param e An instance of an ActionEvent class
444                          */
445                         @Override
446                         public void actionPerformed (final ActionEvent e) {
447                                 self.getClient().getContactManager().doEnterOwnData();
448                         }
449                 });
450
451                 // Add item -> menu
452                 menu.add(this.addOwnItem);
453
454                 // 2.2) Edit own data
455                 this.editOwnItem = new JMenuItem(this.getBundle().getString("AddressbookFrame.menuItem.editOwnData.text"));
456                 this.editOwnItem.setToolTipText(this.getBundle().getString("AddressbookFrame.menuItem.editOwnData.toolTipText"));
457
458                 // Add listener to exit menu
459                 this.editOwnItem.addActionListener(new ActionListener() {
460                         /**
461                          * If the user has performed this action
462                          *
463                          * @param e An instance of an ActionEvent class
464                          */
465                         @Override
466                         public void actionPerformed (final ActionEvent e) {
467                                 self.getClient().getContactManager().doChangeOwnData();
468                         }
469                 });
470
471                 // Add item -> menu
472                 menu.add(this.editOwnItem);
473
474                 // Add menu -> menu bar
475                 menuBar.add(menu);
476
477                 // Add menu bar -> frame
478                 this.frame.add(menuBar, BorderLayout.NORTH);
479         }
480
481         /**
482          * Initialize other dialogs (e.g. "Add contact")
483          */
484         private void initOtherDialogs () {
485                 // Init other windows:
486                 // 1) Add contact
487                 initAddContactDialog();
488         }
489
490         /**
491          * Initializes status panel
492          */
493         private void initStatusPanel () {
494                 // Init status label (which needs to be updated
495                 this.statusLabel = new JLabel();
496                 this.updateStatus("initializing");
497
498                 // Init status bar in south
499                 JPanel panel = new JPanel();
500                 panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
501                 panel.add(this.statusLabel);
502                 panel.setBorder(BorderFactory.createEtchedBorder());
503
504                 // Add panel to frame
505                 this.frame.add(panel, BorderLayout.SOUTH);
506         }
507
508         /**
509          * Initializes the table which will show all contacts
510          */
511         private void initTable () {
512                 // Instance table model
513                 this.dataModel = new ContactTableModel(this.getClient());
514
515                 // Instance table
516                 this.dataTable = new JTable(this.dataModel);
517
518                 // Add mouse listener
519                 this.dataTable.addMouseListener(new MouseAdapter() {
520                         /**
521                          * If the user peformed a click on a cell
522                          *
523                          * @param e Mouse event instance
524                          */
525                         @Override
526                         public void mouseClicked (final MouseEvent e) {
527                                 throw new UnsupportedOperationException("Unfinished.");
528                         }
529                 });
530
531                 // Instance scroll pane
532                 JScrollPane scroller = new JScrollPane();
533
534                 // Add table to scroll pane
535                 scroller.setViewportView(this.dataTable);
536                 scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
537                 scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
538
539                 // Add pane to frame
540                 this.frame.add(scroller, BorderLayout.CENTER);
541         }
542
543         /**
544          * Updates status to given type
545          *
546          * @param type Status type
547          */
548         private void updateStatus (final String type) {
549                 // Set status message
550                 this.statusLabel.setText(this.getBundle().getString(String.format("AddressbookFrame.statusLabel.%s.text", type)));
551         }
552 }