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.Dimension;
19 import java.util.List;
20
21 import javax.swing.table.DefaultTableColumnModel;
22 import javax.swing.table.TableColumn;
23
24 import org.xulux.api.gui.IWidget;
25 import org.xulux.guilayer.swing.widgets.MenuItem;
26 import org.xulux.guilayer.swing.widgets.PopupMenu;
27 import org.xulux.guilayer.swing.widgets.Table;
28
29 /**
30 *
31 * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
32 * @version $Id: NyxTableColumnModel.java,v 1.1 2005/12/18 12:58:21 mvdb Exp $
33 */
34 public class NyxTableColumnModel extends DefaultTableColumnModel {
35
36 /**
37 * the table
38 */
39 protected Table table;
40 /**
41 * the model
42 */
43 protected NyxTableColumnModel lockedModel;
44 /**
45 * the lockedwidth
46 */
47 protected int lockedColumnWidth;
48 /**
49 * is the model initializing?
50 */
51 protected boolean initializing;
52 /**
53 *
54 */
55 public NyxTableColumnModel() {
56 super();
57 }
58
59 /**
60 * Initializes the columnModel and sets the table for later reference
61 * @param table - the table that is using the columnModel.
62 */
63 public NyxTableColumnModel(Table table) {
64 setTable(table);
65 initializeColumns();
66 // addColumnModelListener(new TableColumnModelListener() {
67 //
68 // public void columnAdded(TableColumnModelEvent e) {
69 // System.out.println("columnAdded : "+e);
70 // }
71 //
72 // public void columnRemoved(TableColumnModelEvent e) {
73 // System.out.println("columnRemoved : "+e);
74 // }
75 //
76 // public void columnMoved(TableColumnModelEvent e) {
77 // System.out.println("columnMoved : "+e);
78 // }
79 //
80 // public void columnMarginChanged(ChangeEvent e) {
81 // System.out.println("columnMarginChanged : "+e);
82 // }
83 //
84 // public void columnSelectionChanged(ListSelectionEvent e) {
85 // System.out.println("columnSelectionChanged : "+e);
86 // }
87 //
88 // });
89 }
90
91 /**
92 * @param table the table
93 */
94 public void setTable(Table table) {
95 this.table = table;
96 }
97
98 /**
99 * Initializes the columns
100 */
101 private void initializeColumns() {
102 initializing = true;
103 int maxHeight = 0;
104 List list = table.getChildWidgets();
105 if (list == null) {
106 return;
107 }
108 lockedModel = new NyxTableColumnModel();
109 lockedModel.setTable(table);
110 for (int i = 0; i < list.size(); i++) {
111 IWidget widget = (IWidget) list.get(i);
112 if (widget instanceof PopupMenu || widget instanceof MenuItem) {
113 continue;
114 }
115 NyxTableColumn column = new NyxTableColumn(widget);
116 String headerText = widget.getProperty("text");
117 if (headerText != null && headerText.indexOf("\\n") != -1) {
118 column.setHeaderRenderer(new MultipleLineHeaderRenderer(null));
119 }
120 column.setModel(this);
121 column.setModelIndex(i);
122 column.setCellRenderer(new NyxTableCellRenderer(widget, table));
123 int height = widget.getRectangle().getHeight();
124 if (height > maxHeight) {
125 maxHeight = height;
126 }
127 addColumn(column);
128 lockedModel.addColumn(column);
129 }
130 //System.out.println("columns : "+getColumnCount());
131 table.setProperty("rowHeight", String.valueOf(maxHeight));
132 initializing = false;
133 }
134
135 /**
136 *
137 * @return if table has columns that are locked.
138 */
139 public boolean hasLockedColumns() {
140 boolean lockedColumns = false;
141 for (int i = 0; i < getColumnCount(); i++) {
142 if (((NyxTableColumn) getColumn(i)).isLocked()) {
143 lockedColumns = true;
144 break;
145 }
146 }
147 return lockedColumns;
148 }
149
150 /**
151 * Refresh the columns especially when widgets have been refreshed.
152 * @todo Espcially needed when adding or removing widgets.
153 */
154 public void refresh() {
155 // first remove all columns
156 removeAllColumns();
157 // and reinitialize the columns.
158 initializeColumns();
159 }
160
161 /**
162 * Removes all columns from the column list.
163 * This removes also all columns from the locked
164 * table.
165 */
166 protected void removeAllColumns() {
167 for (int i = 0; i < getColumnCount(); i++) {
168 removeColumn(getColumn(i));
169 }
170 // lockedModel gets rebuild anyway, so just destroy it..
171 if (lockedModel != null) {
172 lockedModel.destroy();
173 lockedModel = null;
174 }
175 }
176
177 /**
178 * Removes the unlocked columns from the main table.
179 * Call this one with care, since when there are no locks
180 * all rows will be deleted.
181 * Also wize to call this AFTER the removeLockedColumns.
182 */
183 public void removeUnlockedColumns() {
184 for (int i = 0; i < lockedModel.getColumnCount(); i++) {
185 if (i >= getColumnCount()) {
186 // we are finished..
187 return;
188 }
189 NyxTableColumn column = (NyxTableColumn) lockedModel.getColumn(i);
190 if (!column.isLocked()) {
191 lockedModel.removeColumn(column);
192 i--;
193 }
194 }
195 }
196 /**
197 * Removes locked columns from the current list of columns.
198 */
199 public void removeLockedColumns() {
200 for (int i = 0; i < getColumnCount(); i++) {
201 NyxTableColumn column = (NyxTableColumn) getColumn(i);
202 if (column.isLocked()) {
203 lockedColumnWidth += column.getPreferredWidth();
204 removeColumn(column);
205 i--;
206 }
207 }
208 }
209
210 /**
211 * @return the locked columnWidth. This is set when removing columns
212 */
213 public Dimension getLockedColumnWidth() {
214 return new Dimension(lockedColumnWidth, 0);
215 }
216
217 /**
218 * Set the columnwidth to the new value.
219 * @param width - the width
220 */
221 public void setLockedColumnWidth(int width) {
222 lockedColumnWidth = width;
223 }
224
225 /**
226 *
227 * @return The columnModel to lock columns.
228 */
229 public NyxTableColumnModel getLockedColumnModel() {
230 return lockedModel;
231 }
232
233 /**
234 * destroy the instance variables
235 * Just in case..
236 */
237 public void destroy() {
238 table = null;
239 if (lockedModel != null) {
240 lockedModel.destroy();
241 lockedModel = null;
242 }
243 }
244
245 /**
246 * @see javax.swing.table.TableColumnModel#getColumn(int)
247 */
248 public TableColumn getColumn(int columnIndex) {
249 // System.out.println("Index : "+columnIndex+","+super.getColumn(columnIndex).getModelIndex());
250 return super.getColumn(columnIndex);
251 }
252
253 /**
254 * @return true if this component is still initializing..
255 */
256 public boolean isInitializing() {
257 return initializing;
258 }
259
260 }