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.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.io.IOException;
28 import java.lang.reflect.InvocationTargetException;
29 import java.sql.SQLException;
30 import java.text.MessageFormat;
31 import javax.swing.BorderFactory;
32 import javax.swing.BoxLayout;
33 import javax.swing.DefaultComboBoxModel;
34 import javax.swing.JButton;
35 import javax.swing.JComboBox;
36 import javax.swing.JDialog;
37 import javax.swing.JFormattedTextField;
38 import javax.swing.JFrame;
39 import javax.swing.JLabel;
40 import javax.swing.JMenu;
41 import javax.swing.JMenuBar;
42 import javax.swing.JMenuItem;
43 import javax.swing.JPanel;
44 import javax.swing.JScrollPane;
45 import javax.swing.JTable;
46 import javax.swing.JTextArea;
47 import javax.swing.JTextField;
48 import javax.swing.border.TitledBorder;
49 import javax.swing.table.TableModel;
50 import org.mxchange.addressbook.BaseAddressbookSystem;
51 import org.mxchange.addressbook.application.AddressbookApplication;
52 import org.mxchange.addressbook.exceptions.ContactAlreadyAddedException;
53 import org.mxchange.addressbook.manager.contact.ManageableAddressbookContact;
54 import org.mxchange.jcore.client.Client;
55 import org.mxchange.jcore.exceptions.FrameAlreadyInitializedException;
56 import org.mxchange.jcore.model.contact.Contact;
57 import org.mxchange.jcore.model.contact.gender.Gender;
58 import org.mxchange.jswingcore.client.gui.ClientFrame;
59 import org.mxchange.jswingcore.model.swing.contact.ContactTableModel;
63 * @author Roland Haeder
65 public class AddressbookFrame extends BaseAddressbookSystem implements ClientFrame {
70 private static ClientFrame self;
73 * Singelton getter for this frame instance.
75 * @param client Client instance
76 * @return Returns a singelton instance of this frame
78 public static final ClientFrame getSelfInstance (final Client client) {
80 if (!(self instanceof ClientFrame)) {
81 // Create new instance
82 self = new AddressbookFrame(client);
90 * Dialog box "add contact"
92 private JDialog addContact;
95 * Frame instance for "add own data"
97 private JMenuItem addOwnItem;
100 * Instance to table model
102 private TableModel dataModel;
107 private JTable dataTable;
110 * Frame instance for "edit own data"
112 private JMenuItem editOwnItem;
117 private final JFrame frame;
120 * Whether this frame has been initialized
122 private boolean initialized;
125 * Status label needs to be updated
127 private JLabel statusLabel;
130 * Creates an instance of this frame with a client instance
134 private AddressbookFrame (final Client client) {
136 this.getLogger().trace(MessageFormat.format("client={0}: CALLED!", client)); //NOI18N
138 // Set frame instance
139 this.frame = new JFrame();
140 this.frame.setTitle(this.generateFrameTitle("main")); //NOI18N
143 this.setClient(client);
146 this.getLogger().trace("EXIT!"); //NOI18N
150 public Contact doEnterOwnData () {
152 this.getLogger().trace("CALLED!"); //NOI18N
154 // Is the "add contact" window visible?
155 if (this.addContact.isVisible()) {
156 // Something bad happened
157 throw new IllegalStateException("Window addContact is already visible."); //NOI18N
160 // Disable main window
161 this.frame.setEnabled(false);
163 // Make other window visible
164 this.addContact.setVisible(true);
167 this.getLogger().trace("Returning null : EXIT!"); //NOI18N
169 // Return value is not supported
174 * Shutdown this frame
177 public void doShutdown () {
179 this.getLogger().trace("CALLED!"); //NOI18N
181 // First only show shutdown status
182 this.updateStatus("shutdown"); //NOI18N
185 this.getLogger().trace("EXIT!"); //NOI18N
190 * Enables main window (frame)
193 public void enableMainWindow () {
195 this.getLogger().trace("CALLED!"); //NOI18N
198 this.frame.setEnabled(true);
200 // Request focus for this window
201 this.frame.requestFocus();
204 this.getLogger().trace("EXIT!"); //NOI18N
208 * Setups the frame, do not set isInitialized here
210 * @param client Client instance
213 public void setupFrame (final Client client) throws IOException {
215 this.getLogger().trace(MessageFormat.format("client={0}: CALLED!", client)); //NOI18N
217 // Has the user entered own data?
218 if (((ManageableAddressbookContact) this.getClient().getManager()).isOwnContactAdded()) {
220 this.getLogger().debug("Disabling menus: isOwnContactAdded()=false"); //NOI18N
222 // Not entered yet, so disable "add" menu
223 this.addOwnItem.setEnabled(false);
226 this.editOwnItem.setEnabled(false);
229 // Make the frame visible
230 this.frame.setVisible(true);
233 this.updateStatus("done"); //NOI18N
236 this.getLogger().trace("EXIT!"); //NOI18N
240 * Initalizes this frame. Having initComponents() exposed (publicly
241 * accessible) means that any other object can initialize components which
245 * org.mxchange.jcore.exceptions.FrameAlreadyInitializedException If
246 * this method has been called twice
249 public void init () throws FrameAlreadyInitializedException {
251 this.getLogger().trace("CALLED!"); //NOI18N
253 // Has this frame been initialized?
254 if (this.isInitialized()) {
256 throw new FrameAlreadyInitializedException();
260 this.initComponents();
263 this.initialized = true;
266 this.getLogger().trace("EXIT!"); //NOI18N
270 * Returns field isInitialized. This flag indicates whether this frame has
271 * been initialized or not.
273 * @return Field isInitialized
276 public final boolean isInitialized () {
277 return this.initialized;
281 * Shuts down the application.
284 public void shutdownApplication () {
286 this.getLogger().trace("CALLED!"); //NOI18N
288 // To do this, the frame must be initialized
289 if (!this.isInitialized()) {
290 // Not initalized, so bad call
291 this.getLogger().fatal("Bad call of shutdownApplication(). Please report this."); //NOI18N
295 // Call shutdown method
297 this.getClient().getApplication().doShutdown();
298 } catch (final SQLException | IOException ex) {
300 this.abortProgramWithException(ex);
304 this.getLogger().trace("EXIT!"); //NOI18N
308 * Adds a new menu item with given key to menu instance
310 * @param menu Menu instance to add item to
311 * @param key Message key part
312 * @param listener Listener instance
314 private void addMenuItem (final JMenu menu, final String key, final ActionListener listener) {
316 this.getLogger().trace(MessageFormat.format("menu={0},key={1},listener={2} - CALLED!", menu, key, listener)); //NOI18N
319 JMenuItem item = this.initMenuItemWithTooltip(key);
322 item.addActionListener(listener);
328 this.getLogger().trace("EXIT!"); //NOI18N
332 * Adds a JTextField with label and tool tip to given panel
334 * @param panel Panel instance to add text field
335 * @param key Part of message key from resource bundle
336 * @param fieldLength Length of text field
338 private void addTextFieldWithLabelToPanel (final JPanel panel, final String key, final int fieldLength) {
340 this.getLogger().trace(MessageFormat.format("panel={0},key={1},fieldLength={2} - CALLED!", panel, key, fieldLength)); //NOI18N
342 // Init label for given key
343 JLabel label = new JLabel(this.getBundle().getString(String.format("AddressbookFrame.%s.text", key))); //NOI18N
345 // And input box wih tool tip
346 JTextField field = new JTextField(fieldLength);
347 field.setToolTipText(this.getBundle().getString(String.format("AddressbookFrame.%s.toolTipText", key))); //NOI18N
354 this.getLogger().trace("EXIT!"); //NOI18N
358 * Generates a title for borders
360 * @param key Key part to look for
361 * @return Human-readable title
363 private String generateBorderTitle (final String key) {
364 // Call bundle instance
365 return this.getBundle().getString(String.format("AddressbookFrame.border.%s.title.text", key)); //NOI18N
369 * Generates a title for all frames based on given sub title key. If null is
370 * given, the sub title is not generated.
372 * @param subKey Key for sub title resource
373 * @return A full application title
375 private String generateFrameTitle (final String subKey) {
377 String title = AddressbookApplication.printableTitle();
380 if (subKey != null) {
382 title = String.format("%s - %s", title, this.getBundle().getString(String.format("AddressbookFrame.%s.title.text", subKey))); //NOI18N
390 * Initializes "add" and "cancel" buttons
392 private void initAddCancelButtons () {
394 this.getLogger().trace("CALLED!"); //NOI18N
397 JPanel panel = new JPanel();
398 panel.setLayout(new GridLayout(1, 2, 10, 10));
400 // Generate "add" button
401 JButton addButton = new JButton(this.getBundle().getString("AddressbookFrame.button.addAddress.text"));
404 addButton.addActionListener(new AddActionListener(this.addContact, this));
406 // Add button to panel
407 panel.add(addButton);
409 // Generate "cancel" button
410 JButton cancelButton = new JButton(this.getBundle().getString("AddressbookFrame.button.cancel.text"));
413 cancelButton.addActionListener(new CancelActionListener(this.addContact, this));
415 // Add button to panel
416 panel.add(cancelButton);
418 // Add panel to main panel
419 this.addContact.add(panel);
422 this.getLogger().trace("EXIT!"); //NOI18N
426 * Initializes "add contact" dialog
428 private void initAddContactDialog () {
430 this.getLogger().trace("CALLED!"); //NOI18N
432 // Instance dialog and set title
433 this.addContact = new JDialog();
434 this.addContact.setTitle(this.generateFrameTitle("dialog.addContact")); //NOI18N
437 this.addContact.setLayout(new GridLayout(0, 1, 2, 2));
439 // Only hide it on close and make it appear in middle of screen
440 this.addContact.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
441 this.addContact.setLocationRelativeTo(this.frame);
443 // Set always on top and auto-focus
444 this.addContact.setAlwaysOnTop(true);
445 this.addContact.setAutoRequestFocus(true);
448 this.addContact.setSize(500, 500);
450 // And it is not resizeable
451 this.addContact.setResizable(false);
454 * Add listener which asks for confirmation, if data has been entered
455 * but not saved yet. The user may appriciate this ... ;-)
459 this.addContact.addWindowListener(new WindowAdapter() {
461 * Invoked when a window has been closed.
464 public void windowClosed (final WindowEvent e) {
465 // Enable main window again
466 AddressbookFrame.getSelfInstance(null).enableMainWindow();
470 * Invoked when a window is in the process of being closed. The
471 * close operation can be overridden at this point.
474 public void windowClosing (final WindowEvent e) {
475 e.getWindow().dispose();
481 initNameDataPanel(this.addContact);
483 // 2) "address" panel
484 initAddressDataPanel(this.addContact);
487 initOtherDataPanel(this.addContact);
489 // 4) "Add" and "Cancel" buttons, combined they are unique for this dialog
490 initAddCancelButtons();
492 // x)Only for developing:
493 /* DEBUG: */ this.addContact.setVisible(true);
496 this.getLogger().trace("EXIT!"); //NOI18N
500 * Initializes address panel
502 * @param dialog A JDialog instance to this components to
504 private void initAddressDataPanel (final JDialog dialog) {
506 this.getLogger().trace("CALLED!"); //NOI18N
508 // Panel "address" input boxes
509 JPanel addressPanel = new JPanel();
510 addressPanel.setLayout(new GridLayout(0, 4, 3, 3));
512 // Set border to titled version
513 addressPanel.setBorder(new TitledBorder(this.generateBorderTitle("address"))); //NOI18N
515 // Add text field for street
516 this.addTextFieldWithLabelToPanel(addressPanel, "street", 20); //NOI18N
519 JLabel numberLabel = new JLabel(this.getBundle().getString("AddressbookFrame.number.text"));
521 // And text field, but only accept numbers
522 JFormattedTextField number = new JFormattedTextField();
523 number.setToolTipText(this.getBundle().getString("AddressbookFrame.number.toolTipText"));
525 // Add both to street panel
526 addressPanel.add(numberLabel);
527 addressPanel.add(number);
529 // Label for ZIP code, again numbers only
530 JLabel zipLabel = new JLabel(this.getBundle().getString("AddressbookFrame.zip.text"));
532 // Init text field with label
533 JFormattedTextField zip = new JFormattedTextField();
534 zip.setToolTipText(this.getBundle().getString("AddressbookFrame.zip.toolTipText"));
536 // Add both to street panel
537 addressPanel.add(zipLabel);
538 addressPanel.add(zip);
540 // Add text field for city name
541 this.addTextFieldWithLabelToPanel(addressPanel, "city", 20); //NOI18N
543 // Add panel to dialog
544 dialog.add(addressPanel);
547 this.getLogger().trace("EXIT!"); //NOI18N
551 * Initialize components
553 private void initComponents () {
555 this.getLogger().trace("CALLED!"); //NOI18N
557 // Set default close operation
558 this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
560 // Register shutdown listener
561 this.frame.addWindowListener(new WindowAdapter() {
563 * Invoked when a window has been closed.
566 public void windowClosed (final WindowEvent e) {
567 // Shutdown application cleanly
568 self.shutdownApplication();
572 * Invoked when a window is in the process of being closed. The
573 * close operation can be overridden at this point.
576 public void windowClosing (final WindowEvent e) {
577 // Also shutdown cleanly here
578 self.shutdownApplication();
582 // Setup layout manager
583 this.frame.setLayout(new BorderLayout(2, 2));
586 this.frame.setSize(700, 400);
588 // Center window in middle of screen, instead of top-left corner
589 this.frame.setLocationRelativeTo(null);
600 // Init other windows
604 this.getLogger().trace("EXIT!"); //NOI18N
608 * Initializes a menu item instance with tool tip
610 * @param key Message key part
611 * @return A finished JMenuItem instance
613 private JMenuItem initMenuItemWithTooltip (final String key) {
615 this.getLogger().trace(MessageFormat.format("key={0} - CALLED!", key)); //NOI18N
617 JMenuItem item = new JMenuItem(this.getBundle().getString(String.format("AddressbookFrame.menuItem.%s.text", key))); //NOI18N
618 item.setToolTipText(this.getBundle().getString(String.format("AddressbookFrame.menuItem.%s.toolTipText", key))); //NOI18N
621 this.getLogger().trace(MessageFormat.format("item={0} - EXIT!", item)); //NOI18N
628 * Initializes the menu system
630 private void initMenuSystem () {
632 this.getLogger().trace("CALLED!"); //NOI18N
634 // Init menu bar, menu and item instances
635 JMenuBar menuBar = new JMenuBar();
641 menu = new JMenu(this.getBundle().getString("AddressbookFrame.menu.file.text"));
644 // 1.x) Exit program (should be last)
645 this.addMenuItem(menu, "exitProgram", new ActionListener() { //NOI18N
647 * If the user has performed this action
649 * @param e An instance of an ActionEvent class
652 public void actionPerformed (final ActionEvent e) {
653 self.shutdownApplication();
657 // Add menu -> menu bar
661 // 2) Addressbook menu
662 menu = new JMenu(this.getBundle().getString("AddressbookFrame.menu.addressbook.text"));
665 this.addOwnItem = this.initMenuItemWithTooltip("addOwnData"); //NOI18N
667 // Add listener to exit menu
668 this.addOwnItem.addActionListener(new ActionListener() {
670 * If the user has performed this action
672 * @param e An instance of an ActionEvent class
675 public void actionPerformed (final ActionEvent e) {
677 ((ManageableAddressbookContact) self.getClient().getManager()).doEnterOwnData();
678 } catch (final ContactAlreadyAddedException ex) {
679 // Already added, log away
680 // TODO maybe output message here?
681 self.logException(ex);
682 } catch (final IOException ex) {
683 // Somethind bad happened here
684 // TODO Output error message here?
690 menu.add(this.addOwnItem);
692 // 2.2) Edit own data
693 this.editOwnItem = this.initMenuItemWithTooltip("editOwnData"); //NOI18N
695 // Add listener to exit menu
696 this.editOwnItem.addActionListener(new ActionListener() {
698 * If the user has performed this action
700 * @param e An instance of an ActionEvent class
703 public void actionPerformed (final ActionEvent e) {
705 ((ManageableAddressbookContact) self.getClient().getManager()).doChangeOwnData();
706 } catch (final IOException | SQLException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
707 self.logException(ex);
713 menu.add(this.editOwnItem);
716 // 1) Add new contact
717 this.addMenuItem(menu, "addNewContact", new ActionListener() { //NOI18N
719 * If the user has performed this action
721 * @param e An instance of an ActionEvent class
724 public void actionPerformed (final ActionEvent e) {
725 ((ManageableAddressbookContact) self.getClient().getManager()).doAddOtherAddress();
729 // Add menu -> menu bar
732 // Add menu bar -> frame
733 this.frame.add(menuBar, BorderLayout.NORTH);
736 this.getLogger().trace("EXIT!"); //NOI18N
740 * Initializes name panel
742 * @param dialog A JDialog instance to this components to
744 private void initNameDataPanel (final JDialog dialog) {
746 this.getLogger().trace(MessageFormat.format("dialog={0} - CALLED!", dialog)); //NOI18N
748 // Panel "name" input boxes
749 JPanel namePanel = new JPanel();
750 namePanel.setLayout(new GridLayout(0, 2, 3, 3));
752 // Set border to titled version
753 namePanel.setBorder(new TitledBorder(this.generateBorderTitle("name"))); //NOI18N
756 JLabel gLabel = new JLabel(this.getBundle().getString("AddressbookFrame.gender.text"));
759 Gender[] genders = Gender.values();
761 // Init gender combo box with tool tip
762 JComboBox<Gender> gender = new JComboBox<>(new DefaultComboBoxModel<>(genders));
763 gender.setToolTipText(this.getBundle().getString("AddressbookFrame.gender.toolTipText"));
765 // Add both to gender panel
766 namePanel.add(gLabel);
767 namePanel.add(gender);
769 // Add text field for surname
770 this.addTextFieldWithLabelToPanel(namePanel, "surname", 20); //NOI18N
772 // Add text field for family name
773 this.addTextFieldWithLabelToPanel(namePanel, "familyName", 20); //NOI18N
775 // Finally add panel to dialog
776 dialog.add(namePanel);
779 this.getLogger().trace("EXIT!"); //NOI18N
783 * Initializes "other" data panel
785 * @param dialog A JDialog instance to this components to
786 * TODO Fill this with life
788 private void initOtherDataPanel (final JDialog dialog) {
790 this.getLogger().trace(MessageFormat.format("dialog={0} - CALLED!", dialog)); //NOI18N
792 // Panel "other" input boxes
793 JPanel otherPanel = new JPanel();
794 otherPanel.setLayout(new GridLayout(0, 2, 3, 3));
795 otherPanel.setBorder(new TitledBorder(this.generateBorderTitle("other"))); //NOI18N
797 // Add text field for email address
798 this.addTextFieldWithLabelToPanel(otherPanel, "emailAddress", 20); //NOI18N
800 // Add text field for phone number
801 this.addTextFieldWithLabelToPanel(otherPanel, "phoneNumber", 20); //NOI18N
803 // Add text field for cellphone number
804 this.addTextFieldWithLabelToPanel(otherPanel, "cellphoneNumber", 20); //NOI18N
806 // Add text field for fax number
807 this.addTextFieldWithLabelToPanel(otherPanel, "faxNumber", 20); //NOI18N
810 JLabel commentLabel = new JLabel(this.getBundle().getString("AddressbookFrame.comment.text"));
812 // Init text area with tool tip
813 JTextArea comment = new JTextArea(5, 20);
814 comment.setToolTipText(this.getBundle().getString("AddressbookFrame.comment.toolTipText"));
817 otherPanel.add(commentLabel);
818 otherPanel.add(comment);
820 // Finally add panel to dialog
821 dialog.add(otherPanel);
824 this.getLogger().trace("EXIT!"); //NOI18N
828 * Initialize other dialogs (e.g. "Add contact")
830 private void initOtherDialogs () {
832 this.getLogger().trace("CALLED!"); //NOI18N
834 // Init other windows:
836 initAddContactDialog();
839 this.getLogger().trace("EXIT!"); //NOI18N
843 * Initializes status panel
845 private void initStatusPanel () {
847 this.getLogger().trace("CALLED!"); //NOI18N
849 // Init status label (which needs to be updated
850 this.statusLabel = new JLabel();
851 this.updateStatus("initializing"); //NOI18N
853 // Init status bar in south
854 JPanel panel = new JPanel();
855 panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
856 panel.add(this.statusLabel);
857 panel.setBorder(BorderFactory.createEtchedBorder());
859 // Add panel to frame
860 this.frame.add(panel, BorderLayout.SOUTH);
863 this.getLogger().trace("EXIT!"); //NOI18N
867 * Initializes the table which will show all contacts
869 private void initTable () {
871 this.getLogger().trace("CALLED!"); //NOI18N
873 // Instance table model
874 this.dataModel = new ContactTableModel(this.getClient());
877 this.dataTable = new JTable(this.dataModel);
879 // Add mouse listener
880 this.dataTable.addMouseListener(new MouseAdapter() {
882 * If the user peformed a click on a cell
884 * @param e Mouse event instance
887 public void mouseClicked (final MouseEvent e) {
888 throw new UnsupportedOperationException("Unfinished."); //NOI18N
892 // Instance scroll pane
893 JScrollPane scroller = new JScrollPane();
895 // Add table to scroll pane
896 scroller.setViewportView(this.dataTable);
897 scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
898 scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
901 this.frame.add(scroller, BorderLayout.CENTER);
904 this.getLogger().trace("EXIT!"); //NOI18N
908 * Updates status to given type
910 * @param type Status type
912 private void updateStatus (final String type) {
914 this.getLogger().trace(MessageFormat.format("type={0} - CALLED!", type)); //NOI18N
916 // Set status message
917 this.statusLabel.setText(this.getBundle().getString(String.format("AddressbookFrame.statusLabel.%s.text", type))); //NOI18N
920 this.getLogger().trace("EXIT!"); //NOI18N
924 * Class for "add address" button
926 private static class AddActionListener extends BaseAddressbookSystem implements ActionListener {
930 private final JDialog dialog;
933 * Frame (not JFrame) instance
935 private final ClientFrame frame;
938 * Constructor for action listener
940 * @param dialog Dialog instance to call back
941 * @param frame Frame instance (not JFrame)
943 protected AddActionListener (final JDialog dialog, final ClientFrame frame) {
944 // Set dialog and frame here
945 this.dialog = dialog;
950 * If the action has been performed
952 * @param e The performed action event
955 public void actionPerformed (final ActionEvent e) {
956 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
961 * Class for "cancel address" button
963 private static class CancelActionListener extends BaseAddressbookSystem implements ActionListener {
967 private final JDialog dialog;
970 * Frame (not JFrame) instance
972 private final ClientFrame frame;
975 * Constructor for action listener
977 * @param dialog Dialog instance to call back
978 * @param frame Frame instance (not JFrame)
980 protected CancelActionListener (final JDialog dialog, final ClientFrame frame) {
981 // Set dialog and frame here
982 this.dialog = dialog;
987 * If the action has been performed
989 * @param e The performed action event
992 public void actionPerformed (final ActionEvent e) {
993 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.