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.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;
45 * @author Roland Haeder
47 public class AddressbookFrame extends BaseFrameworkSystem implements ClientFrame {
52 private static ClientFrame self;
55 * Singelton getter for this frame instance.
57 * @param client Client instance
58 * @return Returns a singelton instance of this frame
60 public static final ClientFrame getSelfInstance (final Client client) {
62 if (!(self instanceof ClientFrame)) {
63 // Create new instance
64 self = new AddressbookFrame(client);
71 * Frame instance for "add own data"
73 private JMenuItem addOwnItem;
76 * Instance to table model
78 private TableModel dataModel;
83 private JTable dataTable;
86 * Frame instance for "edit own data"
88 private JMenuItem editOwnItem;
93 private final JFrame frame;
96 * Whether this frame has been initialized
98 private boolean isInitialized;
101 * Status label needs to be updated
103 private JLabel statusLabel;
106 * Creates an instance of this frame with a client instance
110 private AddressbookFrame (final Client client) {
112 this.getLogger().trace(MessageFormat.format("client={0}: CALLED!", client));
114 // Set frame instance
115 this.frame = new JFrame(AddressbookApplication.printableTitle());
118 this.setClient(client);
122 * Shutdown this frame
125 public void doShutdown () {
126 // First only show shutdown status
127 this.updateStatus("shutdown");
131 * Setups the frame, do not set isInitialized here
133 * @param client Client instance
136 public void setupFrame (final Client client) {
138 this.getLogger().trace(MessageFormat.format("client={0}: CALLED!", client));
140 // Has the user entered own data?
141 if (this.getClient().getContactManager().isOwnContactAdded()) {
143 this.getLogger().debug("Disabling menus: isOwnContactAdded()=false");
145 // Not entered yet, so disable "add" menu
146 this.addOwnItem.setEnabled(false);
149 this.editOwnItem.setEnabled(false);
152 // Make the frame visible
153 this.frame.setVisible(true);
156 this.updateStatus("done");
160 * Initalizes this frame. Having initComponents() exposed (publicly
161 * accessible) means that any other object can initialize components which
164 * @throws org.mxchange.addressbook.exceptions.FrameAlreadyInitializedException If this method has been called twice
167 public void init () throws FrameAlreadyInitializedException {
169 this.getLogger().trace("CALLED!");
171 // Has this frame been initialized?
172 if (this.isInitialized()) {
174 throw new FrameAlreadyInitializedException();
178 this.initComponents();
181 this.isInitialized = true;
185 * Returns field isInitialized. This flag indicates whether this frame has been initialized or not.
187 * @return Field isInitialized
190 public final boolean isInitialized () {
191 return this.isInitialized;
195 * Shuts down the application.
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.");
205 this.getClient().getApplication().doShutdown();
209 * Initialize components
211 private void initComponents () {
213 this.getLogger().trace("CALLED!");
215 // Set default close operation
216 this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
218 // Register shutdown listener
219 this.frame.addWindowListener(new WindowAdapter() {
221 * Invoked when a window has been closed.
224 public void windowClosed(final WindowEvent e) {
225 // Shutdown application cleanly
226 self.shutdownApplication();
230 * Invoked when a window is in the process of being closed.
231 * The close operation can be overridden at this point.
234 public void windowClosing(final WindowEvent e) {
235 // Also shutdown cleanly here
236 self.shutdownApplication();
240 // Setup layout manager
241 this.frame.setLayout(new BorderLayout(2, 2));
244 this.frame.setSize(700, 400);
246 // Center window in middle of screen, instead of top-left corner
247 this.frame.setLocationRelativeTo(null);
260 * Initializes the menu system
262 private void initMenuSystem () {
263 // Init menu bar, menu and item instances
264 JMenuBar menuBar = new JMenuBar();
270 menu = new JMenu(this.getBundle().getString("AddressbookFrame.menu.file.text"));
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"));
277 // Add listener to exit menu
278 item.addActionListener(new ActionListener() {
280 * If the user has performed this action
282 * @param e An instance of an ActionEvent class
285 public void actionPerformed (final ActionEvent e) {
286 self.shutdownApplication();
293 // Add menu -> menu bar
297 // 2) Addressbook menu
298 menu = new JMenu(this.getBundle().getString("AddressbookFrame.menu.addressbook.text"));
301 this.addOwnItem = new JMenuItem(this.getBundle().getString("AddressbookFrame.menuItem.addOwnData.text"));
302 this.addOwnItem.setToolTipText(this.getBundle().getString("AddressbookFrame.menuItem.addOwnData.toolTipText"));
304 // Add listener to exit menu
305 this.addOwnItem.addActionListener(new ActionListener() {
307 * If the user has performed this action
309 * @param e An instance of an ActionEvent class
312 public void actionPerformed (final ActionEvent e) {
313 self.getClient().getContactManager().doEnterOwnData();
318 menu.add(this.addOwnItem);
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"));
324 // Add listener to exit menu
325 this.editOwnItem.addActionListener(new ActionListener() {
327 * If the user has performed this action
329 * @param e An instance of an ActionEvent class
332 public void actionPerformed (final ActionEvent e) {
333 self.getClient().getContactManager().doChangeOwnData();
338 menu.add(this.editOwnItem);
340 // Add menu -> menu bar
343 // Add menu bar -> frame
344 this.frame.add(menuBar, BorderLayout.NORTH);
348 * Initializes status panel
350 private void initStatusPanel () {
351 // Init status label (which needs to be updated
352 this.statusLabel = new JLabel();
353 this.updateStatus("initializing");
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());
361 // Add panel to frame
362 this.frame.add(panel, BorderLayout.SOUTH);
366 * Initializes the table which will show all contacts
368 private void initTable () {
369 // Instance table model
370 this.dataModel = new AddressTableModel(this.getClient());
373 this.dataTable = new JTable(this.dataModel);
375 // Add mouse listener
376 this.dataTable.addMouseListener(new MouseAdapter() {
378 * If the user peformed a click on a cell
380 * @param e Mouse event instance
383 public void mouseClicked(final MouseEvent e) {
384 throw new UnsupportedOperationException("Unfinished.");
388 // Add table to frame
389 this.frame.add(this.dataTable, BorderLayout.CENTER);
393 * Updates status to given type
395 * @param type Status type
397 private void updateStatus (final String type) {
398 // Set status message
399 this.statusLabel.setText(this.getBundle().getString("AddressbookFrame.statusLabel." + type + ".text"));