001: package com.xoetrope.carousel.survey;
002:
003: import com.xoetrope.survey.QuestionGroup;
004: import com.xoetrope.survey.Survey;
005: import com.xoetrope.survey.SurveyManager;
006: import java.awt.BorderLayout;
007: import java.awt.Container;
008: import java.awt.Dimension;
009: import java.util.Observable;
010: import java.util.Observer;
011: import java.util.Vector;
012: import javax.swing.BorderFactory;
013: import javax.swing.ImageIcon;
014: import javax.swing.JButton;
015: import javax.swing.JComboBox;
016: import javax.swing.JLabel;
017: import javax.swing.JPanel;
018: import javax.swing.JScrollPane;
019: import javax.swing.JToolBar;
020: import javax.swing.border.EmptyBorder;
021: import javax.swing.border.TitledBorder;
022: import net.xoetrope.xui.XProject;
023: import net.xoetrope.xui.XProjectManager;
024:
025: /**
026: * A panel containing 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 XRulesPanel extends JPanel implements Observer {
035: protected JButton addRuleB, deleteRuleB;
036: protected JButton moveRuleUpB, moveRuleDownB;
037:
038: protected XGroupComboBox groupComboBox;
039: protected XRulesTableModel rulesTableModel;
040: protected XSurveyEditorFrame editorFrame;
041: protected XSurvey survey;
042:
043: public XRulesPanel() {
044: super ();
045: XProject project = XProjectManager.getCurrentProject();
046: editorFrame = (XSurveyEditorFrame) project
047: .getObject("EditorFrame");
048: survey = (XSurvey) project.getObject("Survey");
049: createObservers();
050: init();
051: }
052:
053: protected void init() {
054: setLayout(new BorderLayout());
055:
056: XTable rulesTable = new XTable();
057: rulesTable.setBorder(new EmptyBorder(0, 0, 0, 0));
058: rulesTableModel = new XRulesTableModel(rulesTable);
059: JScrollPane scrollPaneRules = new JScrollPane(rulesTable);
060: TitledBorder rulesBorder = BorderFactory
061: .createTitledBorder("Rules");
062: scrollPaneRules.setBorder(rulesBorder);
063:
064: JToolBar toolBar = new JToolBar();
065: addRuleB = editorFrame.addButton("addRule.png", "add rule",
066: toolBar);
067: deleteRuleB = editorFrame.addButton("deleteRule.png",
068: "delete rule", toolBar);
069: moveRuleUpB = editorFrame.addButton("moveRuleUp.png",
070: "move up", toolBar);
071: moveRuleDownB = editorFrame.addButton("moveRuleDown.png",
072: "move down", toolBar);
073:
074: addRuleB.setEnabled(true);
075: deleteRuleB.setEnabled(false);
076: moveRuleUpB.setEnabled(false);
077: moveRuleDownB.setEnabled(false);
078:
079: groupComboBox = new XGroupComboBox();
080:
081: // tmp
082: Dimension dim = new Dimension(100, 20);
083: groupComboBox.setSize(dim);
084: groupComboBox.setPreferredSize(dim);
085:
086: JLabel comboGroupLabel = new JLabel("group ");
087: toolBar.add(comboGroupLabel);
088: toolBar.add(groupComboBox);
089:
090: add(toolBar, BorderLayout.NORTH);
091: add(scrollPaneRules, BorderLayout.CENTER);
092:
093: addRuleB.addActionListener(rulesTableModel.getAddRule());
094: deleteRuleB.addActionListener(rulesTableModel.getDeleteRule());
095: moveRuleUpB.addActionListener(rulesTableModel.getMoveUpRule());
096: moveRuleDownB.addActionListener(rulesTableModel
097: .getMoveDownRule());
098:
099: groupComboBox.getNotifier().addObserver(rulesTableModel);
100: rulesTableModel.getNotifier().addObserver(new Observer() {
101: public void update(Observable o, Object arg) {
102: boolean state = (arg != null);
103: deleteRuleB.setEnabled(state);
104: moveRuleUpB.setEnabled(state);
105: moveRuleDownB.setEnabled(state);
106: }
107: });
108: }
109:
110: protected void createObservers() {
111: Observer comboObserver = (new Observer() {
112: public void update(Observable o, Object arg) {
113: refresh();
114: }
115: });
116: survey.getAddGroupNotifier().addObserver(comboObserver);
117: survey.getDeleteGroupNotifier().addObserver(comboObserver);
118: }
119:
120: public void refresh() {
121: groupComboBox.updateModel();
122: boolean state = (survey.getGroups().size() > 0);
123: groupComboBox.setEnabled(state);
124: addRuleB.setEnabled(state);
125: if (!state)
126: rulesTableModel.update(null, null);
127: }
128:
129: public XGroupComboBox getGroupComboBox() {
130: return groupComboBox;
131: }
132:
133: public void showRule(XRule rule) {
134: QuestionGroup group = rule.getGroup();
135: int groupIdx = survey.getGroupIdxById(group.getId());
136: groupComboBox.setSelectedIndex(groupIdx);
137: rulesTableModel.selectRule(rule);
138: }
139:
140: public XRulesTableModel getRulesTableModel() {
141: return rulesTableModel;
142: }
143:
144: public JButton getAddRuleB() {
145: return addRuleB;
146: }
147:
148: public JButton getDeleteRuleB() {
149: return deleteRuleB;
150: }
151:
152: public void update(Observable o, Object arg) {
153: if (arg instanceof QuestionGroup) {
154: QuestionGroup group = (QuestionGroup) arg;
155: int groupIdx = survey.getGroupIdxById(group.getId());
156: groupComboBox.setSelectedIndex(groupIdx);
157: } else if (arg instanceof XRule) {
158: XRule rule = (XRule) arg;
159: rulesTableModel.selectRule(rule);
160: }
161: }
162:
163: }
|