]> git.mxchange.org Git - jfinancials-lib.git/blob - src/org/mxchange/addressbook/client/gui/AddressbookFrame.java
Project relocated (a bit better now?) + continued with Swing client
[jfinancials-lib.git] / 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.Color;
20 import java.awt.Cursor;
21 import java.awt.event.ActionEvent;
22 import java.awt.event.ActionListener;
23 import java.awt.event.InputEvent;
24 import java.awt.event.KeyEvent;
25 import java.awt.event.WindowAdapter;
26 import java.awt.event.WindowEvent;
27 import java.util.ResourceBundle;
28 import javax.swing.BorderFactory;
29 import javax.swing.BoxLayout;
30 import javax.swing.GroupLayout;
31 import javax.swing.JLabel;
32 import javax.swing.JMenu;
33 import javax.swing.JMenuBar;
34 import javax.swing.JMenuItem;
35 import javax.swing.JPanel;
36 import javax.swing.JScrollPane;
37 import javax.swing.JTable;
38 import javax.swing.JToolBar;
39 import javax.swing.KeyStroke;
40 import javax.swing.LayoutStyle;
41 import javax.swing.ListSelectionModel;
42 import javax.swing.SwingConstants;
43 import javax.swing.UIManager;
44 import javax.swing.WindowConstants;
45 import org.apache.logging.log4j.LogManager;
46 import org.apache.logging.log4j.Logger;
47 import org.mxchange.addressbook.application.AddressbookApplication;
48 import org.mxchange.addressbook.application.Application;
49 import org.mxchange.addressbook.client.Client;
50 import org.mxchange.addressbook.manager.contact.ManageableContact;
51 import org.mxchange.addressbook.model.address.AddressTableModel;
52
53 /**
54  *
55  * @author Roland Haeder
56  */
57 public class AddressbookFrame extends javax.swing.JFrame implements ClientFrame {
58     /**
59      * Class' logger
60      */
61     private final Logger LOG;
62
63     /**
64      * Client instance
65      */
66     private final Client client;
67
68     /**
69      * Own instance
70      */
71     private static ClientFrame self;
72
73     /**
74      * Bundle instance
75      */
76     private final ResourceBundle bundle;
77
78     /**
79      * Initialize object
80      */
81     {
82         LOG = LogManager.getLogger(this);
83         bundle = ResourceBundle.getBundle("org/mxchange/addressbook/localization/bundle"); // NOI18N
84     }
85
86     /**
87      * Creates an instance of this frame with a client instance
88      * @param client 
89      */
90     private AddressbookFrame (final Client client) {
91         // Debug line
92         this.getLogger().debug("Initializing Swing frame ...");
93
94         // Set client here
95         this.client = client;
96     }
97
98     @Override
99     public Application getApplication () {
100         throw new UnsupportedOperationException("Not implemented.");
101     }
102
103     @Override
104     public final Client getClient () {
105         return this.client;
106     }
107
108     @Override
109     public ManageableContact getContactManager () {
110         throw new UnsupportedOperationException("Not implemented.");
111     }
112
113     @Override
114     public void initFrame () {
115         // Init components
116         this.initComponents();
117     }
118
119     /**
120      * This method is called from within the constructor to initialize the form.
121      * WARNING: Do NOT modify this code. The content of this method is always
122      * regenerated by the Form Editor.
123      */
124     @SuppressWarnings ("unchecked")
125     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
126     private void initComponents() {
127
128         statusPanel = new JPanel();
129         statusLabel = new JLabel();
130         listAddressesPanel = new JScrollPane();
131         addressesTable = new JTable();
132         toolBar = new JToolBar();
133         menuBar = new JMenuBar();
134         mainMenu = new JMenu();
135         exitProgram = new JMenuItem();
136         addressbookMenu = new JMenu();
137         addOwnData = new JMenuItem();
138
139         setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
140         setTitle(AddressbookApplication.printableTitle());
141         setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
142         setName("main"); // NOI18N
143         addWindowListener(new WindowAdapter() {
144             public void windowClosed(WindowEvent evt) {
145                 exitProgram(evt);
146             }
147             public void windowClosing(WindowEvent evt) {
148                 AddressbookFrame.this.windowClosing(evt);
149             }
150         });
151
152         statusPanel.setBorder(BorderFactory.createEtchedBorder(Color.lightGray, new Color(153, 153, 153)));
153         statusPanel.setAutoscrolls(true);
154         statusPanel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
155         statusPanel.setFocusable(false);
156         statusPanel.setName("status"); // NOI18N
157         statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.X_AXIS));
158
159         ResourceBundle bundle = ResourceBundle.getBundle("org/mxchange/addressbook/localization/bundle"); // NOI18N
160         statusLabel.setText(bundle.getString("AddressbookFrame.statusLabel.text_1")); // NOI18N
161         statusLabel.setHorizontalTextPosition(SwingConstants.LEFT);
162         statusLabel.setName("statusLabel"); // NOI18N
163         statusPanel.add(statusLabel);
164
165         listAddressesPanel.setName("listAddressesPanel"); // NOI18N
166
167         addressesTable.setModel(new AddressTableModel(this.getClient()));
168         addressesTable.setColumnSelectionAllowed(true);
169         addressesTable.setFillsViewportHeight(true);
170         addressesTable.setFocusable(false);
171         addressesTable.setName("addressesTable"); // NOI18N
172         listAddressesPanel.setViewportView(addressesTable);
173         addressesTable.getColumnModel().getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
174
175         toolBar.setFloatable(false);
176         toolBar.setRollover(true);
177         toolBar.setName("toolBar"); // NOI18N
178
179         menuBar.setName("menuBar"); // NOI18N
180
181         ResourceBundle bundle1 = ResourceBundle.getBundle("org/mxchange/addressbook/client/gui/Bundle"); // NOI18N
182         mainMenu.setText(bundle1.getString("AddressbookFrame.text")); // NOI18N
183         mainMenu.setFocusable(false);
184         mainMenu.setName(""); // NOI18N
185
186         exitProgram.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK));
187         exitProgram.setText(bundle.getString("AddressbookFrame.exitProgram.text_1")); // NOI18N
188         exitProgram.setName("exitProgram"); // NOI18N
189         exitProgram.addActionListener(new ActionListener() {
190             public void actionPerformed(ActionEvent evt) {
191                 exitProgramActionPerformed(evt);
192             }
193         });
194         mainMenu.add(exitProgram);
195
196         menuBar.add(mainMenu);
197
198         addressbookMenu.setText(bundle.getString("AddressbookFrame.addressbookMenu.text_1")); // NOI18N
199         addressbookMenu.setName("addressbookMenu"); // NOI18N
200
201         addOwnData.setText(bundle1.getString("AddressbookFrame.addOwn.text")); // NOI18N
202         addOwnData.setName("addOwn"); // NOI18N
203         addOwnData.addActionListener(new ActionListener() {
204             public void actionPerformed(ActionEvent evt) {
205                 addOwnDataActionPerformed(evt);
206             }
207         });
208         addressbookMenu.add(addOwnData);
209         addOwnData.getAccessibleContext().setAccessibleName(bundle.getString("AddressbookFrame.addOwnData.AccessibleContext.accessibleName_1")); // NOI18N
210
211         menuBar.add(addressbookMenu);
212
213         setJMenuBar(menuBar);
214
215         GroupLayout layout = new GroupLayout(getContentPane());
216         getContentPane().setLayout(layout);
217         layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
218             .addComponent(statusPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
219             .addComponent(listAddressesPanel, GroupLayout.DEFAULT_SIZE, 700, Short.MAX_VALUE)
220             .addComponent(toolBar, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
221         );
222         layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
223             .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
224                 .addComponent(toolBar, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
225                 .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
226                 .addComponent(listAddressesPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
227                 .addGap(1, 1, 1)
228                 .addComponent(statusPanel, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE))
229         );
230
231         statusPanel.getAccessibleContext().setAccessibleName(bundle1.getString("AddressbookFrame.status.AccessibleContext.accessibleName")); // NOI18N
232         statusPanel.getAccessibleContext().setAccessibleDescription(bundle1.getString("AddressbookFrame.status.AccessibleContext.accessibleDescription")); // NOI18N
233
234         pack();
235     }// </editor-fold>//GEN-END:initComponents
236
237     private void exitProgramActionPerformed(ActionEvent evt) {//GEN-FIRST:event_exitProgramActionPerformed
238         // Close application instance
239         dispose();
240     }//GEN-LAST:event_exitProgramActionPerformed
241
242     private void addOwnDataActionPerformed(ActionEvent evt) {//GEN-FIRST:event_addOwnDataActionPerformed
243         // Asks the user to enter own data
244         this.getClient().getContactManager().doEnterOwnData();
245     }//GEN-LAST:event_addOwnDataActionPerformed
246
247     private void windowClosing(WindowEvent evt) {//GEN-FIRST:event_windowClosing
248         // TODO add your handling code here:
249         dispose();
250     }//GEN-LAST:event_windowClosing
251
252     private void exitProgram(WindowEvent evt) {//GEN-FIRST:event_exitProgram
253         // TODO add your handling code here:
254         this.getClient().getApplication().doShutdown();
255     }//GEN-LAST:event_exitProgram
256
257     /**
258      * Setups the frame
259      * 
260      * @param client Client instance
261      */
262     @Override
263     public void setupFrame (final Client client) {
264         // Has the user entered own data?
265         if (this.getClient().getContactManager().isOwnContactAdded()) {
266             // Debug message
267             this.getLogger().debug("Disabling menus: isOwnContactAdded()=false");
268
269             // Not entered yet, so enable menu
270             addOwnData.setEnabled(false);
271         }
272
273         /*
274          * Set the Nimbus look and feel
275          */
276         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
277         /*
278          * If Nimbus (introduced in Java SE 6) is not available, stay with the
279          * default look and feel. For details see
280          * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
281          */
282         try {
283             for (UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
284                 if ("Nimbus".equals(info.getName())) {
285                     UIManager.setLookAndFeel(info.getClassName());
286                     break;
287                 }
288             }
289         } catch (final ClassNotFoundException ex) {
290             this.getLogger().catching(ex);
291         } catch (final InstantiationException ex) {
292             this.getLogger().catching(ex);
293         } catch (final IllegalAccessException ex) {
294             this.getLogger().catching(ex);
295         } catch (final javax.swing.UnsupportedLookAndFeelException ex) {
296             this.getLogger().catching(ex);
297         }
298         //</editor-fold>
299
300         // All done here
301         statusLabel.setText(bundle.getString("AddressbookFrame.status.done.text"));
302
303         // Debug line
304         this.getLogger().debug("Displaying form ...");
305
306         /*
307          * Create and display the form
308          */
309         java.awt.EventQueue.invokeLater(new Runnable() {
310             @Override
311             public void run () {
312                 AddressbookFrame.getSelfInstance(client).setVisible(true);
313             }
314         });
315     }
316
317     /**
318      * Singelton getter for this frame instance.
319      * 
320      * @param client Client instance
321      * @return Returns a singelton instance of this frame
322      */
323     public static final ClientFrame getSelfInstance (final Client client) {
324         // Is it set?
325         if (!(self instanceof ClientFrame)) {
326             // Create new instance
327             self = new AddressbookFrame(client);
328         }
329     
330         // Return instance
331         return self;
332     }
333
334     /**
335      * Getter for logger
336      *
337      * @return Logger
338      */
339     protected final Logger getLogger () {
340         return this.LOG;
341     }
342
343     // Variables declaration - do not modify//GEN-BEGIN:variables
344     private JMenuItem addOwnData;
345     private JMenu addressbookMenu;
346     private JTable addressesTable;
347     private JMenuItem exitProgram;
348     private JScrollPane listAddressesPanel;
349     private JMenu mainMenu;
350     private JMenuBar menuBar;
351     private JLabel statusLabel;
352     private JPanel statusPanel;
353     private JToolBar toolBar;
354     // End of variables declaration//GEN-END:variables
355 }