View Javadoc

1   package com.explosion.datastream.exql.gui.table.editandrender;
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.Color;
24  import java.awt.Component;
25  import java.awt.event.ActionEvent;
26  import java.awt.event.ActionListener;
27  
28  import javax.swing.DefaultCellEditor;
29  import javax.swing.JButton;
30  import javax.swing.JCheckBox;
31  import javax.swing.JColorChooser;
32  import javax.swing.JDialog;
33  import javax.swing.JTable;
34  
35  import com.explosion.utilities.GeneralUtils;
36  import com.explosion.utilities.preferences.Preference;
37  
38  public class BlobClobCellEditor extends DefaultCellEditor
39  {
40  
41      private Object value = null;
42      
43      public BlobClobCellEditor(JButton b, Component owner)
44      {
45          super(new JCheckBox()); //Unfortunately, the constructor expects a
46                                  // check box, combo box, or text field.
47          editorComponent = b;
48          setClickCountToStart(1); //This is usually 1 or 2.
49  
50          ((JButton) editorComponent).addActionListener(new ColorButtonActionListener(((JButton) editorComponent), this, owner));
51  
52          ((JButton) editorComponent).addActionListener(new ActionListener()
53          {
54  
55              public void actionPerformed(ActionEvent e)
56              {
57                  fireEditingStopped();
58              }
59          });
60      }
61  
62      protected void fireEditingStopped()
63      {
64          super.fireEditingStopped();
65      }
66  
67      public Object getCellEditorValue()
68      {
69          return value;
70      }
71  
72      public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
73      {
74          
75          this.value = value;
76          ((JButton) editorComponent).setText("..");
77  
78          if (value instanceof Preference)
79          {
80            editorComponent.setBackground((Color) ((Preference)value).getValue());
81          }
82          else if (value instanceof Color)
83          {
84              editorComponent.setBackground((Color) value);  
85          }
86          return ((JButton) editorComponent);
87      }
88  
89      /***
90       * Returns the colour value for this editor
91       * @return
92       */
93      public Color getColour()
94      {
95          if (value instanceof Preference)
96          {
97            return (Color) ((Preference)value).getValue();
98          }
99          else if (value instanceof Color)
100         {
101            return (Color) value;  
102         }
103         
104         return null;
105     }
106     
107     /***
108      * Sets the color value for this editor
109      * @param colour
110      * @throws Exception
111      */
112     public void setColour(Color colour) throws Exception
113     {
114         if (value instanceof Preference)
115         {
116           ((Preference)value).setValue(colour);
117         }
118         else if (value instanceof Color)
119         {
120            value = colour;  
121         }
122     }
123 
124 }
125 
126 class ColorButtonActionListener implements ActionListener
127 {
128 
129     private JButton button = null;
130 
131     private JColorChooser colorChooser = new JColorChooser();
132 
133     private BlobClobCellEditor colorEditor = null;
134 
135     private JDialog dialog = null;
136 
137     public ColorButtonActionListener(JButton button, BlobClobCellEditor colorEditor, Component owner)
138     {
139         this.button = button;
140         this.colorEditor = colorEditor;
141 
142         ColorChooserActionListener okListener = new ColorChooserActionListener(colorEditor, colorChooser);
143         dialog = JColorChooser.createDialog(owner, "Pick a Color", true, colorChooser, okListener, null);
144         GeneralUtils.centreWindowInParent(dialog, owner);
145     }
146 
147     public void actionPerformed(ActionEvent e)
148     {
149         button.setBackground(colorEditor.getColour());
150         colorChooser.setColor(colorEditor.getColour());
151         dialog.setLocationRelativeTo(button);
152         dialog.setVisible(true);
153     }
154 }
155 
156 /***
157  * This class just sets the currentcolor of the
158  */
159 
160 class ColorChooserActionListener implements ActionListener
161 {
162 
163     private JColorChooser colorChooser = new JColorChooser();
164 
165     private BlobClobCellEditor colorEditor = null;
166 
167     public ColorChooserActionListener(BlobClobCellEditor colorEditor, JColorChooser colorChooser)
168     {
169         this.colorEditor = colorEditor;
170         this.colorChooser = colorChooser;
171     }
172 
173     public void actionPerformed(ActionEvent e)
174     {
175         try
176         {
177             colorEditor.setColour(colorChooser.getColor());
178         } catch (Exception e1)
179         {
180             com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e1, null);
181         }
182     }
183 
184 }
185