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.util.Vector;
009: import java.awt.Dimension;
010: import java.awt.FlowLayout;
011: import java.awt.GridBagConstraints;
012: import java.awt.GridBagLayout;
013: import java.awt.Insets;
014: import java.awt.Toolkit;
015: import java.awt.event.ActionEvent;
016: import java.awt.event.ActionListener;
017: import javax.swing.*;
018: import javax.swing.border.Border;
019: import javax.swing.border.EtchedBorder;
020: import javax.swing.border.TitledBorder;
021: import net.xoetrope.xui.XProject;
022: import net.xoetrope.xui.XProjectManager;
023:
024: /**
025: * A dialog for adding new groups
026: *
027: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
028: * the GNU Public License (GPL), please see license.txt for more details. If
029: * you make commercial use of this software you must purchase a commercial
030: * license from Xoetrope.</p>
031: * <p> $Revision: 1.5 $</p>
032: */
033: public class NewGroupDialog extends JDialog {
034: public static final int LABEL_WIDTH = 70;
035: public static final int EDIT_WIDTH = 160;
036: public static final int ROW_HEIGHT = 24;
037: public static final int WIDTH = LABEL_WIDTH + EDIT_WIDTH + 30;
038: public static final int HEIGHT = 200;
039: public static final int BUTTON_WIDTH = 80;
040: public static final int BUTTON_HEIGHT = 24;
041:
042: protected XSurvey survey;
043: protected JTextField groupId;
044: protected JTextField groupName;
045: protected JComboBox nextGroup;
046: protected boolean okPressed;
047:
048: /**
049: * Creates a new instance of NewGroupDialog
050: */
051: public NewGroupDialog() {
052: super ();
053: XProject project = XProjectManager.getCurrentProject();
054: survey = (XSurvey) project.getObject("Survey");
055:
056: setModal(true);
057: okPressed = false;
058: init();
059: }
060:
061: /**
062: * Initialises the new group dialog
063: */
064: protected void init() {
065: Dimension labelSize = new Dimension(LABEL_WIDTH, ROW_HEIGHT);
066: Dimension editSize = new Dimension(EDIT_WIDTH, ROW_HEIGHT);
067: Dimension buttonSize = new Dimension(BUTTON_WIDTH,
068: BUTTON_HEIGHT);
069: Dimension size = new Dimension(WIDTH, HEIGHT);
070: setSize(size);
071: setPreferredSize(size);
072:
073: Border titledBorder = BorderFactory
074: .createTitledBorder(" Add new group");
075:
076: JPanel mainPanel = new JPanel();
077: mainPanel.setLayout(new BorderLayout());
078: JPanel editPanel = new JPanel();
079: editPanel.setLayout(new GridBagLayout());
080: JPanel bottomPanel = new JPanel();
081: bottomPanel.setLayout(new FlowLayout());
082:
083: JLabel groupIdLabel = new JLabel("Group id: ");
084: groupIdLabel.setSize(labelSize);
085: groupIdLabel.setPreferredSize(labelSize);
086: groupIdLabel.setMinimumSize(labelSize);
087: groupIdLabel.setHorizontalAlignment(SwingConstants.RIGHT);
088:
089: groupId = new JTextField();
090: groupId.setSize(editSize);
091: groupId.setMinimumSize(editSize);
092: groupId.setPreferredSize(editSize);
093:
094: JLabel groupNameLabel = new JLabel("Group name: ");
095: groupNameLabel.setSize(labelSize);
096: groupNameLabel.setPreferredSize(labelSize);
097: groupNameLabel.setMinimumSize(labelSize);
098: groupNameLabel.setHorizontalAlignment(SwingConstants.RIGHT);
099:
100: groupName = new JTextField();
101: groupName.setSize(editSize);
102: groupName.setPreferredSize(editSize);
103: groupName.setMinimumSize(editSize);
104:
105: JLabel nextGroupLabel = new JLabel("Next group: ");
106: nextGroupLabel.setSize(labelSize);
107: nextGroupLabel.setPreferredSize(labelSize);
108: nextGroupLabel.setMinimumSize(labelSize);
109: nextGroupLabel.setHorizontalAlignment(SwingConstants.RIGHT);
110:
111: nextGroup = new JComboBox(survey.getTargetGroups());
112: nextGroup.setSize(editSize);
113: nextGroup.setMinimumSize(editSize);
114: nextGroup.setPreferredSize(editSize);
115:
116: JButton buttonOk = new JButton("Ok");
117: buttonOk.setSize(buttonSize);
118: buttonOk.setPreferredSize(buttonSize);
119: buttonOk.setMinimumSize(buttonSize);
120: buttonOk.addActionListener(new ActionListener() {
121: public void actionPerformed(ActionEvent e) {
122: okPressed = true;
123: setVisible(false);
124: }
125: });
126:
127: JButton buttonCancel = new JButton("Cancel");
128: buttonCancel.setSize(buttonSize);
129: buttonCancel.setPreferredSize(buttonSize);
130: buttonCancel.setMinimumSize(buttonSize);
131: buttonCancel.addActionListener(new ActionListener() {
132: public void actionPerformed(ActionEvent e) {
133: okPressed = false;
134: setVisible(false);
135: }
136: });
137:
138: GridBagConstraints c = new GridBagConstraints();
139: c.insets = new Insets(1, 1, 1, 1);
140:
141: c.gridx = 0;
142: c.gridy = 0;
143: editPanel.add(groupIdLabel, c);
144: c.gridx = 1;
145: c.gridy = 0;
146: editPanel.add(groupId, c);
147: c.gridx = 0;
148: c.gridy = 1;
149: editPanel.add(groupNameLabel, c);
150: c.gridx = 1;
151: c.gridy = 1;
152: editPanel.add(groupName, c);
153: c.gridx = 0;
154: c.gridy = 2;
155: editPanel.add(nextGroupLabel, c);
156: c.gridx = 1;
157: c.gridy = 2;
158: editPanel.add(nextGroup, c);
159:
160: buttonOk.setMinimumSize(buttonSize);
161: buttonOk.setPreferredSize(buttonSize);
162: buttonOk.setSize(buttonSize);
163: bottomPanel.add(buttonOk);
164: bottomPanel.add(buttonCancel);
165:
166: mainPanel.add(editPanel, BorderLayout.CENTER);
167: mainPanel.add(bottomPanel, BorderLayout.SOUTH);
168:
169: editPanel.setBorder(titledBorder);
170: add(mainPanel);
171: pack();
172:
173: Dimension screenSize = Toolkit.getDefaultToolkit()
174: .getScreenSize();
175: Dimension dialogSize = getSize();
176: setLocation((screenSize.width - dialogSize.width) / 2,
177: (screenSize.height - dialogSize.height) / 2);
178: }
179:
180: /**
181: * Shows this dialog
182: */
183: public boolean showDialog() {
184: groupId.setText(String.valueOf(survey.getNextGroupId()));
185: groupName.setText("");
186: if (survey.getGroups().size() == 0) {
187: nextGroup.setEnabled(false);
188: } else {
189: nextGroup.setEnabled(true);
190: nextGroup.setSelectedIndex(0);
191: }
192: groupName.requestFocus();
193: okPressed = false;
194: setVisible(true);
195: return okPressed;
196: }
197:
198: /**
199: * Gets the id of this group
200: */
201: public int getGroupId() {
202: return new Integer(groupId.getText()).intValue();
203: }
204:
205: public String getGroupName() {
206: return groupName.getText();
207: }
208:
209: public XTargetGroup getNextGroup() {
210: return (XTargetGroup) nextGroup.getSelectedItem();
211: }
212:
213: }
|