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.*;
014: import javax.swing.border.Border;
015: import javax.swing.border.TitledBorder;
016: import net.xoetrope.xui.XProject;
017: import net.xoetrope.xui.XProjectManager;
018:
019: /**
020: * A dialog for adding new options
021: *
022: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
023: * the GNU Public License (GPL), please see license.txt for more details. If
024: * you make commercial use of this software you must purchase a commercial
025: * license from Xoetrope.</p>
026: * <p> $Revision: 1.5 $</p>
027: */
028: public class NewOptionDialog extends JDialog {
029: public static final int LABEL_WIDTH = 80;
030: public static final int EDIT_WIDTH = 160;
031: public static final int ROW_HEIGHT = 24;
032: public static final int WIDTH = LABEL_WIDTH + EDIT_WIDTH + 30;
033: public static final int HEIGHT = 150;
034: public static final int BUTTON_WIDTH = 80;
035: public static final int BUTTON_HEIGHT = 24;
036:
037: protected JTextField optionText;
038: protected JTextField optionId;
039: protected boolean okPressed;
040: protected XSurvey survey;
041:
042: /**
043: * Creates a new instance of NewOptionDialog
044: */
045: public NewOptionDialog() {
046: super ();
047:
048: XProject project = XProjectManager.getCurrentProject();
049: survey = (XSurvey) project.getObject("Survey");
050:
051: setModal(true);
052: okPressed = false;
053: init();
054: }
055:
056: /**
057: * Shows this dialog
058: */
059: public boolean showDialog() {
060: optionId.setText(String.valueOf(survey.getNextOptionId()));
061: optionText.setText("");
062: optionText.requestFocus();
063: optionText.requestFocusInWindow();
064: okPressed = false;
065: setVisible(true);
066: return okPressed;
067: }
068:
069: /**
070: * Initializes this dialog
071: */
072: private void init() {
073: Dimension labelSize = new Dimension(LABEL_WIDTH, ROW_HEIGHT);
074: Dimension editSize = new Dimension(EDIT_WIDTH, ROW_HEIGHT);
075: Dimension buttonSize = new Dimension(BUTTON_WIDTH,
076: BUTTON_HEIGHT);
077: Dimension size = new Dimension(WIDTH, HEIGHT);
078: setSize(size);
079: setPreferredSize(size);
080:
081: Border titledBorder = BorderFactory
082: .createTitledBorder(" Add new option");
083:
084: JPanel mainPanel = new JPanel();
085: mainPanel.setLayout(new BorderLayout());
086: JPanel editPanel = new JPanel();
087: editPanel.setLayout(new GridBagLayout());
088: JPanel bottomPanel = new JPanel();
089: bottomPanel.setLayout(new FlowLayout());
090:
091: JLabel optionTextLabel = new JLabel("Option text: ");
092: optionTextLabel.setSize(labelSize);
093: optionTextLabel.setPreferredSize(labelSize);
094: optionTextLabel.setMinimumSize(labelSize);
095: optionTextLabel.setHorizontalAlignment(SwingConstants.RIGHT);
096:
097: optionText = new JTextField();
098: optionText.setSize(editSize);
099: optionText.setMinimumSize(editSize);
100: optionText.setPreferredSize(editSize);
101:
102: JLabel optionIdLabel = new JLabel("Option id: ");
103: optionIdLabel.setSize(labelSize);
104: optionIdLabel.setPreferredSize(labelSize);
105: optionIdLabel.setMinimumSize(labelSize);
106: optionIdLabel.setHorizontalAlignment(SwingConstants.RIGHT);
107:
108: optionId = new JTextField();
109: optionId.setSize(editSize);
110: optionId.setMinimumSize(editSize);
111: optionId.setPreferredSize(editSize);
112:
113: JButton buttonOk = new JButton("Ok");
114: buttonOk.setSize(buttonSize);
115: buttonOk.setPreferredSize(buttonSize);
116: buttonOk.setMinimumSize(buttonSize);
117: buttonOk.addActionListener(new ActionListener() {
118: public void actionPerformed(ActionEvent e) {
119: okPressed = true;
120: NewOptionDialog.this .setVisible(false);
121: }
122: });
123:
124: JButton buttonCancel = new JButton("Cancel");
125: buttonCancel.setSize(buttonSize);
126: buttonCancel.setPreferredSize(buttonSize);
127: buttonCancel.setMinimumSize(buttonSize);
128: buttonCancel.addActionListener(new ActionListener() {
129: public void actionPerformed(ActionEvent e) {
130: okPressed = false;
131: NewOptionDialog.this .setVisible(false);
132: }
133: });
134:
135: GridBagConstraints c = new GridBagConstraints();
136: c.insets = new Insets(1, 1, 1, 1);
137:
138: c.gridx = 0;
139: c.gridy = 0;
140: editPanel.add(optionIdLabel, c);
141: c.gridx = 1;
142: c.gridy = 0;
143: editPanel.add(optionId, c);
144: c.gridx = 0;
145: c.gridy = 1;
146: editPanel.add(optionTextLabel, c);
147: c.gridx = 1;
148: c.gridy = 1;
149: editPanel.add(optionText, c);
150:
151: bottomPanel.add(buttonOk);
152: bottomPanel.add(buttonCancel);
153: mainPanel.add(editPanel, BorderLayout.CENTER);
154: mainPanel.add(bottomPanel, BorderLayout.SOUTH);
155: editPanel.setBorder(titledBorder);
156: add(mainPanel);
157: pack();
158:
159: Dimension screenSize = Toolkit.getDefaultToolkit()
160: .getScreenSize();
161: Dimension dialogSize = getSize();
162: setLocation((screenSize.width - dialogSize.width) / 2,
163: (screenSize.height - dialogSize.height) / 2);
164: }
165:
166: /**
167: * Gets the id of the new option (created in this dialog)
168: * @return the id of the option
169: */
170: public int getOptionId() {
171: return (new Integer(optionId.getText()).intValue());
172: }
173:
174: /**
175: * Gets the text of the new option
176: * @return the option text
177: */
178: public String getResponse() {
179: return optionText.getText();
180: }
181:
182: }
|