1   package org.wcb.gui.forms.tabbed;
2   
3   import org.wcb.gui.renderer.*;
4   import org.wcb.gui.renderer.jtable.ColumnGroup;
5   import org.wcb.gui.renderer.jtable.GroupableTableHeader;
6   import org.wcb.gui.renderer.jtable.GroupableTableColumnModel;
7   import org.wcb.gui.util.TableUtils;
8   import org.wcb.gui.util.UIHelper;
9   import org.wcb.gui.util.ApplicationPreferences;
10  import org.wcb.gui.util.IPilotsLogApplicationPreferenceKeys;
11  
12  import org.wcb.gui.table.model.LogbookTableModel;
13  import org.wcb.gui.table.LogbookTable;
14  import org.wcb.gui.component.WaveButton;
15  import org.wcb.gui.component.JXTitlePanel;
16  import org.wcb.model.util.SpringUtil;
17  import org.wcb.model.service.IServicesConstants;
18  import org.wcb.resources.MessageResourceRegister;
19  import org.wcb.resources.MessageKey;
20  import org.jdesktop.swingx.border.DropShadowBorder;
21  
22  
23  import javax.swing.*;
24  import javax.swing.table.TableColumn;
25  import javax.swing.border.Border;
26  import javax.swing.border.CompoundBorder;
27  import java.awt.*;
28  import java.awt.print.PrinterException;
29  import java.awt.event.ActionListener;
30  import java.awt.event.ActionEvent;
31  import java.util.*;
32  import java.util.logging.Logger;
33  import java.util.logging.Level;
34  import java.text.MessageFormat;
35  
36  /**
37   * <small>
38   * <p>
39   * Copyright (c)  2006  wbogaardt.
40   * This library is free software; you can redistribute it and/or
41   * modify it under the terms of the GNU Lesser General Public
42   * License as published by the Free Software Foundation; either
43   * version 2.1 of the License, or (at your option) any later version.
44   *
45   * This library is distributed in the hope that it will be useful,
46   * but WITHOUT ANY WARRANTY; without even the implied warranty of
47   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
48   * Lesser General Public License for more details.
49   *
50   * You should have received a copy of the GNU Lesser General Public
51   * License along with this library; if not, write to the Free Software
52   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
53   * <p/>
54   * $File:  $ <br>
55   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Mar 31, 2006 9:13:58 AM $ <br>
56   * </small>
57   *
58   * @author wbogaardt
59   * @version 1
60   *          Date: Mar 31, 2006
61   *          Time: 9:13:58 AM
62   */
63  
64  public class LogbookTableDisplayForm extends JXTitlePanel implements ActionListener {
65  
66  
67      private LogbookTableModel logbookTableModel;
68      private JButton pageBeginingButton;
69      private JButton forward;
70      private JButton back;
71      private JButton pageEndButton;
72      private JButton printCurrentPageButton;
73      private LogbookTable jTableLogbook;
74      private JLabel currentPageLabel;
75      private Logger LOG = Logger.getLogger(LogbookTableDisplayForm.class.getName());
76  
77      public LogbookTableDisplayForm () {
78          super("Logbook", new Color(0x3779ff), UIHelper.getIcon("org/wcb/resources/gui/logbook.png"));
79          Border shadow = new DropShadowBorder(Color.BLACK, 2, 5, .3f, 10, false, true, true, true);
80          setBorder(new CompoundBorder(shadow, getBorder()));
81          initComponents();
82      }
83  
84      /**
85       * Used to refresh the table model data information itself.
86       * This typically needs refresh when a new entry is added.
87       */
88      public void refresh() {
89          if (logbookTableModel != null) {
90              logbookTableModel.refreshModel();
91          }
92      }
93  
94      public void initComponents() {
95          setLayout(new BorderLayout());
96          logbookTableModel = (LogbookTableModel) SpringUtil.getApplicationContext().getBean(IServicesConstants.SWING_LOGBOOK_MODEL);
97          logbookTableModel.setMaxEntriesPerPage(ApplicationPreferences.getInstance().getInt(IPilotsLogApplicationPreferenceKeys.LOG_ENTRIES_PER_PAGE, 15));
98          
99          jTableLogbook = new LogbookTable();
100         MultiLineHeaderRenderer renderer = new MultiLineHeaderRenderer();
101             Enumeration e = jTableLogbook.getColumnModel().getColumns();
102             while (e.hasMoreElements()) {
103               ((TableColumn) e.nextElement()).setHeaderRenderer(renderer);
104             }
105 
106         jTableLogbook.setColumnModel(new GroupableTableColumnModel());
107         jTableLogbook.setTableHeader(new GroupableTableHeader((GroupableTableColumnModel) jTableLogbook.getColumnModel()));
108         jTableLogbook.setModel(logbookTableModel);
109 
110         GroupableTableColumnModel cm = (GroupableTableColumnModel)jTableLogbook.getColumnModel();
111         ColumnGroup firstActionGroup = new ColumnGroup("Actions");
112         firstActionGroup.add(cm.getColumn(0));
113         firstActionGroup.add(cm.getColumn(1));
114         ColumnGroup routeofFlightGroup = new ColumnGroup("Route of Flight");
115         routeofFlightGroup.add(cm.getColumn(4));
116         routeofFlightGroup.add(cm.getColumn(5));
117         routeofFlightGroup.add(cm.getColumn(6));
118         ColumnGroup typesOfFlightGroup = new ColumnGroup("Type of Piloting Time");
119         typesOfFlightGroup.add(cm.getColumn(8));
120         typesOfFlightGroup.add(cm.getColumn(9));
121         typesOfFlightGroup.add(cm.getColumn(10));
122         typesOfFlightGroup.add(cm.getColumn(11));
123         typesOfFlightGroup.add(cm.getColumn(12));
124         typesOfFlightGroup.add(cm.getColumn(13));
125         typesOfFlightGroup.add(cm.getColumn(14));
126         ColumnGroup dayGroup = new ColumnGroup("Day");
127         dayGroup.add(cm.getColumn(15));
128         ColumnGroup nightGroup = new ColumnGroup("Night");
129         nightGroup.add(cm.getColumn(16));
130         ColumnGroup conditionsOfFlightGroup = new ColumnGroup("Conditions of Flight");
131         conditionsOfFlightGroup.add(cm.getColumn(18));
132         conditionsOfFlightGroup.add(cm.getColumn(19));
133         conditionsOfFlightGroup.add(cm.getColumn(20));
134         ColumnGroup actionsGroup = new ColumnGroup("Actions");
135         actionsGroup.add(cm.getColumn(22));
136         actionsGroup.add(cm.getColumn(23));
137         jTableLogbook.getTableHeader();
138         cm.addColumnGroup(firstActionGroup);
139         cm.addColumnGroup(routeofFlightGroup);
140         cm.addColumnGroup(typesOfFlightGroup);
141         cm.addColumnGroup(dayGroup);
142         cm.addColumnGroup(nightGroup);
143         cm.addColumnGroup(conditionsOfFlightGroup);
144         cm.addColumnGroup(actionsGroup);
145 
146 
147         jTableLogbook.setDefaultRenderer(String.class, new ColorRowsTableCellRender());
148         jTableLogbook.setDefaultRenderer(Double.class, new ColorRowsTableCellRender());
149         jTableLogbook.setDefaultRenderer(Integer.class, new ColorRowsTableCellRender());
150         jTableLogbook.setDefaultRenderer(Date.class, new ColorRowsTableCellRender());
151         //new RowButtonTableCell(jTableLogbook, 0);
152         new EditButtonTableCellEditor(jTableLogbook, 0);
153         new DeleteButtonTableCellEditor(jTableLogbook,1);
154         new EditButtonTableCellEditor(jTableLogbook, 22);
155         new DeleteButtonTableCellEditor(jTableLogbook,23);
156         FontMetrics metrics = jTableLogbook.getFontMetrics(new Font("SanSerif", Font.PLAIN, 10));
157         TableUtils.setColumnWidths(jTableLogbook, metrics, jTableLogbook.getInsets(), true,false);
158 
159         JScrollPane jscrolling = new JScrollPane(jTableLogbook, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
160         jscrolling.setBorder(BorderFactory.createTitledBorder("Pilot Log Entries"));
161         jscrolling.setViewportView(jTableLogbook);
162 
163         int viewerPage = 1 + logbookTableModel.getCurrentPage();
164         int totalPages = logbookTableModel.getTotalPages() + 1;
165         currentPageLabel = new JLabel("Page " + viewerPage + " of " +  totalPages);
166         printCurrentPageButton = new WaveButton(MessageResourceRegister.getInstance().getValue(MessageKey.BUTTON_PRINT));
167         printCurrentPageButton.setToolTipText("Print current page");
168         pageBeginingButton = new WaveButton("<<");
169         pageBeginingButton.setToolTipText("First page");
170         forward = new WaveButton(">");
171         forward.setToolTipText("Page forward");
172         back = new WaveButton("<");
173         back.setToolTipText("Page Back");
174         pageEndButton = new WaveButton(">>");
175         pageEndButton.setToolTipText("Last page");
176         printCurrentPageButton.addActionListener(this);
177         forward.addActionListener(this);
178         back.addActionListener(this);
179         pageBeginingButton.addActionListener(this);
180         pageEndButton.addActionListener(this);
181         JPanel footerPanel = new JPanel(new BorderLayout());
182         JPanel pagingPanel = new JPanel(new GridBagLayout());
183         GridBagConstraints gridBagConstraints = new GridBagConstraints();
184         gridBagConstraints.gridx = 0;
185         gridBagConstraints.gridy = 1;
186         gridBagConstraints.anchor = GridBagConstraints.WEST;
187         pagingPanel.add(pageBeginingButton, gridBagConstraints);
188 
189         gridBagConstraints = new GridBagConstraints();
190         gridBagConstraints.gridx = 1;
191         gridBagConstraints.gridy = 1;
192         gridBagConstraints.anchor = GridBagConstraints.WEST;
193         pagingPanel.add(back, gridBagConstraints);
194 
195         gridBagConstraints = new GridBagConstraints();
196         gridBagConstraints.gridx = 2;
197         gridBagConstraints.gridy = 1;
198         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
199         pagingPanel.add(currentPageLabel, gridBagConstraints);
200 
201         gridBagConstraints = new GridBagConstraints();
202         gridBagConstraints.gridx = 3;
203         gridBagConstraints.gridy = 1;
204         gridBagConstraints.anchor = GridBagConstraints.EAST;
205         pagingPanel.add(forward, gridBagConstraints);
206 
207         gridBagConstraints = new GridBagConstraints();
208         gridBagConstraints.gridx = 4;
209         gridBagConstraints.gridy = 1;
210         gridBagConstraints.anchor = GridBagConstraints.EAST;
211         pagingPanel.add(pageEndButton, gridBagConstraints);
212         footerPanel.add(pagingPanel, BorderLayout.CENTER);
213         footerPanel.add(printCurrentPageButton, BorderLayout.EAST);
214 
215         add(jscrolling, BorderLayout.CENTER);
216         add(footerPanel, BorderLayout.SOUTH);
217     }
218 
219     public void actionPerformed(ActionEvent evt) {
220         Object src = evt.getSource();
221         if(src == pageBeginingButton) {
222             logbookTableModel.goToBegining();
223         }
224         if(src == back) {
225             logbookTableModel.previousPage();
226         }
227         if(src == forward) {
228             logbookTableModel.nextPage();
229         }
230         if(src == pageEndButton) {
231             logbookTableModel.goToEnd();
232         }
233         if (src == printCurrentPageButton) {
234             printTable();
235         }
236 
237         int viewerPage = 1 + logbookTableModel.getCurrentPage();
238         int totalPages = logbookTableModel.getTotalPages() + 1;
239         currentPageLabel.setText("Page " + viewerPage + " of " +  totalPages);
240     }
241 
242      private void printTable() {
243         SwingUtilities.invokeLater(new Runnable() {
244             public void run() {
245                 try {
246                     jTableLogbook.print(JTable.PrintMode.FIT_WIDTH, new MessageFormat("Pilot Logbook"), new MessageFormat("Page {0, number}"));
247                 }
248                 catch (PrinterException pe) {
249                     LOG.log(Level.WARNING, "Unable to print ", pe);
250                 }
251             }
252         });
253     }
254 }