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.Dimension;
008: import java.awt.FlowLayout;
009: import java.awt.GridBagConstraints;
010: import java.awt.GridBagLayout;
011: import java.awt.Insets;
012: import java.awt.Toolkit;
013: import java.awt.event.ActionEvent;
014: import java.awt.event.ActionListener;
015: import java.util.Vector;
016: import javax.swing.*;
017: import javax.swing.border.Border;
018: import javax.swing.border.TitledBorder;
019: import net.xoetrope.xui.XProject;
020: import net.xoetrope.xui.XProjectManager;
021:
022: /**
023: * A dialog for adding new rules
024: *
025: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
026: * the GNU Public License (GPL), please see license.txt for more details. If
027: * you make commercial use of this software you must purchase a commercial
028: * license from Xoetrope.</p>
029: * <p> $Revision: 1.5 $</p>
030: */
031: public class NewRuleDialog extends JDialog {
032: public static final int LABEL_WIDTH = 80;
033: public static final int EDIT_WIDTH = 160;
034: public static final int ROW_HEIGHT = 24;
035: public static final int WIDTH = LABEL_WIDTH + EDIT_WIDTH + 30;
036: public static final int HEIGHT = 230;
037: public static final int BUTTON_WIDTH = 80;
038: public static final int BUTTON_HEIGHT = 24;
039:
040: protected XSurvey survey;
041: protected XGroupComboBox groupCombo;
042: protected JTextField ruleId;
043: protected JTextField ruleName;
044: protected JComboBox targetGroup;
045: protected boolean okPressed;
046:
047: /**
048: * Creates a new instance of NewRuleDialog
049: */
050: public NewRuleDialog() {
051: super ();
052: XProject project = XProjectManager.getCurrentProject();
053: survey = (XSurvey) project.getObject("Survey");
054: setModal(true);
055: okPressed = false;
056: init();
057: }
058:
059: /**
060: * Initializes this dialog.
061: */
062: private void init() {
063: Dimension labelSize = new Dimension(LABEL_WIDTH, ROW_HEIGHT);
064: Dimension editSize = new Dimension(EDIT_WIDTH, ROW_HEIGHT);
065: Dimension buttonSize = new Dimension(BUTTON_WIDTH,
066: BUTTON_HEIGHT);
067: Dimension size = new Dimension(WIDTH, HEIGHT);
068: setSize(size);
069: setPreferredSize(size);
070:
071: Border titledBorder = BorderFactory
072: .createTitledBorder(" Add new rule");
073:
074: JPanel mainPanel = new JPanel();
075: mainPanel.setLayout(new BorderLayout());
076: JPanel editPanel = new JPanel();
077: editPanel.setLayout(new GridBagLayout());
078: JPanel bottomPanel = new JPanel();
079: bottomPanel.setLayout(new FlowLayout());
080:
081: JLabel groupLabel = new JLabel("Group: ");
082: groupLabel.setSize(labelSize);
083: groupLabel.setPreferredSize(labelSize);
084: groupLabel.setMinimumSize(labelSize);
085: groupLabel.setHorizontalAlignment(SwingConstants.RIGHT);
086:
087: groupCombo = new XGroupComboBox();
088: groupCombo.setSize(editSize);
089: groupCombo.setMinimumSize(editSize);
090: groupCombo.setPreferredSize(editSize);
091:
092: JLabel ruleIdLabel = new JLabel("Rule id: ");
093: ruleIdLabel.setSize(labelSize);
094: ruleIdLabel.setPreferredSize(labelSize);
095: ruleIdLabel.setMinimumSize(labelSize);
096: ruleIdLabel.setHorizontalAlignment(SwingConstants.RIGHT);
097:
098: ruleId = new JTextField();
099: ruleId.setSize(editSize);
100: ruleId.setMinimumSize(editSize);
101: ruleId.setPreferredSize(editSize);
102:
103: JLabel ruleNameLabel = new JLabel("Rule name: ");
104: ruleNameLabel.setSize(labelSize);
105: ruleNameLabel.setPreferredSize(labelSize);
106: ruleNameLabel.setMinimumSize(labelSize);
107: ruleNameLabel.setHorizontalAlignment(SwingConstants.RIGHT);
108:
109: ruleName = new JTextField();
110: ruleName.setSize(editSize);
111: ruleName.setMinimumSize(editSize);
112: ruleName.setPreferredSize(editSize);
113:
114: JLabel targetGroupLabel = new JLabel("Target group: ");
115: targetGroupLabel.setSize(labelSize);
116: targetGroupLabel.setPreferredSize(labelSize);
117: targetGroupLabel.setMinimumSize(labelSize);
118: targetGroupLabel.setHorizontalAlignment(SwingConstants.RIGHT);
119:
120: targetGroup = new JComboBox(survey.getTargetGroups());
121: targetGroup.setSize(editSize);
122: targetGroup.setMinimumSize(editSize);
123: targetGroup.setPreferredSize(editSize);
124:
125: JButton buttonOk = new JButton("OK ");
126: buttonOk.setSize(buttonSize);
127: buttonOk.setPreferredSize(buttonSize);
128: buttonOk.setMinimumSize(buttonSize);
129: buttonOk.addActionListener(new ActionListener() {
130: public void actionPerformed(ActionEvent e) {
131: okPressed = true;
132: setVisible(false);
133: }
134: });
135:
136: JButton buttonCancel = new JButton("Cancel");
137: buttonCancel.setSize(buttonSize);
138: buttonCancel.setPreferredSize(buttonSize);
139: buttonCancel.setMinimumSize(buttonSize);
140: buttonCancel.addActionListener(new ActionListener() {
141: public void actionPerformed(ActionEvent e) {
142: okPressed = false;
143: setVisible(false);
144: }
145: });
146:
147: GridBagConstraints c = new GridBagConstraints();
148: c.insets = new Insets(1, 1, 1, 1);
149:
150: c.gridx = 0;
151: c.gridy = 0;
152: editPanel.add(groupLabel, c);
153: c.gridx = 1;
154: c.gridy = 0;
155: editPanel.add(groupCombo, c);
156: c.gridx = 0;
157: c.gridy = 1;
158: editPanel.add(ruleIdLabel, c);
159: c.gridx = 1;
160: c.gridy = 1;
161: editPanel.add(ruleId, c);
162: c.gridx = 0;
163: c.gridy = 2;
164: editPanel.add(targetGroupLabel, c);
165: c.gridx = 1;
166: c.gridy = 2;
167: editPanel.add(targetGroup, c);
168: c.gridx = 0;
169: c.gridy = 3;
170: editPanel.add(ruleNameLabel, c);
171: c.gridx = 1;
172: c.gridy = 3;
173: editPanel.add(ruleName, c);
174:
175: bottomPanel.add(buttonOk);
176: bottomPanel.add(buttonCancel);
177: mainPanel.add(editPanel, BorderLayout.CENTER);
178: mainPanel.add(bottomPanel, BorderLayout.SOUTH);
179:
180: editPanel.setBorder(titledBorder);
181:
182: add(mainPanel);
183: pack();
184:
185: Dimension screenSize = Toolkit.getDefaultToolkit()
186: .getScreenSize();
187: Dimension dialogSize = getSize();
188: setLocation((screenSize.width - dialogSize.width) / 2,
189: (screenSize.height - dialogSize.height) / 2);
190: }
191:
192: /**
193: * Shows this dialog
194: * @param group the question group to which the new rule
195: * will be added.
196: * @return true if the OK button was pressed, false if
197: * the CANCEL button was pressed.
198: */
199: public boolean showDialog(XQuestionGroup group) {
200: ruleId.setText(String.valueOf(survey.getNextRuleId()));
201: groupCombo.updateModel();
202: int groupIdx = (group != null ? survey.getGroupIdxById(group
203: .getId()) : 0);
204: groupCombo.setSelectedIndex(groupIdx);
205: ruleName.setText("");
206: if (survey.getGroups().size() == 0)
207: targetGroup.setEnabled(false);
208: else {
209: targetGroup.setEnabled(true);
210: targetGroup.setSelectedIndex(0);
211: }
212:
213: okPressed = false;
214: setVisible(true);
215: return okPressed;
216: }
217:
218: /**
219: * Gets the question group which is currently
220: * selected in the group combobox.
221: * @return the selected question group.
222: */
223: public XQuestionGroup getGroup() {
224: return (XQuestionGroup) groupCombo.getSelectedItem();
225: }
226:
227: /**
228: * Gets the id of the new rule
229: * @return the id
230: */
231: public int getRuleId() {
232: return (new Integer(ruleId.getText()).intValue());
233: }
234:
235: /**
236: * Gets the target question group of the new rule
237: * @return the target question group
238: */
239: public XQuestionGroup getTarget() {
240: return ((XTargetGroup) targetGroup.getSelectedItem())
241: .getGroup();
242: }
243:
244: /**
245: * Gets the name of the new rule
246: * @return the rule name
247: */
248: public String getRuleName() {
249: return ruleName.getText();
250: }
251: }
|