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