]> git.mxchange.org Git - addressbook-lib.git/blob - Addressbook/src/org/mxchange/addressbook/client/gui/AddressbookFrame.java
set visibility for debugging purposes here
[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(500, 400);
276
277                 // Only for developing:
278                 /* DEBUG: */ this.addContact.setVisible(true);
279
280                 /*
281                  * Add listener which asks for confirmation, if data has been entered
282                  * but not saved yet. The user may appriciate this ... ;-)
283                  *
284                  * @TODO Unfinished
285                  */
286                 
287                 // Init 3 panels:
288                 // 1) "Name" panel
289                 initNamePanel();
290         }
291
292         /**
293          * Initialize components
294          */
295         private void initComponents () {
296                 // Debug line
297                 this.getLogger().trace("CALLED!");
298
299                 // Set default close operation
300                 this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
301
302                 // Register shutdown listener
303                 this.frame.addWindowListener(new WindowAdapter() {
304                         /**
305                          * Invoked when a window has been closed.
306                          */
307                         @Override
308                         public void windowClosed (final WindowEvent e) {
309                                 // Shutdown application cleanly
310                                 self.shutdownApplication();
311                         }
312
313                         /**
314                          * Invoked when a window is in the process of being closed. The
315                          * close operation can be overridden at this point.
316                          */
317                         @Override
318                         public void windowClosing (final WindowEvent e) {
319                                 // Also shutdown cleanly here
320                                 self.shutdownApplication();
321                         }
322                 });
323
324                 // Setup layout manager
325                 this.frame.setLayout(new BorderLayout(2, 2));
326
327                 // Set window size
328                 this.frame.setSize(700, 400);
329
330                 // Center window in middle of screen, instead of top-left corner
331                 this.frame.setLocationRelativeTo(null);
332
333                 // Init menu system
334                 initMenuSystem();
335
336                 // Init table
337                 initTable();
338
339                 // Init status panel
340                 initStatusPanel();
341
342                 // Init other windows
343                 initOtherDialogs();
344         }
345
346         /**
347          * Initializes the menu system
348          */
349         private void initMenuSystem () {
350                 // Init menu bar, menu and item instances
351                 JMenuBar menuBar = new JMenuBar();
352                 JMenu menu;
353                 JMenuItem item;
354                 
355                 // Init some menus:
356                 // 1) File menu
357                 menu = new JMenu(this.getBundle().getString("AddressbookFrame.menu.file.text"));
358                 
359                 // Add menu items:
360                 // 1.x) Exit program (should be last)
361                 item = new JMenuItem(this.getBundle().getString("AddressbookFrame.menuItem.exitProgram.text"));
362                 item.setToolTipText(this.getBundle().getString("AddressbookFrame.menuItem.exitProgram.toolTipText"));
363
364                 // Add listener to exit menu
365                 item.addActionListener(new ActionListener() {
366                         /**
367                          * If the user has performed this action
368                          *
369                          * @param e An instance of an ActionEvent class
370                          */
371                         @Override
372                         public void actionPerformed (final ActionEvent e) {
373                                 self.shutdownApplication();
374                         }
375                 });
376
377                 // Add item -> menu
378                 menu.add(item);
379
380                 // Add menu -> menu bar
381                 menuBar.add(menu);
382                 
383                 // Init some menus:
384                 // 2) Addressbook menu
385                 menu = new JMenu(this.getBundle().getString("AddressbookFrame.menu.addressbook.text"));
386
387                 // 2.1) Add own data
388                 this.addOwnItem = new JMenuItem(this.getBundle().getString("AddressbookFrame.menuItem.addOwnData.text"));
389                 this.addOwnItem.setToolTipText(this.getBundle().getString("AddressbookFrame.menuItem.addOwnData.toolTipText"));
390
391                 // Add listener to exit menu
392                 this.addOwnItem.addActionListener(new ActionListener() {
393                         /**
394                          * If the user has performed this action
395                          *
396                          * @param e An instance of an ActionEvent class
397                          */
398                         @Override
399                         public void actionPerformed (final ActionEvent e) {
400                                 self.getClient().getContactManager().doEnterOwnData();
401                         }
402                 });
403
404                 // Add item -> menu
405                 menu.add(this.addOwnItem);
406
407                 // 2.2) Edit own data
408                 this.editOwnItem = new JMenuItem(this.getBundle().getString("AddressbookFrame.menuItem.editOwnData.text"));
409                 this.editOwnItem.setToolTipText(this.getBundle().getString("AddressbookFrame.menuItem.editOwnData.toolTipText"));
410
411                 // Add listener to exit menu
412                 this.editOwnItem.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.getClient().getContactManager().doChangeOwnData();
421                         }
422                 });
423
424                 // Add item -> menu
425                 menu.add(this.editOwnItem);
426
427                 // Add menu -> menu bar
428                 menuBar.add(menu);
429
430                 // Add menu bar -> frame
431                 this.frame.add(menuBar, BorderLayout.NORTH);
432         }
433
434         /**
435          * Initializes name panel
436          */
437         private void initNamePanel () {
438                 // 1) Panel "name" input boxes
439                 JPanel namePanel = new JPanel();
440                 namePanel.setLayout(new BoxLayout(namePanel, BoxLayout.Y_AXIS));
441                 
442                 // Set border to titled version
443                 namePanel.setBorder(new TitledBorder(this.generateBorderTitle("name")));
444                 
445                 // Get all genders
446                 Gender[] genders = Gender.values();
447                 
448                 // Init gender combo box
449                 JComboBox<Gender> gender = new JComboBox<>(new DefaultComboBoxModel<>(genders));
450                 
451                 // Panel for gender
452                 JPanel gPanel = new JPanel();
453                 gPanel.setLayout(new GridLayout(1, 2));
454                 
455                 // Set tooltip text
456                 gPanel.setToolTipText(this.getBundle().getString("AddressbookFrame.gender.tooltipText"));
457                 
458                 // Gender text field
459                 JLabel gLabel = new JLabel(this.getBundle().getString("AddressbookFrame.gender.text"));
460                 
461                 // Add both to gender panel
462                 gPanel.add(gLabel);
463                 gPanel.add(gender);
464                 
465                 // Add gender panel to "name" panel
466                 namePanel.add(gPanel);
467                 
468                 // Panel for surname
469                 JPanel sPanel = new JPanel();
470                 sPanel.setLayout(new GridLayout(1, 2));
471                 
472                 // Set too tip text
473                 sPanel.setToolTipText(this.getBundle().getString("AddressbookFrame.surname.tooltipText"));
474                 
475                 // New label for surname is not needed
476                 JLabel sLabel = new JLabel(this.getBundle().getString("AddressbookFrame.surname.text"));
477                 
478                 // And input box
479                 JTextField surname = new JTextField(20);
480                 
481                 // Add both to surname panel
482                 sPanel.add(sLabel);
483                 sPanel.add(surname);
484                 
485                 // Add surname panel to "name" panel
486                 namePanel.add(sPanel);
487                 
488                 // Panel for surname
489                 JPanel fPanel = new JPanel();
490                 fPanel.setLayout(new GridLayout(1, 2));
491                 
492                 // Set too tip text
493                 fPanel.setToolTipText(this.getBundle().getString("AddressbookFrame.familyName.tooltipText"));
494                 
495                 // New label for surname is not needed
496                 JLabel fLabel = new JLabel(this.getBundle().getString("AddressbookFrame.familyName.text"));
497                 
498                 // And input box
499                 JTextField familyName = new JTextField(20);
500                 
501                 // Add both to surname panel
502                 fPanel.add(fLabel);
503                 fPanel.add(familyName);
504                 
505                 // Add family namepanel to "name" panel
506                 namePanel.add(fPanel);
507                 
508                 // Finally add panel to dialog
509                 this.addContact.add(namePanel);
510         }
511
512         /**
513          * Initialize other dialogs (e.g. "Add contact")
514          */
515         private void initOtherDialogs () {
516                 // Init other windows:
517                 // 1) Add contact
518                 initAddContactDialog();
519         }
520
521         /**
522          * Initializes status panel
523          */
524         private void initStatusPanel () {
525                 // Init status label (which needs to be updated
526                 this.statusLabel = new JLabel();
527                 this.updateStatus("initializing");
528
529                 // Init status bar in south
530                 JPanel panel = new JPanel();
531                 panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
532                 panel.add(this.statusLabel);
533                 panel.setBorder(BorderFactory.createEtchedBorder());
534
535                 // Add panel to frame
536                 this.frame.add(panel, BorderLayout.SOUTH);
537         }
538
539         /**
540          * Initializes the table which will show all contacts
541          */
542         private void initTable () {
543                 // Instance table model
544                 this.dataModel = new ContactTableModel(this.getClient());
545
546                 // Instance table
547                 this.dataTable = new JTable(this.dataModel);
548
549                 // Add mouse listener
550                 this.dataTable.addMouseListener(new MouseAdapter() {
551                         /**
552                          * If the user peformed a click on a cell
553                          *
554                          * @param e Mouse event instance
555                          */
556                         @Override
557                         public void mouseClicked (final MouseEvent e) {
558                                 throw new UnsupportedOperationException("Unfinished.");
559                         }
560                 });
561
562                 // Instance scroll pane
563                 JScrollPane scroller = new JScrollPane();
564
565                 // Add table to scroll pane
566                 scroller.setViewportView(this.dataTable);
567                 scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
568                 scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
569
570                 // Add pane to frame
571                 this.frame.add(scroller, BorderLayout.CENTER);
572         }
573
574         /**
575          * Updates status to given type
576          *
577          * @param type Status type
578          */
579         private void updateStatus (final String type) {
580                 // Set status message
581                 this.statusLabel.setText(this.getBundle().getString(String.format("AddressbookFrame.statusLabel.%s.text", type)));
582         }
583 }