1   package org.wcb.gui.renderer;
2   
3   import org.wcb.gui.dialog.AircraftEditDialog;
4   import org.wcb.gui.dialog.LogbookEditDialog;
5   import org.wcb.gui.dialog.AirportEntryDialog;
6   import org.wcb.gui.dialog.AircraftTypeEntryDialog;
7   import org.wcb.resources.MessageResourceRegister;
8   import org.wcb.resources.MessageKey;
9   import org.wcb.model.vo.hibernate.AircraftBO;
10  import org.wcb.model.vo.hibernate.Logbook;
11  import org.wcb.model.vo.hibernate.AirportBO;
12  import org.wcb.model.vo.hibernate.AircraftTypeBO;
13  import org.wcb.model.vo.TotalLogEntriesVO;
14  
15  import javax.swing.*;
16  import javax.swing.table.TableCellRenderer;
17  import javax.swing.table.TableCellEditor;
18  import javax.swing.table.TableColumnModel;
19  import java.awt.event.ActionListener;
20  import java.awt.event.ActionEvent;
21  import java.awt.*;
22  import java.net.URL;
23  
24  /**
25   * <small>
26   * Copyright (c)  2006  wbogaardt.
27   * This library is free software; you can redistribute it and/or
28   * modify it under the terms of the GNU Lesser General Public
29   * License as published by the Free Software Foundation; either
30   * version 2.1 of the License, or (at your option) any later version.
31   *
32   * This library is distributed in the hope that it will be useful,
33   * but WITHOUT ANY WARRANTY; without even the implied warranty of
34   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
35   * Lesser General Public License for more details.
36   *
37   * You should have received a copy of the GNU Lesser General Public
38   * License along with this library; if not, write to the Free Software
39   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
40   * <p/>
41   * $File:  $ <br>
42   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Mar 30, 2006 10:27:43 AM $ <br>
43   * </small>
44   *
45   * @author wbogaardt
46   * @version 1
47   *          Date: Mar 30, 2006
48   *          Time: 10:27:43 AM
49   */
50  
51  public class EditButtonTableCellEditor extends AbstractCellEditor
52          implements TableCellRenderer, TableCellEditor, ActionListener
53  {
54      private JButton renderButton;
55      private JButton editButton;
56      private Object tableObject;
57      private JLabel blankLabel = new JLabel("");
58  
59      public EditButtonTableCellEditor(JTable table, int column)
60      {
61          super();
62          URL rptImageURL = EditButtonTableCellEditor.class.getClassLoader().getResource("org/wcb/resources/gui/edit.png");
63          ImageIcon editIcon = new ImageIcon(rptImageURL);
64          renderButton = new JButton(editIcon);
65          renderButton.setToolTipText(MessageResourceRegister.getInstance().getValue(MessageKey.BUTTON_EDIT));
66          editButton = new JButton(editIcon);
67          editButton.setToolTipText(MessageResourceRegister.getInstance().getValue(MessageKey.BUTTON_EDIT));
68          editButton.setFocusPainted(false);
69          editButton.addActionListener(this);
70          TableColumnModel columnModel = table.getColumnModel();
71          columnModel.getColumn(column).setCellRenderer(this);
72          columnModel.getColumn(column).setCellEditor(this);
73          columnModel.getColumn(column).setMaxWidth(25);
74      }
75  
76      public Component getTableCellRendererComponent(
77              JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
78      {
79          if(value instanceof TotalLogEntriesVO)
80          {
81              blankLabel.setBackground(new Color(0x000a5a));
82              return blankLabel;
83          }
84          if (hasFocus)
85          {
86              renderButton.setForeground(table.getForeground());
87              renderButton.setBackground(UIManager.getColor("Button.background"));
88          }
89          else if (isSelected)
90          {
91              renderButton.setForeground(table.getSelectionForeground());
92              renderButton.setBackground(table.getSelectionBackground());
93          }
94          else
95          {
96              renderButton.setForeground(table.getForeground());
97              renderButton.setBackground(UIManager.getColor("Button.background"));
98          }
99          return renderButton;
100     }
101 
102     public Component getTableCellEditorComponent(
103             JTable table, Object value, boolean isSelected, int row, int column)
104     {
105         if(value instanceof TotalLogEntriesVO) {
106             tableObject = "";
107             blankLabel.setBackground(new Color(0x000a5a));
108             return blankLabel;
109         }
110         tableObject = value;
111         return editButton;
112     }
113 
114     public Object getCellEditorValue()
115     {
116         return tableObject;
117     }
118 
119     public void actionPerformed(ActionEvent e)
120     {
121         fireEditingStopped();
122         if (tableObject instanceof AircraftBO)
123         {
124             AircraftEditDialog dialog = new AircraftEditDialog((AircraftBO) tableObject);
125             dialog.setVisible(true);
126         }
127         if (tableObject instanceof Logbook)
128         {
129             LogbookEditDialog dialog = new LogbookEditDialog((Logbook) tableObject);
130             dialog.setVisible(true);
131         }
132         if (tableObject instanceof AirportBO)
133         {
134             AirportEntryDialog dialog = new AirportEntryDialog();
135             dialog.setAirport((AirportBO) tableObject);
136             dialog.setVisible(true);
137         }
138         if (tableObject instanceof AircraftTypeBO) {
139             AircraftTypeEntryDialog dialog = new AircraftTypeEntryDialog((AircraftTypeBO) tableObject);
140             dialog.setVisible(true);
141         }
142     }
143 
144 }