1 16 17 package com.jaspersoft.jasperserver.irplugin.gui.inputcontrols; 18 19 import com.jaspersoft.jasperserver.api.metadata.xml.domain.impl.ResourceDescriptor; 20 import com.jaspersoft.jasperserver.irplugin.gui.inputcontrols.ui.BasicInputControlUI; 21 import com.jaspersoft.jasperserver.irplugin.gui.inputcontrols.ui.InputControlUI; 22 import java.text.ParseException ; 23 import java.util.regex.Pattern ; 24 import javax.swing.JComponent ; 25 import java.text.SimpleDateFormat ; 26 27 31 public class BasicInputControl { 32 33 39 public static java.util.Map valueHistories = new java.util.HashMap (); 40 41 protected ResourceDescriptor inputControl = null; 42 private ResourceDescriptor dataType = null; 43 private InputControlUI inputControlUI; 44 45 46 public BasicInputControl() { 47 setInputControlUI(new BasicInputControlUI()); 48 } 49 50 public JComponent getUI() 51 { 52 return (JComponent )getInputControlUI(); 53 } 54 55 public Object getValue() throws InputValidationException 56 { 57 return validate(); 58 } 59 60 public ResourceDescriptor getInputControl() { 61 return inputControl; 62 } 63 64 public void setInputControl(ResourceDescriptor inputControl) 65 { 66 setInputControl(inputControl, null); 67 } 68 69 public void setInputControl(ResourceDescriptor inputControl, ResourceDescriptor dataType) { 70 this.inputControl = inputControl; 71 this.dataType = dataType; 72 73 String label = inputControl.getLabel() + ((inputControl.isMandatory()) ? "*" : ""); 74 getInputControlUI().setLabel(label); 75 getInputControlUI().setReadOnly( inputControl.isReadOnly() ); 76 77 java.util.List history = getHistory(inputControl.getUriString()); 78 java.util.List revisedHistory = new java.util.ArrayList (); 79 if (dataType != null && dataType.getDataType() == dataType.DT_TYPE_DATE) 80 { 81 SimpleDateFormat format = new SimpleDateFormat (it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties() 82 .getProperty("dateformat", "d/M/y")); 83 for (int i=0; i<history.size(); ++i) 84 { 85 Object obj = history.get(i); 86 if (obj instanceof java.util.Date ) 87 { 88 revisedHistory.add( format.format(obj)); 89 } 90 else 91 revisedHistory.add( obj); 92 } 93 } 94 else if (dataType != null && dataType.getDataType() == dataType.DT_TYPE_DATE) 95 { 96 SimpleDateFormat format = new SimpleDateFormat (it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties() 97 .getProperty("timeformat", "d/M/y H:m:s")); 98 for (int i=0; i<history.size(); ++i) 99 { 100 Object obj = history.get(i); 101 if (obj instanceof java.util.Date ) 102 { 103 revisedHistory.add( format.format(obj)); 104 } 105 else 106 revisedHistory.add( obj); 107 } 108 } 109 else 110 { 111 revisedHistory = history; 112 } 113 getInputControlUI().setHistory( revisedHistory ); 114 115 116 117 } 118 119 public void addHistoryValue(String controlUri, Object value) 120 { 121 122 if (controlUri == null) return; 123 java.util.List list = getHistory(controlUri); 124 if (list.contains( value)) list.remove(value ); 125 list.add(0,value); 126 } 127 128 public java.util.List getHistory(String controlUri) 129 { 130 if (controlUri == null) return null; 131 132 java.util.List list = null; 133 if ( valueHistories.get(controlUri) == null) 134 { 135 list = new java.util.ArrayList (); 136 valueHistories.put(controlUri, list); 137 return list; 138 } 139 140 return (java.util.List )valueHistories.get(controlUri); 141 } 142 143 public InputControlUI getInputControlUI() { 144 return inputControlUI; 145 } 146 147 public void setInputControlUI(InputControlUI inputControlUI) { 148 this.inputControlUI = inputControlUI; 149 } 150 151 public Object validate() throws InputValidationException 152 { 153 Object val = getInputControlUI().getValue(); 154 if (getInputControl().getControlType() == ResourceDescriptor.IC_TYPE_SINGLE_VALUE) 155 { 156 if (getDataType() != null) 158 { 159 if (val == null) return null; 160 161 String strVal = ""+val; 162 163 164 165 switch(getDataType().getDataType()) 166 { 167 case ResourceDescriptor.DT_TYPE_TEXT : 168 { 169 if (getDataType().getPattern() != null 178 && getDataType().getPattern().trim().length() > 0 179 && !Pattern.matches(getDataType().getPattern(), (strVal))) 180 { 181 throw new InputValidationException("" + getInputControl().getLabel() + " does not match the pattern (" + getDataType().getPattern() +")"); 182 } 183 val = strVal; 184 break; 185 } 186 case ResourceDescriptor.DT_TYPE_NUMBER : 187 { 188 try 190 { 191 if (strVal.trim().length() == 0) return null; 192 val = new java.math.BigDecimal (strVal); 193 } 194 catch(NumberFormatException e) 195 { 196 throw new InputValidationException("" + getInputControl().getLabel() + " is not a valid number"); 197 } 198 break; 199 } 200 case ResourceDescriptor.DT_TYPE_DATE_TIME : 201 { 202 SimpleDateFormat format = new SimpleDateFormat (it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties() 203 .getProperty("timeformat", "d/M/y H:m:s")); 204 try 205 { 206 if (strVal.trim().length() == 0) return null; 207 val = format.parse(strVal); 208 } 209 catch (ParseException e) 210 { 211 throw new InputValidationException("" + getInputControl().getLabel() + " is not a valid datetime in the form " + 212 it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties().getProperty("timeformat", "d/M/y H:m:s") ); 213 } 214 break; 215 } 216 217 218 case ResourceDescriptor.DT_TYPE_DATE : 219 { 220 SimpleDateFormat format = new SimpleDateFormat ( it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties() 221 .getProperty("dateformat", "d/M/y") ); 222 223 try 224 { 225 if (strVal.trim().length() == 0) return null; 226 val = format.parse(strVal); 227 } 228 catch (ParseException e) 229 { 230 throw new InputValidationException("" + getInputControl().getLabel() + " is not a valid date in the form " + 231 it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties().getProperty("dateformat", "d/M/y") ); 232 } 233 break; 234 } 235 } 236 } 237 } 238 239 return val; 240 } 241 242 public ResourceDescriptor getDataType() { 243 return dataType; 244 } 245 246 public void setDataType(ResourceDescriptor dataType) { 247 this.dataType = dataType; 248 } 249 } 250 | Popular Tags |