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.
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 XAnswersTableModel extends AbstractTableModel implements
027: 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:
032: protected XTable table;
033: protected XTableColumn[] columns = new XTableColumn[3];
034:
035: protected Condition condition;
036:
037: /**
038: * Creates a new instance of XAnswersTableModel
039: */
040: public XAnswersTableModel() {
041: super ();
042: condition = null;
043: }
044:
045: /**
046: * Creates a new instance of XAnswersTableModel
047: * @param t the table component wich will visualize
048: * the data held by this model.
049: */
050: public XAnswersTableModel(XTable t) {
051: table = t;
052: columns[0] = new XTableColumn("id", 20, JLabel.LEFT);
053: columns[1] = new XTableColumn("option", 20, JLabel.LEFT);
054: columns[2] = new XTableColumn("answer", 20, JLabel.LEFT,
055: new JCheckBox());
056: table.setModel(this , columns, this );
057: }
058:
059: /**
060: * Gets the columns of this model.
061: * @return table containing the columns.
062: */
063: public XTableColumn[] getColumns() {
064: return columns;
065: }
066:
067: /**
068: * Gets the number of columns in this model
069: * @return the number of columns.
070: */
071: public int getColumnCount() {
072: return columns.length;
073: }
074:
075: /**
076: * Gets the name of the column located at the
077: * specified index
078: * @param the index of the requested column
079: * @return the name of the column
080: */
081: public String getColumnName(int col) {
082: return columns[col].getName();
083: }
084:
085: /**
086: * Gets the number of rows in the model.
087: * @return the number of rows
088: */
089: public int getRowCount() {
090: return (condition != null ? condition.getQuestion()
091: .getOptions().size() : 0);
092: }
093:
094: /**
095: * Gets the type of the column located at
096: * the specified index
097: * @param idx the index of the column being queried
098: * @return the Class representing the column
099: */
100: public Class getColumnClass(int idx) {
101: return (idx == COL_ANSWER ? Boolean.class : super
102: .getColumnClass(idx));
103: }
104:
105: /**
106: * Returns an attribute value for the cell at the specified rowIndex
107: * and columnIndex
108: * @param rowIndex the row whose value is to be queried
109: * @param columnIndex the index wshose value is to be queried
110: * @return the value Object at the specified cell.
111: */
112: public Object getValueAt(int rowIndex, int columnIndex) {
113: if (condition == null || rowIndex < 0
114: || rowIndex > getRowCount())
115: return "";
116: Vector options = condition.getQuestion().getOptions();
117: Option option = (Option) options.get(rowIndex);
118: switch (columnIndex) {
119: case COL_OPTIONID:
120: return String.valueOf(option.getId());
121: case COL_OPTION:
122: return option.getText();
123: case COL_ANSWER:
124: return new Boolean(condition.getOptions().contains(option));
125: }
126: return "";
127: }
128:
129: /**
130: * Adds the new questin option to this model
131: * @param option to option to be added
132: */
133: protected void addOption(Option option) {
134: Question question = condition.getQuestion();
135: if (question.getQuestionType() == Question.MUTUALLY_EXCLUSIVE) {
136: condition.getOptions().removeAllElements();
137: condition.getAnswers().removeAllElements();
138: condition.addAnswer(option, "<checked>");
139: } else if (question.getQuestionType() == Question.MULTIPLE_CHOICE) {
140: condition.addAnswer(option, "<checked>");
141: }
142: }
143:
144: /**
145: * Removes the specified option from this model
146: * @param option the option to be removed
147: */
148: protected void removeOption(Option option) {
149: condition.removeAnswer(option);
150: }
151:
152: /**
153: * Sets the value of an attribute at the specified rowIndex and columnIndex
154: * @param value the new value
155: * @pram rowIndex the row whose value is to be changed
156: * @param columnIndex the column whose value is to be changed
157: */
158: public void setValueAt(Object value, int rowIndex, int columnIndex) {
159: if (condition == null || columnIndex != COL_ANSWER)
160: return;
161: XSurvey.setProjectModified(true);
162: Vector options = condition.getQuestion().getOptions();
163: Option option = (Option) options.get(rowIndex);
164: Boolean answer = (Boolean) value;
165: if (answer.booleanValue())
166: addOption(option);
167: else
168: condition.removeAnswer(option);
169: table.revalidate();
170: table.repaint();
171: }
172:
173: /**
174: * Indicates whether the specified cell is editable
175: * @param row of the cell being queried
176: * @param col the column of the cell being queried
177: * @return true if the cell editable, false otherwise
178: */
179: public boolean isCellEditable(int row, int col) {
180: return (columns[col].getCellEditor() != null);
181: }
182:
183: /*
184: * Sets the new Condition object of this model.
185: * @param c the new Condition object.
186: */
187: public void setCondition(Condition c) {
188: condition = c;
189: table.clearSelection();
190: table.revalidate();
191: table.repaint();
192: }
193:
194: public void mouseClicked(MouseEvent e) {
195: }
196:
197: public void mousePressed(MouseEvent e) {
198: }
199:
200: public void mouseReleased(MouseEvent e) {
201: }
202:
203: public void mouseEntered(MouseEvent e) {
204: }
205:
206: public void mouseExited(MouseEvent e) {
207: }
208: }
|