001: package jimm.datavision.gui.parameter;
002:
003: import jimm.datavision.Parameter;
004: import jimm.util.I18N;
005: import java.awt.BorderLayout;
006: import java.awt.CardLayout;
007: import java.awt.Frame;
008: import java.awt.Dimension;
009: import java.awt.event.ActionListener;
010: import java.awt.event.ActionEvent;
011: import java.awt.event.WindowAdapter;
012: import java.awt.event.WindowEvent;
013: import java.util.List;
014: import java.util.HashMap;
015: import java.util.Iterator;
016: import javax.swing.*;
017: import javax.swing.event.ListSelectionListener;
018: import javax.swing.event.ListSelectionEvent;
019:
020: /**
021: * A modal dialog used to ask the user for all runtime report parameter
022: * values. The cards used to dispay editable values are lazily instantiated.
023: *
024: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
025: */
026: public class ParamAskWin extends JDialog implements ActionListener,
027: ListSelectionListener {
028:
029: protected static final int HORIZ_GAP = 20;
030: protected static final int VERT_GAP = 20;
031: protected static final int EDIT_PANEL_WIDTH = 300;
032: protected static final int EDIT_PANEL_HEIGHT = 200;
033: protected static final int TEXT_FIELD_COLS = 24;
034: protected static final int MAX_LIST_VISIBLE = 4;
035: protected static final String CARD_BOOL_NAME = "bool";
036: protected static final String CARD_SINGLE_STRING_NAME = "single-string";
037: protected static final String CARD_RANGE_STRING_NAME = "range-string";
038: protected static final String CARD_LIST_SINGLE_STRING_NAME = "list-single-string";
039: protected static final String CARD_LIST_MULTIPLE_STRING_NAME = "list-multiple-string";
040: protected static final String CARD_SINGLE_DATE_NAME = "single-date";
041: protected static final String CARD_RANGE_DATE_NAME = "range-date";
042:
043: protected List parameters;
044: protected Parameter selectedParameter;
045: protected boolean cancelled;
046: protected JList questionList;
047: protected JPanel cardPanel;
048: protected HashMap createdInquisitors;
049:
050: /**
051: * Constructor.
052: *
053: * @param parent frame with which this dialog should be associated
054: * @param parameters a list of parameters
055: */
056: public ParamAskWin(Frame parent, List parameters) {
057: super (parent, I18N.get("ParamAskWin.title"), true); // Modal
058: this .parameters = parameters;
059: createdInquisitors = new HashMap();
060: selectedParameter = null;
061: buildWindow();
062: questionList.setSelectedIndex(0); // Select first question
063: questionList.setVisibleRowCount(Math.max(parameters.size(),
064: MAX_LIST_VISIBLE));
065: pack();
066: setVisible(true);
067: }
068:
069: protected void buildWindow() {
070: getContentPane().setLayout(new BorderLayout());
071: getContentPane().add(questionPanel(), BorderLayout.NORTH);
072: getContentPane().add(editPanel(), BorderLayout.CENTER);
073: getContentPane().add(buttonPanel(), BorderLayout.SOUTH);
074:
075: addWindowListener(new WindowAdapter() {
076: public void windowClosing(WindowEvent e) {
077: dispose();
078: cancelled = true;
079: }
080: });
081: }
082:
083: protected JPanel questionPanel() {
084: JPanel panel = new JPanel();
085:
086: DefaultListModel model = new DefaultListModel();
087: questionList = new JList(model);
088: questionList.addListSelectionListener(this );
089: questionList
090: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
091:
092: for (Iterator iter = parameters.iterator(); iter.hasNext();)
093: model.addElement(((Parameter) iter.next()).getQuestion());
094:
095: panel.add(new JScrollPane(questionList));
096: return panel;
097: }
098:
099: /**
100: * We create a dummy blank panel. Additional panels are created as they
101: * are needed.
102: *
103: * @return a dummy blank panel
104: */
105: protected JPanel editPanel() {
106: cardPanel = new JPanel();
107: cardPanel.setLayout(new CardLayout(HORIZ_GAP, VERT_GAP));
108:
109: // Panels are created and added as they are needed. We start with
110: // a dummy blank one.
111: JPanel panel = new JPanel();
112: panel.setPreferredSize(new Dimension(EDIT_PANEL_WIDTH,
113: EDIT_PANEL_HEIGHT));
114: cardPanel.add(panel, "dummy-blank-panel");
115:
116: return cardPanel;
117: }
118:
119: protected JPanel buttonPanel() {
120: // OK and Cancel buttons
121: JPanel buttonPanel = new JPanel();
122: JButton button;
123:
124: buttonPanel.add(button = new JButton(I18N
125: .get("ParamAskWin.run_report")));
126: button.addActionListener(this );
127: button.setDefaultCapable(true);
128: getRootPane().setDefaultButton(button);
129:
130: buttonPanel.add(button = new JButton(I18N.get("GUI.cancel")));
131: button.addActionListener(this );
132:
133: return buttonPanel;
134: }
135:
136: public boolean userCancelled() {
137: return cancelled;
138: }
139:
140: /**
141: * Displays parameter fill-in-the-blanks whenever a new question is selected.
142: * Before displaying the new values we save the old values for the previously
143: * selected question.
144: */
145: public void valueChanged(ListSelectionEvent e) {
146: copyValuesToSelectedParameter(); // Previously selected param
147: int i = questionList.getSelectedIndex();
148: if (i >= 0) {
149: selectedParameter = (Parameter) parameters.get(i);
150: selectAndFillCard();
151: } else
152: selectedParameter = null;
153: }
154:
155: /**
156: * Displays and fills the edit field for the currently selected parameter.
157: * Inquisitors are lazily instantiated.
158: */
159: protected void selectAndFillCard() {
160: Inquisitor inq = (Inquisitor) createdInquisitors
161: .get(selectedParameter);
162: if (inq == null) {
163: inq = Inquisitor.create(selectedParameter);
164: createdInquisitors.put(selectedParameter, inq);
165: cardPanel.add(inq.getPanel(), inq.getPanelName());
166: }
167:
168: CardLayout cardLayout = (CardLayout) cardPanel.getLayout();
169: cardLayout.show(cardPanel, inq.getPanelName());
170:
171: // Fill card
172: inq.copyParamIntoGUI();
173: }
174:
175: /**
176: * Copy all values in GUI into the associated selected parameter.
177: */
178: protected void copyValuesToSelectedParameter() {
179: if (selectedParameter != null) {
180: Inquisitor inq = (Inquisitor) createdInquisitors
181: .get(selectedParameter);
182: inq.copyGUIIntoParam();
183: }
184: }
185:
186: /**
187: * Handles the buttons.
188: *
189: * @param e action event
190: */
191: public void actionPerformed(ActionEvent e) {
192: String cmd = e.getActionCommand();
193: if (I18N.get("ParamAskWin.run_report").equals(cmd)) {
194: cancelled = false;
195: copyValuesToSelectedParameter();
196: dispose();
197: } else if (I18N.get("GUI.cancel").equals(cmd)) {
198: cancelled = true;
199: dispose();
200: }
201: }
202:
203: }
|