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: * A model for rules 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 XRulesTableModel extends AbstractTableModel implements
035: MouseListener, ListSelectionListener, Observer {
036:
037: public static final int COL_GROUPID = 0;
038: public static final int COL_RULEID = 1;
039: public static final int COL_RULENAME = 2;
040: public static final int COL_TARGETID = 3;
041:
042: protected XQuestionGroup currentGroup;
043: protected XSurvey survey;
044: protected XTable table;
045: protected Observable notifier;
046: protected JPopupMenu popupMenu;
047: protected NewRuleDialog newRuleDialog;
048:
049: // actions
050: protected ActionListener addRule, deleteRule, moveUpRule,
051: moveDownRule;
052:
053: protected XTableColumn[] columns = new XTableColumn[4];
054:
055: public XRulesTableModel(XTable t) {
056: table = t;
057: XProject project = XProjectManager.getCurrentProject();
058: survey = (XSurvey) project.getObject("Survey");
059: notifier = new RuleChangeNotifier();
060: currentGroup = survey.getGroup(0);
061:
062: columns[0] = new XTableColumn("Group id", 20, JLabel.LEFT,
063: (JTextField) null);
064: columns[1] = new XTableColumn("Rule id", 20, JLabel.LEFT,
065: new JTextField());
066: columns[2] = new XTableColumn("Rule name ", 300, JLabel.LEFT,
067: new JTextField());
068: columns[3] = new XTableColumn("Target", 100, JLabel.LEFT,
069: new JComboBox(survey.getTargetGroups()));
070:
071: table.getSelectionModel().addListSelectionListener(this );
072:
073: addRule = new AddRule();
074: deleteRule = new DeleteRule();
075: moveUpRule = new MoveUpRule();
076: moveDownRule = new MoveDownRule();
077:
078: createPopupMenu();
079: newRuleDialog = new NewRuleDialog();
080:
081: validate();
082: }
083:
084: public JTable getTable() {
085: return table;
086: }
087:
088: public void validate() {
089: table.setModel(this , columns, this );
090: }
091:
092: public void refresh() {
093: table.clearSelection();
094: table.revalidate();
095: table.repaint();
096: }
097:
098: public ActionListener getAddRule() {
099: return addRule;
100: }
101:
102: public ActionListener getDeleteRule() {
103: return deleteRule;
104: }
105:
106: public ActionListener getMoveUpRule() {
107: return moveUpRule;
108: }
109:
110: public ActionListener getMoveDownRule() {
111: return moveDownRule;
112: }
113:
114: private void createPopupMenu() {
115: JMenuItem menuItem;
116: popupMenu = new JPopupMenu();
117: popupMenu.add(new JLabel("Rule Option"));
118: popupMenu.addSeparator();
119: menuItem = new JMenuItem("Move up");
120: popupMenu.add(menuItem);
121: menuItem.addActionListener(moveUpRule);
122: menuItem = new JMenuItem("Move down");
123: popupMenu.add(menuItem);
124: menuItem.addActionListener(moveDownRule);
125: popupMenu.addSeparator();
126: menuItem = new JMenuItem("Add..");
127: popupMenu.add(menuItem);
128: menuItem.addActionListener(addRule);
129: menuItem = new JMenuItem("Delete..");
130: popupMenu.add(menuItem);
131: menuItem.addActionListener(deleteRule);
132: }
133:
134: public Observable getNotifier() {
135: return notifier;
136: }
137:
138: public boolean isCellEditable(int row, int col) {
139: return (columns[col].getCellEditor() != null);
140: }
141:
142: public int getRowCount() {
143: return (currentGroup != null ? currentGroup.getRules().size()
144: : 0);
145: }
146:
147: public XTableColumn[] getColumns() {
148: return columns;
149: }
150:
151: public int getColumnCount() {
152: int cc = 0;
153: for (int i = 0; i < columns.length; i++)
154: if (columns[i].isActive())
155: cc++;
156:
157: return cc;
158: }
159:
160: public String getColumnName(int col) {
161: int nCol = -1, i = 0;
162: for (; i < columns.length; i++) {
163: if (columns[i].isActive())
164: nCol++;
165: if (nCol == col)
166: break;
167: }
168: return columns[i].getName();
169: }
170:
171: public XRule getSelectedRule() {
172: XRule rule = null;
173: int row = table.getSelectedRow();
174: if (currentGroup != null && row != -1) {
175: rule = (XRule) currentGroup.getRules().get(row);
176: }
177: return rule;
178: }
179:
180: public Object getValueAt(int rowIndex, int columnIndex) {
181: if (currentGroup == null || rowIndex < 0
182: || rowIndex >= getRowCount())
183: return "";
184:
185: Vector rules = currentGroup.getRules();
186: XRule rule = (XRule) rules.get(rowIndex);
187: switch (columnIndex) {
188: case COL_RULEID:
189: return String.valueOf(rule.getId());
190: case COL_RULENAME:
191: return rule.getName();
192: case COL_GROUPID:
193: QuestionGroup owningGroup = rule.getGroup();
194: return owningGroup.toString();
195: case COL_TARGETID:
196: QuestionGroup targetGroup = rule.getTarget();
197: return (targetGroup != null ? targetGroup.toString()
198: : "none");
199: }
200: return "";
201: }
202:
203: public void setValueAt(Object value, int rowIndex, int columnIndex) {
204: if (currentGroup == null || rowIndex < 0
205: || rowIndex >= getRowCount())
206: return;
207:
208: Vector rules = currentGroup.getRules();
209: XRule rule = (XRule) rules.get(rowIndex);
210: switch (columnIndex) {
211: case COL_RULEID:
212: int ruleId = new Integer(value.toString()).intValue();
213: rule.setId(ruleId);
214: break;
215: case COL_RULENAME:
216: rule.setName(value.toString());
217: break;
218: case COL_TARGETID:
219: XQuestionGroup target = ((XTargetGroup) value).getGroup();
220: rule.changeTarget(target);
221: break;
222: }
223: }
224:
225: private void showPopupMenu(MouseEvent e) {
226: if (e.isPopupTrigger()) {
227: Component source = (Component) e.getSource();
228: boolean isQuestionOption = ((source instanceof XTable) || (source instanceof JTextField));
229: Point p = e.getPoint();
230: if (!isQuestionOption) {
231: System.out.println(source.getParent().getParent()
232: .getParent().getParent());
233: // table->viewport->scrollpane->XPopupPanel
234: //((XResponseSetEditor)( ( XPopupPanel )source.getParent().getParent().getParent() ).getOwner()).addPopupActions();
235: } else
236: popupMenu.show(source, p.x, p.y);
237: }
238: }
239:
240: public void mouseClicked(MouseEvent e) {
241: }
242:
243: public void mousePressed(MouseEvent e) {
244: showPopupMenu(e);
245: }
246:
247: public void mouseReleased(MouseEvent e) {
248: showPopupMenu(e);
249: }
250:
251: public void mouseEntered(MouseEvent e) {
252: }
253:
254: public void mouseExited(MouseEvent e) {
255: }
256:
257: public void valueChanged(ListSelectionEvent e) {
258: int row = table.getSelectedRow();
259: XRule rule = (row >= 0 ? currentGroup.getRule(row) : null);
260: notifier.notifyObservers(rule);
261: }
262:
263: private int getRuleIdx(Vector rules, XRule rule) {
264: int idx = -1;
265: for (int i = 0; i < rules.size() && idx == -1; i++) {
266: XRule r = (XRule) rules.get(i);
267: if (r.equals(rule))
268: idx = i;
269: }
270: return idx;
271: }
272:
273: public void selectRule(XRule rule) {
274: int selectedRow = table.getSelectedRow();
275: int row = currentGroup.getRuleIdx(rule);
276: if (row != -1 && selectedRow != row)
277: table.getSelectionModel().setSelectionInterval(row, row);
278: else if (row == -1)
279: table.clearSelection();
280: table.repaint();
281: }
282:
283: public void update(Observable o, Object arg) {
284: XQuestionGroup newGroup = (XQuestionGroup) arg;
285: if (newGroup == null || !newGroup.equals(currentGroup)) {
286: currentGroup = newGroup;
287: table.clearSelection();
288: table.revalidate();
289: table.repaint();
290: notifier.notifyObservers(null);
291: }
292: }
293:
294: private class RuleChangeNotifier extends Observable {
295: public void notifyObservers(Object o) {
296: setChanged();
297: super .notifyObservers(o);
298: }
299: }
300:
301: private class MoveUpRule implements ActionListener {
302: public void actionPerformed(ActionEvent e) {
303: /*
304: int row = table.getSelectedRow();
305: if ( row <= 0 || row >= getRowCount() ) return;
306:
307: Vector rules = null;
308: if ( groupIdx == -1 ) {
309: Enumeration enumeration = rulesGroups.elements();
310: while ( enumeration.hasMoreElements() ) {
311: rules = ( Vector ) enumeration.nextElement();
312: if ( row - rules.size() < 0 ) break;
313: row -= rules.size();
314: }
315: } else {
316: rules = ( Vector ) rulesGroups.get( groupIdx );
317: }
318: if ( row == 0 ) return;
319:
320: XRule rule = ( XRule ) rules.get( row );
321: rules.remove( rule );
322: rules.add( --row, rule );
323: table.getSelectionModel().setSelectionInterval( row, row );
324: table.revalidate();
325: table.repaint();
326: notifier.notifyObservers( rule );
327: */
328: }
329: }
330:
331: private class MoveDownRule implements ActionListener {
332: public void actionPerformed(ActionEvent e) {
333: /*
334: int row = table.getSelectedRow();
335: if ( row < 0 || row >= getRowCount() - 1 ) return;
336:
337: Vector rules = null;
338: if ( groupIdx == -1 ) {
339: Enumeration enumeration = rulesGroups.elements();
340: while ( enumeration.hasMoreElements() ) {
341: rules = ( Vector ) enumeration.nextElement();
342: if ( row - rules.size() < 0 ) break;
343: row -= rules.size();
344: }
345: } else {
346: rules = ( Vector ) rulesGroups.get( groupIdx );
347: }
348: if ( row == rules.size() - 1 ) return;
349:
350: XRule rule = ( XRule ) rules.get( row );
351: rules.remove( rule );
352: rules.add( ++row, rule );
353: table.getSelectionModel().setSelectionInterval( row, row );
354: table.revalidate();
355: table.repaint();
356: notifier.notifyObservers( rule );
357: */
358: }
359: }
360:
361: private class AddRule implements ActionListener {
362: public void actionPerformed(ActionEvent e) {
363: if (newRuleDialog.showDialog(currentGroup)) {
364: XQuestionGroup group = (XQuestionGroup) newRuleDialog
365: .getGroup();
366: int ruleId = newRuleDialog.getRuleId();
367: XQuestionGroup target = (XQuestionGroup) newRuleDialog
368: .getTarget();
369: String ruleName = newRuleDialog.getRuleName();
370:
371: XRule rule = new XRule(ruleId, group, target, ruleName);
372: int row = survey.getGroupIdxById(group.getId());
373: group.addNewRule(ruleId, target, ruleName);
374:
375: table.revalidate();
376: table.repaint();
377: }
378: }
379: }
380:
381: private class DeleteRule implements ActionListener {
382: public void actionPerformed(ActionEvent e) {
383: int row = table.getSelectedRow();
384: if ((row < 0) || (row >= getRowCount()))
385: return;
386:
387: Vector rules = currentGroup.getRules();
388: XRule rule = (XRule) rules.get(row);
389: currentGroup.deleteRule(rule);
390: table.clearSelection();
391: table.revalidate();
392: table.repaint();
393: notifier.notifyObservers(null);
394: }
395: }
396:
397: }
|