001: package com.xoetrope.carousel.survey;
002:
003: import com.xoetrope.survey.Question;
004: import com.xoetrope.survey.QuestionGroup;
005: import java.util.Enumeration;
006: import java.util.Vector;
007: import net.xoetrope.editor.project.XEditorProject;
008: import net.xoetrope.xui.XProject;
009: import net.xoetrope.xui.XProjectManager;
010:
011: /**
012: * A wrapper class for QuestionGroup, notifies its observs (i.e graphic editor)
013: * when question is added, deleted, etc..
014: *
015: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
016: * the GNU Public License (GPL), please see license.txt for more details. If
017: * you make commercial use of this software you must purchase a commercial
018: * license from Xoetrope.</p>
019: * <p> $Revision: 1.5 $</p>
020: */
021: public class XQuestionGroup extends QuestionGroup {
022: protected Vector rules;
023: protected XNotifier addQuestionNotifier, deleteQuestionNotifier;
024: protected XNotifier addRuleNotifier, deleteRuleNotifier;
025: protected XNotifier nextGroupNotifier;
026: protected XNotifier positionQuestionNotifier;
027: protected XSurvey survey;
028:
029: public XQuestionGroup(int id, int ng, String n) {
030: super (id, ng, n);
031:
032: XProject project = XProjectManager.getCurrentProject();
033: XEditorProject editorProject = (project instanceof XEditorProject ? (XEditorProject) project
034: : null);
035: survey = (XSurvey) project.getObject("Survey");
036:
037: rules = new Vector();
038: addQuestionNotifier = new XNotifier(editorProject, true);
039: deleteQuestionNotifier = new XNotifier(editorProject, true);
040: addRuleNotifier = new XNotifier(editorProject, true);
041: deleteRuleNotifier = new XNotifier(editorProject, true);
042: nextGroupNotifier = new XNotifier(editorProject, true);
043: positionQuestionNotifier = new XNotifier(editorProject, true);
044: }
045:
046: public XQuestionGroup(int id, String n) {
047: super (id, n);
048:
049: XProject project = XProjectManager.getCurrentProject();
050: XEditorProject editorProject = (project instanceof XEditorProject ? (XEditorProject) project
051: : null);
052: survey = (XSurvey) project.getObject("Survey");
053:
054: rules = new Vector();
055: addQuestionNotifier = new XNotifier(editorProject, true);
056: deleteQuestionNotifier = new XNotifier(editorProject, true);
057: addRuleNotifier = new XNotifier(editorProject, true);
058: deleteRuleNotifier = new XNotifier(editorProject, true);
059: nextGroupNotifier = new XNotifier(editorProject, true);
060: positionQuestionNotifier = new XNotifier(editorProject, true);
061: }
062:
063: public void setName(String newName) {
064: boolean state = !name.equals(newName);
065: super .setName(newName);
066: if (state)
067: XSurvey.setProjectModified(true);
068: }
069:
070: public void setId(int newId) {
071: boolean state = (newId != groupId);
072: super .setId(newId);
073: if (state)
074: XSurvey.setProjectModified(true);
075: }
076:
077: public void setNextGroupId(int newId) {
078: boolean state = (newId != nextGroup);
079: super .setNextGroupId(newId);
080: if (state)
081: XSurvey.setProjectModified(true);
082: }
083:
084: public void addNewRule(int id, XQuestionGroup targetGroup,
085: String name) {
086: XRule rule = new XRule(id, this , targetGroup, name);
087: rules.add(rule);
088: addRuleNotifier.notifyObservers(rule);
089: }
090:
091: public void addNewRule() {
092: addNewRule(survey.getNextRuleId(), null, "new rule");
093: }
094:
095: public void deleteRule(XRule rule) {
096: if (rules.remove(rule))
097: deleteRuleNotifier.notifyObservers(rule);
098: }
099:
100: public void addQuestion(Question question) {
101: super .addQuestion(question);
102: addQuestionNotifier.notifyObservers(question);
103: }
104:
105: public void removeQuestion(Question question) {
106: super .removeQuestion(question);
107: deleteQuestionNotifier.notifyObservers(question);
108: }
109:
110: public void addNewQuestion(int id, String text, int type) {
111: Question question = new Question(id, this , type, text);
112: questions.add(question);
113: addQuestionNotifier.notifyObservers(question);
114: }
115:
116: public void addNewQuestion() {
117: int newQuestionId = survey.getNextQuestionId();
118: addNewQuestion(newQuestionId, "", Question.DEFAULT_TYPE);
119: }
120:
121: public void deleteQuestion(Question question) {
122: if (questions.remove(question))
123: deleteQuestionNotifier.notifyObservers(question);
124: }
125:
126: public void changeNextGroupId(XQuestionGroup group) {
127: int newNextGroup = (group != null ? group.getId() : -1);
128: if (nextGroup != newNextGroup) {
129: nextGroup = newNextGroup;
130: nextGroupNotifier.notifyObservers(new Integer(nextGroup));
131: }
132: }
133:
134: public void updateRuleTargets(XQuestionGroup deletedGroup) {
135: Enumeration enumRules = rules.elements();
136: while (enumRules.hasMoreElements()) {
137: XRule rule = (XRule) enumRules.nextElement();
138: XQuestionGroup targetGroup = rule.getTarget();
139: if (targetGroup == null
140: || targetGroup.getId() != deletedGroup.getId())
141: continue;
142: rule.changeTarget(null);
143: }
144: }
145:
146: public XNotifier getAddQuestionNotifier() {
147: return addQuestionNotifier;
148: }
149:
150: public XNotifier getDeleteQuestionNotifier() {
151: return deleteQuestionNotifier;
152: }
153:
154: public XNotifier getAddRuleNotifier() {
155: return addRuleNotifier;
156: }
157:
158: public XNotifier getDeleteRuleNotifier() {
159: return deleteRuleNotifier;
160: }
161:
162: public XNotifier getNextGroupNotifier() {
163: return nextGroupNotifier;
164: }
165:
166: public XNotifier getPositionQuestionNotifier() {
167: return positionQuestionNotifier;
168: }
169:
170: public Vector getRules() {
171: return rules;
172: }
173:
174: public XRule getRule(int idx) {
175: return (idx >= 0 && idx < rules.size() ? (XRule) rules.get(idx)
176: : null);
177: }
178:
179: public int getRuleIdx(XRule rule) {
180: int idx = -1;
181: for (int i = 0; i < rules.size() && idx == -1; i++) {
182: XRule r = (XRule) rules.get(i);
183: if (r.equals(rule))
184: idx = i;
185: }
186: return idx;
187: }
188:
189: public int getQuestionIdx(Question question) {
190: int idx = -1;
191: for (int i = 0; i < questions.size() && idx == -1; i++) {
192: Question q = (Question) questions.get(i);
193: if (q.equals(question))
194: idx = i;
195: }
196: return idx;
197: }
198:
199: public void setRules(Vector r) {
200: rules = r;
201: }
202:
203: public void addRule(XRule rule) {
204: rules.add(rule);
205: }
206:
207: public boolean removeRule(XRule rule) {
208: return rules.remove(rule);
209: }
210: }
|