KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > gui > parameter > ParamAskWin


1 package jimm.datavision.gui.parameter;
2 import jimm.datavision.Parameter;
3 import jimm.util.I18N;
4 import java.awt.BorderLayout JavaDoc;
5 import java.awt.CardLayout JavaDoc;
6 import java.awt.Frame JavaDoc;
7 import java.awt.Dimension JavaDoc;
8 import java.awt.event.ActionListener JavaDoc;
9 import java.awt.event.ActionEvent JavaDoc;
10 import java.awt.event.WindowAdapter JavaDoc;
11 import java.awt.event.WindowEvent JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import javax.swing.*;
16 import javax.swing.event.ListSelectionListener JavaDoc;
17 import javax.swing.event.ListSelectionEvent JavaDoc;
18
19 /**
20  * A modal dialog used to ask the user for all runtime report parameter
21  * values. The cards used to dispay editable values are lazily instantiated.
22  *
23  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
24  */

25 public class ParamAskWin
26     extends JDialog
27     implements ActionListener JavaDoc, ListSelectionListener JavaDoc
28 {
29
30 protected static final int HORIZ_GAP = 20;
31 protected static final int VERT_GAP = 20;
32 protected static final int EDIT_PANEL_WIDTH = 300;
33 protected static final int EDIT_PANEL_HEIGHT = 200;
34 protected static final int TEXT_FIELD_COLS = 24;
35 protected static final int MAX_LIST_VISIBLE = 4;
36 protected static final String JavaDoc CARD_BOOL_NAME = "bool";
37 protected static final String JavaDoc CARD_SINGLE_STRING_NAME = "single-string";
38 protected static final String JavaDoc CARD_RANGE_STRING_NAME = "range-string";
39 protected static final String JavaDoc CARD_LIST_SINGLE_STRING_NAME =
40     "list-single-string";
41 protected static final String JavaDoc CARD_LIST_MULTIPLE_STRING_NAME =
42     "list-multiple-string";
43 protected static final String JavaDoc CARD_SINGLE_DATE_NAME = "single-date";
44 protected static final String JavaDoc CARD_RANGE_DATE_NAME = "range-date";
45
46 protected List JavaDoc parameters;
47 protected Parameter selectedParameter;
48 protected boolean cancelled;
49 protected JList questionList;
50 protected JPanel cardPanel;
51 protected HashMap JavaDoc createdInquisitors;
52
53 /**
54  * Constructor.
55  *
56  * @param parent frame with which this dialog should be associated
57  * @param parameters a list of parameters
58  */

59 public ParamAskWin(Frame JavaDoc parent, List JavaDoc parameters) {
60     super(parent, I18N.get("ParamAskWin.title"), true); // Modal
61
this.parameters = parameters;
62     createdInquisitors = new HashMap JavaDoc();
63     selectedParameter = null;
64     buildWindow();
65     questionList.setSelectedIndex(0); // Select first question
66
questionList.setVisibleRowCount(Math.max(parameters.size(),
67                          MAX_LIST_VISIBLE));
68     pack();
69     setVisible(true);
70 }
71
72 protected void buildWindow() {
73     getContentPane().setLayout(new BorderLayout JavaDoc());
74     getContentPane().add(questionPanel(), BorderLayout.NORTH);
75     getContentPane().add(editPanel(), BorderLayout.CENTER);
76     getContentPane().add(buttonPanel(), BorderLayout.SOUTH);
77
78     addWindowListener(new WindowAdapter JavaDoc() {
79     public void windowClosing(WindowEvent JavaDoc e) {
80         dispose();
81         cancelled = true;
82     }
83     });
84 }
85
86 protected JPanel questionPanel() {
87     JPanel panel = new JPanel();
88
89     DefaultListModel model = new DefaultListModel();
90     questionList = new JList(model);
91     questionList.addListSelectionListener(this);
92     questionList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
93
94     for (Iterator JavaDoc iter = parameters.iterator(); iter.hasNext(); )
95     model.addElement(((Parameter)iter.next()).getQuestion());
96
97     panel.add(new JScrollPane(questionList));
98     return panel;
99 }
100
101 /**
102  * We create a dummy blank panel. Additional panels are created as they
103  * are needed.
104  *
105  * @return a dummy blank panel
106  */

107 protected JPanel editPanel() {
108     cardPanel = new JPanel();
109     cardPanel.setLayout(new CardLayout JavaDoc(HORIZ_GAP, VERT_GAP));
110
111     // Panels are created and added as they are needed. We start with
112
// a dummy blank one.
113
JPanel panel = new JPanel();
114     panel.setPreferredSize(new Dimension JavaDoc(EDIT_PANEL_WIDTH, EDIT_PANEL_HEIGHT));
115     cardPanel.add(panel, "dummy-blank-panel");
116
117     return cardPanel;
118 }
119
120 protected JPanel buttonPanel() {
121     // OK and Cancel buttons
122
JPanel buttonPanel = new JPanel();
123     JButton button;
124
125     buttonPanel.add(button = new JButton(I18N.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() { return cancelled; }
137
138 /**
139  * Displays parameter fill-in-the-blanks whenever a new question is selected.
140  * Before displaying the new values we save the old values for the previously
141  * selected question.
142  */

143 public void valueChanged(ListSelectionEvent JavaDoc e) {
144     copyValuesToSelectedParameter(); // Previously selected param
145
int i = questionList.getSelectedIndex();
146     if (i >= 0) {
147     selectedParameter = (Parameter)parameters.get(i);
148     selectAndFillCard();
149     }
150     else
151     selectedParameter = null;
152 }
153
154 /**
155  * Displays and fills the edit field for the currently selected parameter.
156  * Inquisitors are lazily instantiated.
157  */

158 protected void selectAndFillCard() {
159     Inquisitor inq = (Inquisitor)createdInquisitors.get(selectedParameter);
160     if (inq == null) {
161     inq = Inquisitor.create(selectedParameter);
162     createdInquisitors.put(selectedParameter, inq);
163     cardPanel.add(inq.getPanel(), inq.getPanelName());
164     }
165
166     CardLayout JavaDoc cardLayout = (CardLayout JavaDoc)cardPanel.getLayout();
167     cardLayout.show(cardPanel, inq.getPanelName());
168
169     // Fill card
170
inq.copyParamIntoGUI();
171 }
172
173 /**
174  * Copy all values in GUI into the associated selected parameter.
175  */

176 protected void copyValuesToSelectedParameter() {
177     if (selectedParameter != null) {
178     Inquisitor inq = (Inquisitor)createdInquisitors.get(selectedParameter);
179     inq.copyGUIIntoParam();
180     }
181 }
182
183 /**
184  * Handles the buttons.
185  *
186  * @param e action event
187  */

188 public void actionPerformed(ActionEvent JavaDoc e) {
189     String JavaDoc cmd = e.getActionCommand();
190     if (I18N.get("ParamAskWin.run_report").equals(cmd)) {
191     cancelled = false;
192     copyValuesToSelectedParameter();
193     dispose();
194     }
195     else if (I18N.get("GUI.cancel").equals(cmd)) {
196     cancelled = true;
197     dispose();
198     }
199 }
200
201 }
202
Popular Tags