1 package com.calipso.reportgenerator.userinterface; 2 3 import com.calipso.reportgenerator.common.LanguageTraslator; 4 import com.calipso.reportgenerator.common.ReportSpec; 5 import com.calipso.reportgenerator.common.InfoException; 6 import com.calipso.reportgenerator.common.ShowExceptionMessageDialog; 7 import com.calipso.reportgenerator.reportdefinitions.types.ReportDataType; 8 9 import javax.swing.*; 10 import java.awt.*; 11 import java.awt.event.ActionListener ; 12 import java.awt.event.ActionEvent ; 13 import java.util.*; 14 import java.text.DateFormat ; 15 import java.text.SimpleDateFormat ; 16 import java.text.ParseException ; 17 18 19 22 23 public class UserParametersUI extends JDialog implements ActionListener { 24 25 private Vector userParametersCollection; 26 private HashMap params; 27 private boolean isMapGenerated; 28 private HashMap variablesNames; 29 private JButton btAccept; 30 private JButton btCancel; 31 private int WIDTH = 90; 32 private int HEIGHT = 26; 33 34 38 public UserParametersUI(Frame owner, Vector userParametersCollection){ 39 super(owner, true); 40 this.userParametersCollection = userParametersCollection; 41 this.variablesNames = new HashMap(); 42 } 43 44 47 public void showUI(){ 48 getContentPane().add(createCenterPanel(), BorderLayout.CENTER); 49 getContentPane().add(createSouthPanel(), BorderLayout.SOUTH); 50 this.pack(); 51 setLocation(getDefaultLocation()); 52 this.setVisible(true); 53 } 54 55 private Point getDefaultLocation() { 56 Point ownerLocation = getOwner().getLocation(); 57 Dimension ownerSize = getOwner().getSize(); 58 Dimension size = getSize(); 59 60 int x = ownerLocation.x + ownerSize.width / 2 - size.width / 2; 61 int y = ownerLocation.y + ownerSize.height / 2 - size.height / 2; 62 return new Point(x, y); 63 } 64 65 69 private JPanel createSouthPanel(){ 70 JPanel southPanel = new JPanel(new BorderLayout()); 71 setTitle(LanguageTraslator.traslate("186")); 72 btAccept = new JButton(LanguageTraslator.traslate("112")); 73 btAccept.setSize(new Dimension(WIDTH, HEIGHT)); 74 btAccept.setPreferredSize(new Dimension(WIDTH, HEIGHT)); 75 btAccept.setMaximumSize(new Dimension(WIDTH, HEIGHT)); 76 btAccept.setMinimumSize(new Dimension(WIDTH, HEIGHT)); 77 btAccept.addActionListener(this); 78 btCancel = new JButton(LanguageTraslator.traslate("113")); 79 btCancel.addActionListener(this); 80 btCancel.setSize(new Dimension(WIDTH, HEIGHT)); 81 btCancel.setPreferredSize(new Dimension(WIDTH, HEIGHT)); 82 btCancel.setMaximumSize(new Dimension(WIDTH, HEIGHT)); 83 btCancel.setMinimumSize(new Dimension(WIDTH, HEIGHT)); 84 JPanel eastSouthPanel = new JPanel(new FlowLayout()); 85 eastSouthPanel.add(btAccept); 86 eastSouthPanel.add(btCancel); 87 southPanel.add(eastSouthPanel, BorderLayout.EAST); 88 return southPanel; 89 } 90 91 96 private JPanel createCenterPanel(){ 97 JPanel centerPanel = new JPanel(new GridLayout(userParametersCollection.size(), 1)); 98 for(Enumeration enumeration = userParametersCollection.elements() ; enumeration.hasMoreElements();){ 99 UserParameterElement paramValueElement = (UserParameterElement) enumeration.nextElement(); 100 JLabel label = new JLabel(paramValueElement.getName()); 101 centerPanel.add(label, BorderLayout.CENTER); 104 centerPanel.add(getValuesPanel(paramValueElement)); 105 } 106 return centerPanel; 107 } 108 109 115 private JPanel getValuesPanel(UserParameterElement paramValueElement){ 116 JPanel pnlValues = new JPanel(new GridLayout(paramValueElement.getValues().size(), paramValueElement.getValues().size())); 117 for(int i=0 ; i<paramValueElement.getValues().size() ; i++){ 118 String currentKey = paramValueElement.getKeyAt(i); 119 JLabel label = new JLabel(currentKey); 120 pnlValues.add(label); 121 122 UserParameterTextField textField = null; 123 if(paramValueElement.getDimensionDataType() == ReportDataType.DATETIME_TYPE || 124 paramValueElement.getDimensionDataType() == ReportDataType.DATE_TYPE) { 125 String formatedDate = getDateFrom(paramValueElement.getValues().get(currentKey).toString()); 126 textField = new DateTextField(formatedDate); 127 } else if(paramValueElement.getDimensionDataType() == ReportDataType.STRING_TYPE) { 128 textField = new StringTextField(paramValueElement.getValues().get(currentKey).toString()); 129 } 130 pnlValues.add(textField); 131 initializeDictionary(paramValueElement.getFilterDefinitionName(), currentKey, textField); 132 133 } 134 return pnlValues; 135 } 136 137 private String getDateFrom(String dateString) { 138 String returnVal = null; 139 try { 140 DateFormat dateFormat = new SimpleDateFormat ("yyyyMMdd"); 141 Date date = dateFormat.parse(dateString); 142 DateFormat second = SimpleDateFormat.getDateInstance(DateFormat.SHORT, LanguageTraslator.getLocale()); 143 returnVal = second.format(date); 144 } catch (ParseException e) { 145 e.printStackTrace(); 146 } 147 return returnVal; 148 } 149 150 156 private void initializeDictionary(String name, String paramType, JTextField textfield){ 157 if(paramType.equals(LanguageTraslator.traslate("146"))){ variablesNames.put(name + " FROM", textfield); 159 }else if(paramType.equals(LanguageTraslator.traslate("147"))){ variablesNames.put(name + " TO", textfield); 161 }else if(paramType.equals(LanguageTraslator.traslate("149"))){ variablesNames.put(name + " QUANTITY", textfield); 163 }else if(paramType.equals(LanguageTraslator.traslate("150"))){ variablesNames.put(name + " VALUE", textfield); 165 } 166 } 167 168 172 public void actionPerformed(ActionEvent ae) { 173 isMapGenerated = false; 174 if(ae.getSource() == btAccept){ 175 Hashtable incorrectInputs = validateUserParameters(); 176 if(incorrectInputs.size() > 0){ 177 showMessage(incorrectInputs); 178 return; 179 } 180 try { 181 params = getUserParameters(); 182 } catch (InfoException e) { 183 ShowExceptionMessageDialog.initExceptionDialogMessage(LanguageTraslator.traslate("257"), e); 184 } 185 isMapGenerated = true; 186 this.dispose(); 187 }else{ 188 dispose(); 189 } 190 } 191 192 196 private void showMessage(Hashtable incorrectInputs){ 197 String incorrectInputsString = ""; 198 for(Enumeration enumeration = incorrectInputs.keys() ; enumeration.hasMoreElements() ; ){ 199 String currentKey = enumeration.nextElement().toString(); 200 incorrectInputsString = incorrectInputsString + currentKey + " > " + incorrectInputs.get(currentKey) + '\n'; 201 } 202 JOptionPane.showMessageDialog(this, incorrectInputsString, "Error", JOptionPane.ERROR_MESSAGE); 203 } 204 205 209 private HashMap getUserParameters() throws InfoException { 210 HashMap userParameters = new HashMap(); 211 Set keys = variablesNames.keySet(); 212 for(Iterator iterator = keys.iterator() ; iterator.hasNext() ; ){ 213 String currentKey = iterator.next().toString(); 214 String [] tokens = prepareTokens(currentKey); 215 userParameters.put(tokens[0].toUpperCase() + tokens[1], ((UserParameterTextField)variablesNames.get(currentKey)).getFieldText()); 217 } 218 return userParameters; 219 } 220 221 225 private Hashtable validateUserParameters(){ 226 Hashtable incorrectMap = new Hashtable(); 227 Set keys = variablesNames.keySet(); 228 for(Iterator iterator = keys.iterator() ; iterator.hasNext() ; ){ 229 String currentKey = iterator.next().toString(); 230 String []tokens = prepareTokens(currentKey); 231 String returnedVal = validate(tokens); 232 if(!returnedVal.equals("")){ 233 String [] incorrectTokens = prepareTokens(returnedVal); 234 incorrectMap.put(incorrectTokens[0], LanguageTraslator.traslate("152") + incorrectTokens[1] 235 + ", " + incorrectTokens[2]); 236 } 237 } 238 return incorrectMap; 239 } 240 241 246 private String validate(String []tokens){ 247 String from="0", to="0"; 248 if(tokens[tokens.length-1].equals("TO")){ 249 to = ((JTextField)variablesNames.get(tokens[0] + " " + tokens[tokens.length-1])).getText(); 250 from = ((JTextField)variablesNames.get(tokens[0] + " " + "FROM")).getText(); 251 }else if(tokens[tokens.length-1].equals("QUANTITY")){ 252 to = ((JTextField)variablesNames.get(tokens[0] + " " + tokens[tokens.length-1])).getText(); 253 from = ((JTextField)variablesNames.get(tokens[0] + " " + "FROM")).getText(); 254 }else if(tokens[tokens.length-1].equals("VALUE")){ 255 to = ((JTextField)variablesNames.get(tokens[0] + " " + tokens[tokens.length-1])).getText(); 256 }else{ 257 from = "-1"; 258 } 259 260 if(from.compareTo(to) > 0){ 261 return tokens[0] + " " + from + " " + to; 262 } 263 return ""; 264 } 265 266 271 private String [] prepareTokens(String currentKey){ 272 StringTokenizer stringTokenizer = new StringTokenizer(currentKey); 273 String [] tokens = new String [stringTokenizer.countTokens()]; 274 for(int i=0 ; stringTokenizer.hasMoreTokens() ; i++){ 275 tokens[i] = stringTokenizer.nextToken(); 276 } 277 return tokens; 278 } 279 280 284 public HashMap getParams(){ 285 return params; 286 } 287 288 293 public boolean isGenerated(){ 294 return isMapGenerated; 295 } 296 297 305 public static boolean editParams(Frame owner, ReportSpec reportSpec, Map params) { 306 UserParametersCollection userParametersCollection = new UserParametersCollection(reportSpec); 307 UserParametersUI userParametersUI = new UserParametersUI(owner, userParametersCollection.getUserParametersCollection()); 308 if(userParametersCollection.getUserParametersCollection().size() > 0) { 309 userParametersUI.showUI(); 310 boolean result = userParametersUI.isGenerated(); 311 if (result) { 312 params.putAll(userParametersUI.getParams()); 313 } 314 return result; 315 } 316 return true; 317 } 318 } 319 | Popular Tags |