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.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;
55 * @author Roland Haeder
57 public class AddressbookFrame extends javax.swing.JFrame implements ClientFrame {
61 private final Logger LOG;
66 private final Client client;
71 private static ClientFrame self;
76 private final ResourceBundle bundle;
82 LOG = LogManager.getLogger(this);
83 bundle = ResourceBundle.getBundle("org/mxchange/addressbook/localization/bundle"); // NOI18N
87 * Creates an instance of this frame with a client instance
90 private AddressbookFrame (final Client client) {
92 this.getLogger().debug("Initializing Swing frame ...");
99 public Application getApplication () {
100 throw new UnsupportedOperationException("Not implemented.");
104 public final Client getClient () {
109 public ManageableContact getContactManager () {
110 throw new UnsupportedOperationException("Not implemented.");
114 public void initFrame () {
116 this.initComponents();
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.
124 @SuppressWarnings ("unchecked")
125 // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
126 private void initComponents() {
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();
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) {
147 public void windowClosing(WindowEvent evt) {
148 AddressbookFrame.this.windowClosing(evt);
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));
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);
165 listAddressesPanel.setName("listAddressesPanel"); // NOI18N
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);
175 toolBar.setFloatable(false);
176 toolBar.setRollover(true);
177 toolBar.setName("toolBar"); // NOI18N
179 menuBar.setName("menuBar"); // NOI18N
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
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);
194 mainMenu.add(exitProgram);
196 menuBar.add(mainMenu);
198 addressbookMenu.setText(bundle.getString("AddressbookFrame.addressbookMenu.text_1")); // NOI18N
199 addressbookMenu.setName("addressbookMenu"); // NOI18N
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);
208 addressbookMenu.add(addOwnData);
209 addOwnData.getAccessibleContext().setAccessibleName(bundle.getString("AddressbookFrame.addOwnData.AccessibleContext.accessibleName_1")); // NOI18N
211 menuBar.add(addressbookMenu);
213 setJMenuBar(menuBar);
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)
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)
228 .addComponent(statusPanel, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE))
231 statusPanel.getAccessibleContext().setAccessibleName(bundle1.getString("AddressbookFrame.status.AccessibleContext.accessibleName")); // NOI18N
232 statusPanel.getAccessibleContext().setAccessibleDescription(bundle1.getString("AddressbookFrame.status.AccessibleContext.accessibleDescription")); // NOI18N
235 }// </editor-fold>//GEN-END:initComponents
237 private void exitProgramActionPerformed(ActionEvent evt) {//GEN-FIRST:event_exitProgramActionPerformed
238 // Close application instance
240 }//GEN-LAST:event_exitProgramActionPerformed
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
247 private void windowClosing(WindowEvent evt) {//GEN-FIRST:event_windowClosing
248 // TODO add your handling code here:
250 }//GEN-LAST:event_windowClosing
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
260 * @param client Client instance
263 public void setupFrame (final Client client) {
264 // Has the user entered own data?
265 if (this.getClient().getContactManager().isOwnContactAdded()) {
267 this.getLogger().debug("Disabling menus: isOwnContactAdded()=false");
269 // Not entered yet, so enable menu
270 addOwnData.setEnabled(false);
274 * Set the Nimbus look and feel
276 //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
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
283 for (UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
284 if ("Nimbus".equals(info.getName())) {
285 UIManager.setLookAndFeel(info.getClassName());
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);
301 statusLabel.setText(bundle.getString("AddressbookFrame.status.done.text"));
304 this.getLogger().debug("Displaying form ...");
307 * Create and display the form
309 java.awt.EventQueue.invokeLater(new Runnable() {
312 AddressbookFrame.getSelfInstance(client).setVisible(true);
318 * Singelton getter for this frame instance.
320 * @param client Client instance
321 * @return Returns a singelton instance of this frame
323 public static final ClientFrame getSelfInstance (final Client client) {
325 if (!(self instanceof ClientFrame)) {
326 // Create new instance
327 self = new AddressbookFrame(client);
339 protected final Logger getLogger () {
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