1 package jimm.datavision.gui.parameter; 2 import jimm.datavision.Parameter; 3 import jimm.util.I18N; 4 import java.awt.BorderLayout ; 5 import java.awt.CardLayout ; 6 import java.awt.Frame ; 7 import java.awt.Dimension ; 8 import java.awt.event.ActionListener ; 9 import java.awt.event.ActionEvent ; 10 import java.awt.event.WindowAdapter ; 11 import java.awt.event.WindowEvent ; 12 import java.util.List ; 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import javax.swing.*; 16 import javax.swing.event.ListSelectionListener ; 17 import javax.swing.event.ListSelectionEvent ; 18 19 25 public class ParamAskWin 26 extends JDialog 27 implements ActionListener , ListSelectionListener 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 CARD_BOOL_NAME = "bool"; 37 protected static final String CARD_SINGLE_STRING_NAME = "single-string"; 38 protected static final String CARD_RANGE_STRING_NAME = "range-string"; 39 protected static final String CARD_LIST_SINGLE_STRING_NAME = 40 "list-single-string"; 41 protected static final String CARD_LIST_MULTIPLE_STRING_NAME = 42 "list-multiple-string"; 43 protected static final String CARD_SINGLE_DATE_NAME = "single-date"; 44 protected static final String CARD_RANGE_DATE_NAME = "range-date"; 45 46 protected List parameters; 47 protected Parameter selectedParameter; 48 protected boolean cancelled; 49 protected JList questionList; 50 protected JPanel cardPanel; 51 protected HashMap createdInquisitors; 52 53 59 public ParamAskWin(Frame parent, List parameters) { 60 super(parent, I18N.get("ParamAskWin.title"), true); this.parameters = parameters; 62 createdInquisitors = new HashMap (); 63 selectedParameter = null; 64 buildWindow(); 65 questionList.setSelectedIndex(0); 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 ()); 74 getContentPane().add(questionPanel(), BorderLayout.NORTH); 75 getContentPane().add(editPanel(), BorderLayout.CENTER); 76 getContentPane().add(buttonPanel(), BorderLayout.SOUTH); 77 78 addWindowListener(new WindowAdapter () { 79 public void windowClosing(WindowEvent 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 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 107 protected JPanel editPanel() { 108 cardPanel = new JPanel(); 109 cardPanel.setLayout(new CardLayout (HORIZ_GAP, VERT_GAP)); 110 111 JPanel panel = new JPanel(); 114 panel.setPreferredSize(new Dimension (EDIT_PANEL_WIDTH, EDIT_PANEL_HEIGHT)); 115 cardPanel.add(panel, "dummy-blank-panel"); 116 117 return cardPanel; 118 } 119 120 protected JPanel buttonPanel() { 121 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 143 public void valueChanged(ListSelectionEvent e) { 144 copyValuesToSelectedParameter(); 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 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 cardLayout = (CardLayout )cardPanel.getLayout(); 167 cardLayout.show(cardPanel, inq.getPanelName()); 168 169 inq.copyParamIntoGUI(); 171 } 172 173 176 protected void copyValuesToSelectedParameter() { 177 if (selectedParameter != null) { 178 Inquisitor inq = (Inquisitor)createdInquisitors.get(selectedParameter); 179 inq.copyGUIIntoParam(); 180 } 181 } 182 183 188 public void actionPerformed(ActionEvent e) { 189 String 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 |