]> git.mxchange.org Git - addressbook-swing.git/blob - Addressbook/src/org/mxchange/addressbook/client/gui/AddressbookFrame.java
8f8cee6d1795935fd7a8789188676fa8aa760e47
[addressbook-swing.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.contact.ContactTableModel;
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
165          * org.mxchange.addressbook.exceptions.FrameAlreadyInitializedException If
166          * this method has been called twice
167          */
168         @Override
169         public void init () throws FrameAlreadyInitializedException {
170                 // Debug line
171                 this.getLogger().trace("CALLED!");
172
173                 // Has this frame been initialized?
174                 if (this.isInitialized()) {
175                         // Throw exception
176                         throw new FrameAlreadyInitializedException();
177                 }
178
179                 // Init components
180                 this.initComponents();
181
182                 // Set flag
183                 this.isInitialized = true;
184         }
185
186         /**
187          * Returns field isInitialized. This flag indicates whether this frame has
188          * been initialized or not.
189          *
190          * @return Field isInitialized
191          */
192         @Override
193         public final boolean isInitialized () {
194                 return this.isInitialized;
195         }
196
197         /**
198          * Shuts down the application.
199          */
200         @Override
201         public void shutdownApplication () {
202                 // To do this, the frame must be initialized
203                 if (!this.isInitialized()) {
204                         // Not initalized, so bad call
205                         this.getLogger().fatal("Bad call of shutdownApplication(). Please report this.");
206                         return;
207                 }
208                 this.getClient().getApplication().doShutdown();
209         }
210
211         /**
212          * Initialize components
213          */
214         private void initComponents () {
215                 // Debug line
216                 this.getLogger().trace("CALLED!");
217
218                 // Set default close operation
219                 this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
220
221                 // Register shutdown listener
222                 this.frame.addWindowListener(new WindowAdapter() {
223                         /**
224                          * Invoked when a window has been closed.
225                          */
226                         @Override
227                         public void windowClosed (final WindowEvent e) {
228                                 // Shutdown application cleanly
229                                 self.shutdownApplication();
230                         }
231
232                         /**
233                          * Invoked when a window is in the process of being closed. The
234                          * close operation can be overridden at this point.
235                          */
236                         @Override
237                         public void windowClosing (final WindowEvent e) {
238                                 // Also shutdown cleanly here
239                                 self.shutdownApplication();
240                         }
241                 });
242
243                 // Setup layout manager
244                 this.frame.setLayout(new BorderLayout(2, 2));
245
246                 // Set window size
247                 this.frame.setSize(700, 400);
248
249                 // Center window in middle of screen, instead of top-left corner
250                 this.frame.setLocationRelativeTo(null);
251
252                 // Init menu system
253                 initMenuSystem();
254
255                 // Init table
256                 initTable();
257
258                 // Init status panel
259                 initStatusPanel();
260         }
261
262         /**
263          * Initializes the menu system
264          */
265         private void initMenuSystem () {
266                 // Init menu bar, menu and item instances
267                 JMenuBar menuBar = new JMenuBar();
268                 JMenu menu;
269                 JMenuItem item;
270
271         // Init some menus:
272                 // 1) File menu
273                 menu = new JMenu(this.getBundle().getString("AddressbookFrame.menu.file.text"));
274
275         // Add menu items:
276                 // 1.x) Exit program (should be last)
277                 item = new JMenuItem(this.getBundle().getString("AddressbookFrame.menuItem.exitProgram.text"));
278                 item.setToolTipText(this.getBundle().getString("AddressbookFrame.menuItem.exitProgram.toolTipText"));
279
280                 // Add listener to exit menu
281                 item.addActionListener(new ActionListener() {
282                         /**
283                          * If the user has performed this action
284                          *
285                          * @param e An instance of an ActionEvent class
286                          */
287                         @Override
288                         public void actionPerformed (final ActionEvent e) {
289                                 self.shutdownApplication();
290                         }
291                 });
292
293                 // Add item -> menu
294                 menu.add(item);
295
296                 // Add menu -> menu bar
297                 menuBar.add(menu);
298
299         // Init some menus:
300                 // 2) Addressbook menu
301                 menu = new JMenu(this.getBundle().getString("AddressbookFrame.menu.addressbook.text"));
302
303                 // 2.1) Add own data
304                 this.addOwnItem = new JMenuItem(this.getBundle().getString("AddressbookFrame.menuItem.addOwnData.text"));
305                 this.addOwnItem.setToolTipText(this.getBundle().getString("AddressbookFrame.menuItem.addOwnData.toolTipText"));
306
307                 // Add listener to exit menu
308                 this.addOwnItem.addActionListener(new ActionListener() {
309                         /**
310                          * If the user has performed this action
311                          *
312                          * @param e An instance of an ActionEvent class
313                          */
314                         @Override
315                         public void actionPerformed (final ActionEvent e) {
316                                 self.getClient().getContactManager().doEnterOwnData();
317                         }
318                 });
319
320                 // Add item -> menu
321                 menu.add(this.addOwnItem);
322
323                 // 2.2) Edit own data
324                 this.editOwnItem = new JMenuItem(this.getBundle().getString("AddressbookFrame.menuItem.editOwnData.text"));
325                 this.editOwnItem.setToolTipText(this.getBundle().getString("AddressbookFrame.menuItem.editOwnData.toolTipText"));
326
327                 // Add listener to exit menu
328                 this.editOwnItem.addActionListener(new ActionListener() {
329                         /**
330                          * If the user has performed this action
331                          *
332                          * @param e An instance of an ActionEvent class
333                          */
334                         @Override
335                         public void actionPerformed (final ActionEvent e) {
336                                 self.getClient().getContactManager().doChangeOwnData();
337                         }
338                 });
339
340                 // Add item -> menu
341                 menu.add(this.editOwnItem);
342
343                 // Add menu -> menu bar
344                 menuBar.add(menu);
345
346                 // Add menu bar -> frame
347                 this.frame.add(menuBar, BorderLayout.NORTH);
348         }
349
350         /**
351          * Initializes status panel
352          */
353         private void initStatusPanel () {
354                 // Init status label (which needs to be updated
355                 this.statusLabel = new JLabel();
356                 this.updateStatus("initializing");
357
358                 // Init status bar in south
359                 JPanel panel = new JPanel();
360                 panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
361                 panel.add(this.statusLabel);
362                 panel.setBorder(BorderFactory.createEtchedBorder());
363
364                 // Add panel to frame
365                 this.frame.add(panel, BorderLayout.SOUTH);
366         }
367
368         /**
369          * Initializes the table which will show all contacts
370          */
371         private void initTable () {
372                 // Instance table model
373                 this.dataModel = new ContactTableModel(this.getClient());
374
375                 // Instance table
376                 this.dataTable = new JTable(this.dataModel);
377
378                 // Add mouse listener
379                 this.dataTable.addMouseListener(new MouseAdapter() {
380                         /**
381                          * If the user peformed a click on a cell
382                          *
383                          * @param e Mouse event instance
384                          */
385                         @Override
386                         public void mouseClicked (final MouseEvent e) {
387                                 throw new UnsupportedOperationException("Unfinished.");
388                         }
389                 });
390
391                 // Add table to frame
392                 this.frame.add(this.dataTable, BorderLayout.CENTER);
393         }
394
395         /**
396          * Updates status to given type
397          *
398          * @param type Status type
399          */
400         private void updateStatus (final String type) {
401                 // Set status message
402                 this.statusLabel.setText(this.getBundle().getString("AddressbookFrame.statusLabel." + type + ".text"));
403         }
404 }