001: package com.xoetrope.carousel.survey;
002:
003: import com.xoetrope.survey.Question;
004: import java.awt.BorderLayout;
005: import java.awt.Dimension;
006: import java.awt.FlowLayout;
007: import java.awt.GridBagConstraints;
008: import java.awt.GridBagLayout;
009: import java.awt.Insets;
010: import java.awt.Toolkit;
011: import java.awt.event.ActionEvent;
012: import java.awt.event.ActionListener;
013: import javax.swing.BorderFactory;
014: import javax.swing.JButton;
015: import javax.swing.JComboBox;
016: import javax.swing.JDialog;
017: import javax.swing.JLabel;
018: import javax.swing.JPanel;
019: import javax.swing.JTextField;
020: import javax.swing.SwingConstants;
021: import javax.swing.border.Border;
022: import net.xoetrope.xui.XProject;
023: import net.xoetrope.xui.XProjectManager;
024:
025: /**
026: * A dialog for adding new questions
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 NewQuestionDialog extends JDialog {
035: public static final int LABEL_WIDTH = 80;
036: public static final int EDIT_WIDTH = 160;
037: public static final int ROW_HEIGHT = 24;
038: public static final int WIDTH = LABEL_WIDTH + EDIT_WIDTH + 30;
039: public static final int HEIGHT = 230;
040: public static final int BUTTON_WIDTH = 80;
041: public static final int BUTTON_HEIGHT = 24;
042:
043: protected XSurvey survey;
044: protected XGroupComboBox groupCombo;
045: protected JComboBox questionType;
046: protected JTextField questionId;
047: protected JTextField questionText;
048: protected boolean okPressed;
049:
050: /**
051: * Creates a new instance of NewQuestionDialog
052: */
053: public NewQuestionDialog() {
054: super ();
055:
056: XProject project = XProjectManager.getCurrentProject();
057: survey = (XSurvey) project.getObject("Survey");
058:
059: setModal(true);
060: okPressed = false;
061: init();
062: }
063:
064: /**
065: * Shows this dialog
066: * @param group the question group to which to new question
067: * will be added.
068: * @return true if the OK button was pressed, false if the
069: * CANCEL button was pressed
070: */
071: public boolean showDialog(XQuestionGroup group) {
072: groupCombo.updateModel();
073: questionId.setText(String.valueOf(survey.getNextQuestionId()));
074: questionText.setText("");
075: int groupIdx = (group != null ? survey.getGroupIdxById(group
076: .getId()) : 0);
077: groupCombo.setSelectedIndex(groupIdx);
078: questionText.requestFocus();
079: questionText.requestFocusInWindow();
080: okPressed = false;
081: setVisible(true);
082: return okPressed;
083: }
084:
085: /**
086: * Initializes this dialog.
087: */
088: private void init() {
089: Dimension labelSize = new Dimension(LABEL_WIDTH, ROW_HEIGHT);
090: Dimension editSize = new Dimension(EDIT_WIDTH, ROW_HEIGHT);
091: Dimension buttonSize = new Dimension(BUTTON_WIDTH,
092: BUTTON_HEIGHT);
093: Dimension size = new Dimension(WIDTH, HEIGHT);
094: setSize(size);
095: setPreferredSize(size);
096:
097: Border titledBorder = BorderFactory
098: .createTitledBorder(" Add new question");
099:
100: JPanel mainPanel = new JPanel();
101: mainPanel.setLayout(new BorderLayout());
102: JPanel editPanel = new JPanel();
103: editPanel.setLayout(new GridBagLayout());
104: JPanel bottomPanel = new JPanel();
105: bottomPanel.setLayout(new FlowLayout());
106:
107: JLabel groupLabel = new JLabel("Group: ");
108: groupLabel.setSize(labelSize);
109: groupLabel.setPreferredSize(labelSize);
110: groupLabel.setMinimumSize(labelSize);
111: groupLabel.setHorizontalAlignment(SwingConstants.RIGHT);
112:
113: groupCombo = new XGroupComboBox();
114: groupCombo.setSize(editSize);
115: groupCombo.setPreferredSize(editSize);
116: groupCombo.setMinimumSize(editSize);
117:
118: JLabel questionIdLabel = new JLabel("Question id: ");
119: questionIdLabel.setSize(labelSize);
120: questionIdLabel.setPreferredSize(labelSize);
121: questionIdLabel.setMinimumSize(labelSize);
122: questionIdLabel.setHorizontalAlignment(SwingConstants.RIGHT);
123:
124: questionId = new JTextField();
125: questionId.setSize(editSize);
126: questionId.setMinimumSize(editSize);
127: questionId.setPreferredSize(editSize);
128:
129: JLabel questionTextLabel = new JLabel("Question text: ");
130: questionTextLabel.setSize(labelSize);
131: questionTextLabel.setPreferredSize(labelSize);
132: questionTextLabel.setMinimumSize(labelSize);
133: questionTextLabel.setHorizontalAlignment(SwingConstants.RIGHT);
134:
135: questionText = new JTextField();
136: questionText.setSize(editSize);
137: questionText.setPreferredSize(editSize);
138: questionText.setMinimumSize(editSize);
139:
140: JLabel questionTypeLabel = new JLabel("Question type: ");
141: questionTypeLabel.setSize(labelSize);
142: questionTypeLabel.setPreferredSize(labelSize);
143: questionTypeLabel.setMinimumSize(labelSize);
144: questionTypeLabel.setHorizontalAlignment(SwingConstants.RIGHT);
145:
146: questionType = new JComboBox(Question.QUESTION_TYPES);
147: questionType.setSize(editSize);
148: questionType.setMinimumSize(editSize);
149: questionType.setPreferredSize(editSize);
150:
151: JButton buttonOk = new JButton("Ok");
152: buttonOk.setMinimumSize(buttonSize);
153: buttonOk.setPreferredSize(buttonSize);
154: buttonOk.setSize(buttonSize);
155: buttonOk.addActionListener(new ActionListener() {
156: public void actionPerformed(ActionEvent e) {
157: okPressed = true;
158: setVisible(false);
159: }
160: });
161:
162: JButton buttonCancel = new JButton("Cancel");
163: buttonCancel.setPreferredSize(buttonSize);
164: buttonCancel.setMinimumSize(buttonSize);
165: buttonCancel.setSize(buttonSize);
166: buttonCancel.addActionListener(new ActionListener() {
167: public void actionPerformed(ActionEvent e) {
168: okPressed = false;
169: setVisible(false);
170: }
171: });
172:
173: GridBagConstraints c = new GridBagConstraints();
174: c.insets = new Insets(1, 1, 1, 1);
175:
176: c.gridx = 0;
177: c.gridy = 0;
178: editPanel.add(groupLabel, c);
179: c.gridx = 1;
180: c.gridy = 0;
181: editPanel.add(groupCombo, c);
182:
183: c.gridx = 0;
184: c.gridy = 1;
185: editPanel.add(questionIdLabel, c);
186: c.gridx = 1;
187: c.gridy = 1;
188: editPanel.add(questionId, c);
189: c.gridx = 0;
190: c.gridy = 2;
191: editPanel.add(questionTextLabel, c);
192: c.gridx = 1;
193: c.gridy = 2;
194: editPanel.add(questionText, c);
195: c.gridx = 0;
196: c.gridy = 3;
197: editPanel.add(questionTypeLabel, c);
198: c.gridx = 1;
199: c.gridy = 3;
200: editPanel.add(questionType, c);
201: c.gridx = 0;
202: c.gridy = 4;
203: editPanel.add(buttonOk, c);
204: c.gridx = 1;
205: c.gridy = 4;
206: editPanel.add(buttonCancel, c);
207:
208: bottomPanel.add(buttonOk);
209: bottomPanel.add(buttonCancel);
210:
211: mainPanel.add(editPanel, BorderLayout.CENTER);
212: mainPanel.add(bottomPanel, BorderLayout.SOUTH);
213:
214: editPanel.setBorder(titledBorder);
215:
216: add(mainPanel);
217: pack();
218:
219: Dimension screenSize = Toolkit.getDefaultToolkit()
220: .getScreenSize();
221: Dimension dialogSize = getSize();
222: setLocation((screenSize.width - dialogSize.width) / 2,
223: (screenSize.height - dialogSize.height) / 2);
224: }
225:
226: /**
227: * Gets the question group which is currently selected
228: * in the "question groups" combobox.
229: * @return the selected XQuestionGroup object.
230: */
231: public XQuestionGroup getGroup() {
232: return (XQuestionGroup) groupCombo.getModel().getSelectedItem();
233: }
234:
235: /**
236: * Gets the id of the new question.
237: * @return the question id.
238: */
239: public int getQuestionId() {
240: return (new Integer(questionId.getText()).intValue());
241: }
242:
243: /**
244: * Gets the type of the new question
245: * @return the int indicating the question type.
246: */
247: public int getQuestionType() {
248: String typeText = questionType.getSelectedItem().toString();
249: return Question.getType(typeText);
250: }
251:
252: /**
253: * Gets the text of a new question
254: * @return the question text
255: */
256: public String getQuestionText() {
257: return questionText.getText();
258: }
259:
260: }
|