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.beans.PropertyChangeEvent;
19  import java.beans.PropertyChangeListener;
20  
21  import javax.swing.JComponent;
22  import javax.swing.table.TableCellEditor;
23  import javax.swing.table.TableCellRenderer;
24  import javax.swing.table.TableColumn;
25  
26  import org.xulux.api.gui.IWidget;
27  import org.xulux.gui.XuluxCombo;
28  import org.xulux.guilayer.swing.widgets.Table;
29  import org.xulux.utils.BooleanUtils;
30  
31  /**
32   * Override the standard TableColumn, so we can use instances of widgets to set columns
33   * , instead of dynamically creating them all the time.
34   *
35   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
36   * @version $Id: NyxTableColumn.java,v 1.1 2005/12/18 12:58:21 mvdb Exp $
37   */
38  public class NyxTableColumn extends TableColumn {
39  
40      /**
41       * The widget
42       */
43      private IWidget widget;
44      /**
45       * The columnmodel
46       */
47      private NyxTableColumnModel model;
48  
49      /**
50       *
51       */
52      public NyxTableColumn() {
53          super();
54      }
55  
56      /**
57       * @param widget - the widget the create the column for.
58       * @widgetproperty resizable - specifies if the widget is resizable in a table or not
59       */
60      public NyxTableColumn(IWidget widget) {
61          this.widget = widget;
62          addPropertyChangeListener(new PropertyChangeListener() {
63  
64              public void propertyChange(PropertyChangeEvent evt) {
65                  String property = evt.getPropertyName();
66                  if ("preferredWith".equals(property) || "width".equals(property)) {
67                      // do something..
68                      if (isLocked()) {
69                          if (getModel() != null && !getModel().isInitializing()) {
70                              int chgValue = ((Integer) evt.getNewValue()).intValue() - ((Integer) evt.getOldValue()).intValue();
71                              int resultValue = (int) getModel().getLockedColumnWidth().getWidth() + chgValue;
72                              getModel().setLockedColumnWidth(resultValue);
73                              Table table = (Table) NyxTableColumn.this.widget.getParent();
74                              table.getLockedJTable().setPreferredScrollableViewportSize(getModel().getLockedColumnWidth());
75                          }
76                      }
77                  }
78              }
79  
80          });
81          String resizable = widget.getProperty("resizable");
82          if ("false".equals(resizable)) {
83              setResizable(false);
84          } else {
85              // we default to resizable..
86              setResizable(true);
87          }
88          setHeaderValue(widget.getProperty("text"));
89          int width = widget.getRectangle().getWidth();
90          if (width == 0) {
91              width = ((JComponent)widget.getNativeWidget()).getPreferredSize().width;
92          }
93          setPreferredWidth(width);
94          setWidth(width);
95          if (widget instanceof XuluxCombo) {
96              setCellEditor(new NyxTableCellEditor(widget));
97          }
98      }
99  
100     /**
101      * @param modelIndex the modelIndex
102      */
103     public NyxTableColumn(int modelIndex) {
104         super(modelIndex);
105     }
106 
107     /**
108      * @param modelIndex modelIndex
109      * @param width width
110      */
111     public NyxTableColumn(int modelIndex, int width) {
112         super(modelIndex, width);
113     }
114 
115     /**
116      * @param modelIndex modelIndex
117      * @param width width
118      * @param cellRenderer cellRenderer
119      * @param cellEditor cellEditor
120      */
121     public NyxTableColumn(int modelIndex, int width, TableCellRenderer cellRenderer, TableCellEditor cellEditor) {
122         super(modelIndex, width, cellRenderer, cellEditor);
123     }
124 
125     /**
126      * @return is the column is locked or not.
127      */
128     public boolean isLocked() {
129         return BooleanUtils.toBoolean(widget.getProperty("locked"));
130     }
131 
132     /**
133      * Set the model for easy access to the model.
134      * @param model - the nyx columnmodel.
135      */
136     public void setModel(NyxTableColumnModel model) {
137         this.model = model;
138     }
139 
140     /**
141      * @return - the nyx column model.
142      */
143     public NyxTableColumnModel getModel() {
144         return this.model;
145     }
146 
147 }