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.Color;
24  import java.awt.Component;
25  
26  import javax.swing.BorderFactory;
27  import javax.swing.JLabel;
28  import javax.swing.JTable;
29  import javax.swing.border.Border;
30  import javax.swing.table.TableCellRenderer;
31  
32  import com.explosion.utilities.preferences.Preference;
33  
34  public class ColorPreferenceRenderer extends JLabel implements TableCellRenderer
35  {
36  
37      Border unselectedBorder = null;
38  
39      Border selectedBorder = null;
40  
41      boolean isBordered = true;
42  
43      public ColorPreferenceRenderer(boolean isBordered)
44      {
45          super();
46          this.isBordered = isBordered;
47          setOpaque(true); //MUST do this for background to show up.
48      }
49  
50      public Component getTableCellRendererComponent(JTable table, Object prefrence, boolean isSelected, boolean hasFocus, int row, int column)
51      {
52          /* Set up the value for the column */
53          Preference pref = (Preference) prefrence;
54          Object renderableValue = pref.getValue();
55          if (renderableValue == null && pref.getDefaultValue() != null) renderableValue = pref.getDefaultValue();
56  
57          if (renderableValue == null)
58              setBackground(Color.white);
59          else
60              setBackground((Color) renderableValue);
61  
62          if (isBordered)
63          {
64              if (isSelected)
65              {
66                  if (selectedBorder == null)
67                  {
68                      selectedBorder = BorderFactory.createMatteBorder(2, 5, 2, 5, table.getSelectionBackground());
69                  }
70                  setBorder(selectedBorder);
71              } else
72              {
73                  if (unselectedBorder == null)
74                  {
75                      unselectedBorder = BorderFactory.createMatteBorder(2, 5, 2, 5, table.getBackground());
76                  }
77                  setBorder(unselectedBorder);
78              }
79          }
80          return this;
81      }
82  }