001: package com.xoetrope.carousel.survey;
002:
003: import java.awt.Component;
004: import java.awt.Point;
005: import java.awt.event.MouseEvent;
006: import java.awt.event.MouseListener;
007: import javax.swing.JLabel;
008: import javax.swing.JMenuItem;
009: import javax.swing.JPopupMenu;
010: import javax.swing.JTable;
011: import javax.swing.JTextField;
012: import javax.swing.table.TableCellEditor;
013: import javax.swing.table.TableModel;
014: import net.xoetrope.editor.*;
015: import com.xoetrope.survey.Question;
016:
017: /**
018: * A table extension class for managing questions
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.3 $</p>
025: */
026: public class XQuestionManagerTable extends JTable implements
027: MouseListener {
028: private int editorY;
029: private XResponseSetEditor responseEditor;
030:
031: public XQuestionManagerTable(TableModel tm) {
032: super (tm);
033: editorY = -1;
034: addMouseListener(this );
035: }
036:
037: public TableCellEditor getCellEditor(int row, int column) {
038: if (column == 1) {
039: editorY = row;
040: Question question = (Question) getModel()
041: .getValueAt(row, 3);
042: Object value0 = getModel().getValueAt(row, column);
043: int numOptions = question.getNumOptions();
044: String[] tempArray = new String[numOptions];
045: for (int i = 0; i < numOptions; i++)
046: tempArray[i] = question.getOptionText(i);
047:
048: responseEditor = new XResponseSetEditor(tempArray,
049: getRowHeight());
050: responseEditor.setSelectedItem(dataModel.getValueAt(row,
051: column));
052: responseEditor.setFont(XEditorDefaults.defaultFont);
053: responseEditor.addExtraMouseListener(this );
054: return (responseEditor);
055: }
056:
057: TableCellEditor editor = super .getCellEditor(row, column);
058: editor.cancelCellEditing();
059: return editor;
060: }
061:
062: public void mouseClicked(MouseEvent e) {
063: }
064:
065: public void mousePressed(MouseEvent e) {
066: showPopupMenu(e);
067: }
068:
069: public void mouseReleased(MouseEvent e) {
070: showPopupMenu(e);
071: }
072:
073: public void mouseMoved(MouseEvent e) {
074: }
075:
076: public void mouseDragged(MouseEvent e) {
077: }
078:
079: public void mouseEntered(MouseEvent e) {
080: }
081:
082: public void mouseExited(MouseEvent e) {
083: }
084:
085: private void showPopupMenu(MouseEvent e) {
086: if (e.isPopupTrigger()) {
087: Component source = (Component) e.getSource();
088: boolean isQuestionOption = (source instanceof XQuestionManagerTable || source instanceof JTextField);
089: JPopupMenu popupMenu = new JPopupMenu();
090: popupMenu.add(new JLabel(
091: isQuestionOption ? " Question Options"
092: : " Response Options"));
093: popupMenu.addSeparator();
094: popupMenu.add(new JMenuItem("Move up"));
095: popupMenu.add(new JMenuItem("Move down"));
096: popupMenu.addSeparator();
097: popupMenu.add(new JMenuItem("Add..."));
098: popupMenu.add(new JMenuItem("Delete..."));
099: Point p = e.getPoint();
100: if (!isQuestionOption) {
101: System.out.println(source.getParent().getParent()
102: .getParent().getParent());
103: // table->viewport->scrollpane->XPopupPanel
104: ((XResponseSetEditor) ((XPopupPanel) source.getParent()
105: .getParent().getParent()).getOwner())
106: .addPopupActions();
107: } else
108: popupMenu.show(source, p.x, p.y);
109: }
110: }
111: }
|