View Javadoc

1   /*
2      Copyright 2002-2006 Martin van den Bemt
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15  */
16  package org.xulux.guilayer.swing.models;
17  
18  import java.awt.Component;
19  
20  import javax.swing.JTable;
21  import javax.swing.border.LineBorder;
22  import javax.swing.table.DefaultTableCellRenderer;
23  
24  import org.xulux.api.core.PartRequest;
25  import org.xulux.api.gui.IWidget;
26  import org.xulux.api.rules.IRuleEngine;
27  import org.xulux.core.XuluxContext;
28  import org.xulux.gui.utils.ColorUtils;
29  import org.xulux.guilayer.swing.widgets.Table;
30  
31  /**
32   * The cellrenderer takes care of the look and content of a cell.
33   * We should make our own cellRenderer probably, so we can use the functionalily of widgets..
34   *
35   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
36   * @version $Id: NyxTableCellRenderer.java,v 1.1 2005/12/18 12:58:21 mvdb Exp $
37   */
38  public class NyxTableCellRenderer extends DefaultTableCellRenderer {
39  
40      /**
41       * the widget
42       */
43      protected IWidget widget;
44      /**
45       * the parent table
46       */
47      protected Table parent;
48      /**
49       * the request
50       */
51      protected PartRequest request;
52  
53      /**
54       * @param widget the widget
55       * @param parent the parent table
56       */
57      public NyxTableCellRenderer(IWidget widget, Table parent) {
58          super();
59          this.widget = widget;
60          this.parent = parent;
61          request = new CellPartRequest(widget);
62      }
63  
64      /**
65       * @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object,
66       *                       boolean, boolean, int, int)
67       */
68      public Component getTableCellRendererComponent(
69          JTable table,
70          Object value,
71          boolean isSelected,
72          boolean hasFocus,
73          int row,
74          int column) {
75          request.setValue(value);
76          widget.setLazyProperty("cellvalue", value);
77          widget.setLazyProperty("rowValue", table.getModel().getValueAt(row, -1));
78          XuluxContext.getRuleEngine().fireFieldRequest(widget, request, IRuleEngine.PRE_REQUEST);
79          // check to see if a rule had a different value in mind
80          Object newValue = widget.getProperty("value");
81          if (newValue != null) {
82            value = newValue;
83            // clear the value property
84            widget.setLazyProperty("value", null);
85          }
86          super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
87          widget.setLazyProperty("cellvalue", null);
88          widget.setLazyProperty("rowvalue", null);
89          // refresh widget so gui changes can propegate.
90          //Component comp = (Component) widget.getNativeWidget();
91          if (!isSelected) {
92              String bgColor = widget.getProperty("background-color-enabled");
93              if (bgColor != null) {
94                  // make sure that it gets painted ok!
95                  setOpaque(true);
96                  // should be a lot easier in the new widget structure..
97                  setBackground(ColorUtils.getSwingColor(bgColor));
98              }
99          }
100         String border = widget.getProperty("border");
101         if (border != null) {
102           int thickness = 1;
103           try {
104             thickness = Integer.parseInt(widget.getProperty("border-thickness"));
105           } catch(NumberFormatException nfe) {
106           }
107           String borderColor = widget.getProperty("border-color");
108           setBorder(new LineBorder(ColorUtils.getSwingColor(borderColor),thickness));
109         }
110         String tooltipText = widget.getProperty("tooltip");
111         if (tooltipText != null) {
112           	setToolTipText(tooltipText);
113         }
114         return this;
115     }
116 
117     /**
118      * Destroys all data know to the cellrenderer.
119      *
120      */
121     public void destroy() {
122         widget = null;
123     }
124 
125 }