001: package com.xoetrope.carousel.survey;
002:
003: import com.xoetrope.survey.Question;
004: import com.xoetrope.survey.QuestionGroup;
005: import com.xoetrope.survey.Survey;
006: import com.xoetrope.survey.SurveyManager;
007: import java.awt.BorderLayout;
008: import java.awt.Container;
009: import java.awt.Dimension;
010: import java.util.Observable;
011: import java.util.Observer;
012: import java.util.Vector;
013: import javax.swing.BorderFactory;
014: import javax.swing.ImageIcon;
015: import javax.swing.JButton;
016: import javax.swing.JComboBox;
017: import javax.swing.JLabel;
018: import javax.swing.JPanel;
019: import javax.swing.JScrollPane;
020: import javax.swing.JToolBar;
021: import javax.swing.border.EmptyBorder;
022: import javax.swing.border.TitledBorder;
023: import net.xoetrope.xui.XProject;
024: import net.xoetrope.xui.XProjectManager;
025:
026: /**
027: * A panel containing question table
028: *
029: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
030: * the GNU Public License (GPL), please see license.txt for more details. If
031: * you make commercial use of this software you must purchase a commercial
032: * license from Xoetrope.</p>
033: * <p> $Revision: 1.5 $</p>
034: */
035: public class XQuestionsPanel extends JPanel {
036:
037: protected JButton addQuestionB, deleteQuestionB, moveQuestionUpB,
038: moveQuestionDownB;
039: protected XSurveyEditorFrame editorFrame;
040: protected XSurvey survey;
041: protected XQuestionsTableModel questionsTableModel;
042: protected XGroupComboBox groupComboBox;
043: protected JLabel comboBoxLabel;
044:
045: public XQuestionsPanel() {
046: super ();
047: XProject project = XProjectManager.getCurrentProject();
048: editorFrame = (XSurveyEditorFrame) project
049: .getObject("EditorFrame");
050: survey = (XSurvey) project.getObject("Survey");
051: init();
052: createObservers();
053: }
054:
055: protected void init() {
056: setLayout(new BorderLayout());
057:
058: XTable questionsTable = new XTable();
059: questionsTable.setBorder(new EmptyBorder(0, 0, 0, 0));
060: questionsTableModel = new XQuestionsTableModel(questionsTable);
061: JScrollPane scrollPaneQuestions = new JScrollPane(
062: questionsTable);
063: TitledBorder questionBorder = BorderFactory
064: .createTitledBorder("Questions");
065: scrollPaneQuestions.setBorder(questionBorder);
066:
067: JToolBar questionsToolBar = new JToolBar();
068: addQuestionB = editorFrame.addButton("addQuestion.png",
069: "add question", questionsToolBar);
070: deleteQuestionB = editorFrame.addButton("deleteQuestion.png",
071: "delete question", questionsToolBar);
072: moveQuestionUpB = editorFrame.addButton("moveQuestionUp.png",
073: "move up", questionsToolBar);
074: moveQuestionDownB = editorFrame.addButton(
075: "moveQuestionDown.png", "move down", questionsToolBar);
076:
077: groupComboBox = new XGroupComboBox();
078:
079: comboBoxLabel = new JLabel("group ");
080: questionsToolBar.add(comboBoxLabel);
081: questionsToolBar.add(groupComboBox);
082:
083: deleteQuestionB.setEnabled(false);
084: moveQuestionUpB.setEnabled(false);
085: moveQuestionDownB.setEnabled(false);
086:
087: add(questionsToolBar, BorderLayout.NORTH);
088: add(scrollPaneQuestions, BorderLayout.CENTER);
089:
090: questionsTableModel.getNotifier().addObserver(new Observer() {
091: public void update(Observable o, Object arg) {
092: boolean state = (arg != null);
093: deleteQuestionB.setEnabled(state);
094: moveQuestionUpB.setEnabled(state);
095: moveQuestionDownB.setEnabled(state);
096: }
097: });
098: groupComboBox.getNotifier().addObserver(questionsTableModel);
099:
100: addQuestionB.addActionListener(questionsTableModel
101: .getAddQuestion());
102: deleteQuestionB.addActionListener(questionsTableModel
103: .getDeleteQuestion());
104: moveQuestionUpB.addActionListener(questionsTableModel
105: .getMoveQuestionUp());
106: moveQuestionDownB.addActionListener(questionsTableModel
107: .getMoveQuestionDown());
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: addQuestionB.setEnabled(state);
125: if (!state)
126: questionsTableModel.update(null, null);
127: }
128:
129: public void showQuestion(Question question) {
130: int groupIdx = survey.getGroupIdxById(question.getGroup()
131: .getId());
132: groupComboBox.setSelectedIndex(groupIdx);
133: questionsTableModel.selectQuestion(question);
134: }
135:
136: public XQuestionsTableModel getQuestionsTableModel() {
137: return questionsTableModel;
138: }
139:
140: public JButton getAddQuestionB() {
141: return addQuestionB;
142: }
143:
144: public XGroupComboBox getGroupComboBox() {
145: return groupComboBox;
146: }
147:
148: protected JLabel getComboBoxLabel() {
149: return comboBoxLabel;
150: }
151:
152: }
|