001: package com.xoetrope.editor.netbeans.actions;
002:
003: import java.util.ArrayList;
004:
005: import java.awt.BorderLayout;
006: import java.awt.Container;
007: import java.awt.Dimension;
008: import java.awt.GridLayout;
009: import java.awt.HeadlessException;
010: import java.awt.event.ActionEvent;
011: import java.awt.event.ActionListener;
012: import javax.swing.BorderFactory;
013: import javax.swing.Box;
014: import javax.swing.BoxLayout;
015: import javax.swing.JButton;
016: import javax.swing.JComboBox;
017: import javax.swing.JDialog;
018: import javax.swing.JFrame;
019: import javax.swing.JLabel;
020: import javax.swing.JPanel;
021: import javax.swing.JTextField;
022:
023: /**
024: * A dialog for presentation of options. Ok and Cancel buttons are automatically
025: * added.
026: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
027: * the GNU Public License (GPL), please see license.txt for more details. If
028: * you make commercial use of this software you must purchase a commercial
029: * license from Xoetrope.</p>
030: * <p> $Revision: 1.3 $</p>
031: */
032: public class OptionDialog extends JDialog implements ActionListener {
033: public static final int TEXT_INPUT = 0;
034: public static final int NUMBER_INPUT = 1;
035: public static final int LIST_INPUT = 2;
036:
037: public static final int ID_OK = 0;
038: public static final int ID_CANCEL = 1;
039:
040: private JPanel contentPanel;
041: private JButton cancelBtn, okBtn;
042: private ArrayList options;
043: private int status = ID_CANCEL;
044:
045: /**
046: * Create a new option dialog
047: * @param frame the parent frame
048: * @param title the dialog title
049: * @throws HeadlessException
050: */
051: public OptionDialog(JFrame frame, String title)
052: throws HeadlessException {
053: super (frame, title, true);
054:
055: options = new ArrayList();
056: Container contentPane = getContentPane();
057: contentPane.setLayout(new BorderLayout());
058:
059: contentPanel = new JPanel();
060: contentPanel.setLayout(new GridLayout(0, 2, 2, 2));
061: contentPanel.setBorder(BorderFactory.createEmptyBorder(10, 10,
062: 10, 10));
063: contentPanel.setMinimumSize(new Dimension(210, 40));
064: contentPane.add(contentPanel, BorderLayout.CENTER);
065:
066: JPanel buttonPanel = new JPanel();
067: buttonPanel.setLayout(new BoxLayout(buttonPanel,
068: BoxLayout.LINE_AXIS));
069: buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 10,
070: 10, 10));
071: buttonPanel.add(Box.createHorizontalGlue());
072: buttonPanel.add(cancelBtn = new JButton("Cancel"));
073: buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
074: buttonPanel.add(okBtn = new JButton("OK"));
075:
076: cancelBtn.addActionListener(this );
077: okBtn.addActionListener(this );
078:
079: contentPane.add(buttonPanel, BorderLayout.PAGE_END);
080: }
081:
082: /**
083: * Respond to the button clicks and dismiss
084: * the dialog once the state has been set
085: * @param e
086: */
087: public void actionPerformed(ActionEvent e) {
088: if (e.getSource() == okBtn)
089: status = ID_OK;
090: else
091: status = ID_CANCEL;
092:
093: setVisible(false);
094: }
095:
096: /**
097: * Get the state following dismissal of the dialog
098: * @return
099: */
100: public int getStatus() {
101: return status;
102: }
103:
104: /**
105: * Add an option
106: * @param optionType the type of the option, controls the input control type
107: * @param caption the caption for this option
108: * @param initialValue the initial value if any
109: */
110: public void addOption(int optionType, String caption,
111: String initialValue) {
112: addOption(optionType, caption, initialValue, null);
113: }
114:
115: /**
116: * Add an option
117: * @param optionType the type of the option, controls the input control type
118: * @param caption the caption for this option
119: * @param initialValue the initial value if any
120: * @param choices he list options
121: */
122: public void addOption(int optionType, String caption,
123: String initialValue, String[] choices) {
124: if ((optionType == TEXT_INPUT) || (optionType == NUMBER_INPUT)) {
125: JTextField option = new JTextField(initialValue);
126: contentPanel.add(new JLabel(caption));
127: contentPanel.add(option);
128: options.add(option);
129: } else if (optionType == LIST_INPUT) {
130: JComboBox option = new JComboBox(choices);
131: contentPanel.add(new JLabel(caption));
132: contentPanel.add(option);
133: option.setSelectedItem(initialValue);
134: options.add(option);
135: }
136: }
137:
138: /**
139: * Get the input or selected value for the object
140: * @param idx the index of the object
141: * @return the value as a string
142: */
143: public String getOption(int idx) {
144: return ((JTextField) options.get(idx)).getText();
145: }
146:
147: /**
148: * Display the dialog
149: */
150: public void setVisible(boolean state) {
151: if (state) {
152: contentPanel.doLayout();
153: pack();
154: }
155: super.setVisible(state);
156: }
157: }
|