001: package com.xoetrope.carousel.survey;
002:
003: import com.xoetrope.survey.Option;
004: import com.xoetrope.survey.Question;
005: import java.awt.event.ActionEvent;
006: import java.awt.event.ActionListener;
007: import java.util.Observable;
008: import java.util.Observer;
009: import java.util.Vector;
010: import java.awt.Component;
011: import java.awt.Point;
012: import java.awt.event.MouseEvent;
013: import java.awt.event.MouseListener;
014: import javax.swing.JLabel;
015: import javax.swing.JMenuItem;
016: import javax.swing.JPopupMenu;
017: import javax.swing.JTextField;
018: import javax.swing.JFormattedTextField;
019: import javax.swing.event.ListSelectionEvent;
020: import javax.swing.event.ListSelectionListener;
021: import javax.swing.table.AbstractTableModel;
022: import net.xoetrope.editor.project.XEditorProject;
023: import net.xoetrope.xui.XProject;
024: import net.xoetrope.xui.XProjectManager;
025:
026: //import org.netbeans.api.debugger.jpda.This;
027:
028: /**
029: * A model for options table
030: *
031: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
032: * the GNU Public License (GPL), please see license.txt for more details. If
033: * you make commercial use of this software you must purchase a commercial
034: * license from Xoetrope.</p>
035: * <p> $Revision: 1.5 $</p>
036: */
037: public class XOptionsTableModel extends AbstractTableModel implements
038: MouseListener, Observer, ListSelectionListener {
039: public static final int COL_RESPONSE = 0;
040: public static final int COL_VALUE = 1;
041:
042: protected XTable table;
043: protected JPopupMenu popupMenu;
044: protected NewOptionDialog newOptionDialog;
045: protected XNotifier notifier;
046: protected ActionListener addOption, deleteOption, moveUpOption,
047: moveDownOption;
048:
049: protected Question question;
050:
051: protected XTableColumn[] columns = new XTableColumn[2];
052:
053: public XOptionsTableModel(XTable t) {
054: table = t;
055: XProject project = XProjectManager.getCurrentProject();
056: XEditorProject editorProject = (project instanceof XEditorProject ? (XEditorProject) project
057: : null);
058: notifier = new XNotifier(editorProject, true);
059:
060: JFormattedTextField ft = new JFormattedTextField();
061: ft.setValue(new Integer(100));
062: columns[0] = new XTableColumn("Response", 300, JLabel.LEFT,
063: new JTextField());
064: columns[1] = new XTableColumn("ID", 20, JLabel.LEFT, ft);
065:
066: table.setModel(this , columns, this );
067: table.getSelectionModel().addListSelectionListener(this );
068: question = null;
069:
070: addOption = new AddOption();
071: deleteOption = new DeleteOption();
072: moveUpOption = new MoveUpOption();
073: moveDownOption = new MoveDownOption();
074:
075: newOptionDialog = new NewOptionDialog();
076: createPopupMenu();
077: }
078:
079: public Observable getNotifier() {
080: return notifier;
081: }
082:
083: public void refresh() {
084: table.clearSelection();
085: table.revalidate();
086: table.repaint();
087: }
088:
089: public ActionListener getAddOption() {
090: return addOption;
091: }
092:
093: public ActionListener getDeleteOption() {
094: return deleteOption;
095: }
096:
097: public ActionListener getMoveOptionUp() {
098: return moveUpOption;
099: }
100:
101: public ActionListener getMoveOptionDown() {
102: return moveDownOption;
103: }
104:
105: private void createPopupMenu() {
106: JMenuItem menuItem;
107: popupMenu = new JPopupMenu();
108: popupMenu.add(new JLabel("Question Option"));
109: popupMenu.addSeparator();
110: menuItem = new JMenuItem("Move up");
111: popupMenu.add(menuItem);
112: menuItem.addActionListener(moveUpOption);
113: menuItem = new JMenuItem("Move down");
114: popupMenu.add(menuItem);
115: menuItem.addActionListener(moveDownOption);
116: popupMenu.addSeparator();
117: menuItem = new JMenuItem("Add..");
118: popupMenu.add(menuItem);
119: menuItem.addActionListener(addOption);
120: menuItem = new JMenuItem("Delete..");
121: popupMenu.add(menuItem);
122: menuItem.addActionListener(deleteOption);
123: }
124:
125: public XTableColumn[] getColumns() {
126: return columns;
127: }
128:
129: public String getColumnName(int col) {
130: return columns[col].getName();
131: }
132:
133: public boolean isCellEditable(int row, int col) {
134: return (columns[col].getCellEditor() != null);
135: }
136:
137: public int getRowCount() {
138: if (question == null)
139: return 0;
140:
141: Vector options = question.getOptions();
142: return (options != null ? options.size() : 0);
143: }
144:
145: public int getColumnCount() {
146: return columns.length;
147: }
148:
149: public Object getValueAt(int rowIndex, int columnIndex) {
150: if (question == null || rowIndex < 0
151: || rowIndex > getRowCount())
152: return "";
153: Option option = (Option) question.getOptions().get(rowIndex);
154:
155: switch (columnIndex) {
156: case COL_RESPONSE:
157: return option.getText();
158: case COL_VALUE:
159: return String.valueOf(option.getId());
160: }
161:
162: return "";
163: }
164:
165: public void setValueAt(Object value, int rowIndex, int columnIndex) {
166: if (question == null)
167: return;
168: Option option = (Option) question.getOptions().get(rowIndex);
169:
170: switch (columnIndex) {
171: case COL_RESPONSE:
172: String newResponse = value.toString();
173: if (!newResponse.equals(option.getText()))
174: XSurvey.setProjectModified(true);
175: option.setText(newResponse);
176: notifier.notifyObservers(option);
177: break;
178: case COL_VALUE:
179: int newId = new Integer(value.toString()).intValue();
180: boolean state = (newId != option.getId());
181: if (newId != option.getId())
182: XSurvey.setProjectModified(true);
183: option.setId(newId);
184: notifier.notifyObservers(option);
185: break;
186: }
187: }
188:
189: private void showPopupMenu(MouseEvent e) {
190: if (e.isPopupTrigger()) {
191: Component source = (Component) e.getSource();
192: boolean isQuestionOption = (source instanceof XTable || source instanceof JTextField);
193: Point p = e.getPoint();
194: if (!isQuestionOption) {
195: System.out.println(source.getParent().getParent()
196: .getParent().getParent());
197: // table->viewport->scrollpane->XPopupPanel
198: //((XResponseSetEditor)( ( XPopupPanel )source.getParent().getParent().getParent() ).getOwner()).addPopupActions();
199: } else
200: popupMenu.show(source, p.x, p.y);
201: }
202: }
203:
204: public void mouseClicked(MouseEvent e) {
205: }
206:
207: public void mousePressed(MouseEvent e) {
208: showPopupMenu(e);
209: }
210:
211: public void mouseReleased(MouseEvent e) {
212: showPopupMenu(e);
213: }
214:
215: public void mouseEntered(MouseEvent e) {
216: }
217:
218: public void mouseExited(MouseEvent e) {
219: }
220:
221: public void update(Observable o, Object arg) {
222: question = (Question) arg;
223: table.clearSelection();
224: table.revalidate();
225: table.repaint();
226: }
227:
228: public void valueChanged(ListSelectionEvent e) {
229: Option option = null;
230: int row = table.getSelectedRow();
231: if (row >= 0 && row < getRowCount())
232: option = question.getOption(row);
233: notifier.notifyObservers(option);
234: }
235:
236: private class AddOption implements ActionListener {
237: public void actionPerformed(ActionEvent e) {
238: if (newOptionDialog.showDialog()) {
239: String response = newOptionDialog.getResponse();
240: int optionId = newOptionDialog.getOptionId();
241: Option option = new Option(optionId, response);
242: question.addOption(option);
243: int idx = question.getOptions().size() - 1;
244: table.getSelectionModel()
245: .setSelectionInterval(idx, idx);
246: table.revalidate();
247: table.repaint();
248: notifier.notifyObservers(option);
249: }
250: }
251: }
252:
253: private class DeleteOption implements ActionListener {
254: public void actionPerformed(ActionEvent e) {
255: int idx = table.getSelectedRow();
256: question.deleteOption(idx);
257: table.clearSelection();
258: table.revalidate();
259: table.repaint();
260: notifier.notifyObservers(null);
261: }
262: }
263:
264: private class MoveUpOption implements ActionListener {
265: public void actionPerformed(ActionEvent e) {
266: int idx = table.getSelectedRow();
267: if (idx <= 0 || idx >= getRowCount())
268: return;
269: Vector options = question.getOptions();
270: if (options == null)
271: return;
272:
273: Option option = (Option) options.get(idx);
274: options.remove(option);
275: options.add(--idx, option);
276: table.getSelectionModel().setSelectionInterval(idx, idx);
277: table.revalidate();
278: table.repaint();
279: notifier.notifyObservers(option);
280: }
281: }
282:
283: private class MoveDownOption implements ActionListener {
284: public void actionPerformed(ActionEvent e) {
285: int idx = table.getSelectedRow();
286: if (idx < 0 || idx >= getRowCount() - 1)
287: return;
288: Vector options = question.getOptions();
289: if (options == null)
290: return;
291: Option option = (Option) options.get(idx);
292: options.remove(option);
293: options.add(++idx, option);
294: table.getSelectionModel().setSelectionInterval(idx, idx);
295: table.revalidate();
296: table.repaint();
297: notifier.notifyObservers(option);
298: }
299: }
300:
301: private class OptionChangeNotifier extends Observable {
302: public void notifyObservers(Object o) {
303: setChanged();
304: super.notifyObservers(o);
305: }
306: }
307:
308: }
|