001: /*
002: * SwingML Copyright (C) 2002 SwingML Team
003: *
004: * This library is free software; you can redistribute it and/or modify it under
005: * the terms of the GNU Lesser General Public License as published by the Free
006: * Software Foundation; either version 2 of the License, or (at your option) any
007: * later version.
008: *
009: * This library is distributed in the hope that it will be useful, but WITHOUT
010: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012: * details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with this library; if not, write to the Free Software Foundation, Inc.,
016: * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: * Authors: Ezequiel Cuellar <ecuellar@crosslogic.com> Farid Ibrahim
019: * <faridibrahim@lycos.com>
020: */
021: package org.swingml.model;
022:
023: import java.util.*;
024:
025: import javax.swing.*;
026: import javax.swing.table.*;
027:
028: import org.swingml.*;
029: import org.swingml.component.*;
030: import org.swingml.system.*;
031: import org.swingml.tablebrowser.ext.*;
032:
033: public class TableColumnModel extends SwingMLModel {
034:
035: private String alignment = Constants.LEFT;
036: private TableCellEditor cellEditor;
037: private SwingMLModel cellEditorModel;
038: private int columnOrder = -1;
039: private Vector items;
040: private boolean sortable = true;
041: private Object type;
042: private int width = -1;
043:
044: public TableColumnModel() {
045: super ();
046: }
047:
048: public void addChild(Object aChild) {
049:
050: if (aChild instanceof SwingMLModel) {
051: setCellEditorModel((SwingMLModel) aChild);
052: }
053: }
054:
055: public String getAlignment() {
056: return alignment;
057: }
058:
059: public TableCellEditor getCellEditor() {
060: return cellEditor;
061: }
062:
063: public SwingMLModel getCellEditorModel() {
064: return cellEditorModel;
065: }
066:
067: /**
068: * @return Returns the columnOrder.
069: */
070: public int getColumnOrder() {
071: return columnOrder;
072: }
073:
074: public TableCellEditor getCustomEditor() {
075: if (getCellEditor() == null) {
076: if (getCellEditorModel() != null) {
077: SwingMLModel ceModel = getCellEditorModel();
078: if (ceModel instanceof JComboBoxModel) {
079: JComboBoxModel m = (JComboBoxModel) ceModel;
080: setCellEditor(new DefaultCellEditor(
081: new JComboBoxComponent(m)));
082: } else if (ceModel instanceof JFormattedTextFieldModel) {
083: JFormattedTextFieldModel m = (JFormattedTextFieldModel) ceModel;
084: setCellEditor(new TableBrowserCellEditor(
085: new DefaultCellEditor(
086: new JFormattedTextFieldComponent(m))));
087: } else if (ceModel instanceof IconModel) {
088: setCellEditor(new IconComponentCellEditor());
089: }
090: }
091: }
092: return getCellEditor();
093: }
094:
095: public Vector getItems() {
096: return this .items;
097: }
098:
099: public Object getType() {
100: return this .type;
101: }
102:
103: /**
104: * @return Returns the width.
105: */
106: public int getWidth() {
107: return width;
108: }
109:
110: /**
111: * Returns whether or not this column is a hidden column.
112: *
113: * @return true if hidden
114: */
115: public boolean isHidden() {
116: return !isVisible();
117: }
118:
119: public boolean isSortable() {
120: return sortable;
121: }
122:
123: public void setAlignment(String anAlignment) {
124: this .alignment = anAlignment;
125: }
126:
127: public void setCellEditor(TableCellEditor anEditor) {
128: this .cellEditor = anEditor;
129: }
130:
131: public void setCellEditorModel(SwingMLModel aModel) {
132: this .cellEditorModel = aModel;
133: }
134:
135: /**
136: * @param order The columnOrder to set.
137: */
138: public void setColumnOrder(int order) {
139: this .columnOrder = order;
140: }
141:
142: /**
143: * Sets whether or not this column is a hidden column.
144: *
145: * @param hidden
146: */
147: public void setHidden(boolean hidden) {
148: setVisible(!hidden);
149: }
150:
151: public void setItems(Vector aItems) {
152: this .items = aItems;
153: }
154:
155: public void setSortable(boolean isSortable) {
156: this .sortable = isSortable;
157: }
158:
159: public void setType(Object aType) {
160: this .type = aType;
161: }
162:
163: /**
164: * @param aWidth The width to set.
165: */
166: public void setWidth(int aWidth) {
167: this .width = aWidth;
168: }
169:
170: public void validate() {
171: if (this .getType().getClass().equals(Boolean.class)
172: && this .getItems() != null) {
173: SwingMLLogger
174: .getInstance()
175: .log(
176: "Syntax error: The parameter ITEMS in the element TC with text "
177: + super .getText()
178: + " should not be used since its TYPE is BOOLEAN.");
179: }
180: if ((this .getType().getClass().equals(JComboBox.class) || this
181: .getType().getClass().equals(ColorComponent.class))
182: && this .getItems() == null) {
183: SwingMLLogger
184: .getInstance()
185: .log(
186: "Syntax error: The parameter ITEMS in the element TC with text "
187: + super .getText()
188: + " should be used since its TYPE is either COMBO or COLOR.");
189: }
190: if (this .getType().getClass().equals(ColorComponent.class)
191: && this .getItems() != null) {
192: Object theColor = null;
193: String theColorName = null;
194: Iterator theItems = items.iterator();
195: while (theItems.hasNext()) {
196: theColorName = (String) theItems.next();
197: theColor = ColorComponent.getColorByName(theColorName);
198: if (theColor == null) {
199: SwingMLLogger
200: .getInstance()
201: .log(
202: "Syntax error: The color "
203: + theColorName
204: + " specified in the ITEMS parameter in the element TC with text"
205: + super .getText()
206: + " is not supported. Verify the name of the color "
207: + theColorName + ".");
208: }
209: }
210: }
211: }
212: }
|