001: package com.xoetrope.carousel.survey;
002:
003: import com.xoetrope.survey.Condition;
004: import com.xoetrope.survey.Option;
005: import com.xoetrope.survey.Question;
006: import java.awt.event.MouseEvent;
007: import java.awt.event.MouseListener;
008: import java.util.Observable;
009: import java.util.Observer;
010: import java.util.Vector;
011: import javax.swing.JCheckBox;
012: import javax.swing.JComboBox;
013: import javax.swing.JLabel;
014: import javax.swing.JTextField;
015: import javax.swing.table.AbstractTableModel;
016:
017: /**
018: * A model for the user's answer table when the question type is "free text"
019: *
020: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
021: * the GNU Public License (GPL), please see license.txt for more details. If
022: * you make commercial use of this software you must purchase a commercial
023: * license from Xoetrope.</p>
024: * <p> $Revision: 1.5 $</p>
025: */
026: public class XAnswersTableModelFree extends AbstractTableModel
027: implements MouseListener {
028: public static final int COL_OPTIONID = 0;
029: public static final int COL_OPTION = 1;
030: public static final int COL_ANSWER = 2;
031: public static final int COL_ACTIVE = 3;
032:
033: protected XTable table;
034: protected XTableColumn[] columns = new XTableColumn[4];
035:
036: protected Condition condition;
037:
038: /**
039: * Creates a new instance of XAnsersTableModelFree
040: */
041: public XAnswersTableModelFree() {
042: super ();
043: condition = null;
044: }
045:
046: /**
047: * Creates a new instance of XAnswersTableModelFree
048: * @param t the table component which will visulise
049: * the data held by this table model.
050: */
051: public XAnswersTableModelFree(XTable t) {
052: table = t;
053:
054: columns[0] = new XTableColumn("id", 20, JLabel.LEFT);
055: columns[1] = new XTableColumn("option", 20, JLabel.LEFT);
056: columns[2] = new XTableColumn("answer", 20, JLabel.LEFT,
057: new JTextField());
058: columns[3] = new XTableColumn("active", 20, JLabel.LEFT,
059: new JCheckBox());
060:
061: table.setModel(this , columns, this );
062: }
063:
064: /**
065: * Gets the columns of this table model
066: * @return the table cotianing the columns
067: */
068: public XTableColumn[] getColumns() {
069: return columns;
070: }
071:
072: /**
073: * Gets the number of columns of this data table.
074: * @return the number of columns.
075: */
076: public int getColumnCount() {
077: return columns.length;
078: }
079:
080: /**
081: * Gets the name of the column located at the specified index
082: * @param idx the index of the column being queried
083: */
084: public String getColumnName(int idx) {
085: return columns[idx].getName();
086: }
087:
088: /**
089: * Gets the number of rows in this data table
090: * @return the number of rows.
091: */
092: public int getRowCount() {
093: return (condition != null ? condition.getQuestion()
094: .getOptions().size() : 0);
095: }
096:
097: /**
098: * Gets the type of the column located at the specified index
099: * @param idx the index of the column being queried
100: * @param the Class object representing the column
101: */
102: public Class getColumnClass(int idx) {
103: return (idx == COL_ACTIVE ? Boolean.class : super
104: .getColumnClass(idx));
105: }
106:
107: /**
108: * Gets the value of the specified cell at rowIndex and columnIndex
109: * @param rowIndex the row of the cell to be queried
110: * @param columnIndex the column of the cell to be queried
111: * @return the value of the cell
112: */
113: public Object getValueAt(int rowIndex, int columnIndex) {
114: if (condition == null || rowIndex < 0
115: || rowIndex > getRowCount())
116: return "";
117: Vector options = condition.getQuestion().getOptions();
118: Option option = (Option) options.get(rowIndex);
119: switch (columnIndex) {
120: case COL_OPTIONID:
121: return String.valueOf(option.getId());
122: case COL_OPTION:
123: return option.getText();
124: case COL_ANSWER:
125: return condition.getAnswer(option);
126: case COL_ACTIVE:
127: return new Boolean(condition.getOptions().contains(option));
128: }
129: return "";
130: }
131:
132: /**
133: * Adds the new option to the question object
134: * held by this model.
135: * @param option the new option to be added
136: */
137: protected void addOption(Option option) {
138: Question question = condition.getQuestion();
139: if (question.getQuestionType() == Question.MUTUALLY_EXCLUSIVE) {
140: condition.getOptions().removeAllElements();
141: condition.getAnswers().removeAllElements();
142: condition.addAnswer(option, "<checked>");
143: } else if (question.getQuestionType() == Question.MULTIPLE_CHOICE) {
144: condition.addAnswer(option, "<checked>");
145: }
146:
147: }
148:
149: /**
150: * Removes the option from the question object held by this model
151: * @param option the option to be removed
152: */
153: protected void removeOption(Option option) {
154: condition.removeAnswer(option);
155: }
156:
157: /**
158: * Sets the value of the specified cell
159: * @param value the new value
160: * @param rowIndex the row of the cell
161: * @param columnIndex the index of the cell
162: */
163: public void setValueAt(Object value, int rowIndex, int columnIndex) {
164: String answer;
165: if (condition == null)
166: return;
167: Vector options = condition.getQuestion().getOptions();
168: Option option = (Option) options.get(rowIndex);
169: switch (columnIndex) {
170: case COL_ANSWER:
171: removeOption(option);
172: answer = (String) value;
173: condition.addAnswer(option, answer);
174: break;
175: case COL_ACTIVE:
176: Boolean state = (Boolean) value;
177: if (state.booleanValue()) {
178: answer = (String) getValueAt(rowIndex, COL_ANSWER);
179: condition.addAnswer(option, answer);
180: } else {
181: condition.removeAnswer(option);
182: }
183: break;
184: }
185: table.revalidate();
186: table.repaint();
187: }
188:
189: /**
190: * Indicates whether the cell is editable
191: * @param row the row of the cell being queried
192: * @param col the column of the cell being queried
193: * @return true if the cell is editable, false otherwise
194: */
195: public boolean isCellEditable(int row, int col) {
196: return (columns[col].getCellEditor() != null);
197: }
198:
199: /**
200: * Sets the Condition object of this model
201: * @param c the new Condition object.
202: */
203: public void setCondition(Condition c) {
204: condition = c;
205: table.clearSelection();
206: table.revalidate();
207: table.repaint();
208: }
209:
210: public void mouseClicked(MouseEvent e) {
211: }
212:
213: public void mousePressed(MouseEvent e) {
214: }
215:
216: public void mouseReleased(MouseEvent e) {
217: }
218:
219: public void mouseEntered(MouseEvent e) {
220: }
221:
222: public void mouseExited(MouseEvent e) {
223: }
224: }
|