View Javadoc

1   package com.explosion.utilities.preferences.editandrender.table;
2   
3   /*
4    * =============================================================================
5    * 
6    * Copyright 2004 Stephen Cowx
7    * 
8    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9    * use this file except in compliance with the License. You may obtain a copy of
10   * the License at
11   * 
12   * http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   * 
20   * =============================================================================
21   */
22  
23  import java.awt.Component;
24  import java.awt.event.ActionEvent;
25  import java.awt.event.ActionListener;
26  
27  import javax.swing.DefaultCellEditor;
28  import javax.swing.JCheckBox;
29  import javax.swing.JTable;
30  
31  import com.explosion.utilities.exception.ExceptionManagerFactory;
32  import com.explosion.utilities.preferences.Preference;
33  
34  public class BooleanPreferenceEditor extends DefaultCellEditor
35  {
36      private JCheckBox checkBox = new JCheckBox(); 
37      private Object value = null;
38      
39      public BooleanPreferenceEditor()
40      {
41          super(new JCheckBox());
42           
43          ((JCheckBox) editorComponent).addActionListener(new CheckBoxActionListener((JCheckBox) editorComponent, this));
44  
45          ((JCheckBox) editorComponent).addActionListener(new ActionListener()
46          {
47  
48              public void actionPerformed(ActionEvent e)
49              {
50                  fireEditingStopped();
51              }
52          });
53      }
54  
55      protected void fireEditingStopped()
56      {
57          super.fireEditingStopped();
58      }
59  
60      public Object getCellEditorValue()
61      {
62          return value;
63      }
64  
65      public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
66      {
67          
68          this.value = value;
69  
70          if (value instanceof Preference)
71          {
72              ((JCheckBox) editorComponent).setSelected( ((Boolean) ((Preference)value).getValue()).booleanValue());
73          }
74          else if (value instanceof Boolean)
75          {
76              ((JCheckBox) editorComponent).setSelected(((Boolean)value).booleanValue());
77          }
78          return editorComponent;
79      }
80  
81      /***
82       * Returns the colour value for this editor
83       * @return
84       */
85      public Boolean getTruth()
86      {
87          if (value instanceof Preference)
88          {
89            return (Boolean) ((Preference)value).getValue();
90          }
91          else if (value instanceof Boolean)
92          {
93             return (Boolean) value;  
94          }
95          
96          return null;
97      }
98      
99      /***
100      * Sets the color value for this editor
101      * @param colour
102      * @throws Exception
103      */
104     public void setTruth(boolean truth) throws Exception
105     {
106         if (value instanceof Preference)
107         {
108           ((Preference)value).setValue(new Boolean(truth));
109         }
110         else if (value instanceof Boolean)
111         {
112            value = new Boolean(truth);  
113         }
114     }
115 
116 }
117 
118 class CheckBoxActionListener implements ActionListener
119 {
120 
121     private BooleanPreferenceEditor editor = null;
122     private JCheckBox checkBox;
123 
124     public CheckBoxActionListener(JCheckBox checkBox, BooleanPreferenceEditor colorEditor)
125     {
126         this.editor = colorEditor;
127         this.checkBox = checkBox;
128     }
129 
130     public void actionPerformed(ActionEvent e)
131     {
132         try
133         {
134             editor.setTruth(checkBox.isSelected());
135         }
136         catch (Exception e1)
137         {
138             ExceptionManagerFactory.getExceptionManager().manageException(e1,"Exception caught while setting value of boolean property");
139         }
140     }
141 }