001: package com.xoetrope.carousel.survey;
002:
003: import com.xoetrope.survey.Condition;
004: import com.xoetrope.survey.Question;
005: import com.xoetrope.survey.QuestionGroup;
006: import java.awt.event.ActionEvent;
007: import java.awt.event.ActionListener;
008: import java.util.Observable;
009: import java.util.Observer;
010: import java.util.Vector;
011: import java.awt.Component;
012: import java.awt.Point;
013: import java.awt.event.MouseEvent;
014: import java.awt.event.MouseListener;
015: import javax.swing.JLabel;
016: import javax.swing.JMenuItem;
017: import javax.swing.JPopupMenu;
018: import javax.swing.JTable;
019: import javax.swing.JTextField;
020: import javax.swing.event.ListSelectionEvent;
021: import javax.swing.table.AbstractTableModel;
022: import javax.swing.event.ListSelectionListener;
023: import net.xoetrope.editor.project.XEditorProject;
024: import net.xoetrope.xui.XProject;
025: import net.xoetrope.xui.XProjectManager;
026:
027: /**
028: * A model for the conditions table
029: *
030: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
031: * the GNU Public License (GPL), please see license.txt for more details. If
032: * you make commercial use of this software you must purchase a commercial
033: * license from Xoetrope.</p>
034: * <p> $Revision: 1.5 $</p>
035: */
036: public class XConditionsTableModel extends AbstractTableModel implements
037: MouseListener, Observer, ListSelectionListener {
038: public static final int COL_QUESTIONID = 0;
039: public static final int COL_QUESTION = 1;
040: public static final int COL_QUESTIONTYPE = 2;
041:
042: protected XRule rule;
043: protected XTable table;
044: protected JPopupMenu popupMenu;
045: protected NewConditionDialog newResponseDialog;
046: protected XNotifier notifier;
047: protected ActionListener addResponse, deleteResponse;
048:
049: protected XTableColumn[] columns = new XTableColumn[3];
050:
051: public XConditionsTableModel(XTable t) {
052: table = t;
053: XProject project = XProjectManager.getCurrentProject();
054: XEditorProject editorProject = (project instanceof XEditorProject ? (XEditorProject) project
055: : null);
056:
057: notifier = new XNotifier(editorProject, true);
058:
059: columns[0] = new XTableColumn("Question id", 130, JLabel.LEFT);
060: columns[1] = new XTableColumn("Question", 130, JLabel.LEFT);
061: columns[2] = new XTableColumn("Question type", 130, JLabel.LEFT);
062:
063: table.getSelectionModel().addListSelectionListener(this );
064: table.setModel(this , columns, this );
065: rule = null;
066:
067: addResponse = new AddCondition();
068: deleteResponse = new DeleteResponse();
069:
070: newResponseDialog = new NewConditionDialog();
071: createPopupMenu();
072: }
073:
074: public JTable getTable() {
075: return table;
076: }
077:
078: public Observable getNotifier() {
079: return notifier;
080: }
081:
082: public ActionListener getAddResponse() {
083: return addResponse;
084: }
085:
086: public ActionListener getDeleteResponse() {
087: return deleteResponse;
088: }
089:
090: public void refresh() {
091: table.clearSelection();
092: table.revalidate();
093: table.repaint();
094: }
095:
096: public void createPopupMenu() {
097: JMenuItem menuItem;
098: popupMenu = new JPopupMenu();
099: popupMenu.add(new JLabel("Reponse Option"));
100: popupMenu.addSeparator();
101: menuItem = new JMenuItem("Add..");
102: popupMenu.add(menuItem);
103: menuItem.addActionListener(addResponse);
104: menuItem = new JMenuItem("Delete..");
105: popupMenu.add(menuItem);
106: menuItem.addActionListener(deleteResponse);
107: }
108:
109: public String getColumnName(int col) {
110: return columns[col].getName();
111: }
112:
113: public int getRowCount() {
114: return (rule != null ? rule.getConditions().size() : 0);
115: }
116:
117: public int getColumnCount() {
118: return columns.length;
119: }
120:
121: public boolean isCellEditable(int row, int col) {
122: Vector responses = rule.getConditions();
123: Condition response = (Condition) responses.get(row);
124:
125: QuestionGroup group = rule.getGroup();
126: Question question = response.getQuestion();
127:
128: boolean b = columns[col].getCellEditor() != null;
129: b &= (question.getQuestionType() == Question.FREE_TEXT);
130: return b;
131: }
132:
133: public void selectCondition(Condition condition) {
134: int selectedRow = table.getSelectedRow();
135: int row = -1;
136: Vector conditions = rule.getConditions();
137: for (int i = 0; i < conditions.size() && row == -1; i++) {
138: Condition c = (Condition) conditions.get(i);
139: if (condition.equals(c))
140: row = i;
141: }
142: if (row != -1 && row != selectedRow)
143: table.getSelectionModel().setSelectionInterval(row, row);
144: else if (row == -1)
145: table.clearSelection();
146: table.repaint();
147: }
148:
149: public Object getValueAt(int rowIndex, int columnIndex) {
150: if (rule == null || rowIndex < 0 || rowIndex > getRowCount())
151: return "";
152:
153: Condition condition = (Condition) rule.getConditions().get(
154: rowIndex);
155:
156: Question question = condition.getQuestion();
157:
158: String s = "";
159: switch (columnIndex) {
160: case COL_QUESTIONID:
161: s = (question != null ? String.valueOf(question.getId())
162: : "0");
163: break;
164: case COL_QUESTION:
165: s = (question != null ? question.getText() : "");
166: break;
167: case COL_QUESTIONTYPE:
168: s = (question != null ? question.getQuestionTypeText() : "");
169: break;
170: }
171: return s;
172: }
173:
174: private void showPopupMenu(MouseEvent e) {
175: if (e.isPopupTrigger()) {
176: Component source = (Component) e.getSource();
177: boolean isQuestionOption = (source instanceof XTable || source instanceof JTextField);
178: Point p = e.getPoint();
179: if (!isQuestionOption) {
180: System.out.println(source.getParent().getParent()
181: .getParent().getParent());
182: // table->viewport->scrollpane->XPopupPanel
183: //s((XResponseSetEditor)( ( XPopupPanel )source.getParent().getParent().getParent() ).getOwner()).addPopupActions();
184: } else
185: popupMenu.show(source, p.x, p.y);
186: }
187: }
188:
189: public void mouseClicked(MouseEvent e) {
190: }
191:
192: public void mousePressed(MouseEvent e) {
193: showPopupMenu(e);
194: }
195:
196: public void mouseReleased(MouseEvent e) {
197: showPopupMenu(e);
198: }
199:
200: public void mouseEntered(MouseEvent e) {
201: }
202:
203: public void mouseExited(MouseEvent e) {
204: }
205:
206: public void valueChanged(ListSelectionEvent e) {
207: Condition condition = null;
208: int row = table.getSelectedRow();
209: if (row >= 0 && row < getRowCount())
210: condition = (Condition) rule.getConditions().get(row);
211: notifier.notifyObservers(condition, false);
212: }
213:
214: public void update(Observable o, Object arg) {
215: rule = (XRule) arg;
216: table.clearSelection();
217: table.revalidate();
218: table.repaint();
219: }
220:
221: private class AddCondition implements ActionListener {
222: public void actionPerformed(ActionEvent e) {
223: if (newResponseDialog.showDialog(rule)) {
224: Question question = newResponseDialog.getQuestion();
225: rule.addCondition(question);
226: table.revalidate();
227: table.repaint();
228: }
229: }
230: }
231:
232: private class DeleteResponse implements ActionListener {
233: public void actionPerformed(ActionEvent e) {
234: int row = table.getSelectedRow();
235: if (row < 0 || row >= getRowCount())
236: return;
237: rule.getConditions().remove(row);
238: table.clearSelection();
239: table.revalidate();
240: table.repaint();
241: notifier.notifyObservers(null);
242: }
243: }
244:
245: }
|